1 # Copyright (C) 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);
9 use PublicInbox::Spawn qw(which);
12 local $ENV{HOME} = abs_path('t');
13 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
15 # Integration tests for HTTP cloning + mirroring
16 require_mods qw(psgi -httpd 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, { nproc => 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
105 xbail('-fetch fail ',
106 [ xqx([which('find'), "$tmpdir/m", qw(-type f -ls) ]) ],
108 is($?, 0, '--exit-code 0 after fetch updated');
109 my @after = grep { !$before{$_} } glob("$tmpdir/m/git/*");
110 push @new_epochs, @after;
113 $fetch_each_epoch->();
115 my $mset = $mibx->search->reopen->mset('m:15@example.com');
116 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
117 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'index updated');
118 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
119 $mset = $mibx->search->reopen->mset('m:15@example.com');
120 is(scalar($mset->items), 1, 'found message in mirror');
123 $mime->header_set('Message-ID', '<10@example.com>');
124 $mime->header_set('Subject', 'subject = 10');
127 local $SIG{__WARN__} = sub { push @warn, @_ };
128 ok($v2w->purge($mime), 'purge a message');
129 my $warn = join('', @warn);
130 like($warn, qr/purge rewriting/);
131 my @subj = ($warn =~ m/^# subject .*$/mg);
132 is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
137 my $msgs = $mibx->over->get_thread('10@example.com');
138 my $to_purge = $msgs->[0]->{blob};
139 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
140 $mset = $ibx->search->reopen->mset('m:10@example.com');
141 is(scalar($mset->items), 0, 'purged message gone from origin');
143 $fetch_each_epoch->();
146 PublicInbox::InboxWritable::cleanup($mibx);
148 my $cmd = [ qw(-index --prune -j0), "$tmpdir/m" ];
149 my ($out, $err) = ('', '');
150 my $opt = { 1 => \$out, 2 => \$err };
151 ok(run_script($cmd, undef, $opt), '-index --prune');
152 like($err, qr/discontiguous range/, 'warned about discontiguous range');
153 unlike($err, qr/fatal/, 'no scary fatal error shown');
156 $mset = $mibx->search->reopen->mset('m:10@example.com');
157 is(scalar($mset->items), 0, 'purged message not found in mirror');
158 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'minmax still synced');
159 for my $i ((1..9),(11..15)) {
160 $mset = $mibx->search->mset("m:$i\@example.com");
161 is(scalar($mset->items), 1, "$i\@example.com remains visible");
163 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
167 local $SIG{__WARN__} = sub { push @warn, @_ };
169 is_deeply(\@warn, [], 'no warnings from index_sync after purge');
172 # deletes happen in a different fetch window
174 $mset = $mibx->search->reopen->mset('m:1@example.com');
175 is(scalar($mset->items), 1, '1@example.com visible in mirror');
176 $mime->header_set('Message-ID', '<1@example.com>');
177 $mime->header_set('Subject', 'subject = 1');
178 ok($v2w->remove($mime), 'removed <1@example.com> from source');
181 $fetch_each_epoch->();
182 PublicInbox::InboxWritable::cleanup($mibx);
184 my $cmd = [ qw(-index -j0), "$tmpdir/m" ];
185 my ($out, $err) = ('', '');
186 my $opt = { 1 => \$out, 2 => \$err };
187 ok(run_script($cmd, undef, $opt), 'index ran');
188 is($err, '', 'no errors reported by index');
189 $mset = $mibx->search->reopen->mset('m:1@example.com');
190 is(scalar($mset->items), 0, '1@example.com no longer visible in mirror');
193 if ('sequential-shard') {
194 $mset = $mibx->search->mset('m:15@example.com');
195 is(scalar($mset->items), 1, 'large message not indexed');
196 remove_tree(glob("$tmpdir/m/xap*"), glob("$tmpdir/m/msgmap.*"));
197 my $cmd = [ qw(-index -j9 --sequential-shard), "$tmpdir/m" ];
198 ok(run_script($cmd), '--sequential-shard works');
199 my @shards = glob("$tmpdir/m/xap*/?");
200 is(scalar(@shards), 8, 'got expected shard count');
201 PublicInbox::InboxWritable::cleanup($mibx);
202 $mset = $mibx->search->mset('m:15@example.com');
203 is(scalar($mset->items), 1, 'search works after --sequential-shard');
207 $mime->header_set('Message-ID', '<2big@a>');
209 $mime->body_str_set("z\n" x 1024);
210 ok($v2w->add($mime), "add big message");
213 $fetch_each_epoch->();
214 PublicInbox::InboxWritable::cleanup($mibx);
215 my $cmd = [qw(-index -j0), "$tmpdir/m", "--max-size=$max" ];
216 my $opt = { 2 => \(my $err) };
217 ok(run_script($cmd, undef, $opt), 'indexed with --max-size');
218 like($err, qr/skipping [a-f0-9]{40,}/, 'warned about skipping message');
219 $mset = $mibx->search->reopen->mset('m:2big@a');
220 is(scalar($mset->items), 0, 'large message not indexed');
223 open my $fh, '>>', $pi_config or die;
224 print $fh <<EOF or die;
230 $cmd = [ qw(-index -j0 --reindex), "$tmpdir/m" ];
231 ok(run_script($cmd, undef, $opt), 'reindexed w/ indexMaxSize in file');
232 like($err, qr/skipping [a-f0-9]{40,}/, 'warned about skipping message');
233 $mset = $mibx->search->reopen->mset('m:2big@a');
234 is(scalar($mset->items), 0, 'large message not re-indexed');
236 ok(scalar(@new_epochs), 'new epochs were created and fetched');
237 for my $d (@new_epochs) {
238 is(xqx(['git', "--git-dir=$d", 'config', qw(include.path)]),
239 "../../all.git/config\n",
243 if ('test read-only epoch dirs') {
244 my @git = ('git', "--git-dir=$new_epochs[0]");
245 my $get_objs = [@git,
246 qw(cat-file --buffer --batch-check --batch-all-objects)];
247 my $before = [sort xqx($get_objs)];
249 remove_tree(map { "$new_epochs[0]/$_" } qw(objects refs/heads));
250 chmod(0555, $new_epochs[0]) or xbail "chmod: $!";
253 unlink("$tmpdir/m/manifest.js.gz") or xbail "unlink: $!";
255 run_script([qw(-fetch -q)], undef, {-C => "$tmpdir/m"}) or
256 xbail '-fetch failed';
258 ok(!-d "$new_epochs[0]/objects", 'no objects after fetch to R/O dir');
260 chmod(0755, $new_epochs[0]) or xbail "chmod: $!";
261 mkdir("$new_epochs[0]/objects") or xbail "mkdir: $!";
262 mkdir("$new_epochs[0]/refs/heads") or xbail "mkdir: $!";
265 run_script([qw(-fetch -q)], undef, {-C => "$tmpdir/m", 2 => \$err}) or
266 xbail '-fetch failed '.$err;
267 is_deeply([ sort xqx($get_objs) ], $before,
268 'fetch restored objects once GIT_DIR became writable');
272 my $dst = "$tmpdir/partial";
273 run_script([qw(-clone -q --epoch=~0), "http://$host:$port/v2/", $dst]);
274 is($?, 0, 'no error from partial clone');
275 my @g = glob("$dst/git/*.git");
276 my @w = grep { -w $_ } @g;
277 my @r = grep { ! -w $_ } @g;
279 @w = grep { (stat($_))[2] & 0200 } @g;
280 @r = grep { !((stat($_))[2] & 0200) } @g;
282 is(scalar(@w), 1, 'one writable directory');
283 my ($w) = ($w[0] =~ m!/([0-9]+)\.git\z!);
285 m!/([0-9]+)\.git\z! or xbail "no digit in $_";
287 } @r), scalar(@r), 'writable epoch # exceeds read-only ones');
288 run_script([qw(-fetch -q)], undef, { -C => $dst });
289 is($?, 0, 'no error from partial fetch');
292 run_script([qw(-clone -q --epoch=~1..),
293 "http://$host:$port/v2/", $dst]);
294 my @g2 = glob("$dst/git/*.git") ;
295 is_deeply(\@g2, \@g, 'cloned again');
296 is(scalar(grep { (stat($_))[2] & 0200 } @g2), scalar(@w) + 1,
297 'got one more cloned epoch');
299 # make 0.git writable and fetch into it, relies on culled manifest
300 chmod(0755, $g2[0]) or xbail "chmod: $!";
301 my @before = glob("$g2[0]/objects/*/*");
302 run_script([qw(-fetch -q)], undef, { -C => $dst });
303 is($?, 0, 'no error from partial fetch');
304 my @after = glob("$g2[0]/objects/*/*");
305 ok(scalar(@before) < scalar(@after), 'fetched after chmod 0755 0.git');
307 # ensure culled manifest is maintained after fetch
308 gunzip("$dst/manifest.js.gz" => \(my $m), MultiStream => 1) or
309 xbail "gunzip: $GunzipError";
310 $m = PublicInbox::Config->json->decode($m);
311 for my $k (keys %$m) { # /$name/git/$N.git
312 my ($nr) = ($k =~ m!/git/([0-9]+)\.git\z!);
313 ok(-w "$dst/git/$nr.git", "writable $nr.git in manifest");
315 for my $ro (grep { !-w $_ } @g2) {
316 my ($nr) = ($ro =~ m!/git/([0-9]+)\.git\z!);
317 is(grep(m!/git/$nr\.git\z!, keys %$m), 0,
318 "read-only $nr.git not in manifest")
319 or xbail([sort keys %$m]);
324 my $oldrev = '0b3e19584c90d958a723ac2d3dec3f84f5513688~1';
325 # 3e0e596105198cfa (wwwlisting: allow hiding entries from manifest, 2019-06-09)
326 $oldrev = xqx([qw(git rev-parse), $oldrev], undef, { 2 => \$err });
328 skip("no detected public-inbox GIT_DIR ($err)", 1) if $?;
329 require_mods('Email::MIME', 1); # for legacy revision
330 # using plackup to test old PublicInbox::WWW since -httpd from
331 # back then relied on some packages we no longer depend on
332 my $plackup = require_cmd('plackup', 1) or skip('no plackup in path', 1);
333 require PublicInbox::Lock;
335 my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!);
336 my $wt = "t/data-gen/$base.pre-manifest-$oldrev";
337 my $lk = PublicInbox::Lock->new(__FILE__);
339 my $psgi = "$wt/app.psgi";
340 if (!-f $psgi) { # checkout a pre-manifest.js.gz version
341 my $t = File::Temp->new(TEMPLATE => 'g-XXXX', TMPDIR => 1);
342 my $env = { GIT_INDEX_FILE => $t->filename };
343 xsys([qw(git read-tree), $oldrev], $env) and xbail 'read-tree';
344 xsys([qw(git checkout-index -a), "--prefix=$wt/"], $env)
345 and xbail 'checkout-index';
346 my $f = "$wt/app.psgi.tmp.$$";
347 open my $fh, '>', $f or xbail $!;
348 print $fh <<'EOM' or xbail $!;
350 use PublicInbox::WWW;
351 my $www = PublicInbox::WWW->new;
352 builder { enable 'Head'; sub { $www->call(@_) } }
354 close $fh or xbail $!;
355 rename($f, $psgi) or xbail $!;
359 $rdr->{run_mode} = 0;
361 my $cmd = [$plackup, qw(-Enone -Ilib), "--host=$host", "--port=$port"];
363 open $rdr->{2}, '>>', "$tmpdir/plackup.err.log" or xbail "open: $!";
364 open $rdr->{1}, '>>&', $rdr->{2} or xbail "open: $!";
365 my $env = { PERL5LIB => 'lib', PERL_INLINE_DIRECTORY => undef };
366 $td = start_script($cmd, $env, $rdr);
367 # wait for plackup socket()+bind()+listen()
368 my %opt = ( Proto => 'tcp', Type => Socket::SOCK_STREAM(),
369 PeerAddr => "$host:$port" );
372 last if IO::Socket::INET->new(%opt);
374 IO::Socket::INET->new(%opt) or xbail "connect $host:$port: $!";
375 my $dst = "$tmpdir/scrape";
376 @cmd = (qw(-clone -q), "http://$host:$port/v2", $dst);
377 run_script(\@cmd, undef, { 2 => \($err = '') });
378 is($?, 0, 'scraping clone on old PublicInbox::WWW')
380 my @g_all = glob("$dst/git/*.git");
381 ok(scalar(@g_all) > 1, 'cloned multiple epochs');
384 @cmd = (qw(-clone -q --epoch=~0), "http://$host:$port/v2", $dst);
385 run_script(\@cmd, undef, { 2 => \($err = '') });
386 is($?, 0, 'partial scraping clone on old PublicInbox::WWW');
387 my @g_last = grep { (stat($_))[2] & 0200 } glob("$dst/git/*.git");
388 is_deeply(\@g_last, [ $g_all[-1] ], 'partial clone of ~0 worked');
390 chmod(0755, $g_all[0]) or xbail "chmod $!";
391 my @before = glob("$g_all[0]/objects/*/*");
392 run_script([qw(-fetch -v)], undef, { -C => $dst, 2 => \($err = '') });
393 is($?, 0, 'scraping fetch on old PublicInbox::WWW') or diag $err;
394 my @after = glob("$g_all[0]/objects/*/*");
395 ok(scalar(@before) < scalar(@after),
396 'fetched 0.git after enabling write-bit');