Require TEST_LIGHTTPD var to run the lighttpd tests
[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 plan skip_all => 'set TEST_LIGHTTPD to enable this test' 
12     unless $ENV{TEST_LIGHTTPD};
13
14 eval "use Catalyst::Devel 1.0";
15 plan skip_all => 'Catalyst::Devel required' if $@;
16
17 eval "use File::Copy::Recursive";
18 plan skip_all => 'File::Copy::Recursive required' if $@;
19
20 eval "use Test::Harness";
21 plan skip_all => 'Test::Harness required' if $@;
22
23 my $lighttpd_bin = $ENV{LIGHTTPD_BIN};
24 plan skip_all => 'Please set LIGHTTPD_BIN to the path to lighttpd'
25     unless $lighttpd_bin && -x $lighttpd_bin;
26
27 plan tests => 1;
28
29 # clean up
30 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
31
32 # create a TestApp and copy the test libs into it
33 mkdir "$FindBin::Bin/../t/tmp";
34 chdir "$FindBin::Bin/../t/tmp";
35 system "perl -I$FindBin::Bin/../lib $FindBin::Bin/../script/catalyst.pl TestApp";
36 chdir "$FindBin::Bin/..";
37 File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
38
39 # remove TestApp's tests
40 rmtree 't/tmp/TestApp/t';
41
42 # Create a temporary lighttpd config
43 my $docroot = "$FindBin::Bin/../t/tmp";
44 my $port    = 8529;
45
46 # Clean up docroot path
47 $docroot =~ s{/t/..}{};
48
49 my $conf = <<"END";
50 # basic lighttpd config file for testing fcgi+catalyst
51 server.modules = (
52     "mod_access",
53     "mod_fastcgi",
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 # catalyst app specific fcgi setup
66 fastcgi.server = (
67     "" => (
68         "FastCgiTest" => (
69             "socket"       => "$docroot/test.socket",
70             "check-local"  => "disable",
71             "bin-path"     => "$docroot/TestApp/script/testapp_fastcgi.pl",
72             "min-procs"    => 1,
73             "max-procs"    => 1,
74             "idle-timeout" => 20
75         )
76     )
77 )
78 END
79
80 open(my $lightconf, '>', "$docroot/lighttpd.conf") 
81   or die "Can't open $docroot/lighttpd.conf: $!";
82 print {$lightconf} $conf or die "Write error: $!";
83 close $lightconf;
84
85 my $pid = open my $lighttpd, "$lighttpd_bin -D -f $docroot/lighttpd.conf 2>&1 |" 
86     or die "Unable to spawn lighttpd: $!";
87     
88 # wait for it to start
89 while ( check_port( 'localhost', $port ) != 1 ) {
90     diag "Waiting for server to start...";
91     sleep 1;
92 }
93
94 # run the testsuite against the server
95 $ENV{CATALYST_SERVER} = "http://localhost:$port";
96
97 my @tests = glob('t/live_*');
98 eval {
99     runtests(@tests);
100 };
101 ok(!$@, 'lighttpd tests ran OK');
102
103 # shut it down
104 kill 'INT', $pid;
105 close $lighttpd;
106
107 # clean up
108 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
109
110 sub check_port {
111     my ( $host, $port ) = @_;
112
113     my $remote = IO::Socket::INET->new(
114         Proto    => "tcp",
115         PeerAddr => $host,
116         PeerPort => $port
117     );
118     if ($remote) {
119         close $remote;
120         return 1;
121     }
122     else {
123         return 0;
124     }
125 }