OSDN Git Service

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