1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
49 package RT::Test::Apache;
55 "mod_perl" => [qw(authz_host env alias perl)],
56 "fastcgi" => [qw(authz_host env alias mime fastcgi)],
60 my $apache_module_prefix = $ENV{RT_TEST_APACHE_MODULES};
63 || RT::Test->find_executable('apxs')
64 || RT::Test->find_executable('apxs2');
66 if ($apxs and not $apache_module_prefix) {
67 $apache_module_prefix = `$apxs -q LIBEXECDIR`;
68 chomp $apache_module_prefix;
71 $apache_module_prefix ||= 'modules';
75 my $passwd = File::Spec->rel2abs( File::Spec->catfile(
76 't', 'data', 'configs', 'passwords' ) );
80 AuthName "restricted area"
87 my ($self, %config) = @_;
88 my %tmp = %{$config{tmp}};
89 my %info = $self->apache_server_info( %config );
92 open( my $fh, '<', $tmp{'config'}{'RT'} ) or die $!;
97 my $tmpl = File::Spec->rel2abs( File::Spec->catfile(
98 't', 'data', 'configs',
99 'apache'. $info{'version'} .'+'. $config{variant} .'.conf'
102 listen => $config{port},
103 server_root => $info{'HTTPD_ROOT'} || $ENV{'HTTPD_ROOT'}
104 || Test::More::BAIL_OUT("Couldn't figure out server root"),
105 document_root => $RT::MasonComponentRoot,
106 tmp_dir => "$tmp{'directory'}",
107 rt_bin_path => $RT::BinPath,
108 rt_sbin_path => $RT::SbinPath,
109 rt_site_config => $ENV{'RT_SITE_CONFIG'},
110 load_modules => $info{load_modules},
111 basic_auth => $config{basic_auth} ? $self->basic_auth : "",
113 foreach (qw(log pid lock)) {
114 $opt{$_ .'_file'} = File::Spec->catfile(
115 "$tmp{'directory'}", "apache.$_"
119 $tmp{'config'}{'apache'} = File::Spec->catfile(
120 "$tmp{'directory'}", "apache.conf"
122 $self->process_in_file(
124 out => $tmp{'config'}{'apache'},
128 $self->fork_exec($info{'executable'}, '-f', $tmp{'config'}{'apache'});
131 while ( !-s $opt{'pid_file'} ) {
137 unless (-e $opt{'pid_file'} and open($pid_fh, '<', $opt{'pid_file'})) {
138 Test::More::BAIL_OUT("Couldn't start apache server, no pid file (unknown error)")
139 unless -e $opt{log_file};
141 open my $log, "<", $opt{log_file};
142 my $error = do {local $/; <$log>};
144 $RT::Logger->error($error) if $error;
145 Test::More::BAIL_OUT("Couldn't start apache server!");
153 Test::More::ok($pid, "Started apache server #$pid");
157 sub apache_server_info {
161 my $bin = $res{'executable'} = $ENV{'RT_TEST_APACHE'}
162 || $self->find_apache_server
163 || Test::More::BAIL_OUT("Couldn't find apache server, use RT_TEST_APACHE");
165 Test::More::BAIL_OUT(
166 "Couldn't find apache modules directory (set APXS= or RT_TEST_APACHE_MODULES=)"
167 ) unless -d $apache_module_prefix;
170 RT::Test::diag("Using '$bin' apache executable for testing");
172 my $info = `$bin -V`;
173 ($res{'version'}) = ($info =~ m{Server\s+version:\s+Apache/(\d+\.\d+)\.});
174 Test::More::BAIL_OUT(
175 "Couldn't figure out version of the server"
176 ) unless $res{'version'};
178 my %opts = ($info =~ m/^\s*-D\s+([A-Z_]+?)(?:="(.*)")$/mg);
179 %res = (%res, %opts);
182 map {s/^\s+//; s/\s+$//; $_}
183 grep $_ !~ /Compiled in modules/i,
184 split /\r*\n/, `$bin -l`
187 Test::More::BAIL_OUT(
188 "Unsupported apache version $res{version}"
189 ) unless exists $MODULES{$res{version}};
191 Test::More::BAIL_OUT(
192 "Unsupported apache variant $res{variant}"
193 ) unless exists $MODULES{$res{version}}{$res{variant}};
195 my @mlist = @{$MODULES{$res{version}}{$res{variant}}};
196 push @mlist, "authn_file", "auth_basic", "authz_user" if $res{basic_auth};
198 $res{'load_modules'} = '';
199 foreach my $mod ( @mlist ) {
200 next if grep $_ =~ /^(mod_|)$mod\.c$/, @{ $res{'modules'} };
202 my $so_file = $apache_module_prefix."/mod_".$mod.".so";
203 Test::More::BAIL_OUT( "Couldn't load $mod module (expected in $so_file)" )
205 $res{'load_modules'} .=
206 "LoadModule ${mod}_module $so_file\n";
211 sub find_apache_server {
213 return $_ foreach grep defined,
214 map RT::Test->find_executable($_),
215 qw(httpd apache apache2 apache1);
219 sub apache_mpm_type {
221 my $apache = $self->find_apache_server;
222 my $out = `$apache -l`;
223 if ( $out =~ /^\s*(worker|prefork|event|itk)\.c\s*$/m ) {
231 RT::Test::__disconnect_rt();
233 unless ( defined $pid ) {
234 die "cannot fork: $!";
237 die "can't exec `". join(' ', @_) ."` program: $!";
239 RT::Test::__reconnect_rt();
244 sub process_in_file {
246 my %args = ( in => undef, options => undef, @_ );
248 my $text = RT::Test->file_content( $args{'in'} );
249 while ( my ($opt) = ($text =~ /\%\%(.+?)\%\%/) ) {
250 my $value = $args{'options'}{ lc $opt };
251 die "no value for $opt" unless defined $value;
253 $text =~ s/\%\%\Q$opt\E\%\%/$value/g;
256 my ($out_fh, $out_conf);
257 unless ( $args{'out'} ) {
258 ($out_fh, $out_conf) = tempfile();
260 $out_conf = $args{'out'};
261 open( $out_fh, '>', $out_conf )
262 or die "couldn't open '$out_conf': $!";
267 return ($out_fh, $out_conf);