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