annotate proxy_protocol2_port.t @ 1427:eb1d883305ea

Tests: avoid edge cases in upstream random two test. Unavailable servers contribute to the number of attempts, if selected, before the balancer would fall back to the default round-robin method. This means that it's quite possible to get server with more connections. To facilitate with selecting two alive servers, down server was removed from the upstream configuration at the cost of slightly worse coverage.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 10 Jan 2019 17:42:34 +0300
parents 97c8280de681
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1309
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Andrey Zelenkov
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6 # Tests for proxy_protocol_port variable.
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 ###############################################################################
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10 use warnings;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use strict;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13 use Test::More;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17 use lib 'lib';
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18 use Test::Nginx;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 ###############################################################################
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22 select STDERR; $| = 1;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23 select STDOUT; $| = 1;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25 my $t = Test::Nginx->new()->has(qw/http realip/);
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
27 $t->write_file_expand('nginx.conf', <<'EOF')->plan(8);
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
29 %%TEST_GLOBALS%%
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31 daemon off;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33 events {
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34 }
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36 http {
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37 %%TEST_GLOBALS_HTTP%%
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39 log_format port $proxy_protocol_port;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41 server {
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
42 listen 127.0.0.1:8080 proxy_protocol;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43 server_name localhost;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45 add_header X-PP-Port $proxy_protocol_port;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 add_header X-Remote-Port $remote_port;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 location /pp {
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49 real_ip_header proxy_protocol;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 error_page 404 =200 /t;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52 location /pp/real {
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 set_real_ip_from 127.0.0.1/32;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 }
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 }
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 location /log {
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58 access_log %%TESTDIR%%/port.log port;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59 }
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60 }
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61 }
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 EOF
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65 $t->write_file('t', 'SEE-THIS');
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66 $t->run();
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
68 ###############################################################################
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70 my $p = pack("N3C", 0x0D0A0D0A, 0x000D0A51, 0x5549540A, 0x21);
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71 my $tcp4 = $p . pack("CnN2n2", 0x11, 12, 0xc0000201, 0xc0000202, 123, 5678);
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72 my $tcp6 = $p . pack("CnNx8NNx8Nn2", 0x21, 36,
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73 0x20010db8, 0x00000001, 0x20010db8, 0x00000002, 123, 5678);
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 my $unk = $p . pack("CnC4", 0x44, 4, 1, 2, 3, 4);
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76 # realip
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 like(pp_get('/pp', $tcp4), qr/X-PP-Port: 123\x0d/, 'pp port tcp4');
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 like(pp_get('/pp', $tcp6), qr/X-PP-Port: 123\x0d/, 'pp port tcp6');
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80 unlike(pp_get('/pp', $unk), qr/X-PP-Port/, 'pp port unknown');
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82 # remote_port
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 like(pp_get('/pp/real', $tcp4), qr/X-Remote-Port: 123\x0d/, 'remote port tcp4');
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
85 unlike(pp_get('/pp', $tcp4), qr/X-Remote-Port: 123\x0d/, 'no remote port tcp4');
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86 like(pp_get('/pp/real', $tcp6), qr/X-Remote-Port: 123\x0d/, 'remote port tcp6');
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
87 unlike(pp_get('/pp', $tcp6), qr/X-Remote-Port: 123\x0d/, 'no remote port tcp6');
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89 # log
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
91 pp_get('/log', $tcp4);
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
92
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93 $t->stop();
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
94
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
95 my $log = $t->read_file('/port.log');
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
96 chomp $log;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
97
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
98 is($log, 123, 'pp port log');
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
99
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
100 ###############################################################################
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
101
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
102 sub pp_get {
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
103 my ($url, $proxy) = @_;
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
104 return http($proxy . <<EOF);
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
105 GET $url HTTP/1.0
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
106 Host: localhost
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
107
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
108 EOF
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
109 }
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
110
3eb1de7e3b41 Tests: proxy protocol v2 tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
111 ###############################################################################