lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / lei-externals.t
blob4f2dd6baf57e38729b4ced46a987e1fead6de860
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; use v5.10.1; use PublicInbox::TestCommon;
5 use Fcntl qw(SEEK_SET);
6 require_git 2.6;
7 require_mods(qw(json DBD::SQLite Xapian));
8 use POSIX qw(WTERMSIG WIFSIGNALED SIGPIPE);
10 my @onions = map { "http://$_.onion/meta/" } qw(
11         4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd
12         ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd
13         7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd);
15 my $test_external_remote = sub {
16         my ($url, $k) = @_;
17 SKIP: {
18         skip "$k unset", 1 if !$url;
19         require_cmd 'curl', 1 or skip 'curl missing', 1;
20         if ($url =~ m!\.onion/!) {
21                 require_cmd 'torsocks', 1 or skip 'no torsocks', 1;
22         }
23         my $mid = '20140421094015.GA8962@dcvr.yhbt.net';
24         my @cmd = ('q', '--only', $url, '-q', "m:$mid");
25         lei_ok(@cmd, \"query $url");
26         is($lei_err, '', "no errors on $url");
27         my $res = json_utf8->decode($lei_out);
28         is($res->[0]->{'m'}, $mid, "got expected mid from $url") or
29                 skip 'further remote tests', 1;
30         lei_ok(@cmd, 'd:..20101002', \'no results, no error');
31         is($lei_err, '', 'no output on 404, matching local FS behavior');
32         is($lei_out, "[null]\n", 'got null results');
33         my ($pid_before, $pid_after);
34         if (-d $ENV{XDG_RUNTIME_DIR} && -w _) {
35                 lei_ok 'daemon-pid';
36                 chomp($pid_before = $lei_out);
37                 ok($pid_before, 'daemon is live');
38         }
39         for my $out ([], [qw(-f mboxcl2)]) {
40                 pipe(my ($r, $w)) or BAIL_OUT $!;
41                 open my $err, '+>', undef or BAIL_OUT $!;
42                 my $opt = { run_mode => 0, 1 => $w, 2 => $err };
43                 my $cmd = [qw(lei q -qt), @$out, 'z:1..'];
44                 my $tp = start_script($cmd, undef, $opt);
45                 close $w;
46                 sysread($r, my $buf, 1);
47                 close $r; # trigger SIGPIPE
48                 $tp->join;
49                 ok(WIFSIGNALED($?), "signaled @$out");
50                 is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out");
51                 no_coredump;
52                 seek($err, 0, 0);
53                 my @err = <$err>;
54                 is_deeply(\@err, [], "no errors @$out");
55         }
56         if (-d $ENV{XDG_RUNTIME_DIR} && -w _) {
57                 lei_ok 'daemon-pid';
58                 chomp(my $pid_after = $lei_out);
59                 is($pid_after, $pid_before, 'pid unchanged') or
60                         skip 'daemon died', 1;
61                 skip 'not killing persistent lei-daemon', 2 if
62                                 $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
63                 lei_ok 'daemon-kill';
64                 my $alive = 1;
65                 for (1..100) {
66                         $alive = kill(0, $pid_after) or last;
67                         tick();
68                 }
69                 ok(!$alive, 'daemon-kill worked');
70                 no_coredump;
71         }
72 } # /SKIP
73 }; # /sub
75 my ($ro_home, $cfg_path) = setup_public_inboxes;
76 test_lei(sub {
77         my $home = $ENV{HOME};
78         my $config_file = "$home/.config/lei/config";
79         my $store_dir = "$home/.local/share/lei";
80         lei_ok 'ls-external', \'ls-external on fresh install';
81         ignore_inline_c_missing($lei_err);
82         is($lei_out.$lei_err, '', 'ls-external no output, yet');
83         ok(!-e $config_file && !-e $store_dir,
84                 'nothing created by ls-external');
86         ok(!lei('add-external', "$home/nonexistent"),
87                 "fails on non-existent dir");
88         like($lei_err, qr/not a directory/, 'noted non-existence');
89         mkdir "$home/new\nline" or BAIL_OUT "mkdir: $!";
90         ok(!lei('add-external', "$home/new\nline"), "fails on newline");
91         like($lei_err, qr/`\\n' not allowed/, 'newline noted in error');
92         lei_ok('ls-external', \'ls-external works after add failure');
93         is($lei_out.$lei_err, '', 'ls-external still has no output');
94         my $cfg = PublicInbox::Config->new($cfg_path);
95         $cfg->each_inbox(sub {
96                 my ($ibx) = @_;
97                 lei_ok(qw(add-external -q), $ibx->{inboxdir},
98                                 \'added external');
99                 is($lei_out.$lei_err, '', 'no output');
100         });
101         ok(-s $config_file, 'add-external created config');
102         my $lcfg = PublicInbox::Config->new($config_file);
103         $cfg->each_inbox(sub {
104                 my ($ibx) = @_;
105                 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
106                         "configured boost on $ibx->{name}");
107         });
108         lei_ok 'ls-external';
109         like($lei_out, qr/boost=0\n/s, 'ls-external has output');
110         lei_ok qw(add-external -q https://EXAMPLE.com/ibx), \'add remote';
111         is($lei_err, '', 'no warnings after add-external');
113         {
114                 lei_ok qw(ls-external --remote);
115                 my $r_only = +{ map { $_ => 1 } split(/^/m, $lei_out) };
116                 lei_ok qw(ls-external --local);
117                 my $l_only = +{ map { $_ => 1 } split(/^/m, $lei_out) };
118                 lei_ok 'ls-external';
119                 is_deeply([grep { $l_only->{$_} } keys %$r_only], [],
120                         'no locals in --remote');
121                 is_deeply([grep { $r_only->{$_} } keys %$l_only], [],
122                         'no remotes in --local');
123                 my $all = +{ map { $_ => 1 } split(/^/m, $lei_out) };
124                 is_deeply($all, { %$r_only, %$l_only },
125                                 'default output combines remote + local');
126                 lei_ok qw(ls-external --remote --local);
127                 my $both = +{ map { $_ => 1 } split(/^/m, $lei_out) };
128                 is_deeply($all, $both, '--remote --local == no args');
129         }
131         lei_ok qw(_complete lei forget-external), \'complete for externals';
132         my %comp = map { $_ => 1 } split(/\s+/, $lei_out);
133         ok($comp{'https://example.com/ibx/'}, 'forget external completion');
134         my @dirs;
135         $cfg->each_inbox(sub {
136                 my ($ibx) = @_;
137                 push @dirs, $ibx->{inboxdir};
138                 ok($comp{$ibx->{inboxdir}}, "local $ibx->{name} completion");
139         });
140         for my $u (qw(h http https https: https:/ https:// https://e
141                         https://example https://example. https://example.co
142                         https://example.com https://example.com/
143                         https://example.com/i https://example.com/ibx)) {
144                 lei_ok(qw(_complete lei forget-external), $u,
145                         \"partial completion for URL $u");
146                 is($lei_out, "https://example.com/ibx/\n",
147                         "completed partial URL $u");
148                 for my $qo (qw(-I --include --exclude --only)) {
149                         lei_ok(qw(_complete lei q), $qo, $u,
150                                 \"partial completion for URL q $qo $u");
151                         is($lei_out, "https://example.com/ibx/\n",
152                                 "completed partial URL $u on q $qo");
153                 }
154         }
155         lei_ok(qw(_complete lei add-external), 'https://',
156                 \'add-external hostname completion');
157         is($lei_out, "https://example.com/\n", 'completed up to hostname');
159         lei_ok('ls-external');
160         like($lei_out, qr!https://example\.com/ibx/!s, 'added canonical URL');
161         is($lei_err, '', 'no warnings on ls-external');
162         lei_ok(qw(forget-external -q https://EXAMPLE.com/ibx));
163         lei_ok('ls-external');
164         unlike($lei_out, qr!https://example\.com/ibx/!s,
165                 'removed canonical URL');
167         # do some queries
168         ok(!lei(qw(q s:prefix -o maildir:/dev/null)), 'bad maildir');
169         like($lei_err, qr!/dev/null exists and is not a directory!,
170                 'error shown');
171         is($? >> 8, 1, 'errored out with exit 1');
173         ok(!lei(qw(q s:prefix -o), "mboxcl2:$home"), 'bad mbox');
174         like($lei_err, qr!\Q$home\E exists and is not a writable file!,
175                 'error shown');
176         is($? >> 8, 1, 'errored out with exit 1');
178         ok(!lei(qw(q s:prefix -o Mbox2:/dev/stdout)), 'bad format');
179         like($lei_err, qr/bad mbox format: mbox2/, 'error shown');
180         is($? >> 8, 1, 'errored out with exit 1');
182         # note, on a Bourne shell users should be able to use either:
183         #       s:"use boolean prefix"
184         #       "s:use boolean prefix"
185         # or use single quotes, it should not matter.  Users only need
186         # to know shell quoting rules, not Xapian quoting rules.
187         # No double-quoting should be imposed on users on the CLI
188         lei_ok('q', 's:use boolean prefix');
189         like($lei_out, qr/search: use boolean prefix/,
190                 'phrase search got result');
191         my $res = json_utf8->decode($lei_out);
192         is(scalar(@$res), 2, 'only 2 element array (1 result)');
193         is($res->[1], undef, 'final element is undef'); # XXX should this be?
194         is(ref($res->[0]), 'HASH', 'first element is hashref');
195         lei_ok('q', '--pretty', 's:use boolean prefix');
196         my $pretty = json_utf8->decode($lei_out);
197         is_deeply($res, $pretty, '--pretty is identical after decode');
199         {
200                 open my $fh, '+>', undef or BAIL_OUT $!;
201                 $fh->autoflush(1);
202                 print $fh 's:use d:..5.days.from.now' or BAIL_OUT $!;
203                 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
204                 lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $fh },
205                                 \'--stdin on regular file works');
206                 like($lei_out, qr/use boolean/, '--stdin on regular file');
207         }
208         {
209                 pipe(my ($r, $w)) or BAIL_OUT $!;
210                 print $w 's:use' or BAIL_OUT $!;
211                 close $w or BAIL_OUT $!;
212                 lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $r },
213                                 \'--stdin on pipe file works');
214                 like($lei_out, qr/use boolean prefix/, '--stdin on pipe');
215         }
216         ok(!lei(qw(q -q --stdin s:use)), "--stdin and argv don't mix");
217         like($lei_err, qr/no query allowed.*--stdin/,
218                 '--stdin conflict error message');
220         for my $fmt (qw(ldjson ndjson jsonl)) {
221                 lei_ok('q', '-f', $fmt, 's:use boolean prefix');
222                 is($lei_out, json_utf8->encode($pretty->[0])."\n", "-f $fmt");
223         }
225         require IO::Uncompress::Gunzip;
226         for my $sfx ('', '.gz') {
227                 my $f = "$home/mbox$sfx";
228                 lei_ok('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
229                 my $cat = $sfx eq '' ? sub {
230                         open my $mb, '<', $f or fail "no mbox: $!";
231                         <$mb>
232                 } : sub {
233                         my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
234                         <$z>;
235                 };
236                 my @s = grep(/^Subject:/, $cat->());
237                 is(scalar(@s), 1, "1 result in mbox$sfx");
238                 lei_ok('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
239                 is(grep(!/^#/, $lei_err), 0, 'no errors from augment') or
240                         diag $lei_err;
241                 @s = grep(/^Subject:/, my @wtf = $cat->());
242                 is(scalar(@s), 2, "2 results in mbox$sfx");
244                 lei_ok('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
245                 is(grep(!/^#/, $lei_err), 0, "no errors on no results ($sfx)")
246                         or diag $lei_err;
248                 my @s2 = grep(/^Subject:/, $cat->());
249                 is_deeply(\@s2, \@s,
250                         "same 2 old results w/ --augment and bad search $sfx");
252                 lei_ok('q', '-o', "mboxcl2:$f", 's:nonexistent');
253                 my @res = $cat->();
254                 is_deeply(\@res, [], "clobber w/o --augment $sfx");
255         }
256         ok(!lei('q', '-o', "$home/mbox", 's:nope'),
257                         'fails if mbox format unspecified');
258         like($lei_err, qr/unable to determine mbox/, 'mbox-related message');
260         ok(!lei(qw(q --no-local s:see)), '--no-local');
261         is($? >> 8, 1, 'proper exit code');
262         like($lei_err, qr/no local or remote.+? to search/, 'no inbox');
264         for my $no (['--no-local'], ['--no-external'],
265                         [qw(--no-local --no-external)]) {
266                 lei_ok(qw(q mid:testmessage@example.com), @$no,
267                         '-I', $dirs[0], \"-I and @$no combine");
268                 $res = json_utf8->decode($lei_out);
269                 is($res->[0]->{'m'}, 'testmessage@example.com',
270                         "-I \$DIR got results regardless of @$no");
271         }
273         {
274                 skip 'TEST_LEI_DAEMON_PERSIST_DIR in use', 1 if
275                                         $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
276                 my @q = qw(q -o mboxcl2:rel.mboxcl2 bye);
277                 lei_ok('-C', $home, @q);
278                 is(unlink("$home/rel.mboxcl2"), 1, '-C works before q');
280                 # we are more flexible than git, here:
281                 lei_ok(@q, '-C', $home);
282                 is(unlink("$home/rel.mboxcl2"), 1, '-C works after q');
283                 mkdir "$home/deep" or BAIL_OUT $!;
284                 lei_ok('-C', $home, @q, '-C', 'deep');
285                 is(unlink("$home/deep/rel.mboxcl2"), 1, 'multiple -C works');
287                 lei_ok('-C', '', '-C', $home, @q, '-C', 'deep', '-C', '');
288                 is(unlink("$home/deep/rel.mboxcl2"), 1, "-C '' accepted");
289                 ok(!-f "$home/rel.mboxcl2", 'wrong path not created');
290         }
291         my %e = (
292                 TEST_LEI_EXTERNAL_HTTPS => 'https://public-inbox.org/meta/',
293                 TEST_LEI_EXTERNAL_ONION => $onions[int(rand(scalar(@onions)))],
294         );
295         for my $k (keys %e) {
296                 my $url = $ENV{$k} // '';
297                 $url = $e{$k} if $url eq '1';
298                 $test_external_remote->($url, $k);
299         }
300 }); # test_lei
301 done_testing;