comparison error_log.t @ 300:d753dfcaa419

Tests: added tests for error_log.
author Sergey Kandaurov <pluknet@nginx.com>
date Fri, 28 Jun 2013 13:35:23 +0400
parents
children ad51e58c2d7a
comparison
equal deleted inserted replaced
299:44c42894fdfd 300:d753dfcaa419
1 #!/usr/bin/perl
2
3 # (C) Nginx, Inc
4
5 # Tests for error_log.
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 => 'not yet') unless $t->has_version('1.5.2');
28
29 $t->plan(25)->write_file_expand('nginx.conf', <<'EOF');
30
31 %%TEST_GLOBALS%%
32
33 daemon off;
34
35 events {
36 }
37
38 http {
39 %%TEST_GLOBALS_HTTP%%
40
41 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m;
42 limit_req zone=one;
43
44 server {
45 listen 127.0.0.1:8080;
46 server_name localhost;
47
48 location /debug {
49 error_log %%TESTDIR%%/e_debug_debug.log debug;
50 error_log %%TESTDIR%%/e_debug_info.log info;
51 error_log stderr debug;
52 }
53 location /info {
54 limit_req_log_level info;
55 error_log %%TESTDIR%%/e_info_debug.log debug;
56 error_log %%TESTDIR%%/e_info_info.log info;
57 error_log %%TESTDIR%%/e_info_notice.log notice;
58 error_log stderr info;
59 }
60 location /notice {
61 limit_req_log_level notice;
62 error_log %%TESTDIR%%/e_notice_info.log info;
63 error_log %%TESTDIR%%/e_notice_notice.log notice;
64 error_log %%TESTDIR%%/e_notice_warn.log warn;
65 error_log stderr notice;
66 }
67 location /warn {
68 limit_req_log_level warn;
69 error_log %%TESTDIR%%/e_warn_notice.log notice;
70 error_log %%TESTDIR%%/e_warn_warn.log warn;
71 error_log %%TESTDIR%%/e_warn_error.log error;
72 error_log stderr warn;
73 }
74 location /error {
75 error_log %%TESTDIR%%/e_error_warn.log warn;
76 error_log %%TESTDIR%%/e_error_error.log;
77 error_log %%TESTDIR%%/e_error_alert.log alert;
78 error_log stderr;
79 }
80
81 location /file_low {
82 error_log %%TESTDIR%%/e_multi_low.log warn;
83 error_log %%TESTDIR%%/e_multi_low.log;
84 }
85 location /file_dup {
86 error_log %%TESTDIR%%/e_multi.log;
87 error_log %%TESTDIR%%/e_multi.log;
88 }
89 location /file_high {
90 error_log %%TESTDIR%%/e_multi_high.log emerg;
91 error_log %%TESTDIR%%/e_multi_high.log;
92 }
93
94 location /stderr_low {
95 error_log stderr warn;
96 error_log stderr;
97 }
98 location /stderr_dup {
99 error_log stderr;
100 error_log stderr;
101 }
102 location /stderr_high {
103 error_log stderr emerg;
104 error_log stderr;
105 }
106 }
107 }
108
109 EOF
110
111 open OLDERR, ">&", \*STDERR;
112 open STDERR, '>', $t->testdir() . '/stderr' or die "Can't reopen STDERR: $!";
113 open my $stderr, '<', $t->testdir() . '/stderr'
114 or die "Can't open stderr file: $!";
115
116 $t->run();
117
118 open STDERR, ">&", \*OLDERR;
119
120 ###############################################################################
121
122 # charge limit_req
123
124 http_get('/');
125
126 SKIP: {
127
128 skip "no --with-debug", 3 unless $t->has_module('--with-debug');
129
130 http_get('/debug');
131 isnt(lines($t, 'e_debug_debug.log', '[debug]'), 0, 'file debug debug');
132 is(lines($t, 'e_debug_info.log', '[debug]'), 0, 'file debug info');
133 isnt(lines($t, 'stderr', '[debug]'), 0, 'stderr debug');
134
135 }
136
137 http_get('/info');
138 is(lines($t, 'e_info_debug.log', '[info]'), 1, 'file info debug');
139 is(lines($t, 'e_info_info.log', '[info]'), 1, 'file info info');
140 is(lines($t, 'e_info_notice.log', '[info]'), 0, 'file info notice');
141 is(lines($t, 'stderr', '[info]'), 1, 'stderr info');
142
143 http_get('/notice');
144 is(lines($t, 'e_notice_info.log', '[notice]'), 1, 'file notice info');
145 is(lines($t, 'e_notice_notice.log', '[notice]'), 1, 'file notice notice');
146 is(lines($t, 'e_notice_warn.log', '[notice]'), 0, 'file notice warn');
147 is(lines($t, 'stderr', '[notice]'), 1, 'stderr notice');
148
149 http_get('/warn');
150 is(lines($t, 'e_warn_notice.log', '[warn]'), 1, 'file warn notice');
151 is(lines($t, 'e_warn_warn.log', '[warn]'), 1, 'file warn warn');
152 is(lines($t, 'e_warn_error.log', '[warn]'), 0, 'file warn error');
153 is(lines($t, 'stderr', '[warn]'), 1, 'stderr warn');
154
155 http_get('/error');
156 is(lines($t, 'e_error_warn.log', '[error]'), 1, 'file error warn');
157 is(lines($t, 'e_error_error.log', '[error]'), 1, 'file error error');
158 is(lines($t, 'e_error_alert.log', '[error]'), 0, 'file error alert');
159 is(lines($t, 'stderr', '[error]'), 1, 'stderr error');
160
161 # count log messages emitted with various error_log levels
162
163 http_get('/file_low');
164 is(lines($t, 'e_multi_low.log', '[error]'), 2, 'file low');
165
166 http_get('/file_dup');
167 is(lines($t, 'e_multi.log', '[error]'), 2, 'file dup');
168
169 http_get('/file_high');
170 is(lines($t, 'e_multi_high.log', '[error]'), 1, 'file high');
171
172 http_get('/stderr_low');
173 is(lines($t, 'stderr', '[error]'), 2, 'stderr low');
174
175 http_get('/stderr_dup');
176 is(lines($t, 'stderr', '[error]'), 2, 'stderr dup');
177
178 http_get('/stderr_high');
179 is(lines($t, 'stderr', '[error]'), 1, 'stderr high');
180
181 ###############################################################################
182
183 sub lines {
184 my ($t, $file, $pattern) = @_;
185
186 if ($file eq 'stderr') {
187 return map { $_ =~ /\Q$pattern\E/ } (<$stderr>);
188 }
189
190 my $path = $t->testdir() . '/' . $file;
191 open my $fh, '<', $path or return "$!";
192 my $value = map { $_ =~ /\Q$pattern\E/ } (<$fh>);
193 close $fh;
194 return $value;
195 }
196
197 ###############################################################################