File Coverage

blib/lib/Apache/TestConfig.pm
Criterion Covered Total %
statement 274 1084 25.3
branch 55 446 12.3
condition 33 328 10.1
subroutine 59 140 42.1
pod 5 104 4.8
total 426 2102 20.3


line stmt bran cond sub pod time code
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::TestConfig;
17              
18 6     6   74 use strict;
  6         52  
  6         79  
19 6     6   115 use warnings FATAL => 'all';
  6         55  
  6         81  
20              
21 6     6   87 use constant WIN32 => $^O eq 'MSWin32';
  6         53  
  6         159  
22 6     6   97 use constant OSX => $^O eq 'darwin';
  6         72  
  6         86  
23 6     6   89 use constant CYGWIN => $^O eq 'cygwin';
  6         52  
  6         77  
24 6     6   1028 use constant NETWARE => $^O eq 'NetWare';
  6         72  
  6         259  
25 6     6   99 use constant SOLARIS => $^O eq 'solaris';
  6         56  
  6         85  
26 6     6   107 use constant WINFU => WIN32 || NETWARE;
  6         54  
  6         74  
27 6 50 33 6   388 use constant COLOR => ($ENV{APACHE_TEST_COLOR} && -t STDOUT) ? 1 : 0;
  6         59  
  6         442  
28              
29 6     6   96 use constant DEFAULT_PORT => 8529;
  6         51  
  6         75  
30              
31             use constant IS_MOD_PERL_2 =>
32 6   50 6   115     eval { require mod_perl2 } || 0;
  6         61  
  6         59  
  6         218  
33              
34 6   33     237 use constant IS_MOD_PERL_2_BUILD => IS_MOD_PERL_2 &&
35 6     6   108     require Apache2::Build && Apache2::Build::IS_MOD_PERL_BUILD();
  6         55  
36              
37 18         467 use constant IS_APACHE_TEST_BUILD =>
38 6     6   123     grep { -e "$_/lib/Apache/TestConfig.pm" } qw(Apache-Test . ..);
  6         54  
  6         79  
39              
40 6     6   106 use constant CUSTOM_CONFIG_FILE => 'Apache/TestConfigData.pm';
  6         56  
  6         224  
41              
42 6     6   347 use lib ();
  6         123  
  6         63  
43 6     6   103 use File::Copy ();
  6         55  
  6         57  
44 6     6   92 use File::Find qw(finddepth);
  6         89  
  6         129  
45 6     6   96 use File::Basename qw(dirname);
  6         90  
  6         141  
46 6     6   96 use File::Path ();
  6         55  
  6         56  
47 6         112 use File::Spec::Functions qw(catfile abs2rel splitdir canonpath
48 6     6   91 catdir file_name_is_absolute devnull);
  6         55  
49 6     6   101 use Cwd qw(fastcwd);
  6         57  
  6         103  
50 6     6   7654 use Socket ();
  6         66  
  6         62  
51 6     6   113 use Symbol ();
  6         57  
  6         58  
52              
53 6     6   160 use Apache::TestConfigPerl ();
  6         60  
  6         59  
54 6     6   172 use Apache::TestConfigParse ();
  6         66  
  6         61  
55 6     6   124 use Apache::TestTrace;
  6         56  
  6         122  
56 6     6   213 use Apache::TestServer ();
  6         63  
  6         66  
57 6     6   150 use Apache::TestRun ();
  6         63  
  6         236  
58              
59 6     6   91 use vars qw(%Usage);
  6         55  
  6         115  
