provide proper debug output in test if compiling psgi fails
[catagits/Catalyst-Runtime.git] / t / optional_lighttpd-fastcgi-non-root.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 plan tests => 1;
36
37 make_test_app;
38
39 # Create a temporary lighttpd config
40 my $docroot = "$FindBin::Bin/../t/tmp";
41 my $port    = 8529;
42
43 # Clean up docroot path
44 $docroot =~ s{/t/\.\.}{};
45
46 my $perl5lib = join($Config::Config{path_sep}, "$docroot/../../lib", $ENV{PERL5LIB} || ());
47
48 my $conf = <<"END";
49 # basic lighttpd config file for testing fcgi+catalyst
50 server.modules = (
51     "mod_access",
52     "mod_fastcgi",
53     "mod_rewrite",
54     "mod_accesslog"
55 )
56
57 server.document-root = "$docroot"
58
59 server.errorlog    = "$docroot/error.log"
60 accesslog.filename = "$docroot/access.log"
61
62 server.bind = "127.0.0.1"
63 server.port = $port
64
65 # Work around inability to hit http://localhost/deep/path
66 # without a trailing slash
67 url.rewrite = ( "deep/path\$" => "deep/path/" )
68
69 # catalyst app specific fcgi setup
70 fastcgi.server = (
71     "/deep/path" => (
72         "FastCgiTest" => (
73             "socket"          => "$docroot/test.socket",
74             "check-local"     => "disable",
75             "bin-path"        => "$docroot/TestApp/script/testapp_fastcgi.pl",
76             "min-procs"       => 1,
77             "max-procs"       => 1,
78             "idle-timeout"    => 20,
79             "bin-environment" => (
80                 "PERL5LIB" => "$perl5lib"
81             )
82         )
83     )
84 )
85 END
86
87 open(my $lightconf, '>', "$docroot/lighttpd.conf")
88   or die "Can't open $docroot/lighttpd.conf: $!";
89 print {$lightconf} $conf or die "Write error: $!";
90 close $lightconf;
91
92 my $pid = open my $lighttpd, "$lighttpd_bin -D -f $docroot/lighttpd.conf 2>&1 |"
93     or die "Unable to spawn lighttpd: $!";
94
95 # wait for it to start
96 while ( check_port( 'localhost', $port ) != 1 ) {
97     diag "Waiting for server to start...";
98     sleep 1;
99 }
100
101 # run the testsuite against the server
102 $ENV{CATALYST_SERVER} = "http://localhost:$port/deep/path";
103
104 my @tests = (shift) || glob('t/aggregate/live_*');
105 eval {
106     runtests(@tests);
107 };
108 ok(!$@, 'lighttpd tests ran OK');
109
110 # shut it down
111 kill 'INT', $pid;
112 close $lighttpd;
113
114 # clean up
115 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
116
117 sub check_port {
118     my ( $host, $port ) = @_;
119
120     my $remote = IO::Socket::INET->new(
121         Proto    => "tcp",
122         PeerAddr => $host,
123         PeerPort => $port
124     );
125     if ($remote) {
126         close $remote;
127         return 1;
128     }
129     else {
130         return 0;
131     }
132 }