]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-unix.t
363f36489931ee5bcb5e7bf5ee066809c5abe055
[public-inbox.git] / t / httpd-unix.t
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # Tests for binding Unix domain sockets
4 use strict;
5 use warnings;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 use Errno qw(EADDRINUSE);
9 use Cwd qw(abs_path);
10 use Carp qw(croak);
11 require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
12 use IO::Socket::UNIX;
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $unix = "$tmpdir/unix.sock";
15 my $psgi = './t/httpd-corner.psgi';
16 my $out = "$tmpdir/out.log";
17 my $err = "$tmpdir/err.log";
18 my $td;
19
20 my $spawn_httpd = sub {
21         my (@args) = @_;
22         my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi ];
23         $td = start_script($cmd);
24 };
25
26 {
27         require PublicInbox::Daemon;
28         my $l = "$tmpdir/named.sock";
29         my $s = IO::Socket::UNIX->new(Listen => 5, Local => $l,
30                                         Type => SOCK_STREAM);
31         is(PublicInbox::Daemon::sockname($s), $l, 'sockname works for UNIX');
32 }
33
34 ok(!-S $unix, 'UNIX socket does not exist, yet');
35 $spawn_httpd->("-l$unix", '-W0');
36 my %o = (Peer => $unix, Type => SOCK_STREAM);
37 for (1..1000) {
38         last if -S $unix && IO::Socket::UNIX->new(%o);
39         select undef, undef, undef, 0.02
40 }
41
42 ok(-S $unix, 'UNIX socket was bound by -httpd');
43 sub check_sock ($) {
44         my ($unix) = @_;
45         my $sock = IO::Socket::UNIX->new(Peer => $unix, Type => SOCK_STREAM);
46         warn "E: $! connecting to $unix\n" unless defined $sock;
47         ok($sock, 'client UNIX socket connected');
48         ok($sock->write("GET /host-port HTTP/1.0\r\n\r\n"),
49                 'wrote req to server');
50         ok($sock->read(my $buf, 4096), 'read response');
51         like($buf, qr!\r\n\r\n127\.0\.0\.1:0\z!,
52                 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
53 }
54
55 check_sock($unix);
56
57 { # do not clobber existing socket
58         my %err = ( 'linux' => EADDRINUSE, 'freebsd' => EADDRINUSE );
59         open my $out, '>>', "$tmpdir/1" or die "redirect failed: $!";
60         open my $err, '>>', "$tmpdir/2" or die "redirect failed: $!";
61         my $cmd = ['-httpd', '-l', $unix, '-W0', $psgi];
62         my $ftd = start_script($cmd, undef, { 1 => $out, 2 => $err });
63         $ftd->join;
64         isnt($?, 0, 'httpd failure set $?');
65         SKIP: {
66                 my $ec = $err{$^O} or
67                         skip("not sure if $^O fails with EADDRINUSE", 1);
68                 is($? >> 8, $ec, 'httpd failed with EADDRINUSE');
69         };
70         open my $fh, "$tmpdir/2" or die "failed to open $tmpdir/2: $!";
71         local $/;
72         my $e = <$fh>;
73         like($e, qr/no listeners bound/i, 'got error message');
74         is(-s "$tmpdir/1", 0, 'stdout was empty');
75 }
76
77 {
78         is($td->kill, 1, 'terminate existing process');
79         $td->join;
80         is($?, 0, 'existing httpd exited successfully');
81         ok(-S $unix, 'unix socket still exists');
82 }
83
84 # portable Perl can delay or miss signal dispatches due to races,
85 # so disable some tests on systems lacking signalfd(2) or EVFILT_SIGNAL
86 my $has_sigfd = PublicInbox::Sigfd->new({}, 0) ? 1 : $ENV{TEST_UNRELIABLE};
87
88 sub delay_until {
89         my $cond = shift;
90         my $end = time + 30;
91         do {
92                 return if $cond->();
93                 select undef, undef, undef, 0.012;
94         } until (time > $end);
95         Carp::confess('condition failed');
96 }
97
98 SKIP: {
99         require_mods('Net::Server::Daemonize', 52);
100         $has_sigfd or skip('signalfd / EVFILT_SIGNAL not available', 52);
101         my $pid_file = "$tmpdir/pid";
102         my $read_pid = sub {
103                 my $f = shift;
104                 open my $fh, '<', $f or die "open $f failed: $!";
105                 my $pid = do { local $/; <$fh> };
106                 chomp($pid) or die("pid file not ready $!");
107                 $pid;
108         };
109
110         for my $w (qw(-W0 -W1)) {
111                 # wait for daemonization
112                 $spawn_httpd->("-l$unix", '-D', '-P', $pid_file, $w);
113                 $td->join;
114                 is($?, 0, "daemonized $w process");
115                 check_sock($unix);
116                 ok(-s $pid_file, "$w pid file written");
117                 my $pid = $read_pid->($pid_file);
118                 is(kill('TERM', $pid), 1, "signaled daemonized $w process");
119                 delay_until(sub { !kill(0, $pid) });
120                 is(kill(0, $pid), 0, "daemonized $w process exited");
121                 ok(!-e $pid_file, "$w pid file unlinked at exit");
122         }
123
124         # try a USR2 upgrade with workers:
125         my $httpd = abs_path('blib/script/public-inbox-httpd');
126         $psgi = abs_path($psgi);
127         my $opt = { run_mode => 0 };
128
129         my @args = ("-l$unix", '-D', '-P', $pid_file, -1, $out, -2, $err);
130         $td = start_script([$httpd, @args, $psgi], undef, $opt);
131         $td->join;
132         is($?, 0, "daemonized process again");
133         check_sock($unix);
134         ok(-s $pid_file, 'pid file written');
135         my $pid = $read_pid->($pid_file);
136
137         # stop worker to ensure check_sock below hits $new_pid
138         kill('TTOU', $pid) or die "TTOU failed: $!";
139
140         kill('USR2', $pid) or die "USR2 failed: $!";
141         delay_until(sub {
142                 $pid != (eval { $read_pid->($pid_file) } // $pid)
143         });
144         my $new_pid = $read_pid->($pid_file);
145         isnt($new_pid, $pid, 'new child started');
146         ok($new_pid > 0, '$new_pid valid');
147         delay_until(sub { -s "$pid_file.oldbin" });
148         my $old_pid = $read_pid->("$pid_file.oldbin");
149         is($old_pid, $pid, '.oldbin pid file written');
150         ok($old_pid > 0, '$old_pid valid');
151
152         check_sock($unix); # ensures $new_pid is ready to receive signals
153
154         # first, back out of the upgrade
155         kill('QUIT', $new_pid) or die "kill new PID failed: $!";
156         delay_until(sub {
157                 $pid == (eval { $read_pid->($pid_file) } // 0)
158         });
159         is($read_pid->($pid_file), $pid, 'old PID file restored');
160         ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
161
162         # retry USR2 upgrade
163         kill('USR2', $pid) or die "USR2 failed: $!";
164         delay_until(sub {
165                 $pid != (eval { $read_pid->($pid_file) } // $pid)
166         });
167         $new_pid = $read_pid->($pid_file);
168         isnt($new_pid, $pid, 'new child started again');
169         $old_pid = $read_pid->("$pid_file.oldbin");
170         is($old_pid, $pid, '.oldbin pid file written');
171
172         # drop the old parent
173         kill('QUIT', $old_pid) or die "QUIT failed: $!";
174         delay_until(sub { !kill(0, $old_pid) });
175         ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
176
177         # drop the new child
178         check_sock($unix);
179         kill('QUIT', $new_pid) or die "QUIT failed: $!";
180         delay_until(sub { !kill(0, $new_pid) });
181         ok(!-f $pid_file, 'PID file is gone');
182
183
184         # try USR2 without workers (-W0)
185         $td = start_script([$httpd, @args, '-W0', $psgi], undef, $opt);
186         $td->join;
187         is($?, 0, 'daemonized w/o workers');
188         check_sock($unix);
189         $pid = $read_pid->($pid_file);
190
191         # replace running process
192         kill('USR2', $pid) or die "USR2 failed: $!";
193         delay_until(sub { !kill(0, $pid) });
194
195         check_sock($unix);
196         $pid = $read_pid->($pid_file);
197         kill('QUIT', $pid) or die "USR2 failed: $!";
198         delay_until(sub { !kill(0, $pid) });
199         ok(!-f $pid_file, 'PID file is gone');
200 }
201
202 done_testing();