annotate proxy_implicit.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 d57c7877b618
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1633
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Sergey Kandaurov
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6 # Tests for an upstream implicitly defined by proxy_pass.
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 ###############################################################################
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10 use warnings;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use strict;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13 use Test::More;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15 use Socket qw/ SOCK_STREAM IPPROTO_TCP AF_INET6 /;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19 use lib 'lib';
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 use Test::Nginx;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22 ###############################################################################
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24 select STDERR; $| = 1;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25 select STDOUT; $| = 1;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
27 eval { die if $Socket::VERSION < 1.96; };
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28 plan(skip_all => 'Socket too old for getaddrinfo') if $@;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
29
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30 my $t = Test::Nginx->new()->has(qw/http proxy/);
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32 $t->write_file_expand('nginx.conf', <<'EOF');
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34 %%TEST_GLOBALS%%
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36 daemon off;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38 events {
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39 }
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41 http {
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
42 %%TEST_GLOBALS_HTTP%%
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 server {
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45 listen 127.0.0.1:8080;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 listen [::1]:%%PORT_8080%%;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47 server_name localhost;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49 location / {
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 proxy_pass http://localhost:%%PORT_8080%%/stub;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 proxy_next_upstream http_404;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52 add_header X-Addr $upstream_addr always;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 }
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 location /var {
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56 proxy_pass http://$arg_b/stub;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 proxy_next_upstream http_404;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58 add_header X-Addr $upstream_addr always;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59 }
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61 location /stub { }
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62 }
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 }
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65 EOF
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67 $t->try_run('no inet6 support')->plan(3);
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
68
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69 ###############################################################################
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71 my $p = port(8080);
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72 my @addrs = resolve('localhost');
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73 my $exp = qr/$addrs[0]:$p/ if @addrs == 1;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 my $v1 = "$addrs[0]:$p", my $v2 = "$addrs[1]:$p" if @addrs == 2;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75 $exp = qr/\Q$v1, $v2\E|\Q$v2, $v1\E/ if @addrs == 2;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76 die "too many addresses in localhost" if @addrs > 2;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 like(http_get('/'), qr/Not Found/, 'implicit upstream');
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 like(http_get('/'), $exp, 'implicit upstream all tried');
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80 like(http_get("/var?b=localhost:$p"), qr/Not Found/,
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81 'implicit upstream by variable');
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83 ###############################################################################
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
85 sub resolve {
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86 my ($name) = @_;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
87
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88 my $ai_addrconfig = eval { Socket::AI_ADDRCONFIG() };
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89 my ($err, @res) = Socket::getaddrinfo($name, "",
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90 { socktype => SOCK_STREAM, protocol => IPPROTO_TCP,
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
91 flags => $ai_addrconfig });
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
92 die "Cannot getaddrinfo - $err" if $err;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
94 my @addrs;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
95 foreach my $ai (@res) {
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
96 my ($err, $addr) = Socket::getnameinfo($ai->{addr},
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
97 Socket::NI_NUMERICHOST(), Socket::NIx_NOSERV());
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
98 die "Cannot getnameinfo - $err" if $err;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
99 $addr = '[' . $addr . ']' if $ai->{family} == AF_INET6;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
100 push @addrs, $addr;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
101 }
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
102 return @addrs;
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
103 }
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
104
d57c7877b618 Tests: basic proxy tests with implicit upstream.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
105 ###############################################################################