Mercurial > hg > nginx-tests
comparison upstream_ip_hash_ipv6.t @ 1429:f7f387086278
Tests: upstream ip_hash tests with IPv6 and unix sockets.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 15 Jan 2019 12:32:18 +0300 |
parents | |
children | 40e5f2a0a238 |
comparison
equal
deleted
inserted
replaced
1428:4c43a0ebcd2d | 1429:f7f387086278 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 # (C) Sergey Kandaurov | |
5 # (C) Nginx, Inc. | |
6 | |
7 # Tests for upstream ip_hash balancer with IPv6 and unix sockets. | |
8 | |
9 ############################################################################### | |
10 | |
11 use warnings; | |
12 use strict; | |
13 | |
14 use Test::More; | |
15 | |
16 use Socket qw/ CRLF /; | |
17 | |
18 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
19 | |
20 use lib 'lib'; | |
21 use Test::Nginx; | |
22 | |
23 ############################################################################### | |
24 | |
25 select STDERR; $| = 1; | |
26 select STDOUT; $| = 1; | |
27 | |
28 my $t = Test::Nginx->new()->has(qw/http proxy upstream_ip_hash realip unix/) | |
29 ->write_file_expand('nginx.conf', <<'EOF')->run(); | |
30 | |
31 %%TEST_GLOBALS%% | |
32 | |
33 daemon off; | |
34 | |
35 events { | |
36 } | |
37 | |
38 http { | |
39 %%TEST_GLOBALS_HTTP%% | |
40 | |
41 upstream u { | |
42 ip_hash; | |
43 server 127.0.0.1:8081; | |
44 server 127.0.0.1:8082; | |
45 } | |
46 | |
47 server { | |
48 listen 127.0.0.1:8080; | |
49 server_name localhost; | |
50 | |
51 add_header X-IP $remote_addr always; | |
52 | |
53 location / { | |
54 set_real_ip_from 127.0.0.0/8; | |
55 proxy_pass http://u; | |
56 } | |
57 | |
58 location /unix { | |
59 proxy_pass http://unix:%%TESTDIR%%/unix.sock; | |
60 proxy_set_header X-Real-IP $http_x_real_ip; | |
61 } | |
62 | |
63 location /ipv6 { | |
64 proxy_pass http://[::1]:%%PORT_8080%%; | |
65 proxy_set_header X-Real-IP $http_x_real_ip; | |
66 } | |
67 } | |
68 | |
69 server { | |
70 listen unix:%%TESTDIR%%/unix.sock; | |
71 listen [::1]:%%PORT_8080%%; | |
72 server_name localhost; | |
73 | |
74 location / { | |
75 set_real_ip_from unix:; | |
76 set_real_ip_from ::1; | |
77 proxy_pass http://u; | |
78 } | |
79 | |
80 location /unix/none { | |
81 proxy_pass http://u; | |
82 } | |
83 } | |
84 | |
85 server { | |
86 listen 127.0.0.1:8081; | |
87 listen 127.0.0.1:8082; | |
88 server_name localhost; | |
89 | |
90 location / { | |
91 add_header X-Port $server_port always; | |
92 } | |
93 } | |
94 } | |
95 | |
96 EOF | |
97 | |
98 plan(skip_all => 'no 127.0.0.1 on host') | |
99 if http_get('/') !~ /X-IP: 127.0.0.1/m; | |
100 | |
101 $t->try_run('no inet6 support')->plan(4); | |
102 | |
103 ############################################################################### | |
104 | |
105 my @ports = my ($port1, $port2) = (port(8081), port(8082)); | |
106 | |
107 is(many('/unix', 30), "$port1: 15, $port2: 15", 'ip_hash realip via unix'); | |
108 is(many('/ipv6', 30), "$port1: 15, $port2: 15", 'ip_hash realip via ipv6'); | |
109 | |
110 is(many_ip6('/', 30), "$port1: 15, $port2: 15", 'ip_hash ipv6'); | |
111 like(many('/unix/none', 30), qr/($port1|$port2): 30/, 'ip_hash unix'); | |
112 | |
113 ############################################################################### | |
114 | |
115 sub many { | |
116 my ($uri, $count) = @_; | |
117 my %ports; | |
118 | |
119 for my $i (1 .. $count) { | |
120 my $req = "GET $uri HTTP/1.0" . CRLF | |
121 . "X-Real-IP: 127.0.$i.2" . CRLF . CRLF; | |
122 | |
123 if (http($req) =~ /X-Port: (\d+)/) { | |
124 $ports{$1} = 0 unless defined $ports{$1}; | |
125 $ports{$1}++; | |
126 } | |
127 } | |
128 | |
129 my @keys = map { my $p = $_; grep { $p == $_ } keys %ports } @ports; | |
130 return join ', ', map { $_ . ": " . $ports{$_} } @keys; | |
131 } | |
132 | |
133 sub many_ip6 { | |
134 my ($uri, $count) = @_; | |
135 my %ports; | |
136 | |
137 for my $i (1 .. $count) { | |
138 my $req = "GET $uri HTTP/1.0" . CRLF | |
139 . "X-Real-IP: ::$i" . CRLF . CRLF; | |
140 | |
141 if (http($req) =~ /X-Port: (\d+)/) { | |
142 $ports{$1} = 0 unless defined $ports{$1}; | |
143 $ports{$1}++; | |
144 } | |
145 } | |
146 | |
147 my @keys = map { my $p = $_; grep { $p == $_ } keys %ports } @ports; | |
148 return join ', ', map { $_ . ": " . $ports{$_} } @keys; | |
149 } | |
150 | |
151 ############################################################################### |