tests: port working_directory tests to Perl 5
[unicorn.git] / t / working_directory.t
blobe7ff43a5a1a24c216b6caa353ada4a9e7c765a81
1 #!perl -w
2 # Copyright (C) unicorn hackers <unicorn-public@yhbt.net>
3 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
4 use v5.14; BEGIN { require './t/lib.perl' };
5 use autodie;
6 mkdir "$tmpdir/alt";
7 my $u_sock = "$tmpdir/u.sock";
8 my $ru = "$tmpdir/alt/config.ru";
9 my $u_conf = "$tmpdir/u.conf.rb";
10 open my $fh, '>', $u_conf;
11 print $fh <<EOM;
12 pid "$tmpdir/pid"
13 preload_app true
14 stderr_path "$err_log"
15 working_directory "$tmpdir/alt" # the whole point of this test
16 before_fork { |_,_| \$master_ppid = Process.ppid }
17 EOM
18 close $fh;
20 my $common_ru = <<'EOM';
21 use Rack::ContentLength
22 use Rack::ContentType, 'text/plain'
23 run lambda { |env| [ 200, {}, [ "#{$master_ppid}\n" ] ] }
24 EOM
26 open $fh, '>', $ru;
27 print $fh <<EOM;
28 #\\--daemonize --listen $u_sock
29 $common_ru
30 EOM
31 close $fh;
33 my $pid;
34 my $stop_daemon = sub {
35         my ($is_END) = @_;
36         kill('TERM', $pid);
37         my $tries = 1000;
38         while (CORE::kill(0, $pid) && --$tries) {
39                 select undef, undef, undef, 0.01;
40         }
41         if ($is_END && CORE::kill(0, $pid)) {
42                 CORE::kill('KILL', $pid);
43                 die "daemonized PID=$pid did not die";
44         } else {
45                 ok(!CORE::kill(0, $pid), 'daemonized unicorn gone');
46                 undef $pid;
47         }
50 END { $stop_daemon->(1) if defined $pid };
52 unicorn('-c', $u_conf)->join; # will daemonize
53 chomp($pid = slurp("$tmpdir/pid"));
55 my $c = unix_start($u_sock, 'GET / HTTP/1.0');
56 my ($status, $hdr) = slurp_hdr($c);
57 chomp(my $bdy = do { local $/; <$c> });
58 is($bdy, 1, 'got expected $master_ppid');
60 $stop_daemon->();
61 check_stderr;
63 if ('test without CLI switches in config.ru') {
64         truncate $err_log, 0;
65         open $fh, '>', $ru;
66         print $fh $common_ru;
67         close $fh;
69         unicorn('-D', '-l', $u_sock, '-c', $u_conf)->join; # will daemonize
70         chomp($pid = slurp("$tmpdir/pid"));
72         $c = unix_start($u_sock, 'GET / HTTP/1.0');
73         ($status, $hdr) = slurp_hdr($c);
74         chomp($bdy = do { local $/; <$c> });
75         is($bdy, 1, 'got expected $master_ppid');
77         $stop_daemon->();
78         check_stderr;
81 if ('ensures broken working_directory (missing config.ru) is OK') {
82         truncate $err_log, 0;
83         unlink $ru;
85         my $auto_reap = unicorn('-c', $u_conf);
86         $auto_reap->join;
87         isnt($?, 0, 'exited with error due to missing config.ru');
89         like(slurp($err_log), qr/rackup file \Q(config.ru)\E not readable/,
90                 'noted unreadability of config.ru in stderr');
93 if ('fooapp.rb (not config.ru) works with working_directory') {
94         truncate $err_log, 0;
95         my $fooapp = "$tmpdir/alt/fooapp.rb";
96         open $fh, '>', $fooapp;
97         print $fh <<EOM;
98 class Fooapp
99   def self.call(env)
100     b = "dir=#{Dir.pwd}"
101     h = { 'content-type' => 'text/plain', 'content-length' => b.bytesize.to_s }
102     [ 200, h, [ b ] ]
103   end
106         close $fh;
107         my $srv = tcp_server;
108         my $auto_reap = unicorn(qw(-c), $u_conf, qw(-I. fooapp.rb),
109                                 { -C => '/', 3 => $srv });
110         $c = tcp_start($srv, 'GET / HTTP/1.0');
111         ($status, $hdr) = slurp_hdr($c);
112         chomp($bdy = do { local $/; <$c> });
113         is($bdy, "dir=$tmpdir/alt",
114                 'fooapp.rb (w/o config.ru) w/ working_directory');
115         close $c;
116         $auto_reap->join('TERM');
117         is($?, 0, 'fooapp.rb process exited');
118         check_stderr;
121 undef $tmpdir;
122 done_testing;