]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei: FD-passing and IPC basics
[public-inbox.git] / t / lei.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 use PublicInbox::Config;
9 my $json = PublicInbox::Config::json() or plan skip_all => 'JSON missing';
10 require_mods(qw(DBD::SQLite Search::Xapian));
11 my ($home, $for_destroy) = tmpdir();
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
13
14 SKIP: {
15         require_mods('IO::FDPass', 51);
16         local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
17         mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
18         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
19
20         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
21         is($err, '', 'no error from daemon-pid');
22         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
23         chomp(my $pid = $out);
24         ok(kill(0, $pid), 'pid is valid');
25         ok(-S $sock, 'sock created');
26
27         ok(!run_script([qw(lei)], undef, $opt), 'no args fails');
28         is($? >> 8, 1, '$? is 1');
29         is($out, '', 'nothing in stdout');
30         like($err, qr/^usage:/sm, 'usage in stderr');
31
32         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
33                 $out = $err = '';
34                 ok(run_script(['lei', @$arg], undef, $opt), "lei @$arg");
35                 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
36                 is($err, '', "nothing in stderr (@$arg)");
37         }
38
39         ok(!run_script([qw(lei DBG-false)], undef, $opt), 'false(1) emulation');
40         is($? >> 8, 1, '$? set correctly');
41         is($err, '', 'no error from false(1) emulation');
42
43         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
44                 $out = $err = '';
45                 ok(!run_script(['lei', @$arg], undef, $opt), "lei @$arg");
46                 is($? >> 8, 1, '$? set correctly');
47                 isnt($err, '', 'something in stderr');
48                 is($out, '', 'nothing in stdout');
49         }
50
51         $out = '';
52         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
53         chomp(my $pid_again = $out);
54         is($pid, $pid_again, 'daemon-pid idempotent');
55
56         ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
57         is($out, '', 'no output from daemon-stop');
58         is($err, '', 'no error from daemon-stop');
59         for (0..100) {
60                 kill(0, $pid) or last;
61                 tick();
62         }
63         ok(!-S $sock, 'sock gone');
64         ok(!kill(0, $pid), 'pid gone after stop');
65
66         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
67         chomp(my $new_pid = $out);
68         ok(kill(0, $new_pid), 'new pid is running');
69         ok(-S $sock, 'sock exists again');
70         unlink $sock or BAIL_OUT "unlink $!";
71         for (0..100) {
72                 kill('CHLD', $new_pid) or last;
73                 tick();
74         }
75         ok(!kill(0, $new_pid), 'daemon exits after unlink');
76 };
77
78 require_ok 'PublicInbox::LeiDaemon';
79
80 done_testing;