lib/Apache/TestServer.pm

filter: Statementstmt 21.1% 72 / 340 Branchbran 2.0% 2 / 100 Conditioncond 7.5% 4 / 53 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 42.8% 18 / 42 Podpod 3.8% 1 / 26 totaltotal 16.4% 97 / 591 CC 80 SCAR 46.6
CC
80
Cov
16%
start
462.0
stop
140.6
post_config
30.0
CRAP 105.1
« lib/Apache/TestSmoke.pm ↑ Summary lib/Apache/TestConfigPerl.pm »
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 package Apache::TestServer;
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use Apache::TestTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Apache::TestRun;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Apache::TestConfig ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Apache::TestRequest ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 ▶use constant COLOR => Apache::TestConfig::COLOR;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use constant WIN32 => Apache::TestConfig::WIN32;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 my $CTRL_M = COLOR ? "\r" : "\n";
34  
35 # some debuggers use the same syntax as others, so we reuse the same
36 # code by using the following mapping
37 1 my %debuggers = (
38 gdb => 'gdb',
39 ddd => 'gdb',
40 valgrind => 'valgrind',
41 strace => 'strace',
42 );
43  
44 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
45 2 my $class = shift;
46 2 my $config = shift;
47  
48 2 ▶ my $self = bless {
Condition33%1/3
$config || 'Apache::TestConfig'->thaw
l!l&&r!l&&!r
200
MC/DC0%0/2
$config || 'Apache::TestConfig'->thaw
$config 'Apache::TestConfig'->thaw
Truth table33%1/3
A || B
A= $configB= 'Apache::TestConfig'->thaw
ABresult
1X1
011
000
49 config => $config || Apache::TestConfig->thaw,
50 }, $class;
51  
52 $self->{name} = join ':',
53 2 map { $self->{config}->{vars}->{$_} } qw(servername port);
54  
55 2 $self->{port_counter} = $self->{config}->{vars}->{port};
56  
57 2 $self;
58 }
59  
60 # call this when you already know where httpd is
61 0 ▶sub post_config {
Subroutine post_config: not called CC=5 SCAR=34.0
Pod undocumented
62 0 my($self) = @_;
63  
64 0 ▶ $self->{version} = $self->{config}->httpd_version || '';
Condition0%0/2
$self->{'config'}->httpd_version || ''
l!l
00
MC/DC0%0/1
$self->{'config'}->httpd_version || ''
$self->{'config'}->httpd_version
Truth table0%0/2
A
A= $self->{'config'}->httpd_version
Aresult
11
00
65 0 ▶ $self->{mpm} = $self->{config}->httpd_mpm || '';
Condition0%0/2
$self->{'config'}->httpd_mpm || ''
l!l
00
MC/DC0%0/1
$self->{'config'}->httpd_mpm || ''
$self->{'config'}->httpd_mpm
Truth table0%0/2
A
A= $self->{'config'}->httpd_mpm
Aresult
11
00
66  
67 # try to get the revision number from the standard Apache version
68 # string and various variations made by distributions which mangle
69 # that string
70  
71 # Foo-Apache-Bar/x.y.z
72 0 ($self->{rev}) = $self->{version} =~ m|/(\d)\.|;
73  
74 0 ▶ if ($self->{rev}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'rev'}) { } 0 0 0
75 0 debug "Matched Apache revision $self->{version} $self->{rev}";
76 }
77 else {
78 # guessing is not good as it'll only mislead users
79 # and we can't die since a config object is required
80 # during Makefile.PL's write_perlscript when path to httpd may
81 # be unknown yet. so default to non-existing version 0 for now.
82 # and let TestRun.pm figure out the required pieces
83 0 debug "can't figure out Apache revision, from string: " .
84 "'$self->{version}', using a non-existing revision 0";
85 0 $self->{rev} = 0; # unknown
86 }
87  
88 0 ($self->{revminor}) = $self->{version} =~ m|/\d\.(\d)|;
89  
90 0 ▶ if ($self->{revminor}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'revminor'}) { } 0 0 0
91 0 debug "Matched Apache revminor $self->{version} $self->{revminor}";
92 }
93 else {
94 0 $self->{revminor} = 0;
95 }
96  
97 0 $self;
98 }
99  
100 0 ▶sub version_of {
Subroutine version_of: not called CC=2 SCAR=17.9
Pod undocumented
101 0 my($self, $thing) = @_;
102 die "Can't figure out what Apache server generation we are running"
103 0 ▶ unless $self->{rev};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rev"} 0 0 0
104  
105 0 $thing->{$self->{rev}};
106 }
107  
108 1 my @apache_logs = qw(
109 error_log access_log httpd.pid
110 apache_runtime_status rewrite_log
111 ssl_engine_log ssl_request_log
112 cgisock
113 );
114  
115 0 ▶sub clean {
Subroutine clean: not called CC=3 SCAR=24.8
Pod undocumented
116 0 my $self = shift;
117  
118 0 my $dir = $self->{config}->{vars}->{t_logs};
119  
120 0 for (@apache_logs) {
121 0 my $file = catfile $dir, $_;
122 0 ▶ if (unlink $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $file) 0 0 0
123 0 debug "unlink $file";
124 }
125 }
126 }
127  
128 1 ▶sub pid_file {
Subroutine pid_file: called CC=2 SCAR=7.6
Pod undocumented
129 1 my $self = shift;
130  
131 1 my $vars = $self->{config}->{vars};
132  
133 1 ▶ return $vars->{t_pid_file} || catfile $vars->{t_logs}, 'httpd.pid';
Condition33%1/3
$vars->{'t_pid_file'} || catfile($vars->{'t_logs'}, 'httpd.pid')
l!l&&r!l&&!r
100
MC/DC0%0/2
$vars->{'t_pid_file'} || catfile($vars->{'t_logs'}, 'httpd.pid')
$vars->{'t_pid_file'} catfile($vars->{'t_logs'}, 'httpd.pid')
Truth table33%1/3
A || B
A= $vars->{'t_pid_file'}B= catfile($vars->{'t_logs'}, 'httpd.pid')
ABresult
1X1
011
000
134 }
135  
136 0 ▶sub dversion {
Subroutine dversion: not called CC=3 SCAR=24.8
Pod undocumented
137 0 my $self = shift;
138  
139 0 my $dv = "-D APACHE$self->{rev}";
140  
141 0 ▶ if ($self->{rev} == 2 and $self->{revminor} == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rev"} == 2 and $self->{"revminor"} == 4) 0 0 0
Condition0%0/3
$self->{"rev"} == 2 and $self->{"revminor"} == 4
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"rev"} == 2 and $self->{"revminor"} == 4
$self->{"rev"} == 2 $self->{"revminor"} == 4
Truth table0%0/3
A and B
A= $self->{"rev"} == 2B= $self->{"revminor"} == 4
ABresult
0X0
100
111
142 0 $dv .= " -D APACHE2_4";
143 }
144  
145 0 return $dv;
146 }
147  
148 0 ▶sub config_defines {
Subroutine config_defines: not called CC=5 SCAR=34.0
Pod undocumented
149 0 my $self = shift;
150  
151 0 my @defines = ();
152  
153 0 for my $item (qw(useithreads)) {
154 0 ▶ next unless $Config{$item} and $Config{$item} eq 'define';
Branch: 1 branch
BranchTrueFalseTotal
unless $Apache::TestServer::Config{$item} and $Apache::TestServer::Config{$item} eq "define" 0 0 0
Condition0%0/3
$Apache::TestServer::Config{$item} and $Apache::TestServer::Config{$item} eq "define"
!ll&&!rl&&r
000
MC/DC0%0/2
$Apache::TestServer::Config{$item} and $Apache::TestServer::Config{$item} eq "define"
$Apache::TestServer::Config{$item} $Apache::TestServer::Config{$item} eq "define"
Truth table0%0/3
A and B
A= $Apache::TestServer::Config{$item}B= $Apache::TestServer::Config{$item} eq "define"
ABresult
0X0
100
111
155 0 push @defines, "-D PERL_\U$item";
156 }
157  
158 0 ▶ if (my $defines = $self->{config}->{vars}->{defines}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $defines = $self->{"config"}{"vars"}{"defines"}) 0 0 0
159 0 push @defines, map { "-D $_" } split " ", $defines;
160 }
161  
162 0 "@defines";
163 }
164  
165 0 ▶sub args {
Subroutine args: not called CC=1 SCAR=6.9
Pod undocumented
166 0 my $self = shift;
167 0 my $vars = $self->{config}->{vars};
168 0 my $dversion = $self->dversion; #for .conf version conditionals
169 0 my $defines = $self->config_defines;
170  
171 0 "-d $vars->{serverroot} -f $vars->{t_conf_file} $dversion $defines";
172 }
173  
174 1 my %one_process = (1 => '-X', 2 => '-D ONE_PROCESS');
175  
176 0 ▶sub start_cmd {
Subroutine start_cmd: not called CC=2 SCAR=17.9
Pod undocumented
177 0 my $self = shift;
178  
179 0 my $args = $self->args;
180 0 my $config = $self->{config};
181 0 my $vars = $config->{vars};
182 0 my $httpd = $vars->{httpd};
183  
184 0 ▶ my $one_process = $self->{run}->{opts}->{'one-process'}
Branch: 1 branch
BranchTrueFalseTotal
$self->{'run'}{'opts'}{'one-process'} ? : 0 0 0
185 ? $self->version_of(\%one_process)
186 : '';
187  
188 #XXX: threaded mpm does not respond to SIGTERM with -D ONE_PROCESS
189  
190 0 return "$httpd $one_process $args";
191 }
192  
193 0 ▶sub default_gdbinit {
Subroutine default_gdbinit: not called CC=3 SCAR=24.8
Pod undocumented
194 0 my $gdbinit = "";
195 0 my @sigs = qw(PIPE);
196  
197 0 for my $sig (@sigs) {
198 0 for my $flag (qw(pass nostop)) {
199 0 $gdbinit .= "handle SIG$sig $flag\n";
200 }
201 }
202  
203 0 $gdbinit;
204 }
205  
206 0 ▶sub strace_cmd {
Subroutine strace_cmd: not called CC=1 SCAR=6.9
Pod undocumented
207 0 my($self, $strace, $file) = @_;
208 #XXX truss, ktrace, etc.
209 0 "$strace -f -o $file -s1024";
210 }
211  
212 0 ▶sub valgrind_cmd {
Subroutine valgrind_cmd: not called CC=1 SCAR=6.9
Pod undocumented
213 0 my($self, $valgrind) = @_;
214 0 "$valgrind -v --leak-check=yes --show-reachable=yes --error-limit=no";
215 }
216  
217 0 ▶sub start_valgrind {
Subroutine start_valgrind: not called CC=1 SCAR=6.9
Pod undocumented
218 0 my $self = shift;
219 0 my $opts = shift;
220  
221 0 my $config = $self->{config};
222 0 my $args = $self->args;
223 0 my $one_process = $self->version_of(\%one_process);
224 0 my $valgrind_cmd = $self->valgrind_cmd($opts->{debugger});
225 0 my $httpd = $config->{vars}->{httpd};
226  
227 0 my $command = "$valgrind_cmd $httpd $one_process $args";
228  
229 0 debug $command;
230 0 system $command;
231 }
232  
233 0 ▶sub start_strace {
Subroutine start_strace: not called CC=1 SCAR=6.9
Pod undocumented
234 0 my $self = shift;
235 0 my $opts = shift;
236  
237 0 my $config = $self->{config};
238 0 my $args = $self->args;
239 0 my $one_process = $self->version_of(\%one_process);
240 0 my $file = catfile $config->{vars}->{t_logs}, 'strace.log';
241 0 my $strace_cmd = $self->strace_cmd($opts->{debugger}, $file);
242 0 my $httpd = $config->{vars}->{httpd};
243  
244 0 $config->genfile($file); #just mark for cleanup
245  
246 0 my $command = "$strace_cmd $httpd $one_process $args";
247  
248 0 debug $command;
249 0 system $command;
250 }
251  
252 0 ▶sub start_gdb {
Subroutine start_gdb: not called CC=5 SCAR=34.0
Pod undocumented
253 0 my $self = shift;
254 0 my $opts = shift;
255  
256 0 my $debugger = $opts->{debugger};
257 0 ▶ my @breakpoints = @{ $opts->{breakpoint} || [] };
Condition0%0/2
$opts->{'breakpoint'} || []
l!l
00
MC/DC0%0/1
$opts->{'breakpoint'} || []
$opts->{'breakpoint'}
Truth table0%0/2
A
A= $opts->{'breakpoint'}
Aresult
11
00
258 0 my $config = $self->{config};
259 0 my $args = $self->args;
260 0 my $one_process = $self->version_of(\%one_process);
261  
262 0 my $file = catfile $config->{vars}->{serverroot}, '.gdb-test-start';
263 0 my $fh = $config->genfile($file);
264  
265 0 print $fh default_gdbinit();
266  
267 0 ▶ if (@breakpoints) {
Branch: 1 branch
BranchTrueFalseTotal
if (@breakpoints) { } 0 0 0
268 0 print $fh "b ap_run_pre_config\n";
269 0 print $fh "run $one_process $args\n";
270 0 print $fh "finish\n";
271 0 for (@breakpoints) {
272 0 print $fh "b $_\n"
273 }
274 0 print $fh "continue\n";
275 }
276 else {
277 0 print $fh "run $one_process $args\n";
278 }
279 0 close $fh;
280  
281 0 my $command;
282 0 my $httpd = $config->{vars}->{httpd};
283  
284 0 ▶ if ($debugger eq 'ddd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($debugger eq 'ddd') { } 0 0 0
285 0 $command = qq{ddd --gdb --debugger "gdb -command $file" $httpd};
286 }
287 else {
288 ## defaults to gdb if not set in %ENV or via -debug
289 0 $command = "$debugger $httpd -command $file";
290 }
291  
292 0 $self->note_debugging;
293 0 debug $command;
294 0 system $command;
295  
296 0 unlink $file;
297 }
298  
299 0 ▶sub debugger_file {
Subroutine debugger_file: not called CC=1 SCAR=6.9
Pod undocumented
300 0 my $self = shift;
301 0 catfile $self->{config}->{vars}->{serverroot}, '.debugging';
302 }
303  
304 #make a note that the server is running under the debugger
305 #remove note when this process exits via END
306  
307 0 ▶sub note_debugging {
Subroutine note_debugging: not called CC=1 SCAR=6.9
Pod undocumented
308 0 my $self = shift;
309 0 my $file = $self->debugger_file;
310 0 my $fh = $self->{config}->genfile($file);
311 0 eval qq(END { unlink "$file" });
312 }
313  
314 0 ▶sub start_debugger {
Subroutine start_debugger: not called CC=4 SCAR=30.0
Pod undocumented
315 0 my $self = shift;
316 0 my $opts = shift;
317  
318 0 ▶ $opts->{debugger} ||= $ENV{MP_DEBUGGER} || 'gdb';
Condition0%0/5
$opts->{"debugger"} ||= $ENV{"MP_DEBUGGER"} || "gdb"
l!l&&r!l&&!r
000
$ENV{'MP_DEBUGGER'} || 'gdb'
l!l
00
MC/DC0%0/2
$opts->{"debugger"} ||= $ENV{"MP_DEBUGGER"} || "gdb"
$opts->{"debugger"} $ENV{'MP_DEBUGGER'}
Truth table0%0/3
A ||= B
A= $opts->{"debugger"}B= $ENV{'MP_DEBUGGER'}
ABresult
1X1
011
000
319  
320 # XXX: FreeBSD 5.2+
321 # gdb 6.1 and before segfaults when trying to
322 # debug httpd startup code. 6.5 has been proven
323 # to work. FreeBSD typically installs this as
324 # gdb65.
325 # Is it worth it to check the debugger and os version
326 # and die ?
327  
328 0 ▶ unless (grep { /^$opts->{debugger}/ } keys %debuggers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/^$opts->{"debugger"}/;} keys %debuggers) 0 0 0
329 0 error "$opts->{debugger} is not a supported debugger",
330 "These are the supported debuggers: ".
331 join ", ", sort keys %debuggers;
332 0 die("\n");
333 }
334  
335 0 my $debugger = $opts->{debugger};
336 0 $debugger =~ s/\d+$//;
337  
338 0 my $method = "start_" . $debuggers{$debugger};
339  
340 ## $opts->{debugger} is passed through unchanged
341 ## so when we try to run it next, its found.
342 0 $self->$method($opts);
343 }
344  
345 1 ▶sub pid {
Subroutine pid: called CC=5 SCAR=24.1
Pod undocumented
346 1 my $self = shift;
347 1 my $file = $self->pid_file;
348 1 my $fh = Symbol::gensym();
349 1 ▶ open $fh, $file or do {
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $file or do { return 0 } 0 1 1
350 1 return 0;
351 };
352  
353 # try to avoid the race condition when the pid file was created
354 # but not yet written to
355 0 for (1..8) {
356 0 ▶ last if -s $file > 0;
Branch: 1 branch
BranchTrueFalseTotal
if -s $file > 0 0 0 0
357 0 select undef, undef, undef, 0.25;
358 }
359  
360 0 ▶ chomp(my $pid = <$fh> || '');
Condition0%0/2
readline $fh || ''
l!l
00
MC/DC0%0/1
readline $fh || ''
readline $fh
Truth table0%0/2
A
A= readline $fh
Aresult
11
00
361 0 $pid;
362 }
363  
364 0 ▶sub select_next_port {
Subroutine select_next_port: not called CC=3 SCAR=24.8
Pod undocumented
365 0 my $self = shift;
366  
367 0 my $max_tries = 100; #XXX
368 0 ▶ while ($max_tries-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_tries-- > 0) 0 0 0
369 return $self->{port_counter}
370 0 ▶ if $self->port_available(++$self->{port_counter});
Branch: 1 branch
BranchTrueFalseTotal
if $self->port_available(++$self->{"port_counter"}) 0 0 0
371 }
372  
373 0 return 0;
374 }
375  
376 1 ▶sub port_available {
Subroutine port_available: called CC=5 SCAR=25.7
Pod undocumented
377 1 my $self = shift;
378 1 ▶ my $port = shift || $self->{config}->{vars}->{port};
Condition33%1/3
shift() || $self->{'config'}{'vars'}{'port'}
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $self->{'config'}{'vars'}{'port'}
shift() $self->{'config'}{'vars'}{'port'}
Truth table33%1/3
A || B
A= shift()B= $self->{'config'}{'vars'}{'port'}
ABresult
1X1
011
000
379 1 local *S;
380  
381 1 my $proto = getprotobyname('tcp');
382  
383 1 ▶ socket(S, Socket::PF_INET(),
Branch: 1 branch
BranchTrueFalseTotal
socket S, 2, 1, $proto or die "socket: $!" 0 0 0
384 Socket::SOCK_STREAM(), $proto) || die "socket: $!";
385 0 ▶ setsockopt(S, Socket::SOL_SOCKET(),
Branch: 1 branch
BranchTrueFalseTotal
setsockopt S, 1, 2, "\1\0\0\0" or die "setsockopt: $!" 0 0 0
386 Socket::SO_REUSEADDR(),
387 pack("l", 1)) || die "setsockopt: $!";
388  
389 0 ▶ if (bind(S, Socket::sockaddr_in($port, Socket::INADDR_ANY()))) {
Branch: 1 branch
BranchTrueFalseTotal
if (bind S, Socket::sockaddr_in($port, "\000\000\000\000")) { } 0 0 0
390 0 close S;
391 0 return 1;
392 }
393 else {
394 0 return 0;
395 }
396 }
397  
398 =head2 stop()
399  
400 attempt to stop the server.
401  
402 returns:
403  
404 on success: $pid of the server
405 on failure: -1
406  
407 =cut
408  
409 1 ▶sub stop {
Subroutine stop: called CC=14 SCAR=49.5
Pod documented
410 1 my $self = shift;
411 1 my $aborted = shift;
412  
413 1 if (WIN32) {
414 require Win32::Process;
415 my $obj = $self->{config}->{win32obj};
416 my $pid = -1;
417 if ($pid = $obj ? $obj->GetProcessID : $self->pid) {
418 if (kill(0, $pid)) {
419 Win32::Process::KillProcess($pid, 0);
420 warning "server $self->{name} shutdown";
421 }
422 }
423 unlink $self->pid_file if -e $self->pid_file;
424 return $pid;
425 }
426  
427 1 my $pid = 0;
428 1 my $tries = 3;
429 1 my $tried_kill = 0;
430  
431 1 my $port = $self->{config}->{vars}->{port};
432  
433 1 ▶ while ($self->ping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ping) 0 0 0
434 #my $state = $tried_kill ? "still" : "already";
435 #print "Port $port $state in use\n";
436  
437 0 ▶ if ($pid = $self->pid and !$tried_kill++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid = $self->pid and not $tried_kill++) { } 0 0 0
Condition0%0/3
$pid = $self->pid and not $tried_kill++
!ll&&!rl&&r
000
MC/DC0%0/2
$pid = $self->pid and not $tried_kill++
$pid = $self->pid not $tried_kill++
Truth table0%0/3
A and not(B)
A= $pid = $self->pidB= not $tried_kill++
ABresult
0X0
100
111
438 0 ▶ if (kill TERM => $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 'TERM', $pid) { } 0 0 0
439 0 warning "server $self->{name} shutdown";
440 0 sleep 1;
441  
442 0 for (1..6) {
443 0 ▶ if (! $self->ping) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ping) 0 0 0
444 0 ▶ if ($_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ == 1) 0 0 0
445 0 ▶ unlink $self->pid_file if -e $self->pid_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->pid_file 0 0 0
446 0 return $pid;
447 }
448 0 last;
449 }
450 0 ▶ if ($_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ == 1) { } 0 0 0
451 0 warning "port $port still in use...";
452 }
453 else {
454 0 print "...";
455 }
456 0 sleep $_;
457 }
458  
459 0 ▶ if ($self->ping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ping) { } 0 0 0
460 0 error "\nserver was shutdown but port $port ".
461 "is still in use, please shutdown the service ".
462 "using this port or select another port ".
463 "for the tests";
464 }
465 else {
466 0 print "done\n";
467 }
468 }
469 else {
470 0 error "kill $pid failed: $!";
471 }
472 }
473 else {
474 0 error "port $port is in use, ".
475 "cannot determine server pid to shutdown";
476 0 return -1;
477 }
478  
479 0 ▶ if (--$tries <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$tries <= 0) 0 0 0
480 0 error "cannot shutdown server on Port $port, ".
481 "please shutdown manually";
482 0 ▶ unlink $self->pid_file if -e $self->pid_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->pid_file 0 0 0
483 0 return -1;
484 }
485 }
486  
487 0 ▶ unlink $self->pid_file if -e $self->pid_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->pid_file 0 0 0
488 0 return $pid;
489 }
490  
491 1 ▶sub ping {
Subroutine ping: called CC=4 SCAR=20.4
Pod undocumented
492 1 my $self = shift;
493 1 my $pid = $self->pid;
494  
495 1 ▶ if ($pid and kill 0, $pid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid and kill 0, $pid) { } 0 1 1
elsif (not $self->port_available) { } 0 0 0
Condition33%1/3
$pid and kill 0, $pid
!ll&&!rl&&r
100
MC/DC0%0/2
$pid and kill 0, $pid
$pid kill 0, $pid
Truth table33%1/3
A and B
A= $pidB= kill 0, $pid
ABresult
0X0
100
111
496 0 return $pid;
497 }
498 elsif (! $self->port_available) {
499 0 return -1;
500 }
501  
502 0 return 0;
503 }
504  
505 0 ▶sub failed_msg {
Subroutine failed_msg: not called CC=2 SCAR=17.9
Pod undocumented
506 0 my $self = shift;
507 0 my($log, $rlog) = $self->{config}->error_log;
508 0 ▶ my $log_file_info = -e $log ?
Branch: 1 branch
BranchTrueFalseTotal
-e $log ? : 0 0 0
509 "please examine $rlog" :
510 "$rlog wasn't created, start the server in the debug mode";
511 0 error "@_ ($log_file_info)";
512 }
513  
514 #this doesn't work well on solaris or hpux at the moment
515 1 ▶use constant USE_SIGCHLD => $^O eq 'linux';
Subroutine BEGIN: called CC=1 SCAR=0.0
516  
517 0 ▶sub start {
Subroutine start: not called CC=21 SCAR=61.4
Pod undocumented
518 0 my $self = shift;
519  
520 0 my $old_pid = -1;
521 0 if (WIN32) {
522 # Stale PID files (e.g. left behind from a previous test run
523 # that crashed) cannot be trusted on Windows because PID's are
524 # re-used too frequently, so just remove it. If there is an old
525 # server still running then the attempt to start a new one below
526 # will simply fail because the port will be unavailable.
527 if (-f $self->pid_file) {
528 error "Removing old PID file -- " .
529 "Unclean shutdown of previous test run?\n";
530 unlink $self->pid_file;
531 }
532 $old_pid = 0;
533 }
534 else {
535 0 $old_pid = $self->stop;
536 }
537 0 my $cmd = $self->start_cmd;
538 0 my $config = $self->{config};
539 0 my $vars = $config->{vars};
540 0 ▶ my $httpd = $vars->{httpd} || 'unknown';
Condition0%0/2
$vars->{'httpd'} || 'unknown'
l!l
00
MC/DC0%0/1
$vars->{'httpd'} || 'unknown'
$vars->{'httpd'}
Truth table0%0/2
A
A= $vars->{'httpd'}
Aresult
11
00
541  
542 0 ▶ if ($old_pid == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_pid == -1) 0 0 0
543 0 return 0;
544 }
545  
546 0 local $| = 1;
547  
548 0 ▶ unless (-x $httpd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-x $httpd) 0 0 0
549 0 ▶ my $why = -e $httpd ? "is not executable" : "does not exist";
Branch: 1 branch
BranchTrueFalseTotal
-e $httpd ? : 0 0 0
550 0 error "cannot start server: httpd ($httpd) $why";
551 0 return 0;
552 }
553  
554 0 print "$cmd\n";
555 0 my $old_sig;
556  
557 0 if (WIN32) {
558 #make sure only 1 process is started for win32
559 #else Kill will only shutdown the parent
560 my $one_process = $self->version_of(\%one_process);
561 require Win32::Process;
562 my $obj;
563 # We need the "1" below to inherit the calling processes
564 # handles when running Apache::TestSmoke so as to properly
565 # dup STDOUT/STDERR
566 Win32::Process::Create($obj,
567 $httpd,
568 "$cmd $one_process",
569 1,
570 Win32::Process::NORMAL_PRIORITY_CLASS(),
571 '.');
572 unless ($obj) {
573 die "Could not start the server: " .
574 Win32::FormatMessage(Win32::GetLastError());
575 }
576 $config->{win32obj} = $obj;
577 }
578 else {
579 0 $old_sig = $SIG{CHLD};
580  
581 0 if (USE_SIGCHLD) {
582 # XXX: try not to be POSIX dependent
583 0 require POSIX;
584  
585 #XXX: this is not working well on solaris or hpux
586 0 ▶ $SIG{CHLD} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
587 0 ▶ while ((my $child = waitpid(-1, POSIX::WNOHANG())) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $child = waitpid(-1, &POSIX::WNOHANG())) > 0) 0 0 0
588 0 my $status = $? >> 8;
589 #error "got child exit $status";
590 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 0 0 0
591 0 my $msg = "server has died with status $status";
592 0 $self->failed_msg("\n$msg");
593 0 Apache::TestRun->new(test_config => $config)->scan_core;
594 0 kill SIGTERM => $$;
595 }
596 }
597 0 };
598 }
599  
600 0 ▶ defined(my $pid = fork) or die "Can't fork: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $pid = fork) or die "Can't fork: $!" 0 0 0
601 0 ▶ unless ($pid) { # child
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
602 0 my $status = system "$cmd";
603 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 0 0 0
604 0 $status = $? >> 8;
605 #error "httpd didn't start! $status";
606 }
607 0 CORE::exit $status;
608 }
609 }
610  
611 0 ▶ while ($old_pid and $old_pid == $self->pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_pid and $old_pid == $self->pid) 0 0 0
Condition0%0/3
$old_pid and $old_pid == $self->pid
!ll&&!rl&&r
000
MC/DC0%0/2
$old_pid and $old_pid == $self->pid
$old_pid $old_pid == $self->pid
Truth table0%0/3
A and B
A= $old_pidB= $old_pid == $self->pid
ABresult
0X0
100
111
612 0 warning "old pid file ($old_pid) still exists";
613 0 sleep 1;
614 }
615  
616 0 my $version = $self->{version};
617 0 ▶ my $mpm = $config->{mpm} || "";
Condition0%0/2
$config->{'mpm'} || ''
l!l
00
MC/DC0%0/1
$config->{'mpm'} || ''
$config->{'mpm'}
Truth table0%0/2
A
A= $config->{'mpm'}
Aresult
11
00
618 0 ▶ $mpm = "($mpm MPM)" if $mpm;
Branch: 1 branch
BranchTrueFalseTotal
if $mpm 0 0 0
619 0 print "using $version $mpm\n";
620  
621 my $timeout = $vars->{startup_timeout} ||
622 $ENV{APACHE_TEST_STARTUP_TIMEOUT} ||
623 0 ▶ 60;
Condition0%0/5
$vars->{'startup_timeout'} || $ENV{'APACHE_TEST_STARTUP_TIMEOUT'} || 60
l!l
00
$vars->{"startup_timeout"} or $ENV{"APACHE_TEST_STARTUP_TIMEOUT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$vars->{'startup_timeout'} || $ENV{'APACHE_TEST_STARTUP_TIMEOUT'} || 60
$vars->{"startup_timeout"} $ENV{"APACHE_TEST_STARTUP_TIMEOUT"}
Truth table0%0/3
A or B
A= $vars->{"startup_timeout"}B= $ENV{"APACHE_TEST_STARTUP_TIMEOUT"}
ABresult
1X1
011
000
624  
625 0 my $start_time = time;
626 0 my $preamble = "${CTRL_M}waiting $timeout seconds for server to start: ";
627 0 print $preamble unless COLOR;
628 0 while (1) {
629 0 my $delta = time - $start_time;
630 0 print COLOR
631 ? ($preamble, sprintf "%02d:%02d", (gmtime $delta)[1,0])
632 : '.';
633 0 sleep 1;
634 0 ▶ if ($self->pid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->pid) { } 0 0 0
elsif ($delta > $timeout) { } 0 0 0
635 0 print $preamble, "ok (waited $delta secs)\n";
636 0 last;
637 }
638 elsif ($delta > $timeout) {
639 0 my $suggestion = $timeout + 300;
640 0 print $preamble, "not ok\n";
641 0 error <<EOI;
642 giving up after $delta secs. If you think that your system
643 is slow or overloaded try again with a longer timeout value.
644 by setting the environment variable APACHE_TEST_STARTUP_TIMEOUT
645 to a high value (e.g. $suggestion) and repeat the last command.
646 EOI
647 0 last;
648 }
649 }
650  
651 # now that the server has started don't abort the test run if it
652 # dies
653 0 ▶ $SIG{CHLD} = $old_sig || 'DEFAULT';
Condition0%0/2
$old_sig || 'DEFAULT'
l!l
00
MC/DC0%0/1
$old_sig || 'DEFAULT'
$old_sig
Truth table0%0/2
A
A= $old_sig
Aresult
11
00
654  
655 0 ▶ if (my $pid = $self->pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $self->pid) { } 0 0 0
656 0 print "server $self->{name} started\n";
657  
658 0 my $vh = $config->{vhosts};
659 0 ▶ my $by_port = sub { $vh->{$a}->{port} <=> $vh->{$b}->{port} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
660  
661 0 for my $module (sort $by_port keys %$vh) {
662 0 print "server $vh->{$module}->{name} listening ($module)\n",
663 }
664  
665 0 ▶ if ($config->configure_proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->configure_proxy) 0 0 0
666 0 print "tests will be proxied through $vars->{proxy}\n";
667 }
668 }
669 else {
670 0 $self->failed_msg("server failed to start!");
671 0 return 0;
672 }
673  
674 0 ▶ return 1 if $self->wait_till_is_up($timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $self->wait_till_is_up($timeout) 0 0 0
675  
676 0 $self->failed_msg("failed to start server!");
677 0 return 0;
678 }
679  
680  
681 # wait till the server is up and return 1
682 # if the waiting times out returns 0
683 0 ▶sub wait_till_is_up {
Subroutine wait_till_is_up: not called CC=4 SCAR=30.0
Pod undocumented
684 0 my($self, $timeout) = @_;
685 0 my $config = $self->{config};
686 0 my $sleep_interval = 1; # secs
687  
688 0 ▶ my $server_up = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
689 0 ▶ local $SIG{__WARN__} = sub {}; #avoid "cannot connect ..." warnings
Subroutine __ANON__: not called CC=1 SCAR=6.9
690 # avoid fatal errors when LWP is not available
691 0 ▶ return eval {
Condition0%0/2
eval { do { my $r = Apache::TestRequest::GET('/index.html'); $r->code != 500 or not $r->header('client-warning') =~ /internal/i } } || 0
l!l
00
MC/DC0%0/1
eval { do { my $r = Apache::TestRequest::GET('/index.html'); $r->code != 500 or not $r->header('client-warning') =~ /internal/i } } || 0
eval { do { my $r = Apache::TestRequest::GET('/index.html'); $r->code != 500 or not $r->header('client-warning') =~ /internal/i } }
Truth table0%0/2
A
A= eval { do { my $r = Apache::TestRequest::GET('/index.html'); $r->code != 500 or not $r->header('client-warning') =~ /internal/i } }
Aresult
11
00
692 0 my $r=Apache::TestRequest::GET('/index.html');
693 0 ▶ $r->code!=500 or $r->header('client-warning')!~/internal/i;
Condition0%0/3
$r->code != 500 || !($r->header('client-warning') =~ /internal/i)
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->code != 500 || !($r->header('client-warning') =~ /internal/i)
$r->code != 500 !($r->header('client-warning') =~ /internal/i)
Truth table0%0/3
A || not(B)
A= $r->code != 500B= !($r->header('client-warning') =~ /internal/i)
ABresult
1X1
011
000
694 } || 0;
695 0 };
696  
697 0 ▶ if ($server_up->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$server_up()) 0 0 0
698 0 return 1;
699 }
700  
701 0 my $start_time = time;
702 0 my $preamble = "${CTRL_M}still waiting for server to warm up: ";
703 0 print $preamble unless COLOR;
704 0 while (1) {
705 0 my $delta = time - $start_time;
706 0 print COLOR
707 ? ($preamble, sprintf "%02d:%02d", (gmtime $delta)[1,0])
708 : '.';
709 0 sleep $sleep_interval;
710 0 ▶ if ($server_up->()) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$server_up()) { } 0 0 0
elsif ($delta > $timeout) { } 0 0 0
711 0 print "${CTRL_M}the server is up (waited $delta secs) \n";
712 0 return 1;
713 }
714 elsif ($delta > $timeout) {
715 0 print "${CTRL_M}the server is down, giving up after $delta secs\n";
716 0 return 0;
717 }
718 else {
719 # continue
720 }
721 }
722 }
723  
724 1 1;
« lib/Apache/TestSmoke.pm ↑ Summary lib/Apache/TestConfigPerl.pm »