OSDN Git Service

ticket #363
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7directord / t / 13_process.t
1 use strict;
2 use warnings;
3 no warnings qw(redefine once);
4 use lib qw(t/lib lib);
5 use subs qw(print);
6 use Cwd;
7 use L7lib;
8 use subs qw(fork chdir);
9 use Test::More tests => 29;
10 use Socket;
11 use Socket6;
12
13 L7lib::chdir();
14 L7lib::comment_out();
15 require './l7directord';
16 override();
17
18 our $health_check_called = 0;
19 our $fork_return = -1;
20 our $fork_called = 0;
21 our $ld_exit_called = 0;
22 our $setsid_return = 0;
23 our $chdir_return = 0;
24 #...............................................
25 # test start
26 #   - check_child_process
27 {
28     set_default();
29     my @got = check_child_process();
30     is_deeply \@got, [], 'check_child_process - no process';
31 }
32 {
33     set_default();
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';
37 }
38 #   - create_check_process
39 {
40     set_default();
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';
44 }
45 {
46     set_default();
47     $fork_return = -1;
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';
52 }
53 {
54     set_default();
55     $fork_return = 0;
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';
60 }
61 {
62     set_default();
63     $fork_return = 1000;
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';
68 }
69 #   - ld_daemon_become_child
70 {
71     set_default();
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';
76 }
77 {
78     set_default();
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';
83 }
84 {
85     set_default();
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';
90 }
91 #   - ld_daemon
92 SKIP: {
93     skip "need root privilege", 2 if ($> != 0);
94     set_default();
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';
99 }
100 {
101     set_default();
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';
106 }
107 {
108     set_default();
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';
113 }
114 {
115     set_default();
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';
121 }
122 {
123     set_default();
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';
129 }
130 # test end
131 #...............................................
132
133 L7lib::comment_in();
134
135 sub set_default {
136     %main::HEALTH_CHECK = ();
137     $health_check_called = 0;
138     $fork_return = -1;
139     $fork_called = 0;
140     $ld_exit_called = 0;
141 }
142 sub override {
143     *health_check = \&__health_check;
144     *ld_log = \&__ld_log;
145     *ld_exit = \&__ld_exit;
146     *fork = \&__fork;
147     *chdir = \&__chdir;
148     *POSIX::setsid = \&__setsid;
149 }
150 sub __health_check {
151     $health_check_called++;
152 }
153 sub __fork {
154     $fork_called++;
155     return $fork_return;
156 }
157 sub __ld_exit {
158     $ld_exit_called++;
159     die @_;
160 }
161 sub __setsid {
162     return $setsid_return;
163 }
164 sub __chdir {
165     return $chdir_return;
166 }
167 sub __ld_log {
168 }