configure lighttpd at root correctly in tests
[catagits/Catalyst-Runtime.git] / t / optional_lighttpd-fastcgi.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 BEGIN {
6     plan skip_all => 'set TEST_LIGHTTPD to enable this test'
7         unless $ENV{TEST_LIGHTTPD};
8 }
9
10 use File::Path;
11 use FindBin;
12 use IO::Socket;
13 use Config ();
14
15 BEGIN {
16     eval "use FCGI";
17     plan skip_all => 'FCGI required' if $@;
18
19     eval "use File::Copy::Recursive";
20     plan skip_all => 'File::Copy::Recursive required' if $@;
21
22     eval "use Test::Harness";
23     plan skip_all => 'Test::Harness required' if $@;
24 }
25
26 use lib 't/lib';
27 use MakeTestApp;
28
29 my $lighttpd_bin = $ENV{LIGHTTPD_BIN} || `which lighttpd`;
30 chomp $lighttpd_bin;
31
32 plan skip_all => 'Please set LIGHTTPD_BIN to the path to lighttpd'
33     unless $lighttpd_bin && -x $lighttpd_bin;
34
35 my $fix_scriptname = '';
36 if (my ($vmajor, $vminor, $vpatch) = `"$lighttpd_bin" -v` =~ /\b(\d+)\.(\d+)\.(\d+)\b/) {
37     if ($vmajor > 1 || ($vmajor == 1 && ("$vminor.$vpatch" >= 4.23))) {
38         $fix_scriptname = '"fix-root-scriptname" => "enable",';
39     }
40 }
41
42 plan tests => 1;
43
44 # this creates t/tmp/TestApp
45 make_test_app;
46
47 # Create a temporary lighttpd config
48 my $docroot = "$FindBin::Bin/../t/tmp";
49 my $port    = 8529;
50
51 # Clean up docroot path
52 $docroot =~ s{/t/\.\.}{};
53
54 my $perl5lib = join($Config::Config{path_sep}, "$docroot/../../lib", $ENV{PERL5LIB} || ());
55
56 my $conf = <<"END";
57 # basic lighttpd config file for testing fcgi+catalyst
58 server.modules = (
59     "mod_access",
60     "mod_fastcgi",
61     "mod_accesslog"
62 )
63
64 server.document-root = "$docroot"
65
66 server.errorlog    = "$docroot/error.log"
67 accesslog.filename = "$docroot/access.log"
68
69 server.bind = "127.0.0.1"
70 server.port = $port
71
72 # catalyst app specific fcgi setup
73 fastcgi.server = (
74     "/" => (
75         "FastCgiTest" => (
76             "socket"          => "$docroot/test.socket",
77             "check-local"     => "disable",
78             "bin-path"        => "$docroot/TestApp/script/testapp_fastcgi.pl",
79             "min-procs"       => 1,
80             "max-procs"       => 1,
81             "idle-timeout"    => 20,
82             $fix_scriptname
83             "bin-environment" => (
84                 "PERL5LIB" => "$perl5lib"
85             )
86         )
87     )
88 )
89 END
90
91 open(my $lightconf, '>', "$docroot/lighttpd.conf")
92   or die "Can't open $docroot/lighttpd.conf: $!";
93 print {$lightconf} $conf or die "Write error: $!";
94 close $lightconf;
95
96 my $pid = open my $lighttpd, "$lighttpd_bin -D -f $docroot/lighttpd.conf 2>&1 |"
97     or die "Unable to spawn lighttpd: $!";
98
99 # wait for it to start
100 while ( check_port( 'localhost', $port ) != 1 ) {
101     diag "Waiting for server to start...";
102     sleep 1;
103 }
104
105 # run the testsuite against the server
106 $ENV{CATALYST_SERVER} = "http://localhost:$port";
107
108 my @tests = (shift) || glob('t/aggregate/live_*');
109 eval {
110     runtests(@tests);
111 };
112 ok(!$@, 'lighttpd tests ran OK');
113
114 # shut it down
115 kill 'INT', $pid;
116 close $lighttpd;
117
118 # clean up
119 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
120
121 sub check_port {
122     my ( $host, $port ) = @_;
123
124     my $remote = IO::Socket::INET->new(
125         Proto    => "tcp",
126         PeerAddr => $host,
127         PeerPort => $port
128     );
129     if ($remote) {
130         close $remote;
131         return 1;
132     }
133     else {
134         return 0;
135     }
136 }