Mercurial > hg > nginx-tests
annotate upstream_ip_hash.t @ 1263:ea3c7659b6c1
Tests: handled early pidfile write on win32 in the run() routine.
In addition to the present waiting for pidfile, which is insufficient on win32
due to the CreateProcess model, and may lead to rare startup races, search now
for the certain error message which indicates started worker process.
This change allows tolerating moderate hiccups on win32 hosts.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 19 Dec 2017 19:55:01 +0300 |
parents | 882267679006 |
children | dab8b5252216 |
rev | line source |
---|---|
294
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
2 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
3 # (C) Maxim Dounin |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
4 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
5 # Tests for upstream ip_hash balancer. |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
6 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
7 ############################################################################### |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
8 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
9 use warnings; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
10 use strict; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
11 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
12 use Test::More; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
13 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
14 use Socket qw/ CRLF /; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
15 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
16 BEGIN { use FindBin; chdir($FindBin::Bin); } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
17 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
18 use lib 'lib'; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
19 use Test::Nginx; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
20 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
21 ############################################################################### |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
22 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
23 select STDERR; $| = 1; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
24 select STDOUT; $| = 1; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
25 |
302
07c600de2f90
Tests: added missing rewrite prerequisites.
Sergey Kandaurov <pluknet@nginx.com>
parents:
294
diff
changeset
|
26 my $t = Test::Nginx->new()->has(qw/http proxy upstream_ip_hash realip rewrite/) |
294
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
27 ->write_file_expand('nginx.conf', <<'EOF')->run(); |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
28 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
29 %%TEST_GLOBALS%% |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
30 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
31 daemon off; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
32 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
33 events { |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
34 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
35 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
36 http { |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
37 %%TEST_GLOBALS_HTTP%% |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
38 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
39 upstream u { |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
40 ip_hash; |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
41 server 127.0.0.1:8081; |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
42 server 127.0.0.1:8082; |
294
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
43 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
44 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
45 upstream u2 { |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
46 ip_hash; |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
47 server 127.0.0.1:8081; |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
48 server 127.0.0.1:8082; |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
49 server 127.0.0.1:8083; |
294
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
50 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
51 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
52 server { |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
53 listen 127.0.0.1:8080; |
294
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
54 server_name localhost; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
55 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
56 set_real_ip_from 127.0.0.0/8; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
57 add_header X-IP $remote_addr; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
58 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
59 location / { |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
60 proxy_pass http://u; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
61 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
62 location /u2 { |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
63 proxy_pass http://u2; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
64 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
65 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
66 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
67 server { |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
68 listen 127.0.0.1:8081; |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
69 listen 127.0.0.1:8082; |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
70 listen 127.0.0.1:8083; |
294
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
71 server_name localhost; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
72 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
73 location / { |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
74 add_header X-Port $server_port; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
75 return 204; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
76 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
77 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
78 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
79 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
80 EOF |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
81 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
82 plan(skip_all => 'no 127.0.0.1 on host') |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
83 if http_get('/') !~ /X-IP: 127.0.0.1/m; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
84 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
85 $t->plan(2); |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
86 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
87 ############################################################################### |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
88 |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
89 my @ports = my ($port1, $port2, $port3) = (port(8081), port(8082), port(8083)); |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
302
diff
changeset
|
90 |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
302
diff
changeset
|
91 is(many('/', 30), "$port1: 15, $port2: 15", 'ip_hash'); |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
302
diff
changeset
|
92 is(many('/u2', 30), "$port1: 10, $port2: 10, $port3: 10", 'ip_hash 3 peers'); |
294
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
93 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
94 ############################################################################### |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
95 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
96 sub many { |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
97 my ($uri, $count) = @_; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
98 my %ports; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
99 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
100 for my $i (1 .. $count) { |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
101 my $req = "GET $uri HTTP/1.0" . CRLF |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
102 . "X-Real-IP: 127.0.$i.2" . CRLF . CRLF; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
103 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
104 if (http($req) =~ /X-Port: (\d+)/) { |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
105 $ports{$1} = 0 unless defined $ports{$1}; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
106 $ports{$1}++; |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
107 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
108 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
109 |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
302
diff
changeset
|
110 my @keys = map { my $p = $_; grep { $p == $_ } keys %ports } @ports; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
302
diff
changeset
|
111 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
294
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
112 } |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
113 |
71e275487aeb
Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
114 ############################################################################### |