]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
t/lei-oneshot: standalone oneshot (non-socket) test
[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 use File::Path qw(rmtree);
10 require_mods(qw(json DBD::SQLite Search::Xapian));
11 my $LEI = 'lei';
12 my $lei = sub {
13         my ($cmd, $env, $opt) = @_;
14         run_script([$LEI, @$cmd], $env, $opt);
15 };
16
17 my ($home, $for_destroy) = tmpdir();
18 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
19 delete local $ENV{XDG_DATA_HOME};
20 delete local $ENV{XDG_CONFIG_HOME};
21 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
22 local $ENV{HOME} = $home;
23 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
24
25 my $test_lei_common = sub {
26         ok(!$lei->([], undef, $opt), 'no args fails');
27         is($? >> 8, 1, '$? is 1');
28         is($out, '', 'nothing in stdout');
29         like($err, qr/^usage:/sm, 'usage in stderr');
30
31         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
32                 $out = $err = '';
33                 ok($lei->($arg, undef, $opt), "lei @$arg");
34                 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
35                 is($err, '', "nothing in stderr (@$arg)");
36         }
37
38         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
39                 $out = $err = '';
40                 ok(!$lei->($arg, undef, $opt), "lei @$arg");
41                 is($? >> 8, 1, '$? set correctly');
42                 isnt($err, '', 'something in stderr');
43                 is($out, '', 'nothing in stdout');
44         }
45
46         # init tests
47         $out = $err = '';
48         my $ok_err_info = sub {
49                 my ($msg) = @_;
50                 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
51                         diag "$msg: err=$err";
52                 $err = '';
53         };
54         my $home_trash = [ "$home/.local", "$home/.config" ];
55         rmtree($home_trash);
56         ok($lei->(['init'], undef, $opt), 'init w/o args');
57         $ok_err_info->('after init w/o args');
58         ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
59         $ok_err_info->('after idempotent init w/o args');
60
61         ok(!$lei->(['init', "$home/x"], undef, $opt),
62                 'init conflict');
63         is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
64         ok(!-e "$home/x", 'nothing created on conflict');
65         rmtree($home_trash);
66
67         $err = '';
68         ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
69         $ok_err_info->('init w/ arg');
70         ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
71         $ok_err_info->('init idempotent w/ arg');
72         ok(-d "$home/x", 'created dir');
73         rmtree([ "$home/x", @$home_trash ]);
74
75         $err = '';
76         ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
77                 'too many args fails');
78         like($err, qr/too many/, 'noted excessive');
79         ok(!-e "$home/x", 'x not created on excessive');
80         for my $d (@$home_trash) {
81                 my $base = (split(m!/!, $d))[-1];
82                 ok(!-d $d, "$base not created");
83         }
84         is($out, '', 'nothing in stdout');
85 };
86
87 my $test_lei_oneshot = $ENV{TEST_LEI_ONESHOT};
88 SKIP: {
89         last SKIP if $test_lei_oneshot;
90         require_mods('IO::FDPass', 16);
91         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
92
93         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
94         is($err, '', 'no error from daemon-pid');
95         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
96         chomp(my $pid = $out);
97         ok(kill(0, $pid), 'pid is valid');
98         ok(-S $sock, 'sock created');
99
100         $test_lei_common->();
101
102         $out = '';
103         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
104         chomp(my $pid_again = $out);
105         is($pid, $pid_again, 'daemon-pid idempotent');
106
107         ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
108         is($out, '', 'no output from daemon-stop');
109         is($err, '', 'no error from daemon-stop');
110         for (0..100) {
111                 kill(0, $pid) or last;
112                 tick();
113         }
114         ok(!-S $sock, 'sock gone');
115         ok(!kill(0, $pid), 'pid gone after stop');
116
117         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
118         chomp(my $new_pid = $out);
119         ok(kill(0, $new_pid), 'new pid is running');
120         ok(-S $sock, 'sock exists again');
121         unlink $sock or BAIL_OUT "unlink $!";
122         for (0..100) {
123                 kill('CHLD', $new_pid) or last;
124                 tick();
125         }
126         ok(!kill(0, $new_pid), 'daemon exits after unlink');
127         # success over socket, can't test without
128         $test_lei_common = undef;
129 };
130
131 require_ok 'PublicInbox::LeiDaemon';
132 $LEI = 'lei-oneshot' if $test_lei_oneshot;
133 $test_lei_common->() if $test_lei_common;
134
135 done_testing;