]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
t/lei: fix TEST_RUN_MODE=0, simplify oneshot fallback
[public-inbox.git] / t / lei.t
1 #!perl -w
2 # Copyright (C) 2020-2021 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_git 2.6;
11 require_mods(qw(json DBD::SQLite Search::Xapian));
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
13 my $lei = sub {
14         my ($cmd, $env, $xopt) = @_;
15         $out = $err = '';
16         if (!ref($cmd)) {
17                 ($env, $xopt) = grep { (!defined) || ref } @_;
18                 $cmd = [ grep { defined && !ref } @_ ];
19         }
20         run_script(['lei', @$cmd], $env, $xopt // $opt);
21 };
22
23 my ($home, $for_destroy) = tmpdir();
24 delete local $ENV{XDG_DATA_HOME};
25 delete local $ENV{XDG_CONFIG_HOME};
26 local $ENV{HOME} = $home;
27 local $ENV{FOO} = 'BAR';
28 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
29 my $home_trash = [ "$home/.local", "$home/.config" ];
30 my $cleanup = sub { rmtree([@$home_trash, @_]) };
31 my $config_file = "$home/.config/lei/config";
32 my $store_dir = "$home/.local/share/lei";
33
34 my $test_help = sub {
35         ok(!$lei->([], undef, $opt), 'no args fails');
36         is($? >> 8, 1, '$? is 1');
37         is($out, '', 'nothing in stdout');
38         like($err, qr/^usage:/sm, 'usage in stderr');
39
40         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
41                 $out = $err = '';
42                 ok($lei->($arg, undef, $opt), "lei @$arg");
43                 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
44                 is($err, '', "nothing in stderr (@$arg)");
45         }
46
47         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
48                 $out = $err = '';
49                 ok(!$lei->($arg, undef, $opt), "lei @$arg");
50                 is($? >> 8, 1, '$? set correctly');
51                 isnt($err, '', 'something in stderr');
52                 is($out, '', 'nothing in stdout');
53         }
54         ok($lei->(qw(init -h), undef, $opt), 'init -h');
55         like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
56                 'actual path shown in init -h');
57         ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }, $opt),
58                 'init with XDG_DATA_HOME');
59         like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
60         is($err, '', 'no errors from init -h');
61
62         ok($lei->(qw(config -h), undef, $opt), 'config-h');
63         like($out, qr! \Q$home\E/\.config/lei/config\b!,
64                 'actual path shown in config -h');
65         ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }, $opt),
66                 'config with XDG_CONFIG_HOME');
67         like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
68         is($err, '', 'no errors from config -h');
69 };
70
71 my $ok_err_info = sub {
72         my ($msg) = @_;
73         is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
74                 diag "$msg: err=$err";
75         $err = '';
76 };
77
78 my $test_init = sub {
79         $cleanup->();
80         ok($lei->(['init'], undef, $opt), 'init w/o args');
81         $ok_err_info->('after init w/o args');
82         ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
83         $ok_err_info->('after idempotent init w/o args');
84
85         ok(!$lei->(['init', "$home/x"], undef, $opt),
86                 'init conflict');
87         is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
88         ok(!-e "$home/x", 'nothing created on conflict');
89         $cleanup->();
90
91         ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
92         $ok_err_info->('init w/ arg');
93         ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
94         $ok_err_info->('init idempotent w/ arg');
95         ok(-d "$home/x", 'created dir');
96         $cleanup->("$home/x");
97
98         ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
99                 'too many args fails');
100         like($err, qr/too many/, 'noted excessive');
101         ok(!-e "$home/x", 'x not created on excessive');
102         for my $d (@$home_trash) {
103                 my $base = (split(m!/!, $d))[-1];
104                 ok(!-d $d, "$base not created");
105         }
106         is($out, '', 'nothing in stdout on init failure');
107 };
108
109 my $test_config = sub {
110         $cleanup->();
111         ok($lei->([qw(config a.b c)], undef, $opt), 'config set var');
112         is($out.$err, '', 'no output on var set');
113         ok($lei->([qw(config -l)], undef, $opt), 'config -l');
114         is($err, '', 'no errors on listing');
115         is($out, "a.b=c\n", 'got expected output');
116         ok(!$lei->([qw(config -f), "$home/.config/f", qw(x.y z)], undef, $opt),
117                         'config set var with -f fails');
118         like($err, qr/not supported/, 'not supported noted');
119         ok(!-f "$home/config/f", 'no file created');
120 };
121
122 my $setup_publicinboxes = sub {
123         state $done = '';
124         return if $done eq $home;
125         use PublicInbox::InboxWritable;
126         for my $V (1, 2) {
127                 run_script([qw(-init -Lmedium), "-V$V", "t$V",
128                                 '--newsgroup', "t.$V",
129                                 "$home/t$V", "http://example.com/t$V",
130                                 "t$V\@example.com" ]) or BAIL_OUT "init v$V";
131         }
132         my $cfg = PublicInbox::Config->new;
133         my $seen = 0;
134         $cfg->each_inbox(sub {
135                 my ($ibx) = @_;
136                 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
137                 my $V = $ibx->version;
138                 my @eml = glob('t/*.eml');
139                 push(@eml, 't/data/0001.patch') if $V == 2;
140                 for (@eml) {
141                         next if $_ eq 't/psgi_v2-old.eml'; # dup mid
142                         $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
143                         $seen++;
144                 }
145                 $im->done;
146                 if ($V == 1) {
147                         run_script(['-index', $ibx->{inboxdir}]) or
148                                 BAIL_OUT 'index v1';
149                 }
150         });
151         $done = $home;
152         $seen || BAIL_OUT 'no imports';
153 };
154
155 my $test_external = sub {
156         $setup_publicinboxes->();
157         $cleanup->();
158         $lei->('ls-external');
159         is($out.$err, '', 'ls-external no output, yet');
160         ok(!-e $config_file && !-e $store_dir,
161                 'nothing created by ls-external');
162
163         my $cfg = PublicInbox::Config->new;
164         $cfg->each_inbox(sub {
165                 my ($ibx) = @_;
166                 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
167                         'added external');
168                 is($out.$err, '', 'no output');
169         });
170         ok(-s $config_file && -e $store_dir,
171                 'add-external created config + store');
172         my $lcfg = PublicInbox::Config->new($config_file);
173         $cfg->each_inbox(sub {
174                 my ($ibx) = @_;
175                 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
176                         "configured boost on $ibx->{name}");
177         });
178         $lei->('ls-external');
179         like($out, qr/boost=0\n/s, 'ls-external has output');
180 };
181
182 my $test_lei_common = sub {
183         $test_help->();
184         $test_config->();
185         $test_init->();
186         $test_external->();
187 };
188
189 if ($ENV{TEST_LEI_ONESHOT}) {
190         require_ok 'PublicInbox::LEI';
191         # force sun_path[108] overflow, "IO::FDPass" avoids warning
192         local $ENV{XDG_RUNTIME_DIR} = "$home/IO::FDPass".('.sun_path' x 108);
193         $test_lei_common->();
194 }
195
196 SKIP: { # real socket
197         require_mods(qw(IO::FDPass Cwd), 46);
198         local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
199         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
200
201         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
202         is($err, '', 'no error from daemon-pid');
203         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
204         chomp(my $pid = $out);
205         ok(kill(0, $pid), 'pid is valid');
206         ok(-S $sock, 'sock created');
207
208         $test_lei_common->();
209
210         $out = '';
211         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
212         chomp(my $pid_again = $out);
213         is($pid, $pid_again, 'daemon-pid idempotent');
214
215         $out = '';
216         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
217         is($err, '', 'no errors in env dump');
218         my @env = split(/\0/, $out);
219         is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
220         is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
221         is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
222
223         $out = '';
224         ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
225         is($out.$err, '', 'no output for unset');
226         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
227         is($err, '', 'no errors in env dump');
228         @env = split(/\0/, $out);
229         is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
230
231         $out = '';
232         ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
233                         undef, $opt), 'unset multiple');
234         is($out.$err, '', 'no errors output for unset');
235         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
236         is($err, '', 'no errors in env dump');
237         @env = split(/\0/, $out);
238         is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
239         $out = '';
240         ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
241         is($out.$err, '', 'no output');
242         ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
243         is($out, '', 'env cleared');
244
245         ok(run_script([qw(lei daemon-kill)], undef, $opt), 'daemon-kill');
246         is($out, '', 'no output from daemon-kill');
247         is($err, '', 'no error from daemon-kill');
248         for (0..100) {
249                 kill(0, $pid) or last;
250                 tick();
251         }
252         ok(!-S $sock, 'sock gone');
253         ok(!kill(0, $pid), 'pid gone after stop');
254
255         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
256         chomp(my $new_pid = $out);
257         ok(kill(0, $new_pid), 'new pid is running');
258         ok(-S $sock, 'sock exists again');
259
260         $out = $err = '';
261         for my $sig (qw(-0 -CHLD)) {
262                 ok(run_script([qw(lei daemon-kill), $sig ], undef, $opt),
263                                         "handles $sig");
264         }
265         is($out.$err, '', 'no output on innocuous signals');
266         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
267         chomp $out;
268         is($out, $new_pid, 'PID unchanged after -0/-CHLD');
269
270         if ('socket inaccessible') {
271                 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
272                 $out = $err = '';
273                 ok(run_script([qw(lei help)], undef, $opt),
274                         'connect fail, one-shot fallback works');
275                 like($err, qr/\bconnect\(/, 'connect error noted');
276                 like($out, qr/^usage: /, 'help output works');
277                 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
278         }
279         if ('oneshot on cwd gone') {
280                 my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
281                 my $d = "$home/to-be-removed";
282                 my $lei_path = 'lei';
283                 # we chdir, so we need an abs_path fur run_script
284                 if (($ENV{TEST_RUN_MODE}//2) != 2) {
285                         $lei_path = PublicInbox::TestCommon::key2script('lei');
286                         $lei_path = Cwd::abs_path($lei_path);
287                 }
288                 mkdir $d or BAIL_OUT "mkdir($d) $!";
289                 chdir $d or BAIL_OUT "chdir($d) $!";
290                 if (rmdir($d)) {
291                         $out = $err = '';
292                         ok(run_script([$lei_path, 'help'], undef, $opt),
293                                 'cwd fail, one-shot fallback works');
294                 } else {
295                         $err = "rmdir=$!";
296                 }
297                 chdir $cwd or BAIL_OUT "chdir($cwd) $!";
298                 like($err, qr/cwd\(/, 'cwd error noted');
299                 like($out, qr/^usage: /, 'help output still works');
300         }
301
302         unlink $sock or BAIL_OUT "unlink($sock) $!";
303         for (0..100) {
304                 kill('CHLD', $new_pid) or last;
305                 tick();
306         }
307         ok(!kill(0, $new_pid), 'daemon exits after unlink');
308         # success over socket, can't test without
309 };
310
311 done_testing;