Mercurial > hg > nginx-tests
comparison stream_access_log.t @ 1024:91e64c1ceec9
Tests: stream access_log tests.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Mon, 05 Sep 2016 22:57:40 +0300 |
parents | |
children | 717030fd5a94 |
comparison
equal
deleted
inserted
replaced
1023:8533222fcfc1 | 1024:91e64c1ceec9 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for stream access_log module and variables. | |
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 use Test::Nginx::Stream qw/ stream /; | |
20 | |
21 ############################################################################### | |
22 | |
23 select STDERR; $| = 1; | |
24 select STDOUT; $| = 1; | |
25 | |
26 my $t = Test::Nginx->new()->has(qw/stream stream_map/); | |
27 | |
28 $t->write_file_expand('nginx.conf', <<'EOF'); | |
29 | |
30 %%TEST_GLOBALS%% | |
31 | |
32 daemon off; | |
33 | |
34 events { | |
35 } | |
36 | |
37 stream { | |
38 log_format test $server_addr; | |
39 log_format vars $connection:$nginx_version:$hostname:$pid; | |
40 log_format addr $binary_remote_addr:$remote_addr:$remote_port: | |
41 $server_addr:$server_port:$upstream_addr; | |
42 log_format date $msec!$time_local!$time_iso8601; | |
43 log_format byte $bytes_received:$bytes_sent: | |
44 $upstream_bytes_sent:$upstream_bytes_received; | |
45 log_format time $upstream_connect_time:$upstream_first_byte_time: | |
46 $upstream_session_time; | |
47 | |
48 access_log %%TESTDIR%%/off.log test; | |
49 | |
50 map $server_port $logme { | |
51 %%PORT_8083%% 1; | |
52 default 0; | |
53 } | |
54 | |
55 server { | |
56 listen 127.0.0.1:8081; | |
57 proxy_pass 127.0.0.1:8080; | |
58 access_log off; | |
59 } | |
60 | |
61 server { | |
62 listen 127.0.0.1:8082; | |
63 proxy_pass 127.0.0.1:8080; | |
64 access_log %%TESTDIR%%/time.log time; | |
65 } | |
66 | |
67 server { | |
68 listen 127.0.0.1:8083; | |
69 listen 127.0.0.1:8084; | |
70 proxy_pass 127.0.0.1:8080; | |
71 access_log %%TESTDIR%%/filtered.log test if=$logme; | |
72 } | |
73 | |
74 server { | |
75 listen 127.0.0.1:8085; | |
76 proxy_pass 127.0.0.1:8080; | |
77 access_log %%TESTDIR%%/complex.log test if=$logme$logme; | |
78 } | |
79 | |
80 server { | |
81 listen 127.0.0.1:8086; | |
82 proxy_pass 127.0.0.1:8080; | |
83 access_log %%TESTDIR%%/compressed.log test | |
84 gzip buffer=1m flush=100ms; | |
85 } | |
86 | |
87 server { | |
88 listen 127.0.0.1:8087; | |
89 proxy_pass 127.0.0.1:8080; | |
90 access_log %%TESTDIR%%/varlog_$bytes_sent.log test; | |
91 } | |
92 | |
93 server { | |
94 listen 127.0.0.1:8088; | |
95 proxy_pass 127.0.0.1:8080; | |
96 access_log %%TESTDIR%%/vars.log vars; | |
97 access_log %%TESTDIR%%/addr.log addr; | |
98 access_log %%TESTDIR%%/date.log date; | |
99 access_log %%TESTDIR%%/byte.log byte; | |
100 } | |
101 } | |
102 | |
103 EOF | |
104 | |
105 $t->run_daemon(\&stream_daemon); | |
106 $t->try_run('no stream access_log')->plan(10); | |
107 | |
108 $t->waitforsocket('127.0.0.1:' . port(8080)); | |
109 | |
110 ############################################################################### | |
111 | |
112 my $str = 'SEE-THIS'; | |
113 | |
114 stream('127.0.0.1:' . port(8081))->io($str); | |
115 stream('127.0.0.1:' . port(8082))->io($str); | |
116 stream('127.0.0.1:' . port(8083))->io($str); | |
117 stream('127.0.0.1:' . port(8084))->io($str); | |
118 stream('127.0.0.1:' . port(8085))->io($str); | |
119 stream('127.0.0.1:' . port(8086))->io($str); | |
120 stream('127.0.0.1:' . port(8087))->io($str); | |
121 | |
122 my $dport = port(8088); | |
123 my $s = stream("127.0.0.1:$dport"); | |
124 my $lhost = $s->sockhost(); | |
125 my $escaped = $s->sockaddr(); | |
126 $escaped =~ s/([^\x20-\x7e])/sprintf('\\x%02X', ord($1))/gmxe; | |
127 my $lport = $s->sockport(); | |
128 my $uport = port(8080); | |
129 | |
130 $s->io($str); | |
131 | |
132 # wait for file to appear with nonzero size thanks to the flush parameter | |
133 | |
134 for (1 .. 10) { | |
135 last if -s $t->testdir() . '/compressed.log'; | |
136 select undef, undef, undef, 0.1; | |
137 } | |
138 | |
139 # verify that "gzip" parameter turns on compression | |
140 | |
141 SKIP: { | |
142 eval { require IO::Uncompress::Gunzip; }; | |
143 skip("IO::Uncompress::Gunzip not installed", 1) if $@; | |
144 | |
145 my $gzipped = $t->read_file('compressed.log'); | |
146 my $log; | |
147 IO::Uncompress::Gunzip::gunzip(\$gzipped => \$log); | |
148 like($log, qr/^127.0.0.1/, 'compressed log - flush time'); | |
149 } | |
150 | |
151 # now verify all other logs | |
152 | |
153 $t->stop(); | |
154 | |
155 is($t->read_file('off.log'), '', 'log off'); | |
156 is($t->read_file('filtered.log'), "127.0.0.1\n", 'log filtering'); | |
157 ok($t->read_file('complex.log'), 'if with complex value'); | |
158 ok($t->read_file('varlog_3.log'), 'variable in file'); | |
159 | |
160 chomp(my $hostname = lc `hostname`); | |
161 like($t->read_file('vars.log'), qr/^\d+:[\d.]+:$hostname:\d+$/, 'log vars'); | |
162 is($t->read_file('addr.log'), | |
163 "$escaped:$lhost:$lport:127.0.0.1:$dport:127.0.0.1:$uport\n", | |
164 'log addr'); | |
165 like($t->read_file('date.log'), qr#^\d+.\d+![-+\w/: ]+![-+\dT:]+$#, 'log date'); | |
166 is($t->read_file('byte.log'), "8:3:8:3\n", 'log bytes'); | |
167 like($t->read_file('time.log'), qr/0\.\d{3}:0\.\d{3}:0\.\d{3}/, 'log time'); | |
168 | |
169 ############################################################################### | |
170 | |
171 sub stream_daemon { | |
172 my $server = IO::Socket::INET->new( | |
173 Proto => 'tcp', | |
174 LocalAddr => '127.0.0.1', | |
175 LocalPort => port(8080), | |
176 Listen => 5, | |
177 Reuse => 1 | |
178 ) | |
179 or die "Can't create listening socket: $!\n"; | |
180 | |
181 local $SIG{PIPE} = 'IGNORE'; | |
182 | |
183 while (my $client = $server->accept()) { | |
184 $client->autoflush(1); | |
185 | |
186 log2c("(new connection $client)"); | |
187 | |
188 $client->sysread(my $buffer, 65536) or next; | |
189 | |
190 log2i("$client $buffer"); | |
191 | |
192 $buffer = "ack"; | |
193 | |
194 log2o("$client $buffer"); | |
195 | |
196 $client->syswrite($buffer); | |
197 | |
198 close $client; | |
199 } | |
200 } | |
201 | |
202 sub log2i { Test::Nginx::log_core('|| <<', @_); } | |
203 sub log2o { Test::Nginx::log_core('|| >>', @_); } | |
204 sub log2c { Test::Nginx::log_core('||', @_); } | |
205 | |
206 ############################################################################### |