Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # (C) Nginx, Inc. |
| 4 | |
| 5 | # Tests for debug_connection with syslog. |
| 6 | |
| 7 | ############################################################################### |
| 8 | |
| 9 | use warnings; |
| 10 | use strict; |
| 11 | |
| 12 | use Test::More; |
| 13 | |
Sergey Kandaurov | 1d1b748 | 2017-02-10 12:41:43 +0300 | [diff] [blame] | 14 | use IO::Select; |
| 15 | |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 16 | BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 17 | |
| 18 | use lib 'lib'; |
| 19 | use Test::Nginx; |
| 20 | |
| 21 | ############################################################################### |
| 22 | |
| 23 | select STDERR; $| = 1; |
| 24 | select STDOUT; $| = 1; |
| 25 | |
Sergey Kandaurov | b972e0f | 2017-05-04 14:31:09 +0300 | [diff] [blame] | 26 | my $t = Test::Nginx->new()->has(qw/http --with-debug proxy/); |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 27 | |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 28 | $t->write_file_expand('nginx.conf', <<'EOF'); |
| 29 | |
| 30 | %%TEST_GLOBALS%% |
| 31 | |
| 32 | daemon off; |
| 33 | |
| 34 | events { |
| 35 | debug_connection ::1; |
| 36 | } |
| 37 | |
| 38 | http { |
| 39 | %%TEST_GLOBALS_HTTP%% |
| 40 | |
Andrey Zelenkov | 6503afd | 2017-09-13 19:04:25 +0300 | [diff] [blame] | 41 | 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 Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 43 | |
| 44 | server { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 45 | listen 127.0.0.1:8080; |
| 46 | listen [::1]:%%PORT_8080%%; |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 47 | server_name localhost; |
| 48 | |
| 49 | location /debug { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 50 | proxy_pass http://[::1]:%%PORT_8080%%/; |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | EOF |
| 56 | |
Sergey Kandaurov | 63ebb11 | 2017-06-15 19:42:26 +0300 | [diff] [blame] | 57 | $t->try_run('no inet6 support')->plan(5); |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 58 | |
| 59 | ############################################################################### |
| 60 | |
Sergey Kandaurov | d50adf9 | 2017-02-10 13:55:39 +0300 | [diff] [blame] | 61 | my ($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 Zelenkov | 6503afd | 2017-09-13 19:04:25 +0300 | [diff] [blame] | 67 | } port(8981), port(8982); |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 68 | |
Sergey Kandaurov | d50adf9 | 2017-02-10 13:55:39 +0300 | [diff] [blame] | 69 | is(get_syslog('/', $s1), '', 'no debug_connection syslog 1'); |
| 70 | is(get_syslog('/', $s2), '', 'no debug_connection syslog 2'); |
| 71 | |
| 72 | my @msgs = get_syslog('/debug', $s1, $s2); |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 73 | like($msgs[0], qr/\[debug\]/, 'debug_connection syslog 1'); |
| 74 | like($msgs[1], qr/\[debug\]/, 'debug_connection syslog 2'); |
| 75 | is($msgs[0], $msgs[1], 'debug_connection syslog1 syslog2 match'); |
| 76 | |
| 77 | ############################################################################### |
| 78 | |
| 79 | sub get_syslog { |
Sergey Kandaurov | d50adf9 | 2017-02-10 13:55:39 +0300 | [diff] [blame] | 80 | my ($uri, @s) = @_; |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 81 | my @data; |
| 82 | |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 83 | http_get($uri); |
| 84 | |
| 85 | map { |
| 86 | my $data = ''; |
Sergey Kandaurov | 1d1b748 | 2017-02-10 12:41:43 +0300 | [diff] [blame] | 87 | IO::Select->new($_)->can_read(1); |
| 88 | while (IO::Select->new($_)->can_read(0.1)) { |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 89 | my ($buffer); |
| 90 | sysread($_, $buffer, 4096); |
| 91 | $data .= $buffer; |
| 92 | } |
| 93 | push @data, $data; |
Sergey Kandaurov | e11d965 | 2014-05-21 17:43:42 +0400 | [diff] [blame] | 94 | } (@s); |
| 95 | |
| 96 | return $data[0] if scalar @data == 1; |
| 97 | return @data; |
| 98 | } |
| 99 | |
| 100 | ############################################################################### |