]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-unix.t
No ext_urls
[public-inbox.git] / t / httpd-unix.t
1 # Copyright (C) 2016-2021 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                 // BAIL_OUT "E: $! connecting to $unix";
47         ok($sock->write("GET /host-port HTTP/1.0\r\n\r\n"),
48                 'wrote req to server');
49         ok($sock->read(my $buf, 4096), 'read response');
50         like($buf, qr!\r\n\r\n127\.0\.0\.1 0\z!,
51                 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
52 }
53
54 check_sock($unix);
55
56 { # do not clobber existing socket
57         my %err = ( 'linux' => EADDRINUSE, 'freebsd' => EADDRINUSE );
58         open my $out, '>>', "$tmpdir/1" or die "redirect failed: $!";
59         open my $err, '>>', "$tmpdir/2" or die "redirect failed: $!";
60         my $cmd = ['-httpd', '-l', $unix, '-W0', $psgi];
61         my $ftd = start_script($cmd, undef, { 1 => $out, 2 => $err });
62         $ftd->join;
63         isnt($?, 0, 'httpd failure set $?');
64         SKIP: {
65                 my $ec = $err{$^O} or
66                         skip("not sure if $^O fails with EADDRINUSE", 1);
67                 is($? >> 8, $ec, 'httpd failed with EADDRINUSE');
68         };
69         open my $fh, "$tmpdir/2" or die "failed to open $tmpdir/2: $!";
70         local $/;
71         my $e = <$fh>;
72         like($e, qr/no listeners bound/i, 'got error message');
73         is(-s "$tmpdir/1", 0, 'stdout was empty');
74 }
75
76 {
77         is($td->kill, 1, 'terminate existing process');
78         $td->join;
79         is($?, 0, 'existing httpd exited successfully');
80         ok(-S $unix, 'unix socket still exists');
81 }
82
83 # portable Perl can delay or miss signal dispatches due to races,
84 # so disable some tests on systems lacking signalfd(2) or EVFILT_SIGNAL
85 my $has_sigfd = PublicInbox::Sigfd->new({}, 0) ? 1 : $ENV{TEST_UNRELIABLE};
86
87 sub delay_until {
88         my $cond = shift;
89         my $end = time + 30;
90         do {
91                 return if $cond->();
92                 select undef, undef, undef, 0.012;
93         } until (time > $end);
94         Carp::confess('condition failed');
95 }
96
97 SKIP: {
98         require_mods('Net::Server::Daemonize', 52);
99         $has_sigfd or skip('signalfd / EVFILT_SIGNAL not available', 52);
100         my $pid_file = "$tmpdir/pid";
101         my $read_pid = sub {
102                 my $f = shift;
103                 open my $fh, '<', $f or die "open $f failed: $!";
104                 my $pid = do { local $/; <$fh> };
105                 chomp($pid) or die("pid file not ready $!");
106                 $pid;
107         };
108
109         for my $w (qw(-W0 -W1)) {
110                 # wait for daemonization
111                 $spawn_httpd->("-l$unix", '-D', '-P', $pid_file, $w);
112                 $td->join;
113                 is($?, 0, "daemonized $w process");
114                 check_sock($unix);
115                 ok(-s $pid_file, "$w pid file written");
116                 my $pid = $read_pid->($pid_file);
117                 is(kill('TERM', $pid), 1, "signaled daemonized $w process");
118                 delay_until(sub { !kill(0, $pid) });
119                 is(kill(0, $pid), 0, "daemonized $w process exited");
120                 ok(!-e $pid_file, "$w pid file unlinked at exit");
121         }
122
123         # try a USR2 upgrade with workers:
124         my $httpd = abs_path('blib/script/public-inbox-httpd');
125         $psgi = abs_path($psgi);
126         my $opt = { run_mode => 0 };
127
128         my @args = ("-l$unix", '-D', '-P', $pid_file, -1, $out, -2, $err);
129         $td = start_script([$httpd, @args, $psgi], undef, $opt);
130         $td->join;
131         is($?, 0, "daemonized process again");
132         check_sock($unix);
133         ok(-s $pid_file, 'pid file written');
134         my $pid = $read_pid->($pid_file);
135
136         # stop worker to ensure check_sock below hits $new_pid
137         kill('TTOU', $pid) or die "TTOU failed: $!";
138
139         kill('USR2', $pid) or die "USR2 failed: $!";
140         delay_until(sub {
141                 $pid != (eval { $read_pid->($pid_file) } // $pid)
142         });
143         my $new_pid = $read_pid->($pid_file);
144         isnt($new_pid, $pid, 'new child started');
145         ok($new_pid > 0, '$new_pid valid');
146         delay_until(sub { -s "$pid_file.oldbin" });
147         my $old_pid = $read_pid->("$pid_file.oldbin");
148         is($old_pid, $pid, '.oldbin pid file written');
149         ok($old_pid > 0, '$old_pid valid');
150
151         check_sock($unix); # ensures $new_pid is ready to receive signals
152
153         # first, back out of the upgrade
154         kill('QUIT', $new_pid) or die "kill new PID failed: $!";
155         delay_until(sub {
156                 $pid == (eval { $read_pid->($pid_file) } // 0)
157         });
158         is($read_pid->($pid_file), $pid, 'old PID file restored');
159         ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
160
161         # retry USR2 upgrade
162         kill('USR2', $pid) or die "USR2 failed: $!";
163         delay_until(sub {
164                 $pid != (eval { $read_pid->($pid_file) } // $pid)
165         });
166         $new_pid = $read_pid->($pid_file);
167         isnt($new_pid, $pid, 'new child started again');
168         $old_pid = $read_pid->("$pid_file.oldbin");
169         is($old_pid, $pid, '.oldbin pid file written');
170
171         # drop the old parent
172         kill('QUIT', $old_pid) or die "QUIT failed: $!";
173         delay_until(sub { !kill(0, $old_pid) });
174         ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
175
176         # drop the new child
177         check_sock($unix);
178         kill('QUIT', $new_pid) or die "QUIT failed: $!";
179         delay_until(sub { !kill(0, $new_pid) });
180         ok(!-f $pid_file, 'PID file is gone');
181
182
183         # try USR2 without workers (-W0)
184         $td = start_script([$httpd, @args, '-W0', $psgi], undef, $opt);
185         $td->join;
186         is($?, 0, 'daemonized w/o workers');
187         check_sock($unix);
188         $pid = $read_pid->($pid_file);
189
190         # replace running process
191         kill('USR2', $pid) or die "USR2 failed: $!";
192         delay_until(sub { !kill(0, $pid) });
193
194         check_sock($unix);
195         $pid = $read_pid->($pid_file);
196         kill('QUIT', $pid) or die "USR2 failed: $!";
197         delay_until(sub { !kill(0, $pid) });
198         ok(!-f $pid_file, 'PID file is gone');
199 }
200
201 done_testing();