60              
61             # variables stored in $Apache::TestConfigData::vars
62             my @data_vars_must = qw(httpd apxs);
63             my @data_vars_opt = qw(user group port);
64             # mapping from $Apache::TestConfigData::vars to $ENV settings
65             my %vars_to_env = (
66                 httpd => 'APACHE_TEST_HTTPD',
67                 apxs => 'APACHE_TEST_APXS',
68                 user => 'APACHE_TEST_USER',
69                 group => 'APACHE_TEST_GROUP',
70                 port => 'APACHE_TEST_PORT',
71             );
72              
73             %Usage = (
74                top_dir => 'top-level directory (default is $PWD)',
75                t_dir => 'the t/ test directory (default is $top_dir/t)',
76                t_conf => 'the conf/ test directory (default is $t_dir/conf)',
77                t_logs => 'the logs/ test directory (default is $t_dir/logs)',
78                t_pid_file => 'location of the pid file (default is $t_logs/httpd.pid)',
79                t_conf_file => 'test httpd.conf file (default is $t_conf/httpd.conf)',
80                src_dir => 'source directory to look for mod_foos.so',
81                serverroot => 'ServerRoot (default is $t_dir)',
82                documentroot => 'DocumentRoot (default is $ServerRoot/htdocs',
83                port => 'Port [port_number|select] (default ' . DEFAULT_PORT . ')',
84                servername => 'ServerName (default is localhost)',
85                user => 'User to run test server as (default is $USER)',
86                group => 'Group to run test server as (default is $GROUP)',
87                bindir => 'Apache bin/ dir (default is apxs -q BINDIR)',
88                sbindir => 'Apache sbin/ dir (default is apxs -q SBINDIR)',
89                httpd => 'server to use for testing (default is $bindir/httpd)',
90                target => 'name of server binary (default is apxs -q TARGET)',
91                apxs => 'location of apxs (default is from Apache2::BuildConfig)',
92                startup_timeout => 'seconds to wait for the server to start (default is 60)',
93                httpd_conf => 'inherit config from this file (default is apxs derived)',
94                httpd_conf_extra=> 'inherit additional config from this file',
95                minclients => 'minimum number of concurrent clients (default is 1)',
96                maxclients => 'maximum number of concurrent clients (default is minclients+1)',
97                perlpod => 'location of perl pod documents (for testing downloads)',
98                proxyssl_url => 'url for testing ProxyPass / https (default is localhost)',
99                sslca => 'location of SSL CA (default is $t_conf/ssl/ca)',
100                sslcaorg => 'SSL CA organization to use for tests (default is asf)',
101                libmodperl => 'path to mod_perl\'s .so (full or relative to LIBEXECDIR)',
102                defines => 'values to add as -D defines (for example, "VAR1 VAR2")',
103                (map { $_ . '_module_name', "$_ module name"} qw(cgi ssl thread access auth php)),
104             );
105              
106             my %filepath_conf_opts = map { $_ => 1 }
107                 qw(top_dir t_dir t_conf t_logs t_pid_file t_conf_file src_dir serverroot
108             documentroot bindir sbindir httpd apxs httpd_conf httpd_conf_extra
109             perlpod sslca libmodperl);
110              
111             sub conf_opt_is_a_filepath {
112 0     0 0 0     my $opt = shift;
113 0 0       0     $opt && exists $filepath_conf_opts{$opt};
114             }
115              
116             sub usage {
117 0     0 0 0     for my $hash (\%Usage) {
118 0         0         for (sort keys %$hash){
119 0         0             printf " -%-18s %s\n", $_, $hash->{$_};
120                     }
121                 }
122             }
123              
124             sub filter_args {
125 15     15 0 232     my($args, $wanted_args) = @_;
126 15         133     my(@pass, %keep);
127              
128 15         147     my @filter = @$args;
129              
130 15 50       227     if (ref($filter[0])) {
131 0         0         push @pass, shift @filter;
132                 }
133              
134 15         169     while (@filter) {
135 5         49         my $key = shift @filter;
136             # optinal - or -- prefix
137 5 100 33     209         if (defined $key && $key =~ /^-?-?(.+)/ && exists $wanted_args->{$1}) {
      66        
138 3 50       34             if (@filter) {
139 3         43                 $keep{$1} = shift @filter;
140                         }
141                         else {
142 0         0                 die "key $1 requires a matching value";
143                         }
144                     }
145                     else {
146 2         23             push @pass, $key;
147                     }
148                 }
149              
150 15         299     return (\@pass, \%keep);
151             }
152              
153             my %passenv = map { $_,1 } qw{
154             APACHE_TEST_APXS
155             APACHE_TEST_HTTPD
156             APACHE_TEST_GROUP
157             APACHE_TEST_USER
158             APACHE_TEST_PORT
159             };
160              
161             sub passenv {
162 0     0 0 0     \%passenv;
163             }
164              
165             sub passenv_makestr {
166 0     0 0 0     my @vars;
167              
168 0         0     for (keys %passenv) {
169 0         0         push @vars, "$_=\$($_)";
170                 }
171              
172 0         0     "@vars";
173             }
174              
175 7     7 0 142 sub server { shift->{server} }
176              
177             sub modperl_build_config {
178              
179 6     6 0 58     my $self = shift;
180              
181 6 50       93     my $server = ref $self ? $self->server : new_test_server();
182              
183             # we don't want to get mp2 preconfigured data in order to be able
184             # to get the interactive tests running.
185 6 50       81     return undef if $ENV{APACHE_TEST_INTERACTIVE_CONFIG_TEST};
186              
187             # we can't do this if we're using httpd 1.3.X
188             # even if mod_perl2 is installed on the box
189             # similarly, we shouldn't be loading mp2 if we're not
190             # absolutely certain we're in a 2.X environment yet
191             # (such as mod_perl's own build or runtime environment)
192 6 50 33     165     if (($server->{rev} && $server->{rev} == 2) ||
      33        
      33        
193                     IS_MOD_PERL_2_BUILD || $ENV{MOD_PERL_API_VERSION}) {
194 0 0       0         eval {
195 0         0             require Apache2::Build;
196                     } or return;
197 0         0         return Apache2::Build->build_config;
198                 }
199              
200 6         166     return;
201             }
202              
203             sub new_test_server {
204 6     6 0 68     my($self, $args) = @_;
205 6   33     208     Apache::TestServer->new($args || $self)
206             }
207              
208             # setup httpd-independent components
209             # for httpd-specific call $self->httpd_config()
210             sub new {
211 6     6 0 65     my $class = shift;
212              
213 6         57     my $args;
214              
215 6 50 33     1000     $args = shift if $_[0] and ref $_[0];
216              
217 6 50       305     $args = $args ? {%$args} : {@_}; #copy
218              
219             #see Apache::TestMM::{filter_args,generate_script}
220             #we do this so 'perl Makefile.PL' can be passed options such as apxs
221             #without forcing regeneration of configuration and recompilation of c-modules
222             #as 't/TEST apxs /path/to/apache/bin/apxs' would do
223 6         102     while (my($key, $val) = each %Apache::TestConfig::Argv) {
224 0         0         $args->{$key} = $val;
225                 }
226              
227 6         165     my $top_dir = fastcwd;
228 6         74     $top_dir = pop_dir($top_dir, 't');
229             # untaint as we are going to use it a lot later on in -T sensitive
230             # operations (.e.g @INC)
231 6 50       125     $top_dir = $1 if $top_dir =~ /(.*)/;
232              
233             # make sure that t/conf/apache_test_config.pm is found
234             # (unfortunately sometimes we get thrown into / by Apache so we
235             # can't just rely on $top_dir
236 6         114     lib->import($top_dir);
237              
238 6         1766     my $thaw = {};
239             #thaw current config
240 6         138     for (qw(conf t/conf)) {
241 12 100       116         last if eval {
242 12         7476             require "$_/apache_test_config.pm";
243 6         379             $thaw = 'apache_test_config'->new;
244 6         95             delete $thaw->{save};
245             #incase class that generated the config was
246             #something else, which we can't be sure how to load
247 6         95             bless $thaw, 'Apache::TestConfig';
248                     };
249                 }
250              
251 6 50 33     139     if ($args->{thaw} and ref($thaw) ne 'HASH') {
252             #dont generate any new config
253 6         58         $thaw->{vars}->{$_} = $args->{$_} for keys %$args;
  6         101  
254 6         85         $thaw->{server} = $thaw->new_test_server;
255 6         384         $thaw->add_inc;
256 6         113         return $thaw;
257                 }
258              
259             #regenerating config, so forget old
260 0 0       0     if ($args->{save}) {
261 0         0         for (qw(vhosts inherit_config modules inc cmodules)) {
262 0 0       0             delete $thaw->{$_} if exists $thaw->{$_};
263                     }
264                 }
265              
266             # custom config options from Apache::TestConfigData
267             # again, this should force reconfiguration
268 0         0     custom_config_add_conf_opts($args);
269              
270 0   0     0     my $self = bless {
271                     clean => {},
272                     vhosts => {},
273                     inherit_config => {},
274                     modules => {},
275                     inc => [],
276                     %$thaw,
277                     mpm => "",
278                     httpd_defines => {},
279                     vars => $args,
280                     postamble => [],
281                     preamble => [],
282                     postamble_hooks => [],
283                     preamble_hooks => [],
284                 }, ref($class) || $class;
285              
286 0         0     my $vars = $self->{vars}; #things that can be overridden
287              
288 0         0     for (qw(save verbose)) {
289 0 0       0         next unless exists $args->{$_};
290 0         0         $self->{$_} = delete $args->{$_};
291                 }
292              
293 0   0     0     $vars->{top_dir} ||= $top_dir;
294              
295 0         0     $self->add_inc;
296              
297             #help to find libmodperl.so
298 0         0     my $src_dir = catfile $vars->{top_dir}, qw(src modules perl);
299 0 0 0     0     $vars->{src_dir} ||= $src_dir if -d $src_dir;
300              
301 0   0     0     $vars->{t_dir} ||= catfile $vars->{top_dir}, 't';
302 0   0     0     $vars->{s