3 no warnings qw(redefine once);
8 use subs qw(fork chdir);
9 use Test::More tests => 29;
15 require './l7directord';
18 our $health_check_called = 0;
19 our $fork_return = -1;
21 our $ld_exit_called = 0;
22 our $setsid_return = 0;
23 our $chdir_return = 0;
24 #...............................................
26 # - check_child_process
29 my @got = check_child_process();
30 is_deeply \@got, [], 'check_child_process - no process';
34 %main::HEALTH_CHECK = ( id1 => { pid => $$ }, id2 => { pid => undef }, id3 => {pid => 100000 } );
35 my @got = check_child_process();
36 is_deeply \@got, ['id2', 'id3'], 'check_child_process - valid process, no process, invalid process';
38 # - create_check_process
41 create_check_process();
42 is $fork_called, 0, 'create_check_process - no list';
43 is $health_check_called, 0, 'create_check_process - no list';
48 create_check_process('id1', 'id2');
49 is $fork_called, 2, 'create_check_process - fork error';
50 is $health_check_called, 0, 'create_check_process - fork error';
51 is_deeply \%main::HEALTH_CHECK, {}, 'create_check_process - fork error';
56 create_check_process('id1');
57 is $fork_called, 1, 'create_check_process - fork child';
58 is $health_check_called, 1, 'create_check_process - fork child';
59 is_deeply \%main::HEALTH_CHECK, { id1 => {} }, 'create_check_process - fork child';
64 create_check_process('id1', 'id2');
65 is $fork_called, 2, 'create_check_process - fork parent';
66 is $health_check_called, 0, 'create_check_process - fork parent';
67 is_deeply \%main::HEALTH_CHECK, { id1 => {pid => 1000}, id2 => {pid => 1000} }, 'create_check_process - fork parent';
69 # - ld_daemon_become_child
72 local $fork_return = 0;
73 eval { ld_daemon_become_child(); };
74 is $fork_called, 1, 'ld_daemon_become_child - fork child';
75 is $ld_exit_called, 0, 'ld_daemon_become_child - become child';
79 local $fork_return = 1;
80 eval { ld_daemon_become_child(); };
81 is $fork_called, 1, 'ld_daemon_become_child - fork parent';
82 is $ld_exit_called, 1, 'ld_daemon_become_child - parent ld_exit';
86 local $fork_return = -1;
87 eval { ld_daemon_become_child(); };
88 is $fork_called, 1, 'ld_daemon_become_child - fork error';
89 is $ld_exit_called, 1, 'ld_daemon_become_child - fork error ld_exit';
93 skip "need root privilege", 2 if ($> != 0);
95 local $fork_return = 0;
96 eval { ld_daemon(); };
97 is $fork_called, 2, 'ld_daemon - fork child 2 times';
98 is $ld_exit_called, 0, 'ld_daemon - become daemon';
102 local $fork_return = 1;
103 eval { ld_daemon(); };
104 is $fork_called, 1, 'ld_daemon - fork parent';
105 is $ld_exit_called, 1, 'ld_daemon - cannot become daemon';
109 local $fork_return = -1;
110 eval { ld_daemon(); };
111 is $fork_called, 1, 'ld_daemon - fork error';
112 is $ld_exit_called, 1, 'ld_daemon - cannot become daemon';
116 local $fork_return = 0;
117 local $setsid_return = -1;
118 eval { ld_daemon(); };
119 is $fork_called, 1, 'ld_daemon - setsid error';
120 is $ld_exit_called, 1, 'ld_daemon - cannot become daemon';
124 local $fork_return = 0;
125 local $chdir_return = -1;
126 eval { ld_daemon(); };
127 is $fork_called, 2, 'ld_daemon - chdir error';
128 is $ld_exit_called, 1, 'ld_daemon - cannot become daemon';
131 #...............................................
136 %main::HEALTH_CHECK = ();
137 $health_check_called = 0;
143 *health_check = \&__health_check;
144 *ld_log = \&__ld_log;
145 *ld_exit = \&__ld_exit;
148 *POSIX::setsid = \&__setsid;
151 $health_check_called++;
162 return $setsid_return;
165 return $chdir_return;