Perlbal/Test.pm

filter: Statementstmt 83.3% 120 / 144 Branchbran 68.1% 45 / 66 Conditioncond 70.8% 17 / 24 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 86.9% 20 / 23 Podpod 0.0% 0 / 14 totaltotal 72.8% 209 / 287 CC 43 SCAR 18.7
CC
43
Cov
78%
resp_from_sock
13.2
dump_res
12.0
start_server
9.9
CRAP 6.5
« Perlbal/Plugin/Include.pm ↑ Summary Perlbal/Plugin/Redirect.pm »
1 #!/usr/bin/perl -w
2  
3 package Perlbal::Test;
4  
5 =head1 NAME
6  
7 Perlbal::Test - Test harness for perlbal server
8  
9 =head1 SYNOPSIS
10  
11 # my $msock = Perlbal::Test::start_server();
12  
13 =head1 DESCRIPTION
14  
15 Perlbal::Test provides access to a perlbal server running on the
16 local host, for testing purposes.
17  
18 The server can be an already-existing server, a child process, or
19 the current process.
20  
21 Various functions are provided to interact with the server.
22  
23 =head1 FUNCTIONS
24  
25 =cut
26  
27 76 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 76 ▶use POSIX qw( :sys_wait_h );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 76 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 76 ▶use Socket qw(MSG_NOSIGNAL IPPROTO_TCP TCP_NODELAY SOL_SOCKET);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 76 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 76 require Exporter;
34 76 ▶use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 76 @ISA = qw(Exporter);
36 76 @EXPORT = qw(ua start_server foreach_aio manage filecontent tempdir new_port
37 manage_multi
38 mgmt_port wait_on_child dump_res resp_from_sock msock);
39  
40 76 our $i_am_parent = 0;
41 76 our $msock; # management sock of child
42 76 our $to_kill = 0;
43 76 our $mgmt_port;
44  
45 76 our $free_port = 60000;
46  
47 =head1 I<mgmt_port()>
48  
49 Return the current management port number.
50  
51 =cut
52  
53 0 ▶sub mgmt_port {
Subroutine mgmt_port: not called CC=1 SCAR=6.9
Pod undocumented
54 0 return $mgmt_port;
55 }
56  
57 76 ▶END {
Subroutine END: called CC=2 SCAR=6.9
58 76 ▶ manage("shutdown") if $i_am_parent;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::Test::i_am_parent 18 58 76
59 }
60  
61 =head1 I<dump_res($http_response)>
62  
63 Return a readable string formatted from an HTTP::Response object.
64 Only the first 80 characters of returned content are returned.
65  
66 =cut
67  
68 0 ▶sub dump_res {
Subroutine dump_res: not called CC=3 SCAR=24.8
Pod undocumented
69 0 my $res = shift;
70 0 my ($pkg, $filename, $line) = caller;
71 0 my $ret = "$filename:$line ==> ";
72 0 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
73 0 $ret .= "[response undefined]\n";
74 0 return $ret;
75 }
76 0 my $ct = $res->content;
77 0 my $len = length $ct;
78 0 ▶ if ($len > 80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 80) 0 0 0
79 0 $ct = substr($ct, 0, 80) . "...";
80 }
81 0 my $status = $res->status_line;
82 0 $status =~ s/[\r\n]//g;
83 0 return $ret . "status=[$status] content=$len" . "[$ct]\n";
84 }
85  
86 =head1 I<tempdir()>
87  
88 Return a newly created temporary directory. The directory will be
89 removed automatically upon program exit.
90  
91 =cut
92  
93 29 ▶sub tempdir {
Subroutine tempdir: called CC=1 SCAR=0.0
Pod undocumented
94 29 require File::Temp;
95 29 return File::Temp::tempdir( CLEANUP => 1 );
96 }
97  
98 =head1 I<new_port()>
99  
100 Return the next free port number in the series. Port numbers are assigned
101 starting at 60000.
102  
103 =cut
104  
105 1086 ▶sub new_port {
Subroutine new_port: called CC=2 SCAR=6.9
Pod undocumented
106 1086 ▶ test_port() ? return $free_port++ : return new_port($free_port++);
Branch: 1 branch
BranchTrueFalseTotal
test_port() ? : 171 915 1086
107 }
108  
109 =head1 I<test_port()>
110  
111 Return 1 if the port is free to use for listening on $free_port else return 0.
112  
113 =cut
114  
115 1086 ▶sub test_port {
Subroutine test_port: called CC=2 SCAR=6.9
Pod undocumented
116 1086 ▶ my $sock = IO::Socket::INET->new(LocalPort => $free_port) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("LocalPort", $Perlbal::Test::free_port) or return 0 171 915 1086
117 171 $sock->close();
118 171 return 1;
119 }
120  
121 =head1 I<filecontent($file>>
122  
123 Return a string containing the contents of the file $file. If $file
124 cannot be opened, then return undef.
125  
126 =cut
127  
128 12 ▶sub filecontent {
Subroutine filecontent: called CC=2 SCAR=7.0
Pod undocumented
129 12 my $file = shift;
130 12 my $ct;
131 12 ▶ open (F, $file) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open F, $file or return undef 12 0 12
132 12 $ct = do { local $/; <F>; };
133 12 close F;
134 12 return $ct;
135 }
136  
137 =head1 I<foreach_aio($callback)>
138  
139 Set the server into each AIO mode (none, ioaio) and call the specified
140 callback function with the mode name as argument.
141  
142 =cut
143  
144 4 ▶sub foreach_aio (&) {
Subroutine foreach_aio: called CC=3 SCAR=11.0
Pod undocumented
145 4 my $cb = shift;
146  
147 4 foreach my $mode (qw(none ioaio)) {
148 8 my $line = manage("SERVER aio_mode = $mode");
149 8 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 4 4 8
150 4 $cb->($mode);
151 }
152 }
153  
154 =head1 I<manage($cmd, %opts)>
155  
156 Send a command $cmd to the server, and return the response line from
157 the server.
158  
159 Optional arguments are:
160  
161 quiet_failure => 1
162  
163 Output a warning if the response indicated an error,
164 unless $opts{quiet_failure} is true, or the command
165 was 'shutdown' (which doesn't return a response).
166  
167 =cut
168  
169 99 ▶sub manage {
Subroutine manage: called CC=5 SCAR=16.1
Pod undocumented
170 99 my $cmd = shift;
171 99 my %opts = @_;
172  
173 99 print $msock "$cmd\r\n";
174 99 my $res = <$msock>;
175  
176 99 ▶ if (!$res || $res =~ /^ERR/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res or $res =~ /^ERR/) 29 70 99
Condition100%3/3
not $res or $res =~ /^ERR/
l!l&&r!l&&!r
181170
MC/DC100%2/2
not $res or $res =~ /^ERR/
not $res $res =~ /^ERR/
Truth table100%3/3
not(A) or B
A= not $resB= $res =~ /^ERR/
ABresult
1X1
011
000
177 # Make the result visible in failure cases, unless
178 # the command was 'shutdown'... cause that never
179 # returns anything.
180 warn "Manage command failed: '$cmd' '$res'\n"
181 29 ▶ unless($opts{quiet_failure} || $cmd eq 'shutdown');
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"quiet_failure"} or $cmd eq "shutdown" 6 23 29
Condition100%3/3
$opts{"quiet_failure"} or $cmd eq "shutdown"
l!l&&r!l&&!r
5186
MC/DC100%2/2
$opts{"quiet_failure"} or $cmd eq "shutdown"
$opts{"quiet_failure"} $cmd eq "shutdown"
Truth table100%3/3
A or B
A= $opts{"quiet_failure"}B= $cmd eq "shutdown"
ABresult
1X1
011
000
182  
183 29 return 0;
184 }
185 70 return $res;
186 }
187  
188 =head1 I<manage_multi($cmd)>
189  
190 Send a command $cmd to the server, and return a multi-line
191 response. Return the number zero if there was an error or
192 no response.
193  
194 =cut
195  
196 1 ▶sub manage_multi {
Subroutine manage_multi: called CC=6 SCAR=18.8
Pod undocumented
197 1 my $cmd = shift;
198  
199 1 print $msock "$cmd\r\n";
200 1 my $res;
201 1 ▶ while (<$msock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $Perlbal::Test::msock)) 5 0 5
202 5 ▶ last if /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if /^\./ 1 4 5
203 4 ▶ last if /^ERROR/;
Branch: 1 branch
BranchTrueFalseTotal
if /^ERROR/ 0 4 4
204 4 $res .= $_;
205 }
206 1 ▶ return 0 if !$res || $res =~ /^ERR/;
Branch: 1 branch
BranchTrueFalseTotal
if not $res or $res =~ /^ERR/ 0 1 1
Condition33%1/3
not $res or $res =~ /^ERR/
l!l&&r!l&&!r
001
MC/DC0%0/2
not $res or $res =~ /^ERR/
not $res $res =~ /^ERR/
Truth table33%1/3
not(A) or B
A= not $resB= $res =~ /^ERR/
ABresult
1X1
011
000
207 1 return $res;
208 }
209  
210 =head1 I<start_server($conf)>
211  
212 Optionally start a perlbal server and return a socket connected to its
213 management port.
214  
215 The argument $conf is a string specifying initial configuration
216 commands.
217  
218 If the environment variable TEST_PERLBAL_FOREGROUND is set to a true
219 value then a server will be started in the foreground, in which case
220 this function does not return. When the server function finishes,
221 exit() will be called to terminate the process.
222  
223 If the environment variable TEST_PERLBAL_USE_EXISTING is set to a true
224 value then a socket will be returned which is connected to an existing
225 server's management port.
226  
227 Otherwise, a child process is forked and a socket is returned which is
228 connected to the child's management port.
229  
230 The management port is assigned automatically, a new port number each
231 time this function is called. The starting port number is 60000.
232  
233 =cut
234  
235 37 ▶sub start_server {
Subroutine start_server: called CC=9 SCAR=22.9
Pod undocumented
236 37 my $conf = shift;
237 37 $mgmt_port = new_port();
238  
239 37 ▶ if ($ENV{'TEST_PERLBAL_FOREGROUND'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TEST_PERLBAL_FOREGROUND"}) 0 37 37
240 0 _start_perbal_server($conf, $mgmt_port);
241 }
242  
243 37 ▶ if ($ENV{'TEST_PERLBAL_USE_EXISTING'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TEST_PERLBAL_USE_EXISTING"}) 0 37 37
244 0 my $msock = wait_on_child(0, $mgmt_port);
245 0 return $msock;
246 }
247  
248 37 my $child = fork;
249 37 ▶ if ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 19 18 37
250 19 $i_am_parent = 1;
251 19 $to_kill = $child;
252 19 my $msock = wait_on_child($child, $mgmt_port);
253 19 my $rv = waitpid($child, WNOHANG);
254 19 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 19 19
255 0 die "Child process (webserver) died.\n";
256 }
257 19 print $msock "proc\r\n";
258 19 my $spid = undef;
259 19 ▶ while (<$msock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $msock)) 114 0 114
260 114 ▶ last if m!^\.\r?\n!;
Branch: 1 branch
BranchTrueFalseTotal
if /^\.\r?\n/ 19 95 114
261 95 ▶ next unless /^pid:\s+(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^pid:\s+(\d+)/ 76 19 95
262 19 $spid = $1;
263 }
264 19 ▶ die "Our child was $child, but we connected and it says it's $spid."
Branch: 1 branch
BranchTrueFalseTotal
unless $child == $spid 0 19 19
265 unless $child == $spid;
266  
267 19 return $msock;
268 }
269  
270 # child process...
271 18 _start_perbal_server($conf, $mgmt_port);
272 }
273  
274 # Start a perlbal server running and tell it to listen on the specified
275 # management port number. This function does not return.
276  
277 18 ▶sub _start_perbal_server {
Subroutine _start_perbal_server: called CC=3 SCAR=12.3
278 18 my ($conf, $mgmt_port) = @_;
279  
280 18 require Perlbal;
281  
282 18 $conf .= qq{
283 CREATE SERVICE mgmt
284 SET mgmt.listen = 127.0.0.1:$mgmt_port
285 SET mgmt.role = management
286 ENABLE mgmt
287 };
288  
289 0 ▶ my $out = sub { print STDOUT "$_[0]\n"; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
290 18 ▶ die "Configuration error" unless Perlbal::run_manage_commands($conf, $out);
Branch: 1 branch
BranchTrueFalseTotal
unless Perlbal::run_manage_commands($conf, $out) 0 18 18
291  
292 18 ▶ unless (Perlbal::Socket->WatchedSockets() > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Perlbal::Socket"->WatchedSockets > 0) 0 18 18
293 0 die "Invalid configuration. (shouldn't happen?) Stopping (self=$$).\n";
294 }
295  
296 18 Perlbal::run();
297 0 exit 0;
298 }
299  
300  
301 =head1 I<msock()>
302  
303 Return a reference to the socket connected to the server's management
304 port.
305  
306 =cut
307  
308 6 ▶sub msock {
Subroutine msock: called CC=1 SCAR=0.0
Pod undocumented
309 6 return $msock;
310 }
311  
312  
313 =head1 I<ua()>
314  
315 Return a new instance of LWP::UserAgent.
316  
317 =cut
318  
319 7 ▶sub ua {
Subroutine ua: called CC=1 SCAR=0.0
Pod undocumented
320 7 require LWP::UserAgent;
321 7 return LWP::UserAgent->new;
322 }
323  
324 =head1 I<wait_on_child($pid, $port)>
325  
326 Return a socket which is connected to a child process.
327  
328 $pid specifies the child process id, and $port is the port number on
329 which the child is listening.
330  
331 Several attempts are made; if the child dies or a connection cannot
332 be made within 5 seconds then this function dies with an error message.
333  
334 =cut
335  
336 64 ▶sub wait_on_child {
Subroutine wait_on_child: called CC=5 SCAR=17.0
Pod undocumented
337 64 my $pid = shift;
338 64 my $port = shift;
339  
340 64 my $start = time;
341 64 while (1) {
342 135 $msock = IO::Socket::INET->new(PeerAddr => "127.0.0.1:$port");
343 135 ▶ return $msock if $msock;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::Test::msock 64 71 135
344 71 select undef, undef, undef, 0.25;
345 71 ▶ if ($pid && waitpid($pid, WNOHANG) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid and waitpid($pid, 1) > 0) 0 71 71
Condition33%1/3
$pid and waitpid($pid, 1) > 0
!ll&&!rl&&r
0710
MC/DC0%0/2
$pid and waitpid($pid, 1) > 0
$pid waitpid($pid, 1) > 0
Truth table33%1/3
A and B
A= $pidB= waitpid($pid, 1) > 0
ABresult
0X0
100
111
346 0 die "Child process (webserver) died.\n";
347 }
348 71 ▶ die "Timeout waiting for port $port to startup" if time > $start + 5;
Branch: 1 branch
BranchTrueFalseTotal
if time > $start + 5 0 71 71
349 }
350 }
351  
352 =head1 I<resp_from_sock($sock)>
353  
354 Read an HTTP response from a socket and return it
355 as an HTTP::Response object
356  
357 In scalar mode, return only the $http_response object.
358  
359 In array mode, return an array of ($http_response, $firstline) where
360 $firstline is the first line read from the socket, for example:
361  
362 "HTTP/1.1 200 OK"
363  
364 =cut
365  
366 159 ▶sub resp_from_sock {
Subroutine resp_from_sock: called CC=12 SCAR=25.8
Pod undocumented
367 159 my $sock = shift;
368  
369 159 my $res = "";
370 159 my $firstline = undef;
371  
372 159 ▶ while (<$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 996 0 996
373 996 $res .= $_;
374 996 ▶ $firstline ||= $_;
Condition66%2/3
$firstline ||= $_
l!l&&r!l&&!r
8371590
MC/DC0%0/2
$firstline ||= $_
$firstline $_
Truth table66%2/3
A ||= B
A= $firstlineB= $_
ABresult
1X1
011
000
375 996 ▶ last if ! $_ || /^\r?\n/;
Branch: 1 branch
BranchTrueFalseTotal
if not $_ or /^\r?\n/ 159 837 996
Condition66%2/3
not $_ or /^\r?\n/
l!l&&r!l&&!r
0159837
MC/DC50%1/2
not $_ or /^\r?\n/
not $_ /^\r?\n/
Truth table66%2/3
not(A) or B
A= not $_B= /^\r?\n/
ABresult
1X1
011
000
376 }
377  
378 159 ▶ unless ($firstline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($firstline) 0 159 159
379 0 print STDERR "Didn't get a firstline in HTTP response.\n";
380 0 return undef;
381 }
382  
383 159 my $resp = HTTP::Response->parse($res);
384 159 ▶ return undef unless $resp;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp 0 159 159
385  
386 159 my $cl = $resp->header('Content-Length');
387 159 ▶ if (defined $cl && $cl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cl and $cl > 0) 148 11 159
Condition100%3/3
defined $cl and $cl > 0
!ll&&!rl&&r
29148
MC/DC100%2/2
defined $cl and $cl > 0
defined $cl $cl > 0
Truth table100%3/3
A and B
A= defined $clB= $cl > 0
ABresult
0X0
100
111
388 148 my $content = '';
389 148 my $rv;
390 148 ▶ while (($rv = read($sock, $content, $cl)) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($rv = read($sock, $content, $cl) and ($cl -= $rv) > 0) 0 148 148
Condition66%2/3
$rv = read($sock, $content, $cl) and ($cl -= $rv) > 0
!ll&&!rl&&r
11470
MC/DC0%0/2
$rv = read($sock, $content, $cl) and ($cl -= $rv) > 0
$rv = read($sock, $content, $cl) ($cl -= $rv) > 0
Truth table66%2/3
A and B
A= $rv = read($sock, $content, $cl)B= ($cl -= $rv) > 0
ABresult
0X0
100
111
391 ($cl -= $rv) > 0) {
392 # don't do anything, the loop is it
393 }
394 148 $resp->content($content);
395 }
396  
397 159 ▶ return wantarray ? ($resp, $firstline) : $resp;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 150 9 159
398 }
399  
400 76 1;
« Perlbal/Plugin/Include.pm ↑ Summary Perlbal/Plugin/Redirect.pm »