blob: 5b28d7a340ed2c797b17a1751938ee517fb67ff4 [file] [log] [blame]
Sergey Kandaurove11d9652014-05-21 17:43:42 +04001#!/usr/bin/perl
2
3# (C) Nginx, Inc.
4
5# Tests for debug_connection with syslog.
6
7###############################################################################
8
9use warnings;
10use strict;
11
12use Test::More;
13
Sergey Kandaurov1d1b7482017-02-10 12:41:43 +030014use IO::Select;
15
Sergey Kandaurove11d9652014-05-21 17:43:42 +040016BEGIN { use FindBin; chdir($FindBin::Bin); }
17
18use lib 'lib';
19use Test::Nginx;
20
21###############################################################################
22
23select STDERR; $| = 1;
24select STDOUT; $| = 1;
25
Sergey Kandaurovb972e0f2017-05-04 14:31:09 +030026my $t = Test::Nginx->new()->has(qw/http --with-debug proxy/);
Sergey Kandaurove11d9652014-05-21 17:43:42 +040027
Sergey Kandaurove11d9652014-05-21 17:43:42 +040028$t->write_file_expand('nginx.conf', <<'EOF');
29
30%%TEST_GLOBALS%%
31
32daemon off;
33
34events {
35 debug_connection ::1;
36}
37
38http {
39 %%TEST_GLOBALS_HTTP%%
40
Andrey Zelenkov6503afd2017-09-13 19:04:25 +030041 error_log syslog:server=127.0.0.1:%%PORT_8981_UDP%% alert;
42 error_log syslog:server=127.0.0.1:%%PORT_8982_UDP%% alert;
Sergey Kandaurove11d9652014-05-21 17:43:42 +040043
44 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030045 listen 127.0.0.1:8080;
46 listen [::1]:%%PORT_8080%%;
Sergey Kandaurove11d9652014-05-21 17:43:42 +040047 server_name localhost;
48
49 location /debug {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030050 proxy_pass http://[::1]:%%PORT_8080%%/;
Sergey Kandaurove11d9652014-05-21 17:43:42 +040051 }
52 }
53}
54
55EOF
56
Sergey Kandaurov63ebb112017-06-15 19:42:26 +030057$t->try_run('no inet6 support')->plan(5);
Sergey Kandaurove11d9652014-05-21 17:43:42 +040058
59###############################################################################
60
Sergey Kandaurovd50adf92017-02-10 13:55:39 +030061my ($s1, $s2) = map {
62 IO::Socket::INET->new(
63 Proto => 'udp',
64 LocalAddr => "127.0.0.1:$_"
65 )
66 or die "Can't open syslog socket $_: $!";
Andrey Zelenkov6503afd2017-09-13 19:04:25 +030067} port(8981), port(8982);
Sergey Kandaurove11d9652014-05-21 17:43:42 +040068
Sergey Kandaurovd50adf92017-02-10 13:55:39 +030069is(get_syslog('/', $s1), '', 'no debug_connection syslog 1');
70is(get_syslog('/', $s2), '', 'no debug_connection syslog 2');
71
72my @msgs = get_syslog('/debug', $s1, $s2);
Sergey Kandaurove11d9652014-05-21 17:43:42 +040073like($msgs[0], qr/\[debug\]/, 'debug_connection syslog 1');
74like($msgs[1], qr/\[debug\]/, 'debug_connection syslog 2');
75is($msgs[0], $msgs[1], 'debug_connection syslog1 syslog2 match');
76
77###############################################################################
78
79sub get_syslog {
Sergey Kandaurovd50adf92017-02-10 13:55:39 +030080 my ($uri, @s) = @_;
Sergey Kandaurove11d9652014-05-21 17:43:42 +040081 my @data;
82
Sergey Kandaurove11d9652014-05-21 17:43:42 +040083 http_get($uri);
84
85 map {
86 my $data = '';
Sergey Kandaurov1d1b7482017-02-10 12:41:43 +030087 IO::Select->new($_)->can_read(1);
88 while (IO::Select->new($_)->can_read(0.1)) {
Sergey Kandaurove11d9652014-05-21 17:43:42 +040089 my ($buffer);
90 sysread($_, $buffer, 4096);
91 $data .= $buffer;
92 }
93 push @data, $data;
Sergey Kandaurove11d9652014-05-21 17:43:42 +040094 } (@s);
95
96 return $data[0] if scalar @data == 1;
97 return @data;
98}
99
100###############################################################################