comparison syslog.t @ 404:997f5fe16545

Tests: added syslog tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 21 May 2014 17:43:42 +0400
parents
children 82b15c81978e
comparison
equal deleted inserted replaced
403:22f6aa0d1139 404:997f5fe16545
1 #!/usr/bin/perl
2
3 # (C) Nginx, Inc.
4
5 # Tests for syslog.
6 # Various log levels emitted with limit_req_log_level.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http limit_req/);
26
27 plan(skip_all => 'no syslog') unless $t->has_version('1.7.1');
28
29 $t->plan(56)->write_file_expand('nginx.conf', <<'EOF');
30
31 %%TEST_GLOBALS%%
32
33 error_log syslog:server=127.0.0.1:8083 info;
34 error_log %%TESTDIR%%/f_glob.log info;
35
36 daemon off;
37
38 events {
39 }
40
41 http {
42 %%TEST_GLOBALS_HTTP%%
43
44 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m;
45
46 log_format empty "";
47 log_format logf "$status";
48
49 error_log syslog:server=127.0.0.1:8084 info;
50 error_log %%TESTDIR%%/f_http.log info;
51
52 server {
53 listen 127.0.0.1:8080;
54 server_name localhost;
55
56 location /e {
57 error_log syslog:server=127.0.0.1:8080;
58 }
59 location /a {
60 access_log syslog:server=127.0.0.1:8080;
61 }
62 location /ef {
63 error_log syslog:server=127.0.0.1:8080,facility=user;
64 }
65 location /es {
66 error_log syslog:server=127.0.0.1:8080,severity=alert;
67 }
68 location /et {
69 error_log syslog:server=127.0.0.1:8080,tag=SEETHIS;
70 }
71 location /af {
72 access_log syslog:server=127.0.0.1:8080,facility=user;
73 }
74 location /as {
75 access_log syslog:server=127.0.0.1:8080,severity=alert;
76 }
77 location /at {
78 access_log syslog:server=127.0.0.1:8080,tag=SEETHIS;
79 }
80 location /e2 {
81 error_log syslog:server=127.0.0.1:8080;
82 error_log syslog:server=127.0.0.1:8080;
83 }
84 location /a2 {
85 access_log syslog:server=127.0.0.1:8080;
86 access_log syslog:server=127.0.0.1:8080;
87 }
88 location /a_logf {
89 access_log syslog:server=127.0.0.1:8080 logf;
90 }
91
92 location /debug {
93 limit_req zone=one;
94 error_log syslog:server=127.0.0.1:8080 debug;
95 }
96 location /info {
97 limit_req zone=one;
98 limit_req_log_level info;
99 error_log syslog:server=127.0.0.1:8080 info;
100 }
101 location /notice {
102 limit_req zone=one;
103 limit_req_log_level notice;
104 error_log syslog:server=127.0.0.1:8080 notice;
105 }
106 location /warn {
107 limit_req zone=one;
108 limit_req_log_level warn;
109 error_log syslog:server=127.0.0.1:8080 warn;
110 }
111 location /error {
112 limit_req zone=one;
113 limit_req_log_level error;
114 error_log syslog:server=127.0.0.1:8080;
115 }
116 location /low {
117 error_log syslog:server=127.0.0.1:8080 warn;
118 error_log syslog:server=127.0.0.1:8080;
119 }
120 location /dup {
121 error_log syslog:server=127.0.0.1:8080;
122 error_log syslog:server=127.0.0.1:8080;
123 }
124 location /high {
125 error_log syslog:server=127.0.0.1:8080 emerg;
126 error_log syslog:server=127.0.0.1:8080;
127 }
128 }
129 }
130
131 EOF
132
133 $t->run_daemon(\&syslog_daemon, 8083, $t, 's_glob.log');
134 $t->run_daemon(\&syslog_daemon, 8084, $t, 's_http.log');
135
136 $t->waitforfile($t->testdir . '/s_glob.log');
137 $t->waitforfile($t->testdir . '/s_http.log');
138
139 $t->run();
140
141 ###############################################################################
142
143 parse_syslog_message('error_log', get_syslog('/e'));
144 parse_syslog_message('access_log', get_syslog('/a'));
145
146 like(get_syslog('/ef'), qr/^<11>/, 'error_log facility');
147 like(get_syslog('/es'), qr/^<187>/, 'error_log severity');
148 like(get_syslog('/et'), qr/SEETHIS:/, 'error_log tag');
149
150 like(get_syslog('/af'), qr/^<14>/, 'access_log facility');
151 like(get_syslog('/as'), qr/^<185>/, 'access_log severity');
152 like(get_syslog('/at'), qr/SEETHIS:/, 'access_log tag');
153
154
155 like(get_syslog('/e'),
156 qr/nginx: \d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2} \[error\]/,
157 'error_log format');
158 like(get_syslog('/a_logf'), qr/nginx: 404$/, 'access_log log_format');
159
160 my @lines = split /<\d+>/, get_syslog('/a2');
161 is($lines[1], $lines[2], 'access_log many');
162
163 @lines = split /<\d+>/, get_syslog('/e2');
164 is($lines[1], $lines[2], 'error_log many');
165
166 # error_log log levels
167
168 SKIP: {
169
170 skip "no --with-debug", 1 unless $t->has_module('--with-debug');
171
172 isnt(syslog_lines('/debug', '[debug]'), 0, 'debug');
173
174 }
175
176 # charge limit_req
177
178 get_syslog('/info');
179
180 is(syslog_lines('/info', '[info]'), 1, 'info');
181 is(syslog_lines('/notice', '[notice]'), 1, 'notice');
182 is(syslog_lines('/warn', '[warn]'), 1, 'warn');
183 is(syslog_lines('/error', '[error]'), 1, 'error');
184
185 # count log messages emitted with various error_log levels
186
187 is(syslog_lines('/low', '[error]'), 2, 'low');
188 is(syslog_lines('/dup', '[error]'), 2, 'dup');
189 is(syslog_lines('/high', '[error'), 1, 'high');
190
191 # check for the presence of the syslog messages in the global and http contexts
192
193 is_deeply(levels($t, 's_glob.log'), levels($t, 'f_glob.log'), 'master syslog');
194 is_deeply(levels($t, 's_http.log'), levels($t, 'f_http.log'), 'http syslog');
195
196 ###############################################################################
197
198 sub syslog_lines {
199 my ($uri, $pattern, $port) = @_;
200 return map { $_ =~ /\Q$pattern\E/g } (get_syslog($uri, $port));
201 }
202
203 sub levels {
204 my ($t, $file) = @_;
205 my %levels_hash;
206
207 map { $levels_hash{$_}++; } (read_file($t, $file) =~ /(\[\w+\])/g);
208
209 return \%levels_hash;
210 }
211
212 sub get_syslog {
213 my ($uri, $port) = @_;
214 my ($s);
215 my $rfd = '';
216 my $data = '';
217
218 $port = 8080 unless defined $port;
219
220 eval {
221 local $SIG{ALRM} = sub { die "timeout\n" };
222 local $SIG{PIPE} = sub { die "sigpipe\n" };
223 alarm(1);
224 $s = IO::Socket::INET->new(
225 Proto => 'udp',
226 LocalAddr => "127.0.0.1:$port"
227 );
228 alarm(0);
229 };
230 alarm(0);
231 if ($@) {
232 log_in("died: $@");
233 return undef;
234 }
235
236 http_get($uri);
237
238 vec($rfd, fileno($s), 1) = 1;
239 select $rfd, undef, undef, 1;
240 while (select($rfd, undef, undef, 0.1) > 0
241 && vec($rfd, fileno($s), 1))
242 {
243 my $buffer;
244 sysread($s, $buffer, 4096);
245 $data .= $buffer;
246 }
247 $s->close();
248 return $data;
249 }
250
251 sub read_file {
252 my ($t, $file) = @_;
253 my $path = $t->testdir() . '/' . $file;
254
255 open my $fh, '<', $path or return "$!";
256 local $/;
257 my $content = <$fh>;
258 close $fh;
259 chop $content;
260 return $content;
261 }
262
263 sub parse_syslog_message {
264 my ($desc, $line) = @_;
265
266 unless ($line) {
267 fail("$desc timeout in receiving syslog");
268 }
269
270 my @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
271 'Sep', 'Oct', 'Nov', 'Dec');
272
273 my ($pri, $mon, $mday, $hour, $minute, $sec, $host, $tag, $msg) =
274 $line =~ /^<(\d{1,3})> # PRI
275 ([A-Z][a-z]{2})\s # mon
276 ([ \d]\d)\s(\d{2}):(\d{2}):(\d{2})\s # date
277 ([\S]*)\s # host
278 (\w{1,32}):\s # tag
279 (.*)/x; # MSG
280
281 my $sev = $pri & 0x07;
282 my $fac = ($pri & 0x03f8) >> 3;
283
284 ok(defined($pri), "$desc has PRI");
285 ok($sev >= 0 && $sev <= 7, "$desc valid severity");
286 ok($fac >= 0 && $fac < 24, "$desc valid facility");
287
288 ok(defined($mon), "$desc has month");
289 ok((grep $mon, @months), "$desc valid month");
290
291 ok(defined($mday), "$desc has day");
292 ok($mday <= 31, "$desc valid day");
293
294 ok(defined($hour), "$desc has hour");
295 ok($hour < 24, "$desc valid hour");
296
297 ok(defined($minute), "$desc has minutes");
298 ok($minute < 60, "$desc valid minutes");
299
300 ok(defined($sec), "$desc has seconds");
301 ok($sec < 60, "$desc valid seconds");
302
303 ok(defined($host), "$desc has host");
304 chomp(my $hostname = lc `hostname`);
305 is($host , $hostname, "$desc valid host");
306
307 ok(defined($tag), "$desc has tag");
308 like($tag, qr'\w+', "$desc valid tag");
309
310 ok(length($msg) > 0, "$desc valid CONTENT");
311 }
312
313 ###############################################################################
314
315 sub syslog_daemon {
316 my ($port, $t, $file) = @_;
317
318 my $s = IO::Socket::INET->new(
319 Proto => 'udp',
320 LocalAddr => "127.0.0.1:$port"
321 );
322
323 open my $fh, '>', $t->testdir() . '/' . $file;
324 select $fh; $| = 1;
325
326 while (1) {
327 my $buffer;
328 $s->recv($buffer, 4096);
329 print $fh $buffer . "\n";
330 }
331 }
332
333 ###############################################################################