Mercurial > hg > nginx-tests
comparison upstream_ip_hash.t @ 294:71e275487aeb
Tests: upstream least_conn and ip_hash tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sat, 08 Jun 2013 04:10:53 +0400 |
parents | |
children | 07c600de2f90 |
comparison
equal
deleted
inserted
replaced
293:719285b89d7e | 294:71e275487aeb |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for upstream ip_hash balancer. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 | |
14 use Socket qw/ CRLF /; | |
15 | |
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 | |
26 my $t = Test::Nginx->new()->has(qw/http proxy upstream_ip_hash realip/) | |
27 ->write_file_expand('nginx.conf', <<'EOF')->run(); | |
28 | |
29 %%TEST_GLOBALS%% | |
30 | |
31 daemon off; | |
32 | |
33 events { | |
34 } | |
35 | |
36 http { | |
37 %%TEST_GLOBALS_HTTP%% | |
38 | |
39 upstream u { | |
40 ip_hash; | |
41 server 127.0.0.1:8081; | |
42 server 127.0.0.1:8082; | |
43 } | |
44 | |
45 upstream u2 { | |
46 ip_hash; | |
47 server 127.0.0.1:8081; | |
48 server 127.0.0.1:8082; | |
49 server 127.0.0.1:8083; | |
50 } | |
51 | |
52 server { | |
53 listen 127.0.0.1:8080; | |
54 server_name localhost; | |
55 | |
56 set_real_ip_from 127.0.0.0/8; | |
57 add_header X-IP $remote_addr; | |
58 | |
59 location / { | |
60 proxy_pass http://u; | |
61 } | |
62 location /u2 { | |
63 proxy_pass http://u2; | |
64 } | |
65 } | |
66 | |
67 server { | |
68 listen 127.0.0.1:8081; | |
69 listen 127.0.0.1:8082; | |
70 listen 127.0.0.1:8083; | |
71 server_name localhost; | |
72 | |
73 location / { | |
74 add_header X-Port $server_port; | |
75 return 204; | |
76 } | |
77 } | |
78 } | |
79 | |
80 EOF | |
81 | |
82 plan(skip_all => 'no 127.0.0.1 on host') | |
83 if http_get('/') !~ /X-IP: 127.0.0.1/m; | |
84 | |
85 $t->plan(2); | |
86 | |
87 ############################################################################### | |
88 | |
89 is(many('/', 30), '8081: 15, 8082: 15', 'ip_hash'); | |
90 is(many('/u2', 30), '8081: 10, 8082: 10, 8083: 10', 'ip_hash 3 peers'); | |
91 | |
92 ############################################################################### | |
93 | |
94 sub many { | |
95 my ($uri, $count) = @_; | |
96 my %ports; | |
97 | |
98 for my $i (1 .. $count) { | |
99 my $req = "GET $uri HTTP/1.0" . CRLF | |
100 . "X-Real-IP: 127.0.$i.2" . CRLF . CRLF; | |
101 | |
102 if (http($req) =~ /X-Port: (\d+)/) { | |
103 $ports{$1} = 0 unless defined $ports{$1}; | |
104 $ports{$1}++; | |
105 } | |
106 } | |
107 | |
108 return join ', ', map { $_ . ": " . $ports{$_} } sort keys %ports; | |
109 } | |
110 | |
111 ############################################################################### |