Fixed lighttpd tests to not load File::Slurp until run-time
[catagits/Catalyst-Runtime.git] / t / optional_lighttpd-fastcgi.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use File::Path;
7 use FindBin;
8 use IO::Socket;
9 use Test::More;
10
11 eval "use Catalyst::Devel 1.0";
12 plan skip_all => 'Catalyst::Devel required' if $@;
13
14 eval "use File::Copy::Recursive";
15 plan skip_all => 'File::Copy::Recursive required' if $@;
16
17 my $lighttpd_bin = $ENV{LIGHTTPD_BIN};
18 plan skip_all => 'Please set LIGHTTPD_BIN to run this test'
19     unless $lighttpd_bin && -x $lighttpd_bin;
20
21 plan tests => 1;
22
23 require File::Slurp;
24
25 # clean up
26 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
27
28 # create a TestApp and copy the test libs into it
29 mkdir "$FindBin::Bin/../t/tmp";
30 chdir "$FindBin::Bin/../t/tmp";
31 system "perl -I$FindBin::Bin/../lib $FindBin::Bin/../script/catalyst.pl TestApp";
32 chdir "$FindBin::Bin/..";
33 File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
34
35 # remove TestApp's tests
36 rmtree 't/tmp/TestApp/t';
37
38 # Create a temporary lighttpd config
39 my $docroot = "$FindBin::Bin/../t/tmp";
40 my $port    = 8529;
41
42 # Clean up docroot path
43 $docroot =~ s{/t/..}{};
44
45 my $conf = qq{
46 # basic lighttpd config file for testing fcgi+catalyst
47 server.modules = (
48     "mod_access",
49     "mod_fastcgi",
50     "mod_accesslog"
51 )
52
53 server.document-root = "$docroot"
54
55 server.errorlog    = "$docroot/error.log"
56 accesslog.filename = "$docroot/access.log"
57
58 server.bind = "127.0.0.1"
59 server.port = $port
60
61 # catalyst app specific fcgi setup
62 fastcgi.server = (
63     "" => (
64         "FastCgiTest" => (
65             "socket"       => "$docroot/test.socket",
66             "check-local"  => "disable",
67             "bin-path"     => "$docroot/TestApp/script/testapp_fastcgi.pl",
68             "min-procs"    => 1,
69             "max-procs"    => 1,
70             "idle-timeout" => 20
71         )
72     )
73 )
74 };
75
76 File::Slurp::write_file( "$docroot/lighttpd.conf", $conf );
77
78 my $pid = open my $lighttpd, "$lighttpd_bin -D -f $docroot/lighttpd.conf 2>&1 |" 
79     or die "Unable to spawn lighttpd: $!";
80     
81 # wait for it to start
82 print "Waiting for server to start...\n";
83 while ( check_port( 'localhost', $port ) != 1 ) {
84     sleep 1;
85 }
86
87 # run the testsuite against the server
88 $ENV{CATALYST_SERVER} = "http://localhost:$port";
89 system( 'prove -r -Ilib/ t/live_*' );
90
91 # shut it down
92 kill 'INT', $pid;
93 close $lighttpd;
94
95 # clean up
96 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
97
98 ok( 'done' );
99
100 sub check_port {
101     my ( $host, $port ) = @_;
102
103     my $remote = IO::Socket::INET->new(
104         Proto    => "tcp",
105         PeerAddr => $host,
106         PeerPort => $port
107     );
108     if ($remote) {
109         close $remote;
110         return 1;
111     }
112     else {
113         return 0;
114     }
115 }