OSDN Git Service

2009/12/13 一時更新
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7directord / t / 35_output.t
1 use strict;
2 use warnings;
3 no warnings qw(redefine once);
4 use lib qw(t/lib lib);
5 use subs qw(exit print);
6 use Cwd;
7 use L7lib;
8 use Test::More tests => 33;
9 use Socket;
10 use Socket6;
11
12 L7lib::chdir();
13 L7lib::comment_out();
14 require './l7directord';
15 override();
16
17 our $ld_log_called = 0;
18
19 close *STDOUT;
20 open  *STDOUT, '>', '/dev/null';
21 close *STDERR;
22 open  *STDERR, '>', '/dev/null';
23
24 #...............................................
25 # test start
26 #   - ld_openlog
27 {
28     local *ld_log = \&__ld_log;
29     my $got = ld_openlog();
30     is $got, 1, 'ld_openlog - log_config is undef';
31 }
32 {
33     local *ld_log = \&__ld_log;
34     local $main::DEBUG_LEVEL = 1;
35     my $got = ld_openlog('logfile');
36     is $got, 0, 'ld_openlog - debug mode';
37 }
38 {
39     local *ld_log = \&__ld_log;
40     local $main::CONFIG{supervised} = 1;
41     my $got = ld_openlog('logfile');
42     is $got, 0, 'ld_openlog - supervised mode';
43 }
44 {
45     local *ld_log = \&__ld_log;
46     my $log_config = '/tmp/' . time;
47     my $got = ld_openlog($log_config);
48     is $got, 0, 'ld_openlog - open normal logfile';
49     unlink $log_config;
50 }
51 {
52     local *ld_log = \&__ld_log;
53     my $log_config = '/tmp';
54     my $got = ld_openlog($log_config);
55     is $got, 1, 'ld_openlog - cannot open normal logfile';
56 }
57 {
58     local *ld_log = \&__ld_log;
59     my $log_config = 'local0';
60     my $got = ld_openlog($log_config);
61     is $got, 0, 'ld_openlog - open syslog';
62 }
63 #   - ld_log
64 SKIP: {
65     skip 'aboid logging', 1;
66     my $got = ld_log();
67     is $got, 1, 'ld_log - message is undef';
68 }
69 {
70     local $main::PROC_STAT{pid}  = 12345;
71     local $main::CONFIG{logfile} = '/tmp/' . time;
72     my $got = ld_log('test');
73     is $got, 0, 'ld_log - write message to logfile';
74     unlink $main::CONFIG{logfile};
75 }
76 {
77     local $main::PROC_STAT{pid}  = 12345;
78     local $main::CONFIG{logfile} = '/tmp';
79     my $got = ld_log('test');
80     is $got, 1, 'ld_log - cannot write message to logfile';
81 }
82 {
83     local $main::PROC_STAT{pid}  = 12345;
84     local $main::CONFIG{logfile} = 'local0';
85     my $got = ld_log('test');
86     is $got, 0, 'ld_log - write message to syslog';
87 }
88 {
89     local $main::CONFIG{supervised} = 1;
90     local $main::PROC_STAT{pid}  = 12345;
91     local $main::CONFIG{logfile} = 'local0';
92     my $got = ld_log('test');
93     is $got, 0, 'ld_log - write message to stderr(supervised mode)';
94 }
95 #   - ld_debug
96 {
97     local *ld_log = \&__ld_log;
98     ld_debug();
99     pass 'ld_debug - args is undef';
100 }
101 {
102     local *ld_log = \&__ld_log;
103     ld_debug(undef, 'test');
104     pass 'ld_debug - priority is undef';
105 }
106 {
107     local *ld_log = \&__ld_log;
108     ld_debug('fatal', 'test');
109     pass 'ld_debug - priority is string';
110 }
111 {
112     local *ld_log = \&__ld_log;
113     ld_debug(3, undef);
114     pass 'ld_debug - message is undef';
115 }
116 {
117     local *ld_log = \&__ld_log;
118     local $main::DEBUG_LEVEL = 10;
119     ld_debug(3, 'debug');
120     pass 'ld_debug - priority and message is ok';
121 }
122 #   - ld_exit
123 {
124     local *ld_log = \&__ld_log;
125     local $main::DEBUG_LEVEL = 0;
126     local $ld_log_called = 0;
127     ld_exit();
128     is $ld_log_called, 0, 'ld_exit - args is undef';
129 }
130 {
131     local *ld_log = \&__ld_log;
132     local $main::DEBUG_LEVEL = 0;
133     local $ld_log_called = 0;
134     ld_exit(undef, 'exit');
135     is $ld_log_called, 0, 'ld_exit - status is undef';
136 }
137 {
138     local *ld_log = \&__ld_log;
139     local $main::DEBUG_LEVEL = 0;
140     local $ld_log_called = 0;
141     ld_exit(1, undef);
142     is $ld_log_called, 0, 'ld_exit - message is undef';
143 }
144 {
145     local *ld_log = \&__ld_log;
146     local $main::DEBUG_LEVEL = 0;
147     local $ld_log_called = 0;
148     ld_exit(0, 'exit');
149     is $ld_log_called, 1, 'ld_exit - status is 0 (normal exit)';
150 }
151 {
152     local *ld_log = \&__ld_log;
153     local $main::DEBUG_LEVEL = 3;
154     local $ld_log_called = 0;
155     ld_exit(0, 'exit');
156     is $ld_log_called, 1, 'ld_exit - debug enable';
157 }
158 {
159     local *ld_log = \&__ld_log;
160     local $main::DEBUG_LEVEL = 0;
161     local $ld_log_called = 0;
162     ld_exit(1, 'exit');
163     is $ld_log_called, 1, 'ld_exit - status is 1 (some error)';
164 }
165 #   - init_error
166 {
167     local *ld_log = \&__ld_log;
168     local *ld_exit = \&__ld_exit;
169     local $main::DEBUG_LEVEL = 0;
170     local $ld_log_called = 0;
171     init_error();
172     is $ld_log_called, 0, 'init_error - message is undef';
173 }
174 {
175     local *ld_log = \&__ld_log;
176     local *ld_exit = \&__ld_exit;
177     local $main::DEBUG_LEVEL = 0;
178     local $ld_log_called = 0;
179     init_error('init error');
180     is $ld_log_called, 1, 'init_error - no debug';
181 }
182 {
183     local *ld_log = \&__ld_log;
184     local *ld_exit = \&__ld_exit;
185     local $main::DEBUG_LEVEL = 1;
186     local $ld_log_called = 0;
187     init_error('init error');
188     is $ld_log_called, 1, 'init_error - debug mode';
189 }
190 #   - config_error
191 {
192     local *ld_log  = \&__ld_log;
193     local *ld_exit = \&__ld_exit;
194     local $main::CONFIG_FILE{path} = 'filepath';
195     local $main::PROC_STAT{initialized} = 1;
196     local $main::DEBUG_LEVEL = 1;
197     local $ld_log_called = 0;
198     eval { config_error(1, 'WRN0004'); };
199     is $ld_log_called, 0, 'config_error - debug mode';
200     ok $@, 'config_error - initialized and died';
201 }
202 {
203     local *ld_log  = \&__ld_log;
204     local *ld_exit = \&__ld_exit;
205     local $main::CONFIG_FILE{path} = 'filepath';
206     local $main::PROC_STAT{initialized} = 0;
207     local $main::DEBUG_LEVEL = 1;
208     local $ld_log_called = 0;
209     eval { config_error(1, 'WRN0004'); };
210     is $ld_log_called, 0, 'config_error - debug mode';
211     is $@, '', 'config_error - not initialized exit';
212 }
213 {
214     local *ld_log  = \&__ld_log;
215     local *ld_exit = \&__ld_exit;
216     local $main::CONFIG_FILE{path} = 'filepath';
217     local $main::PROC_STAT{initialized} = 1;
218     local $main::DEBUG_LEVEL = 0;
219     local $ld_log_called = 0;
220     eval { config_error(0, 'WRN0004'); };
221     is $ld_log_called, 1, 'config_error - no debug mode and line is 0';
222     ok $@, 'config_error - initialized and died';
223 }
224 {
225     local *ld_log  = \&__ld_log;
226     local *ld_exit = \&__ld_exit;
227     local $main::CONFIG_FILE{path} = 'filepath';
228     local $main::PROC_STAT{initialized} = 1;
229     local $main::DEBUG_LEVEL = 0;
230     local $ld_log_called = 0;
231     eval { config_error(1, 'WRN0004'); };
232     is $ld_log_called, 2, 'config_error - no debug mode and line is 1';
233     ok $@, 'config_error - initialized and died';
234 }
235 # test end
236 #...............................................
237
238 L7lib::comment_in();
239
240 sub set_default {
241 }
242 sub override {
243     *exit = \&__exit;
244 }
245 sub __exit {
246 }
247 sub __ld_exit {
248 }
249 sub __ld_log {
250     $ld_log_called++;
251 }