annotate upstream_ip_hash.t @ 1961:fe6f22da53ec default tip

Tests: tests for usage of discarded body. The client_max_body_size limit should be ignored when the request body is already discarded. In HTTP/1.x, this is done by checking the r->discard_body flag when the body is being discarded, and because r->headers_in.content_length_n is 0 when it's already discarded. This, however, does not happen with HTTP/2 and HTTP/3, and therefore "error_page 413" does not work without relaxing the limit. Further, with proxy_pass, r->headers_in.content_length_n is used to determine length of the request body, and therefore is not correct if discarding of the request body isn't yet complete. While discarding the request body, r->headers_in.content_length_n contains the rest of the body to discard (or, in case of chunked request body, the rest of the current chunk to discard). Similarly, the $content_length variable uses r->headers_in.content_length if available, and also incorrect. The $content_length variable is used when proxying with fastcgi_pass, grpc_pass, and uwsgi_pass (scgi_pass uses the value calculated based on the actual request body buffers, and therefore works correctly).
author Maxim Dounin <mdounin@mdounin.ru>
date Sat, 27 Apr 2024 18:55:50 +0300
parents dab8b5252216
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1392
dab8b5252216 Tests: added upstream ip_hash test with single peer.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
52 upstream s {
dab8b5252216 Tests: added upstream ip_hash test with single peer.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
53 ip_hash;
dab8b5252216 Tests: added upstream ip_hash test with single peer.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
54 server 127.0.0.1:8081;
dab8b5252216 Tests: added upstream ip_hash test with single peer.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
55 }
dab8b5252216 Tests: added upstream ip_hash test with single peer.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
56
294
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
57 server {
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
58 listen 127.0.0.1:8080;
294
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
59 server_name localhost;
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
60
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
61 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
62 add_header X-IP $remote_addr;
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
63
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
64 location / {
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
65 proxy_pass http://u;
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 location /u2 {
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
68 proxy_pass http://u2;
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
69 }
1392
dab8b5252216 Tests: added upstream ip_hash test with single peer.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
70 location /s {
dab8b5252216 Tests: added upstream ip_hash test with single peer.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
71 proxy_pass http://s;
dab8b5252216 Tests: added upstream ip_hash test with single peer.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
72 }
294
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
73 }
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
74
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
75 server {
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
76 listen 127.0.0.1:8081;
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
77 listen 127.0.0.1:8082;
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
78 listen 127.0.0.1:8083;
294
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
79 server_name localhost;
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
80
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
81 location / {
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
82 add_header X-Port $server_port;
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
83 return 204;
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 }
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 EOF
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
89
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
90 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
91 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
92
1392
dab8b5252216 Tests: added upstream ip_hash test with single peer.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
93 $t->plan(3);
294
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
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 952
diff changeset
97 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
98
e9064d691790 Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 302
diff changeset
99 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
100 is(many('/u2', 30), "$port1: 10, $port2: 10, $port3: 10", 'ip_hash 3 peers');
1392
dab8b5252216 Tests: added upstream ip_hash test with single peer.
Sergey Kandaurov <pluknet@nginx.com>
parents: 974
diff changeset
101 is(many('/s', 30), "$port1: 30", 'ip_hash single peer');
294
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
102
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
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
105 sub many {
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
106 my ($uri, $count) = @_;
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
107 my %ports;
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 for my $i (1 .. $count) {
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
110 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
111 . "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
112
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
113 if (http($req) =~ /X-Port: (\d+)/) {
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
114 $ports{$1} = 0 unless defined $ports{$1};
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
115 $ports{$1}++;
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
116 }
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
117 }
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
118
952
e9064d691790 Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 302
diff changeset
119 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
120 return join ', ', map { $_ . ": " . $ports{$_} } @keys;
294
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
121 }
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
122
71e275487aeb Tests: upstream least_conn and ip_hash tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
123 ###############################################################################