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>
5 use PublicInbox::TestCommon;
6 use File::Path qw(remove_tree make_path);
8 use PublicInbox::Spawn qw(which);
11 local $ENV{HOME} = abs_path('t');
12 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
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;
20 use PublicInbox::Config;
21 # FIXME: too much setup
22 my ($tmpdir, $for_destroy) = tmpdir();
23 my $pi_config = "$tmpdir/config";
25 open my $fh, '>', $pi_config or die "open($pi_config): $!";
26 print $fh <<"" or die "print $pi_config: $!";
28 ; using "mainrepo" rather than "inboxdir" for v1.1.0-pre1 WWW compat below
30 address = test\@example.com
32 close $fh or die "close($pi_config): $!";
34 local $ENV{PI_CONFIG} = $pi_config;
36 my $cfg = PublicInbox::Config->new($pi_config);
37 my $ibx = $cfg->lookup('test@example.com');
38 ok($ibx, 'inbox found');
40 $ibx->{-no_fsync} = 1;
41 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
42 ok $v2w, 'v2w loaded';
44 my $mime = PublicInbox::Eml->new(<<'');
45 From: Me <me@example.com>
46 To: You <you@example.com>
48 Date: Thu, 01 Jan 1970 00:00:00 +0000
50 my $old_rotate_bytes = $v2w->{rotate_bytes};
51 $v2w->{rotate_bytes} = 500; # force rotating
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");
58 my $epoch_max = $v2w->{epoch_max};
59 ok($epoch_max > 0, "multiple epochs");
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');
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});
74 @cmd = (qw(-clone -q), "http://$host:$port/v2/", "$tmpdir/m");
75 run_script(\@cmd) or xbail '-clone';
77 for my $i (0..$epoch_max) {
78 ok(-d "$tmpdir/m/git/$i.git", "epoch $i cloned");
81 @cmd = ("-init", '-j1', '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
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');
87 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'indexed');
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');
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");
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
106 is($?, 0, '--exit-code 0 after fetch updated');
107 my @after = grep { !$before{$_} } glob("$tmpdir/m/git/*");
108 push @new_epochs, @after;
111 $fetch_each_epoch->();
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');
121 $mime->header_set('Message-ID', '<10@example.com>');
122 $mime->header_set('Subject', 'subject = 10');
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");
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');
141 $fetch_each_epoch->();
144 PublicInbox::InboxWritable::cleanup($mibx);
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');
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");
161 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
165 local $SIG{__WARN__} = sub { push @warn, @_ };
167 is_deeply(\@warn, [], 'no warnings from index_sync after purge');
170 # deletes happen in a different fetch window
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');
179 $fetch_each_epoch->();
180 PublicInbox::InboxWritable::cleanup($mibx);
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');
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');
205 $mime->header_set('Message-ID', '<2big@a>');
207 $mime->body_str_set("z\n" x 1024);
208 ok($v2w->add($mime), "add big message");
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');
221 open my $fh, '>>', $pi_config or die;
222 print $fh <<EOF or die;
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');
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",
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)];
247 remove_tree(map { "$new_epochs[0]/$_" } qw(objects refs/heads));
248 chmod(0555, $new_epochs[0]) or xbail "chmod: $!";
251 unlink("$tmpdir/m/manifest.js.gz") or xbail "unlink: $!";
253 run_script([qw(-fetch -q)], undef, {-C => "$tmpdir/m"}) or
254 xbail '-fetch failed';
256 ok(!-d "$new_epochs[0]/objects", 'no objects after fetch to R/O dir');
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: $!";
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');
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!);
279 m!/([0-9]+)\.git\z! or xbail "no digit in $_";
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');
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');
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');
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");
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]);
318 my $v110 = xqx([qw(git rev-parse v1.1.0-pre1)], undef, { 2 => \$err });
320 skip("no detected public-inbox GIT_DIR ($err)", 1) if $?;
321 # using plackup to test old PublicInbox::WWW since -httpd from
322 # back then relied on some packages we no longer depend on
323 my $plackup = which('plackup') or skip('no plackup in path', 1);
324 require PublicInbox::Lock;
326 my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!);
327 my $wt = "t/data-gen/$base.pre-manifest";
328 my $lk = bless { lock_path => __FILE__ }, 'PublicInbox::Lock';
330 my $psgi = "$wt/app.psgi";
331 if (!-f $psgi) { # checkout a pre-manifest.js.gz version
332 my $t = File::Temp->new(TEMPLATE => 'g-XXXX', TMPDIR => 1);
333 my $env = { GIT_INDEX_FILE => $t->filename };
334 xsys([qw(git read-tree), $v110], $env) and xbail 'read-tree';
335 xsys([qw(git checkout-index -a), "--prefix=$wt/"], $env)
336 and xbail 'checkout-index';
337 my $f = "$wt/app.psgi.tmp.$$";
338 open my $fh, '>', $f or xbail $!;
339 print $fh <<'EOM' or xbail $!;
341 use PublicInbox::WWW;
342 my $www = PublicInbox::WWW->new;
343 builder { enable 'Head'; sub { $www->call(@_) } }
345 close $fh or xbail $!;
346 rename($f, $psgi) or xbail $!;
350 $rdr->{run_mode} = 0;
352 my $cmd = [$plackup, qw(-Enone -Ilib), "--host=$host", "--port=$port"];
354 open $rdr->{2}, '>>', "$tmpdir/plackup.err.log" or xbail "open: $!";
355 open $rdr->{1}, '>>&', $rdr->{2} or xbail "open: $!";
356 $td = start_script($cmd, { PERL5LIB => 'lib' }, $rdr);
357 # wait for plackup socket()+bind()+listen()
358 my %opt = ( Proto => 'tcp', Type => Socket::SOCK_STREAM(),
359 PeerAddr => "$host:$port" );
362 last if IO::Socket::INET->new(%opt);
364 my $dst = "$tmpdir/scrape";
365 @cmd = (qw(-clone -q), "http://$host:$port/v2", $dst);
366 run_script(\@cmd, undef, { 2 => \(my $err = '') });
367 is($?, 0, 'scraping clone on old PublicInbox::WWW')
369 my @g_all = glob("$dst/git/*.git");
370 ok(scalar(@g_all) > 1, 'cloned multiple epochs');
373 @cmd = (qw(-clone -q --epoch=~0), "http://$host:$port/v2", $dst);
374 run_script(\@cmd, undef, { 2 => \($err = '') });
375 is($?, 0, 'partial scraping clone on old PublicInbox::WWW');
376 my @g_last = grep { -w $_ } glob("$dst/git/*.git");
377 is_deeply(\@g_last, [ $g_all[-1] ], 'partial clone of ~0 worked');
379 chmod(0755, $g_all[0]) or xbail "chmod $!";
380 my @before = glob("$g_all[0]/objects/*/*");
381 run_script([qw(-fetch -v)], undef, { -C => $dst, 2 => \($err = '') });
382 is($?, 0, 'scraping fetch on old PublicInbox::WWW') or diag $err;
383 my @after = glob("$g_all[0]/objects/*/*");
384 ok(scalar(@before) < scalar(@after),
385 'fetched 0.git after enabling write-bit');