]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-unix.t
daemon: document optional Net::Server dependency
[public-inbox.git] / t / httpd-unix.t
1 # Copyright (C) 2016 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
8 foreach my $mod (qw(Plack::Util Plack::Request Plack::Builder Danga::Socket
9                         HTTP::Parser::XS HTTP::Date HTTP::Status)) {
10         eval "require $mod";
11         plan skip_all => "$mod missing for httpd-unix.t" if $@;
12 }
13
14 use File::Temp qw/tempdir/;
15 use IO::Socket::UNIX;
16 use Cwd qw/getcwd/;
17 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek);
18 my $tmpdir = tempdir('httpd-unix-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 my $unix = "$tmpdir/unix.sock";
20 my $httpd = 'blib/script/public-inbox-httpd';
21 my $psgi = getcwd() . '/t/httpd-corner.psgi';
22 my $out = "$tmpdir/out.log";
23 my $err = "$tmpdir/err.log";
24
25 my $pid;
26 END { kill 'TERM', $pid if defined $pid };
27
28 my $spawn_httpd = sub {
29         my (@args) = @_;
30         $pid = fork;
31         if ($pid == 0) {
32                 exec $httpd, @args, "--stdout=$out", "--stderr=$err", $psgi;
33                 die "FAIL: $!\n";
34         }
35         ok(defined $pid, 'forked httpd process successfully');
36 };
37
38 ok(!-S $unix, 'UNIX socket does not exist, yet');
39 $spawn_httpd->("-l$unix");
40 for (1..1000) {
41         last if -S $unix;
42         select undef, undef, undef, 0.02
43 }
44
45 ok(-S $unix, 'UNIX socket was bound by -httpd');
46 sub check_sock ($) {
47         my ($unix) = @_;
48         my $sock = IO::Socket::UNIX->new(Peer => $unix, Type => SOCK_STREAM);
49         ok($sock, 'client UNIX socket connected');
50         ok($sock->write("GET /host-port HTTP/1.0\r\n\r\n"),
51                 'wrote req to server');
52         ok($sock->read(my $buf, 4096), 'read response');
53         like($buf, qr!\r\n\r\n127\.0\.0\.1:0\z!,
54                 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
55 }
56
57 check_sock($unix);
58
59 { # do not clobber existing socket
60         my $fpid = fork;
61         if ($fpid == 0) {
62                 open STDOUT, '>>', "$tmpdir/1" or die "redirect failed: $!";
63                 open STDERR, '>>', "$tmpdir/2" or die "redirect failed: $!";
64                 exec $httpd, '-l', $unix, '-W0', $psgi;
65                 die "FAIL: $!\n";
66         }
67         is($fpid, waitpid($fpid, 0), 'second httpd exits');
68         isnt($?, 0, 'httpd failed with failure to bind');
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         my $kpid = $pid;
78         $pid = undef;
79         is(kill('TERM', $kpid), 1, 'terminate existing process');
80         is(waitpid($kpid, 0), $kpid, 'existing httpd terminated');
81         is($?, 0, 'existing httpd exited successfully');
82         ok(-S $unix, 'unix socket still exists');
83 }
84
85 SKIP: {
86         eval 'require Net::Server::Daemonize';
87         skip('Net::Server missing for pid-file/daemonization test', 10) if $@;
88
89         # wait for daemonization
90         $spawn_httpd->("-l$unix", '-D', '-P', "$tmpdir/pid");
91         my $kpid = $pid;
92         $pid = undef;
93         is(waitpid($kpid, 0), $kpid, 'existing httpd terminated');
94         check_sock($unix);
95
96         ok(-f "$tmpdir/pid", 'pid file written');
97         open my $fh, '<', "$tmpdir/pid" or die "open failed: $!";
98         my $rpid = <$fh>;
99         chomp $rpid;
100         like($rpid, qr/\A\d+\z/s, 'pid file looks like a pid');
101         is(kill('TERM', $rpid), 1, 'signalled daemonized process');
102         for (1..100) {
103                 kill(0, $rpid) or last;
104                 select undef, undef, undef, 0.02;
105         }
106         is(kill(0, $rpid), 0, 'daemonized process exited')
107 }
108
109 done_testing();