]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
t/cmd_ipc: allow extra errors and add diagnostics
[public-inbox.git] / t / v2mirror.t
1 # Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use v5.10.1;
5 use PublicInbox::TestCommon;
6 use File::Path qw(remove_tree make_path);
7 use Cwd qw(abs_path);
8 use PublicInbox::Spawn qw(which);
9 require_git(2.6);
10 require_cmd('curl');
11 local $ENV{HOME} = abs_path('t');
12 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
13
14 # Integration tests for HTTP cloning + mirroring
15 require_mods(qw(Plack::Util Plack::Builder
16                 HTTP::Date HTTP::Status Search::Xapian DBD::SQLite));
17 use_ok 'PublicInbox::V2Writable';
18 use PublicInbox::InboxWritable;
19 use PublicInbox::Eml;
20 use PublicInbox::Config;
21 # FIXME: too much setup
22 my ($tmpdir, $for_destroy) = tmpdir();
23 my $pi_config = "$tmpdir/config";
24 {
25         open my $fh, '>', $pi_config or die "open($pi_config): $!";
26         print $fh <<"" or die "print $pi_config: $!";
27 [publicinbox "v2"]
28 ; using "mainrepo" rather than "inboxdir" for v1.1.0-pre1 WWW compat below
29         mainrepo = $tmpdir/in
30         address = test\@example.com
31
32         close $fh or die "close($pi_config): $!";
33 }
34 local $ENV{PI_CONFIG} = $pi_config;
35
36 my $cfg = PublicInbox::Config->new($pi_config);
37 my $ibx = $cfg->lookup('test@example.com');
38 ok($ibx, 'inbox found');
39 $ibx->{version} = 2;
40 $ibx->{-no_fsync} = 1;
41 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
42 ok $v2w, 'v2w loaded';
43 $v2w->{parallel} = 0;
44 my $mime = PublicInbox::Eml->new(<<'');
45 From: Me <me@example.com>
46 To: You <you@example.com>
47 Subject: a
48 Date: Thu, 01 Jan 1970 00:00:00 +0000
49
50 my $old_rotate_bytes = $v2w->{rotate_bytes};
51 $v2w->{rotate_bytes} = 500; # force rotating
52 for my $i (1..9) {
53         $mime->header_set('Message-ID', "<$i\@example.com>");
54         $mime->header_set('Subject', "subject = $i");
55         ok($v2w->add($mime), "add msg $i OK");
56 }
57
58 my $epoch_max = $v2w->{epoch_max};
59 ok($epoch_max > 0, "multiple epochs");
60 $v2w->done;
61 {
62         my $smsg = $ibx->over->get_art(1);
63         like($smsg->{lines}, qr/\A[0-9]+\z/, 'lines is a digit');
64         like($smsg->{bytes}, qr/\A[0-9]+\z/, 'bytes is a digit');
65 }
66 $ibx->cleanup;
67
68 local $ENV{TEST_IPV4_ONLY} = 1; # plackup (below) doesn't do IPv6
69 my $rdr = { 3 => tcp_server() };
70 my @cmd = ('-httpd', '-W0', "--stdout=$tmpdir/out", "--stderr=$tmpdir/err");
71 my $td = start_script(\@cmd, undef, $rdr);
72 my ($host, $port) = tcp_host_port(delete $rdr->{3});
73
74 @cmd = (qw(-clone -q), "http://$host:$port/v2/", "$tmpdir/m");
75 run_script(\@cmd) or xbail '-clone';
76
77 for my $i (0..$epoch_max) {
78         ok(-d "$tmpdir/m/git/$i.git", "epoch $i cloned");
79 }
80
81 @cmd = ("-init", '-j1', '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
82         'alt@example.com');
83 ok(run_script(\@cmd), 'initialized public-inbox -V2');
84 my @shards = glob("$tmpdir/m/xap*/?");
85 is(scalar(@shards), 1, 'got a single shard on init');
86
87 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'indexed');
88
89 my $mibx = { inboxdir => "$tmpdir/m", address => 'alt@example.com' };
90 $mibx = PublicInbox::Inbox->new($mibx);
91 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
92
93 for my $i (10..15) {
94         $mime->header_set('Message-ID', "<$i\@example.com>");
95         $mime->header_set('Subject', "subject = $i");
96         ok($v2w->add($mime), "add msg $i OK");
97 }
98 $v2w->done;
99 $ibx->cleanup;
100
101 my @new_epochs;
102 my $fetch_each_epoch = sub {
103         my %before = map { $_ => 1 } glob("$tmpdir/m/git/*");
104         run_script([qw(-fetch --exit-code -q)], undef, {-C => "$tmpdir/m"}) or
105                 xbail '-fetch fail';
106         is($?, 0, '--exit-code 0 after fetch updated');
107         my @after = grep { !$before{$_} } glob("$tmpdir/m/git/*");
108         push @new_epochs, @after;
109 };
110
111 $fetch_each_epoch->();
112
113 my $mset = $mibx->search->reopen->mset('m:15@example.com');
114 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
115 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'index updated');
116 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
117 $mset = $mibx->search->reopen->mset('m:15@example.com');
118 is(scalar($mset->items), 1, 'found message in mirror');
119
120 # purge:
121 $mime->header_set('Message-ID', '<10@example.com>');
122 $mime->header_set('Subject', 'subject = 10');
123 {
124         my @warn;
125         local $SIG{__WARN__} = sub { push @warn, @_ };
126         ok($v2w->purge($mime), 'purge a message');
127         my $warn = join('', @warn);
128         like($warn, qr/purge rewriting/);
129         my @subj = ($warn =~ m/^# subject .*$/mg);
130         is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
131 }
132
133 $v2w->done;
134
135 my $msgs = $mibx->over->get_thread('10@example.com');
136 my $to_purge = $msgs->[0]->{blob};
137 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
138 $mset = $ibx->search->reopen->mset('m:10@example.com');
139 is(scalar($mset->items), 0, 'purged message gone from origin');
140
141 $fetch_each_epoch->();
142 {
143         $ibx->cleanup;
144         PublicInbox::InboxWritable::cleanup($mibx);
145         $v2w->done;
146         my $cmd = [ qw(-index --prune -j0), "$tmpdir/m" ];
147         my ($out, $err) = ('', '');
148         my $opt = { 1 => \$out, 2 => \$err };
149         ok(run_script($cmd, undef, $opt), '-index --prune');
150         like($err, qr/discontiguous range/, 'warned about discontiguous range');
151         unlike($err, qr/fatal/, 'no scary fatal error shown');
152 }
153
154 $mset = $mibx->search->reopen->mset('m:10@example.com');
155 is(scalar($mset->items), 0, 'purged message not found in mirror');
156 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'minmax still synced');
157 for my $i ((1..9),(11..15)) {
158         $mset = $mibx->search->mset("m:$i\@example.com");
159         is(scalar($mset->items), 1, "$i\@example.com remains visible");
160 }
161 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
162
163 {
164         my @warn;
165         local $SIG{__WARN__} = sub { push @warn, @_ };
166         $v2w->index_sync;
167         is_deeply(\@warn, [], 'no warnings from index_sync after purge');
168 }
169
170 # deletes happen in a different fetch window
171 {
172         $mset = $mibx->search->reopen->mset('m:1@example.com');
173         is(scalar($mset->items), 1, '1@example.com visible in mirror');
174         $mime->header_set('Message-ID', '<1@example.com>');
175         $mime->header_set('Subject', 'subject = 1');
176         ok($v2w->remove($mime), 'removed <1@example.com> from source');
177         $v2w->done;
178         $ibx->cleanup;
179         $fetch_each_epoch->();
180         PublicInbox::InboxWritable::cleanup($mibx);
181
182         my $cmd = [ qw(-index -j0), "$tmpdir/m" ];
183         my ($out, $err) = ('', '');
184         my $opt = { 1 => \$out, 2 => \$err };
185         ok(run_script($cmd, undef, $opt), 'index ran');
186         is($err, '', 'no errors reported by index');
187         $mset = $mibx->search->reopen->mset('m:1@example.com');
188         is(scalar($mset->items), 0, '1@example.com no longer visible in mirror');
189 }
190
191 if ('sequential-shard') {
192         $mset = $mibx->search->mset('m:15@example.com');
193         is(scalar($mset->items), 1, 'large message not indexed');
194         remove_tree(glob("$tmpdir/m/xap*"), glob("$tmpdir/m/msgmap.*"));
195         my $cmd = [ qw(-index -j9 --sequential-shard), "$tmpdir/m" ];
196         ok(run_script($cmd), '--sequential-shard works');
197         my @shards = glob("$tmpdir/m/xap*/?");
198         is(scalar(@shards), 8, 'got expected shard count');
199         PublicInbox::InboxWritable::cleanup($mibx);
200         $mset = $mibx->search->mset('m:15@example.com');
201         is(scalar($mset->items), 1, 'search works after --sequential-shard');
202 }
203
204 if ('max size') {
205         $mime->header_set('Message-ID', '<2big@a>');
206         my $max = '2k';
207         $mime->body_str_set("z\n" x 1024);
208         ok($v2w->add($mime), "add big message");
209         $v2w->done;
210         $ibx->cleanup;
211         $fetch_each_epoch->();
212         PublicInbox::InboxWritable::cleanup($mibx);
213         my $cmd = [qw(-index -j0), "$tmpdir/m", "--max-size=$max" ];
214         my $opt = { 2 => \(my $err) };
215         ok(run_script($cmd, undef, $opt), 'indexed with --max-size');
216         like($err, qr/skipping [a-f0-9]{40,}/, 'warned about skipping message');
217         $mset = $mibx->search->reopen->mset('m:2big@a');
218         is(scalar($mset->items), 0, 'large message not indexed');
219
220         {
221                 open my $fh, '>>', $pi_config or die;
222                 print $fh <<EOF or die;
223 [publicinbox]
224         indexMaxSize = 2k
225 EOF
226                 close $fh or die;
227         }
228         $cmd = [ qw(-index -j0 --reindex), "$tmpdir/m" ];
229         ok(run_script($cmd, undef, $opt), 'reindexed w/ indexMaxSize in file');
230         like($err, qr/skipping [a-f0-9]{40,}/, 'warned about skipping message');
231         $mset = $mibx->search->reopen->mset('m:2big@a');
232         is(scalar($mset->items), 0, 'large message not re-indexed');
233 }
234 ok(scalar(@new_epochs), 'new epochs were created and fetched');
235 for my $d (@new_epochs) {
236         is(xqx(['git', "--git-dir=$d", 'config', qw(include.path)]),
237                 "../../all.git/config\n",
238                 'include.path set');
239 }
240
241 if ('test read-only epoch dirs') {
242         my @git = ('git', "--git-dir=$new_epochs[0]");
243         my $get_objs = [@git,
244                 qw(cat-file --buffer --batch-check --batch-all-objects)];
245         my $before = [sort xqx($get_objs)];
246
247         remove_tree(map { "$new_epochs[0]/$_" } qw(objects refs/heads));
248         chmod(0555, $new_epochs[0]) or xbail "chmod: $!";
249
250         # force a refetch
251         unlink("$tmpdir/m/manifest.js.gz") or xbail "unlink: $!";
252
253         run_script([qw(-fetch -q)], undef, {-C => "$tmpdir/m"}) or
254                 xbail '-fetch failed';
255
256         ok(!-d "$new_epochs[0]/objects", 'no objects after fetch to R/O dir');
257
258         chmod(0755, $new_epochs[0]) or xbail "chmod: $!";
259         mkdir("$new_epochs[0]/objects") or xbail "mkdir: $!";
260         mkdir("$new_epochs[0]/refs/heads") or xbail "mkdir: $!";
261
262         my $err = '';
263         run_script([qw(-fetch -q)], undef, {-C => "$tmpdir/m", 2 => \$err}) or
264                 xbail '-fetch failed '.$err;
265         is_deeply([ sort xqx($get_objs) ], $before,
266                 'fetch restored objects once GIT_DIR became writable');
267 }
268
269 {
270         my $dst = "$tmpdir/partial";
271         run_script([qw(-clone -q --epoch=~0), "http://$host:$port/v2/", $dst]);
272         is($?, 0, 'no error from partial clone');
273         my @g = glob("$dst/git/*.git");
274         my @w = grep { -w $_ } @g;
275         my @r = grep { ! -w $_ } @g;
276         is(scalar(@w), 1, 'one writable directory');
277         my ($w) = ($w[0] =~ m!/([0-9]+)\.git\z!);
278         is((grep {
279                 m!/([0-9]+)\.git\z! or xbail "no digit in $_";
280                 $w > ($1 + 0)
281         } @r), scalar(@r), 'writable epoch # exceeds read-only ones');
282         run_script([qw(-fetch -q)], undef, { -C => $dst });
283         is($?, 0, 'no error from partial fetch');
284         remove_tree($dst);
285
286         run_script([qw(-clone -q --epoch=~1..),
287                         "http://$host:$port/v2/", $dst]);
288         my @g2 = glob("$dst/git/*.git") ;
289         is_deeply(\@g2, \@g, 'cloned again');
290         is(scalar(grep { -w $_ } @g2), scalar(@w) + 1,
291                 'got one more cloned epoch');
292
293         # make 0.git writable and fetch into it, relies on culled manifest
294         chmod(0755, $g2[0]) or xbail "chmod: $!";
295         my @before = glob("$g2[0]/objects/*/*");
296         run_script([qw(-fetch -q)], undef, { -C => $dst });
297         is($?, 0, 'no error from partial fetch');
298         my @after = glob("$g2[0]/objects/*/*");
299         ok(scalar(@before) < scalar(@after), 'fetched after chmod 0755 0.git');
300
301         # ensure culled manifest is maintained after fetch
302         gunzip("$dst/manifest.js.gz" => \(my $m), MultiStream => 1) or
303                 xbail "gunzip: $GunzipError";
304         $m = PublicInbox::Config->json->decode($m);
305         for my $k (keys %$m) { # /$name/git/$N.git
306                 my ($nr) = ($k =~ m!/git/([0-9]+)\.git\z!);
307                 ok(-w "$dst/git/$nr.git", "writable $nr.git in manifest");
308         }
309         for my $ro (grep { !-w $_ } @g2) {
310                 my ($nr) = ($ro =~ m!/git/([0-9]+)\.git\z!);
311                 is(grep(m!/git/$nr\.git\z!, keys %$m), 0,
312                         "read-only $nr.git not in manifest")
313                         or xbail([sort keys %$m]);
314         }
315 }
316
317 my $err = '';
318 my $oldrev = '0b3e19584c90d958a723ac2d3dec3f84f5513688~1';
319 # 3e0e596105198cfa (wwwlisting: allow hiding entries from manifest, 2019-06-09)
320 $oldrev = xqx([qw(git rev-parse), $oldrev], undef, { 2 => \$err });
321 SKIP: {
322         skip("no detected public-inbox GIT_DIR ($err)", 1) if $?;
323         require_mods('Email::MIME', 1); # for legacy revision
324         # using plackup to test old PublicInbox::WWW since -httpd from
325         # back then relied on some packages we no longer depend on
326         my $plackup = which('plackup') or skip('no plackup in path', 1);
327         require PublicInbox::Lock;
328         chomp $oldrev;
329         my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!);
330         my $wt = "t/data-gen/$base.pre-manifest-$oldrev";
331         my $lk = bless { lock_path => __FILE__ }, 'PublicInbox::Lock';
332         $lk->lock_acquire;
333         my $psgi = "$wt/app.psgi";
334         if (!-f $psgi) { # checkout a pre-manifest.js.gz version
335                 my $t = File::Temp->new(TEMPLATE => 'g-XXXX', TMPDIR => 1);
336                 my $env = { GIT_INDEX_FILE => $t->filename };
337                 xsys([qw(git read-tree), $oldrev], $env) and xbail 'read-tree';
338                 xsys([qw(git checkout-index -a), "--prefix=$wt/"], $env)
339                         and xbail 'checkout-index';
340                 my $f = "$wt/app.psgi.tmp.$$";
341                 open my $fh, '>', $f or xbail $!;
342                 print $fh <<'EOM' or xbail $!;
343 use Plack::Builder;
344 use PublicInbox::WWW;
345 my $www = PublicInbox::WWW->new;
346 builder { enable 'Head'; sub { $www->call(@_) } }
347 EOM
348                 close $fh or xbail $!;
349                 rename($f, $psgi) or xbail $!;
350         }
351         $lk->lock_release;
352
353         $rdr->{run_mode} = 0;
354         $rdr->{-C} = $wt;
355         my $cmd = [$plackup, qw(-Enone -Ilib), "--host=$host", "--port=$port"];
356         $td->join('TERM');
357         open $rdr->{2}, '>>', "$tmpdir/plackup.err.log" or xbail "open: $!";
358         open $rdr->{1}, '>>&', $rdr->{2} or xbail "open: $!";
359         my $env = { PERL5LIB => 'lib', PERL_INLINE_DIRECTORY => undef };
360         $td = start_script($cmd, $env, $rdr);
361         # wait for plackup socket()+bind()+listen()
362         my %opt = ( Proto => 'tcp', Type => Socket::SOCK_STREAM(),
363                 PeerAddr => "$host:$port" );
364         for (0..50) {
365                 tick();
366                 last if IO::Socket::INET->new(%opt);
367         }
368         my $dst = "$tmpdir/scrape";
369         @cmd = (qw(-clone -q), "http://$host:$port/v2", $dst);
370         run_script(\@cmd, undef, { 2 => \($err = '') });
371         is($?, 0, 'scraping clone on old PublicInbox::WWW')
372                 or diag $err;
373         my @g_all = glob("$dst/git/*.git");
374         ok(scalar(@g_all) > 1, 'cloned multiple epochs');
375
376         remove_tree($dst);
377         @cmd = (qw(-clone -q --epoch=~0), "http://$host:$port/v2", $dst);
378         run_script(\@cmd, undef, { 2 => \($err = '') });
379         is($?, 0, 'partial scraping clone on old PublicInbox::WWW');
380         my @g_last = grep { -w $_ } glob("$dst/git/*.git");
381         is_deeply(\@g_last, [ $g_all[-1] ], 'partial clone of ~0 worked');
382
383         chmod(0755, $g_all[0]) or xbail "chmod $!";
384         my @before = glob("$g_all[0]/objects/*/*");
385         run_script([qw(-fetch -v)], undef, { -C => $dst, 2 => \($err = '') });
386         is($?, 0, 'scraping fetch on old PublicInbox::WWW') or diag $err;
387         my @after = glob("$g_all[0]/objects/*/*");
388         ok(scalar(@before) < scalar(@after),
389                 'fetched 0.git after enabling write-bit');
390
391         $td->join('TERM');
392 }
393
394 done_testing;