]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei: pass FD to CWD via cmsg, use fchdir on server
[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 ($home, $for_destroy) = tmpdir();
14 my $err_filter;
15 my $lei = sub {
16         my ($cmd, $env, $xopt) = @_;
17         $out = $err = '';
18         if (!ref($cmd)) {
19                 ($env, $xopt) = grep { (!defined) || ref } @_;
20                 $cmd = [ grep { defined && !ref } @_ ];
21         }
22         my $res = run_script(['lei', @$cmd], $env, $xopt // $opt);
23         $err_filter and
24                 $err = join('', grep(!/$err_filter/, split(/^/m, $err)));
25         $res;
26 };
27
28 delete local $ENV{XDG_DATA_HOME};
29 delete local $ENV{XDG_CONFIG_HOME};
30 local $ENV{GIT_COMMITTER_EMAIL} = 'lei@example.com';
31 local $ENV{GIT_COMMITTER_NAME} = 'lei user';
32 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
33 local $ENV{HOME} = $home;
34 local $ENV{FOO} = 'BAR';
35 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
36 my $home_trash = [ "$home/.local", "$home/.config" ];
37 my $cleanup = sub { rmtree([@$home_trash, @_]) };
38 my $config_file = "$home/.config/lei/config";
39 my $store_dir = "$home/.local/share/lei";
40
41 my $test_help = sub {
42         ok(!$lei->(), 'no args fails');
43         is($? >> 8, 1, '$? is 1');
44         is($out, '', 'nothing in stdout');
45         like($err, qr/^usage:/sm, 'usage in stderr');
46
47         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
48                 ok($lei->($arg), "lei @$arg");
49                 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
50                 is($err, '', "nothing in stderr (@$arg)");
51         }
52
53         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
54                 ok(!$lei->($arg), "lei @$arg");
55                 is($? >> 8, 1, '$? set correctly');
56                 isnt($err, '', 'something in stderr');
57                 is($out, '', 'nothing in stdout');
58         }
59         ok($lei->(qw(init -h)), 'init -h');
60         like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
61                 'actual path shown in init -h');
62         ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }),
63                 'init with XDG_DATA_HOME');
64         like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
65         is($err, '', 'no errors from init -h');
66
67         ok($lei->(qw(config -h)), 'config-h');
68         like($out, qr! \Q$home\E/\.config/lei/config\b!,
69                 'actual path shown in config -h');
70         ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }),
71                 'config with XDG_CONFIG_HOME');
72         like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
73         is($err, '', 'no errors from config -h');
74 };
75
76 my $ok_err_info = sub {
77         my ($msg) = @_;
78         is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
79                 diag "$msg: err=$err";
80 };
81
82 my $test_init = sub {
83         $cleanup->();
84         ok($lei->('init'), 'init w/o args');
85         $ok_err_info->('after init w/o args');
86         ok($lei->('init'), 'idempotent init w/o args');
87         $ok_err_info->('after idempotent init w/o args');
88
89         ok(!$lei->('init', "$home/x"), 'init conflict');
90         is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
91         ok(!-e "$home/x", 'nothing created on conflict');
92         $cleanup->();
93
94         ok($lei->('init', "$home/x"), 'init conflict resolved');
95         $ok_err_info->('init w/ arg');
96         ok($lei->('init', "$home/x"), 'init idempotent w/ path');
97         $ok_err_info->('init idempotent w/ arg');
98         ok(-d "$home/x", 'created dir');
99         $cleanup->("$home/x");
100
101         ok(!$lei->('init', "$home/x", "$home/2"), 'too many args fails');
102         like($err, qr/too many/, 'noted excessive');
103         ok(!-e "$home/x", 'x not created on excessive');
104         for my $d (@$home_trash) {
105                 my $base = (split(m!/!, $d))[-1];
106                 ok(!-d $d, "$base not created");
107         }
108         is($out, '', 'nothing in stdout on init failure');
109 };
110
111 my $test_config = sub {
112         $cleanup->();
113         ok($lei->(qw(config a.b c)), 'config set var');
114         is($out.$err, '', 'no output on var set');
115         ok($lei->(qw(config -l)), 'config -l');
116         is($err, '', 'no errors on listing');
117         is($out, "a.b=c\n", 'got expected output');
118         ok(!$lei->(qw(config -f), "$home/.config/f", qw(x.y z)),
119                         'config set var with -f fails');
120         like($err, qr/not supported/, 'not supported noted');
121         ok(!-f "$home/config/f", 'no file created');
122 };
123
124 my $setup_publicinboxes = sub {
125         state $done = '';
126         return if $done eq $home;
127         use PublicInbox::InboxWritable;
128         for my $V (1, 2) {
129                 run_script([qw(-init), "-V$V", "t$V",
130                                 '--newsgroup', "t.$V",
131                                 "$home/t$V", "http://example.com/t$V",
132                                 "t$V\@example.com" ]) or BAIL_OUT "init v$V";
133         }
134         my $cfg = PublicInbox::Config->new;
135         my $seen = 0;
136         $cfg->each_inbox(sub {
137                 my ($ibx) = @_;
138                 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
139                 my $V = $ibx->version;
140                 my @eml = glob('t/*.eml');
141                 push(@eml, 't/data/0001.patch') if $V == 2;
142                 for (@eml) {
143                         next if $_ eq 't/psgi_v2-old.eml'; # dup mid
144                         $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
145                         $seen++;
146                 }
147                 $im->done;
148                 if ($V == 1) {
149                         run_script(['-index', $ibx->{inboxdir}]) or
150                                 BAIL_OUT 'index v1';
151                 }
152         });
153         $done = $home;
154         $seen || BAIL_OUT 'no imports';
155 };
156
157 my $test_external = sub {
158         $setup_publicinboxes->();
159         $cleanup->();
160         $lei->('ls-external');
161         is($out.$err, '', 'ls-external no output, yet');
162         ok(!-e $config_file && !-e $store_dir,
163                 'nothing created by ls-external');
164
165         my $cfg = PublicInbox::Config->new;
166         $cfg->each_inbox(sub {
167                 my ($ibx) = @_;
168                 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
169                         'added external');
170                 is($out.$err, '', 'no output');
171         });
172         ok(-s $config_file && -e $store_dir,
173                 'add-external created config + store');
174         my $lcfg = PublicInbox::Config->new($config_file);
175         $cfg->each_inbox(sub {
176                 my ($ibx) = @_;
177                 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
178                         "configured boost on $ibx->{name}");
179         });
180         $lei->('ls-external');
181         like($out, qr/boost=0\n/s, 'ls-external has output');
182
183         # note, on a Bourne shell users should be able to use either:
184         #       s:"use boolean prefix"
185         #       "s:use boolean prefix"
186         # or use single quotes, it should not matter.  Users only need
187         # to know shell quoting rules, not Xapian quoting rules.
188         # No double-quoting should be imposed on users on the CLI
189         $lei->('q', 's:use boolean prefix');
190         like($out, qr/search: use boolean prefix/, 'phrase search got result');
191 };
192
193 my $test_lei_common = sub {
194         $test_help->();
195         $test_config->();
196         $test_init->();
197         $test_external->();
198 };
199
200 if ($ENV{TEST_LEI_ONESHOT}) {
201         require_ok 'PublicInbox::LEI';
202         # force sun_path[108] overflow, ($lei->() filters out this path)
203         my $xrd = "$home/1shot-test".('.sun_path' x 108);
204         local $ENV{XDG_RUNTIME_DIR} = $xrd;
205         $err_filter = qr!\Q$xrd!;
206         $test_lei_common->();
207 }
208
209 SKIP: { # real socket
210         require_mods(qw(Cwd), my $nr = 105);
211         my $nfd = eval { require Socket::MsgHdr; 5 } // do {
212                 require PublicInbox::Spawn;
213                 PublicInbox::Spawn->can('send_cmd4') ? 5 : undef;
214         } //
215         skip 'Socket::MsgHdr or Inline::C missing or unconfigured', $nr;
216
217         local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
218         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/$nfd.seq.sock";
219
220         ok($lei->('daemon-pid'), 'daemon-pid');
221         is($err, '', 'no error from daemon-pid');
222         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
223         chomp(my $pid = $out);
224         ok(kill(0, $pid), 'pid is valid');
225         ok(-S $sock, 'sock created');
226
227         $test_lei_common->();
228
229         ok($lei->('daemon-pid'), 'daemon-pid');
230         chomp(my $pid_again = $out);
231         is($pid, $pid_again, 'daemon-pid idempotent');
232
233         ok($lei->(qw(daemon-kill)), 'daemon-kill');
234         is($out, '', 'no output from daemon-kill');
235         is($err, '', 'no error from daemon-kill');
236         for (0..100) {
237                 kill(0, $pid) or last;
238                 tick();
239         }
240         ok(-S $sock, 'sock still exists');
241         ok(!kill(0, $pid), 'pid gone after stop');
242
243         ok($lei->(qw(daemon-pid)), 'daemon-pid');
244         chomp(my $new_pid = $out);
245         ok(kill(0, $new_pid), 'new pid is running');
246         ok(-S $sock, 'sock still exists');
247
248         for my $sig (qw(-0 -CHLD)) {
249                 ok($lei->('daemon-kill', $sig), "handles $sig");
250         }
251         is($out.$err, '', 'no output on innocuous signals');
252         ok($lei->('daemon-pid'), 'daemon-pid');
253         chomp $out;
254         is($out, $new_pid, 'PID unchanged after -0/-CHLD');
255
256         if ('socket inaccessible') {
257                 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
258                 ok($lei->('help'), 'connect fail, one-shot fallback works');
259                 like($err, qr/\bconnect\(/, 'connect error noted');
260                 like($out, qr/^usage: /, 'help output works');
261                 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
262         }
263         unlink $sock or BAIL_OUT "unlink($sock) $!";
264         for (0..100) {
265                 kill('CHLD', $new_pid) or last;
266                 tick();
267         }
268         ok(!kill(0, $new_pid), 'daemon exits after unlink');
269         # success over socket, can't test without
270 };
271
272 done_testing;