Apache.pm

filter: Statementstmt 68.6% 83 / 121 Branchbran 27.7% 5 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.2% 26 / 32 Podpod 100% 6 / 6 totaltotal 67.8% 120 / 177 CC 17 SCAR 15.7
CC
17
Cov
63%
DEMOLISH
23.5
run
6.5
get_pid
6.0
CRAP 4.8
↑ Summary Apache/Modules.pm »
1 package Test::Instance::Apache;
2  
3 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use File::Which qw/ which /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use IPC::System::Simple qw/ capture /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Net::EmptyPort qw/ empty_port /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Test::Instance::Apache::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Test::Instance::Apache::Modules;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 our $VERSION = '0.001';
17  
18 =head1 NAME
19  
20 Test::Instance::Apache - Create Apache instance for Testing
21  
22 =head1 SYNOPSIS
23  
24 use FindBin qw/ $Bin /;
25 use Test::Instance::Apache;
26  
27 my $instance = Test::Instance::Apache->new(
28 config => [
29 "VirtualHost *" => [
30 DocumentRoot => "$Bin/root",
31 ],
32 ],
33 modules => [ qw/ mpm_prefork authz_core mime / ],
34 );
35  
36 $instance->run;
37  
38 =head1 DESCRIPTION
39  
40 Test::Instance::Apache allows you to spin up a complete Apache instance for
41 testing. This is useful when developing various plugins for Apache, or if your
42 application is tightly integrated to the webserver.
43  
44 =head2 Attributes
45  
46 These are the attributes available on Test::Instance::Apache.
47  
48 =cut
49  
50 has _temp_dir => (
51 is => 'lazy',
52 1 ▶ builder => sub {
Subroutine _build__temp_dir: called CC=1 SCAR=0.0
53 1 return File::Temp->newdir;
54 },
55 4 );
56  
57 =head3 server_root
58  
59 The root folder for creating the Apache instance. This folder is passed to
60 Apache during instantiation as the server root configuration, and normally
61 contains all the configuration files for Apache. If not set during object
62 creation, a new folder will be created using File::Temp.
63  
64 =cut
65  
66 has server_root => (
67 is => 'lazy',
68 1 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_server_root: called CC=1 SCAR=0.0
69 1 my $self = shift;
70 1 return $self->_temp_dir->dirname;
71 },
72 4 );
73  
74 =head3 conf_dir
75  
76 The directory for holding the configuration files. Defaults to
77 C<$server_root/conf>. If set during object creation, then you will need to
78 create the folder manually.
79  
80 =cut
81  
82 has conf_dir => (
83 is => 'lazy',
84 1 ▶ builder => sub {
Subroutine _build_conf_dir: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
85 1 my $self = shift;
86 1 return $self->make_server_dir( 'conf' );
87 },
88 4 );
89  
90 =head3 log_dir
91  
92 The directory for holding all the log files. Defaults to C<$server_root/logs>.
93 If set during object creation, then you will need to create the folder
94 manually.
95  
96 =cut
97  
98 has log_dir => (
99 is => 'lazy',
100 0 ▶ builder => sub {
Subroutine _build_log_dir: not called CC=1 SCAR=6.9
101 0 my $self = shift;
102 0 return $self->make_server_dir( 'logs' );
103 },
104 4 );
105  
106 =head3 conf_file_path
107  
108 The path to the main configuration file. Defaults to C<$conf_dir/httpd.conf>.
109 This is then used by L<Test::Instance::Apache::Config> to create the base
110 configuration file.
111  
112 =cut
113  
114 has conf_file_path => (
115 is => 'lazy',
116 2 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 2 my $self = shift;
118 2 return File::Spec->catfile( $self->conf_dir, 'httpd.conf' );
119 },
120 4 );
121  
122 has _config_manager => (
123 is => 'lazy',
124 2 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
125 2 my $self = shift;
126 return Test::Instance::Apache::Config->new(
127 filename => $self->conf_file_path,
128 config => [
129 PidFile => $self->pid_file_path,
130 Listen => $self->listen_port,
131 2 @{$self->_module_manager->include_modules},
132 2 @{$self->config},
133 ]
134 );
135 },
136 4 );
137  
138 =head3 config
139  
140 Takes an arrayref of values to pass to L<Test::Instance::Apache::Config>.
141  
142 =cut
143  
144 has config => (
145 is => 'ro',
146 2 ▶ default => sub { return [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
147 4 );
148  
149 =head3 modules
150  
151 Takes an arrayref of modules to load into Apache. These are the same names as
152 they appear in C<a2enmod>, so only the modules which are available on your
153 local machine can be used.
154  
155 =cut
156  
157 has modules => (
158 is => 'ro',
159 required => 1,
160 4 ▶ isa => sub { die "modules must be an array!\n" unless ref $_[0] eq 'ARRAY' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 4 4
161 4 );
162  
163 has _module_manager => (
164 is => 'lazy',
165 2 ▶ builder => sub {
Subroutine _build__module_manager: called CC=1 SCAR=0.0
166 2 my $self = shift;
167 2 return Test::Instance::Apache::Modules->new(
168 modules => $self->modules,
169 server_root => $self->server_root,
170 );
171 },
172 4 );
173  
174 =head3 pid_file_path
175  
176 Path to the pid file for Apache. Defaults to C<$server_root/httpd.pid>.
177  
178 =cut
179  
180 has pid_file_path => (
181 is => 'lazy',
182 2 ▶ builder => sub {
Subroutine _build_pid_file_path: called CC=1 SCAR=0.0
183 2 my $self = shift;
184 2 return File::Spec->catfile( $self->server_root, 'httpd.pid' );
185 },
186 4 );
187  
188 =head3 listen_port
189  
190 Port for Apache master process to listen on. If not set, will use
191 L<Net::EmptyPort/empty_port> to find an unused high-number port.
192  
193 =cut
194  
195 has listen_port => (
196 is => 'lazy',
197 2 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
198 2 return empty_port;
199 },
200 4 );
201  
202 =head3 apache_httpd
203  
204 Path to the main Apache process. Uses L<File::Which/which> to determine the
205 path of the binary from your C<$PATH>.
206  
207 =cut
208  
209 has apache_httpd => (
210 is => 'lazy',
211 2 ▶ builder => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
212 2 my ($httpd) = do {
213 local $ENV{PATH} = join( ':',
214 map {
215 10 my $copy = $_;
216 10 ▶ ( $copy =~ s!/bin$!/sbin!
Branch: 1 branch
BranchTrueFalseTotal
$copy =~ s[/bin$][/sbin] ? : 5 5 10
217 ? ( $copy,$_ )
218 : $_
219 )
220 } split ':', $ENV{PATH}
221 2 );
222 2 grep defined, map scalar( which $_ ), qw/ httpd apache apache2 /;
223 };
224 2 ▶ return $httpd if defined $httpd;
Branch: 1 branch
BranchTrueFalseTotal
if defined $httpd 0 2 2
225 2 die "Apache server program not found - please check your path\n";
226 },
227 4 );
228  
229 =head3 pid
230  
231 Pid number for the main Apache process. Set during L</run> and then used during
232 L</DEMOLISH> to kill the correct process.
233  
234 =cut
235  
236 4 has pid => ( is => 'rwp' );
237  
238 0 ▶sub _httpd_cmd {
Subroutine _httpd_cmd: not called CC=1 SCAR=6.9
239 0 my $self = shift;
240  
241 0 return join ( ' ', $self->apache_httpd,
242 '-d', $self->server_root,
243 '-f', $self->conf_file_path,
244 );
245 }
246  
247 =head2 Methods
248  
249 These are the various methods inside this module either for internal or basic
250 usage.
251  
252 =head3 run
253  
254 Sets up all the pre-required folders, writes the config files, loads the
255 required modules, and then starts Apache itself.
256  
257 =cut
258  
259 2 ▶sub run {
Subroutine run: called CC=3 SCAR=18.7
Pod documented
260 2 my $self = shift;
261  
262 2 $self->_config_manager->write_config;
263 2 $self->_module_manager->load_modules;
264 0 $self->log_dir;
265  
266 # capture will wait until the standard apache fork has finished
267 0 capture( $self->_httpd_cmd );
268  
269 0 for (1 .. 10) {
270 0 $self->_set_pid( $self->get_pid );
271 0 ▶ last if defined $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->pid 0 0 0
272 0 sleep 1;
273 }
274 }
275  
276 =head3 make_server_dir
277  
278 Used internally to create folders under the server root. Will take an array of
279 directory names, which are then passed to File::Spec - so if you do the
280 following:
281  
282 $instance->make_server_dir( 'a', 'b', 'c' );
283  
284 Then a path of C<$server_root/a/b/c> will be created.
285  
286 =cut
287  
288 2 ▶sub make_server_dir {
Subroutine make_server_dir: called CC=1 SCAR=0.0
Pod documented
289 2 my ( $self, @dirnames ) = @_;
290 2 my $dir = File::Spec->catdir( $self->server_root, @dirnames );
291 2 mkdir $dir;
292 2 return $dir;
293 }
294  
295 =head3 get_pid
296  
297 Returns the contents of the first line of the pid file. Used internally to set
298 the pid after startup.
299  
300 =cut
301  
302 0 ▶sub get_pid {
Subroutine get_pid: not called CC=2 SCAR=17.9
Pod documented
303 0 my $self = shift;
304  
305 0 my $pid = undef;
306 0 ▶ if ( -f $self->pid_file_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->pid_file_path) 0 0 0
307 0 open( my $fh, '<', $self->pid_file_path );
308 0 $pid = <$fh>; # read first line
309 0 chomp $pid;
310 0 close $fh;
311 }
312 0 return $pid;
313 }
314  
315 =head3 get_logs
316  
317 This will return all the items in the log directory as a hashref of filename
318 and content. This is useful either during test development, or if you are
319 testing exceptions on your application. Please note that it does not recurse
320 subdirectories in the logs folder.
321  
322 =cut
323  
324 0 ▶sub get_logs {
Subroutine get_logs: not called CC=2 SCAR=17.9
Pod documented
325 0 my $self = shift;
326  
327 0 my $logs = {};
328 0 my @files = io->dir( $self->log_dir )->all;
329 0 for my $file ( @files ) {
330 0 $logs->{ $file->filename } = $file->slurp;
331 }
332  
333 0 return $logs;
334 }
335  
336 =head3 debug
337  
338 This is more for use during development of this module - prints out the path of
339 all the files and folders stored as attributes in this module.
340  
341 =cut
342  
343 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
344 0 my $self = shift;
345 0 for my $item ( qw/ server_root conf_dir conf_file_path apache_httpd / ) {
346 0 my $string = sprintf( "%16s | [%s]\n", $item, $self->$item );
347 0 print $string;
348 }
349 }
350  
351 =head3 DEMOLISH
352  
353 Kills the Apache instance started during run.
354  
355 =cut
356  
357 4 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=6 SCAR=31.6
Pod documented
358 4 my $self = shift;
359  
360 4 ▶ if ( my $pid = $self->pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $self->pid) 0 4 4
361 # print "Killing apache with pid " . $pid . "\n";
362 0 for my $signal ( qw/ TERM TERM INT KILL / ) {
363 0 $self->_kill_pid($signal);
364 0 for ( 1..10 ) {
365 0 ▶ last unless $self->_kill_pid( 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_kill_pid(0) 0 0 0
366 0 sleep 1;
367 }
368 0 ▶ last unless $self->_kill_pid( 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_kill_pid(0) 0 0 0
369 }
370 }
371 }
372  
373 0 ▶sub _kill_pid {
Subroutine _kill_pid: not called CC=2 SCAR=17.9
374 0 my ( $self, $signal ) = @_;
375  
376 #print "Signal [" . $signal . "]\n";
377 #print "Pid [" . $self->pid . "]\n";
378 0 ▶ return undef unless $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pid 0 0 0
379 0 my $ret = kill $signal, $self->pid;
380 #print "Kill Return code: [" . $ret . "]\n";
381 0 return $ret;
382 }
383  
384 =head1 AUTHOR
385  
386 Tom Bloor E<lt>t.bloor@shadowcat.co.ukE<gt>
387  
388 Initial development sponsored by L<Runbox|http://www.runbox.com/>
389  
390 =head1 COPYRIGHT
391  
392 Copyright 2016 Tom Bloor
393  
394 =head1 LICENCE
395  
396 This library is free software; you can redistribute it and/or modify
397 it under the same terms as Perl itself.
398  
399 =head1 SEE ALSO
400  
401 =over
402  
403 =item * L<Test::Instance::Apache::Config>
404  
405 =item * L<Test::Instance::Apache::Modules>
406  
407 =item * L<Apache::Test>
408  
409 =back
410  
411 =cut
412  
413 4 1;
↑ Summary Apache/Modules.pm »