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