comparison http_resolver_ipv4.t @ 1943:1786f49cca17

Tests: fixed spurious http_resolver_ipv4.t failures. Previously, to get resolved addresses, the test relied on cached random rotation in resolver, such that a chosen address is eventually changed, which may not happen after several client requests. For this reason, the test is rewritten to get all addresses by switching to the next upstream.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 22 Jan 2024 14:11:12 +0400
parents 0b06942f0b8b
children c287864444f8
comparison
equal deleted inserted replaced
1942:00124c7d0ef1 1943:1786f49cca17
21 ############################################################################### 21 ###############################################################################
22 22
23 select STDERR; $| = 1; 23 select STDERR; $| = 1;
24 select STDOUT; $| = 1; 24 select STDOUT; $| = 1;
25 25
26 my $t = Test::Nginx->new()->has(qw/http proxy rewrite/); 26 my $t = Test::Nginx->new()->has(qw/http proxy/);
27 27
28 $t->write_file_expand('nginx.conf', <<'EOF'); 28 $t->write_file_expand('nginx.conf', <<'EOF');
29 29
30 %%TEST_GLOBALS%% 30 %%TEST_GLOBALS%%
31 31
38 %%TEST_GLOBALS_HTTP%% 38 %%TEST_GLOBALS_HTTP%%
39 39
40 server { 40 server {
41 listen 127.0.0.1:8080; 41 listen 127.0.0.1:8080;
42 server_name localhost; 42 server_name localhost;
43
44 add_header X-Addr $upstream_addr always;
45 proxy_next_upstream http_403;
43 46
44 location / { 47 location / {
45 proxy_pass http://$arg_h:%%PORT_8081%%/; 48 proxy_pass http://$arg_h:%%PORT_8081%%/;
46 resolver 127.0.0.1:%%PORT_8980_UDP%% ipv4=on ipv6=on; 49 resolver 127.0.0.1:%%PORT_8980_UDP%% ipv4=on ipv6=on;
47 } 50 }
57 } 60 }
58 } 61 }
59 62
60 server { 63 server {
61 listen 127.0.0.1:8081; 64 listen 127.0.0.1:8081;
62 server_name localhost;
63
64 location / {
65 return 200 "ipv4";
66 }
67 }
68
69 server {
70 listen [::1]:%%PORT_8081%%; 65 listen [::1]:%%PORT_8081%%;
71 server_name localhost; 66 server_name localhost;
72 67
73 location / { 68 location / {
74 return 200 "ipv6"; 69 # return 403;
75 } 70 }
76 } 71 }
77 } 72 }
78 73
79 EOF 74 EOF
83 $t->run_daemon(\&dns_daemon, port(8980), $t); 78 $t->run_daemon(\&dns_daemon, port(8980), $t);
84 $t->waitforfile($t->testdir . '/' . port(8980)); 79 $t->waitforfile($t->testdir . '/' . port(8980));
85 80
86 ############################################################################### 81 ###############################################################################
87 82
88 like(many('/', 10), qr/ipv4: \d+, ipv6: \d+/, 'ipv4 ipv6'); 83 is(get('/'), '127.0.0.1:8081, [::1]:8081', 'ipv4 ipv6');
89 is(many('/ipv4', 10), 'ipv4: 10', 'ipv4 only'); 84 is(get('/ipv4'), '127.0.0.1:8081', 'ipv4 only');
90 is(many('/ipv6', 10), 'ipv6: 10', 'ipv6 only'); 85 is(get('/ipv6'), '[::1]:8081', 'ipv6 only');
91 86
92 ############################################################################### 87 ###############################################################################
93 88
94 sub many { 89 sub get {
95 my ($uri, $count) = @_; 90 my ($uri) = @_;
96 my %hits;
97 91
98 for (1 .. $count) { 92 my $r = http_get("$uri?h=example.com");
99 if (http_get("$uri?h=example.com") =~ /(ipv(4|6))/) {; 93 my ($addr) = $r =~ /X-Addr: (.+)\x0d/m;
100 $hits{$1} = 0 unless defined $hits{$1}; 94 return $addr;
101 $hits{$1}++;
102 }
103 }
104
105 return join ', ', map { $_ . ": " . $hits{$_} } sort keys %hits;
106 } 95 }
107 96
108 ############################################################################### 97 ###############################################################################
109 98
110 sub reply_handler { 99 sub reply_handler {