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');
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;
19 use PublicInbox::Config;
20 # FIXME: too much setup
21 my ($tmpdir, $for_destroy) = tmpdir();
22 my $pi_config = "$tmpdir/config";
24 open my $fh, '>', $pi_config or die "open($pi_config): $!";
25 print $fh <<"" or die "print $pi_config: $!";
27 ; using "mainrepo" rather than "inboxdir" for v1.1.0-pre1 WWW compat below
29 address = test\@example.com
31 close $fh or die "close($pi_config): $!";
33 local $ENV{PI_CONFIG} = $pi_config;
35 my $cfg = PublicInbox::Config->new($pi_config);
36 my $ibx = $cfg->lookup('test@example.com');
37 ok($ibx, 'inbox found');
39 $ibx->{-no_fsync} = 1;
40 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
41 ok $v2w, 'v2w loaded';
43 my $mime = PublicInbox::Eml->new(<<'');
44 From: Me <me@example.com>
45 To: You <you@example.com>
47 Date: Thu, 01 Jan 1970 00:00:00 +0000
49 my $old_rotate_bytes = $v2w->{rotate_bytes};
50 $v2w->{rotate_bytes} = 500; # force rotating
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");
57 my $epoch_max = $v2w->{epoch_max};
58 ok($epoch_max > 0, "multiple epochs");
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');
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});
73 @cmd = (qw(-clone -q), "http://$host:$port/v2/", "$tmpdir/m");
74 run_script(\@cmd) or xbail '-clone';
76 for my $i (0..$epoch_max) {
77 ok(-d "$tmpdir/m/git/$i.git", "epoch $i cloned");
80 @cmd = ("-init", '-j1', '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
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');
86 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'indexed');
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');
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");
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
105 is($?, 0, '--exit-code 0 after fetch updated');
106 my @after = grep { !$before{$_} } glob("$tmpdir/m/git/*");
107 push @new_epochs, @after;
110 $fetch_each_epoch->();
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');
120 $mime->header_set('Message-ID', '<10@example.com>');
121 $mime->header_set('Subject', 'subject = 10');
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");
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');
140 $fetch_each_epoch->();
143 PublicInbox::InboxWritable::cleanup($mibx);
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');
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");
160 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
164 local $SIG{__WARN__} = sub { push @warn, @_ };
166 is_deeply(\@warn, [], 'no warnings from index_sync after purge');
169 # deletes happen in a different fetch window
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');
178 $fetch_each_epoch->();
179 PublicInbox::InboxWritable::cleanup($mibx);
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');
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');
204 $mime->header_set('Message-ID', '<2big@a>');
206 $mime->body_str_set("z\n" x 1024);
207 ok($v2w->add($mime), "add big message");
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');
220 open my $fh, '>>', $pi_config or die;
221 print $fh <<EOF or die;
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');
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",
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)];
246 remove_tree(map { "$new_epochs[0]/$_" } qw(objects refs/heads));
247 chmod(0555, $new_epochs[0]) or xbail "chmod: $!";
250 unlink("$tmpdir/m/manifest.js.gz") or xbail "unlink: $!";
252 run_script([qw(-fetch -q)], undef, {-C => "$tmpdir/m"}) or
253 xbail '-fetch failed';
255 ok(!-d "$new_epochs[0]/objects", 'no objects after fetch to R/O dir');
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: $!";
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');
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!);
278 m!/([0-9]+)\.git\z! or xbail "no digit in $_";
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');
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');
294 my $v110 = xqx([qw(git rev-parse v1.1.0-pre1)], undef, { 2 => \$err });
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;
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';
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 $!;
317 use PublicInbox::WWW;
318 my $www = PublicInbox::WWW->new;
319 builder { enable 'Head'; sub { $www->call(@_) } }
321 close $fh or xbail $!;
322 rename($f, $psgi) or xbail $!;
326 $rdr->{run_mode} = 0;
328 my $cmd = [$plackup, qw(-Enone -Ilib), "--host=$host", "--port=$port"];
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" );
338 last if IO::Socket::INET->new(%opt);
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')
345 my @g_all = glob("$dst/git/*.git");
346 ok(scalar(@g_all) > 1, 'cloned multiple epochs');
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');