Mercurial > hg > nginx-tests
annotate stream_unix.t @ 1990:85d88cd5091c
Tests: SSI include stub tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 04 Jul 2024 17:42:55 +0300 |
parents | f3ba4c74de31 |
children |
rev | line source |
---|---|
828
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
2 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
3 # (C) Andrey Zelenkov |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
4 # (C) Nginx, Inc. |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
5 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
6 # Simple tests for stream with unix socket. |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
7 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
8 ############################################################################### |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
9 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
10 use warnings; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
11 use strict; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
12 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
13 use Test::More; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
14 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
15 BEGIN { use FindBin; chdir($FindBin::Bin); } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
16 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
17 use lib 'lib'; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
18 use Test::Nginx; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
19 use Test::Nginx::Stream qw/ stream /; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
20 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
21 ############################################################################### |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
22 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
23 select STDERR; $| = 1; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
24 select STDOUT; $| = 1; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
25 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
26 eval { require IO::Socket::UNIX; }; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
27 plan(skip_all => 'IO::Socket::UNIX not installed') if $@; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
28 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
29 my $t = Test::Nginx->new()->has(qw/stream unix/)->plan(2); |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
30 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
31 $t->write_file_expand('nginx.conf', <<'EOF'); |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
32 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
33 %%TEST_GLOBALS%% |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
34 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
35 daemon off; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
36 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
37 events { |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
38 } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
39 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
40 stream { |
1609
f3ba4c74de31
Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents:
974
diff
changeset
|
41 %%TEST_GLOBALS_STREAM%% |
f3ba4c74de31
Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents:
974
diff
changeset
|
42 |
828
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
43 upstream u { |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
44 server unix:%%TESTDIR%%/unix.sock; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
45 } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
46 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
47 server { |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
48 listen 127.0.0.1:8080; |
828
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
49 proxy_pass unix:%%TESTDIR%%/unix.sock; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
50 } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
51 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
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:8081; |
828
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
54 proxy_pass u; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
55 } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
56 } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
57 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
58 EOF |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
59 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
60 my $path = $t->testdir() . '/unix.sock'; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
61 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
62 $t->run_daemon(\&stream_daemon, $path); |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
63 $t->run(); |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
64 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
65 # wait for unix socket to appear |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
66 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
67 for (1 .. 50) { |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
68 last if -S $path; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
69 select undef, undef, undef, 0.1; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
70 } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
71 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
72 ############################################################################### |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
73 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
74 my $str = 'SEE-THIS'; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
75 |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
76 is(stream('127.0.0.1:' . port(8080))->io($str), $str, 'proxy'); |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
77 is(stream('127.0.0.1:' . port(8081))->io($str), $str, 'upstream'); |
828
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
78 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
79 ############################################################################### |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
80 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
81 sub stream_daemon { |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
82 my $server = IO::Socket::UNIX->new( |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
83 Proto => 'tcp', |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
84 Local => shift, |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
85 Listen => 5, |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
86 Reuse => 1 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
87 ) |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
88 or die "Can't create listening socket: $!\n"; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
89 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
90 local $SIG{PIPE} = 'IGNORE'; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
91 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
92 while (my $client = $server->accept()) { |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
93 $client->autoflush(1); |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
94 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
95 log2c("(new connection $client)"); |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
96 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
97 $client->sysread(my $buffer, 65536) or next; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
98 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
99 log2i("$client $buffer"); |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
100 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
101 log2o("$client $buffer"); |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
102 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
103 $client->syswrite($buffer); |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
104 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
105 close $client; |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
106 } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
107 } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
108 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
109 sub log2i { Test::Nginx::log_core('|| <<', @_); } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
110 sub log2o { Test::Nginx::log_core('|| >>', @_); } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
111 sub log2c { Test::Nginx::log_core('||', @_); } |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
112 |
68d5a68eb938
Tests: basic stream tests with unix socket.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff
changeset
|
113 ############################################################################### |