comparison mail_error_log.t @ 569:adbb9b5108aa

Tests: fixed mail error_log test timeouts similar to 22bade4c7e12.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 06 May 2015 11:22:21 +0300
parents a1c93746570d
children f7bc1f74970a
comparison
equal deleted inserted replaced
568:907e89fba9c3 569:adbb9b5108aa
9 9
10 use warnings; 10 use warnings;
11 use strict; 11 use strict;
12 12
13 use Test::More; 13 use Test::More;
14
15 use IO::Select;
14 16
15 BEGIN { use FindBin; chdir($FindBin::Bin); } 17 BEGIN { use FindBin; chdir($FindBin::Bin); }
16 18
17 use lib 'lib'; 19 use lib 'lib';
18 use Test::Nginx; 20 use Test::Nginx;
28 my $t = Test::Nginx->new()->has(qw/mail imap http rewrite/) 30 my $t = Test::Nginx->new()->has(qw/mail imap http rewrite/)
29 ->run_daemon(\&Test::Nginx::IMAP::imap_test_daemon); 31 ->run_daemon(\&Test::Nginx::IMAP::imap_test_daemon);
30 32
31 plan(skip_all => 'no error_log') unless $t->has_version('1.9.0'); 33 plan(skip_all => 'no error_log') unless $t->has_version('1.9.0');
32 34
33 $t->plan(29)->write_file_expand('nginx.conf', <<'EOF'); 35 $t->plan(30)->write_file_expand('nginx.conf', <<'EOF');
34 36
35 %%TEST_GLOBALS%% 37 %%TEST_GLOBALS%%
36 38
37 error_log %%TESTDIR%%/e_glob.log info; 39 error_log %%TESTDIR%%/e_glob.log info;
38 error_log %%TESTDIR%%/e_glob2.log info; 40 error_log %%TESTDIR%%/e_glob2.log info;
50 listen 127.0.0.1:8143; 52 listen 127.0.0.1:8143;
51 protocol imap; 53 protocol imap;
52 54
53 error_log %%TESTDIR%%/e_debug.log debug; 55 error_log %%TESTDIR%%/e_debug.log debug;
54 error_log %%TESTDIR%%/e_info.log info; 56 error_log %%TESTDIR%%/e_info.log info;
57 error_log syslog:server=127.0.0.1:8080 info;
55 error_log syslog:server=127.0.0.1:8082 info; 58 error_log syslog:server=127.0.0.1:8082 info;
56 error_log stderr info; 59 error_log stderr info;
57 } 60 }
58 } 61 }
59 62
117 is_deeply(levels($t, 'e_glob.log'), levels($t, 'e_glob2.log'), 120 is_deeply(levels($t, 'e_glob.log'), levels($t, 'e_glob2.log'),
118 'multiple error global'); 121 'multiple error global');
119 122
120 # syslog 123 # syslog
121 124
122 parse_syslog_message('syslog', $t->read_file('s_info.log')); 125 parse_syslog_message('syslog', get_syslog());
123 126
124 is_deeply(levels($t, 's_glob.log'), levels($t, 'e_glob.log'), 127 is_deeply(levels($t, 's_glob.log'), levels($t, 'e_glob.log'),
125 'global syslog messages'); 128 'global syslog messages');
126 is_deeply(levels($t, 's_info.log'), levels($t, 'e_info.log'), 129 is_deeply(levels($t, 's_info.log'), levels($t, 'e_info.log'),
127 'mail syslog messages'); 130 'mail syslog messages');
149 map { $levels_hash{$_}++; } ($t->read_file($file) =~ /(\[\w+\])/g); 152 map { $levels_hash{$_}++; } ($t->read_file($file) =~ /(\[\w+\])/g);
150 153
151 return \%levels_hash; 154 return \%levels_hash;
152 } 155 }
153 156
157 sub get_syslog {
158 my ($port) = @_;
159 my $data = '';
160 my ($s);
161
162 $port = 8080 unless defined $port;
163
164 eval {
165 local $SIG{ALRM} = sub { die "timeout\n" };
166 local $SIG{PIPE} = sub { die "sigpipe\n" };
167 alarm(1);
168 $s = IO::Socket::INET->new(
169 Proto => 'udp',
170 LocalAddr => "127.0.0.1:$port"
171 );
172 alarm(0);
173 };
174 alarm(0);
175 if ($@) {
176 log_in("died: $@");
177 return undef;
178 }
179
180 Test::Nginx::IMAP->new();
181
182 IO::Select->new($s)->can_read(1.5);
183 while (IO::Select->new($s)->can_read(0.1)) {
184 my $buffer;
185 sysread($s, $buffer, 4096);
186 $data .= $buffer;
187 }
188 $s->close();
189 return $data;
190 }
191
154 sub parse_syslog_message { 192 sub parse_syslog_message {
155 my ($desc, $line) = @_; 193 my ($desc, $line) = @_;
194
195 ok($line, $desc);
196
197 SKIP: {
198 skip "$desc timeout", 18 unless $line;
156 199
157 my @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 200 my @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
158 'Sep', 'Oct', 'Nov', 'Dec'); 201 'Sep', 'Oct', 'Nov', 'Dec');
159 202
160 my ($pri, $mon, $mday, $hour, $minute, $sec, $host, $tag, $msg) = 203 my ($pri, $mon, $mday, $hour, $minute, $sec, $host, $tag, $msg) =
195 like($tag, qr'\w+', "$desc valid tag"); 238 like($tag, qr'\w+', "$desc valid tag");
196 239
197 ok(length($msg) > 0, "$desc valid CONTENT"); 240 ok(length($msg) > 0, "$desc valid CONTENT");
198 } 241 }
199 242
243 }
244
200 ############################################################################### 245 ###############################################################################
201 246
202 sub syslog_daemon { 247 sub syslog_daemon {
203 my ($port, $t, $file) = @_; 248 my ($port, $t, $file) = @_;
204 249