Mercurial > hg > nginx-tests
comparison stream_proxy_protocol.t @ 612:bae9850e566c
Tests: some stream haproxy protocol tests.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Wed, 17 Jun 2015 20:43:20 +0300 |
parents | |
children | 2d9f5f439598 |
comparison
equal
deleted
inserted
replaced
611:a0bcdc51d99b | 612:bae9850e566c |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Andrey Zelenkov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for stream proxy module with haproxy protocol. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use IO::Select; | |
16 use Socket qw/ CRLF /; | |
17 | |
18 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
19 | |
20 use lib 'lib'; | |
21 use Test::Nginx; | |
22 | |
23 ############################################################################### | |
24 | |
25 select STDERR; $| = 1; | |
26 select STDOUT; $| = 1; | |
27 | |
28 my $t = Test::Nginx->new()->has(qw/stream/)->plan(2) | |
29 ->write_file_expand('nginx.conf', <<'EOF'); | |
30 | |
31 %%TEST_GLOBALS%% | |
32 | |
33 daemon off; | |
34 | |
35 events { | |
36 } | |
37 | |
38 stream { | |
39 proxy_protocol on; | |
40 | |
41 server { | |
42 listen 127.0.0.1:8080; | |
43 proxy_pass 127.0.0.1:8081; | |
44 } | |
45 | |
46 server { | |
47 listen 127.0.0.1:8082; | |
48 proxy_pass 127.0.0.1:8081; | |
49 proxy_protocol off; | |
50 } | |
51 } | |
52 | |
53 EOF | |
54 | |
55 $t->run_daemon(\&stream_daemon); | |
56 $t->run()->waitforsocket('127.0.0.1:8081'); | |
57 | |
58 ############################################################################### | |
59 | |
60 my %r = stream_get('close'); | |
61 is($r{'data'}, "PROXY TCP4 127.0.0.1 127.0.0.1 $r{'sp'} 8080" . CRLF . "close", | |
62 'protocol on'); | |
63 | |
64 %r = stream_get('close', '127.0.0.1:8082'); | |
65 is($r{'data'}, 'close', 'protocol off'); | |
66 | |
67 ############################################################################### | |
68 | |
69 sub stream_get { | |
70 my ($data, $peer) = @_; | |
71 | |
72 my $s = stream_connect($peer); | |
73 my $sockport = $s->sockport; | |
74 stream_write($s, $data); | |
75 | |
76 $data = ''; | |
77 while (my $buf = stream_read($s)) { | |
78 $data .= $buf; | |
79 } | |
80 | |
81 return ('data' => $data, 'sp' => $sockport); | |
82 } | |
83 | |
84 sub stream_connect { | |
85 my $peer = shift; | |
86 my $s = IO::Socket::INET->new( | |
87 Proto => 'tcp', | |
88 PeerAddr => $peer || '127.0.0.1:8080' | |
89 ) | |
90 or die "Can't connect to nginx: $!\n"; | |
91 | |
92 return $s; | |
93 } | |
94 | |
95 sub stream_write { | |
96 my ($s, $message) = @_; | |
97 | |
98 local $SIG{PIPE} = 'IGNORE'; | |
99 | |
100 $s->blocking(0); | |
101 while (IO::Select->new($s)->can_write(1.5)) { | |
102 my $n = $s->syswrite($message); | |
103 last unless $n; | |
104 $message = substr($message, $n); | |
105 last unless length $message; | |
106 } | |
107 | |
108 if (length $message) { | |
109 $s->close(); | |
110 } | |
111 } | |
112 | |
113 sub stream_read { | |
114 my ($s) = @_; | |
115 my ($buf); | |
116 | |
117 $s->blocking(0); | |
118 if (IO::Select->new($s)->can_read(3)) { | |
119 $s->sysread($buf, 1024); | |
120 }; | |
121 | |
122 log_in($buf); | |
123 return $buf; | |
124 } | |
125 | |
126 ############################################################################### | |
127 | |
128 sub stream_daemon { | |
129 my $server = IO::Socket::INET->new( | |
130 Proto => 'tcp', | |
131 LocalAddr => '127.0.0.1:8081', | |
132 Listen => 5, | |
133 Reuse => 1 | |
134 ) | |
135 or die "Can't create listening socket: $!\n"; | |
136 | |
137 my $sel = IO::Select->new($server); | |
138 | |
139 local $SIG{PIPE} = 'IGNORE'; | |
140 | |
141 while (my @ready = $sel->can_read) { | |
142 foreach my $fh (@ready) { | |
143 if ($server == $fh) { | |
144 my $new = $fh->accept; | |
145 $new->autoflush(1); | |
146 $sel->add($new); | |
147 | |
148 } elsif (stream_handle_client($fh)) { | |
149 $sel->remove($fh); | |
150 $fh->close; | |
151 } | |
152 } | |
153 } | |
154 } | |
155 | |
156 sub stream_handle_client { | |
157 my ($client) = @_; | |
158 | |
159 log2c("(new connection $client)"); | |
160 | |
161 $client->sysread(my $buffer, 65536) or return 1; | |
162 | |
163 log2i("$client $buffer"); | |
164 | |
165 log2o("$client $buffer"); | |
166 | |
167 $client->syswrite($buffer); | |
168 | |
169 return $buffer =~ /close/; | |
170 } | |
171 | |
172 sub log2i { Test::Nginx::log_core('|| <<', @_); } | |
173 sub log2o { Test::Nginx::log_core('|| >>', @_); } | |
174 sub log2c { Test::Nginx::log_core('||', @_); } | |
175 | |
176 ############################################################################### |