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);
10 local $ENV{HOME} = abs_path('t');
12 # Integration tests for HTTP cloning + mirroring
13 require_mods(qw(Plack::Util Plack::Builder
14 HTTP::Date HTTP::Status Search::Xapian DBD::SQLite));
15 use_ok 'PublicInbox::V2Writable';
16 use PublicInbox::InboxWritable;
18 use PublicInbox::Config;
19 # FIXME: too much setup
20 my ($tmpdir, $for_destroy) = tmpdir();
21 my $pi_config = "$tmpdir/config";
23 open my $fh, '>', $pi_config or die "open($pi_config): $!";
24 print $fh <<"" or die "print $pi_config: $!";
27 address = test\@example.com
29 close $fh or die "close($pi_config): $!";
31 local $ENV{PI_CONFIG} = $pi_config;
33 my $cfg = PublicInbox::Config->new($pi_config);
34 my $ibx = $cfg->lookup('test@example.com');
35 ok($ibx, 'inbox found');
37 $ibx->{-no_fsync} = 1;
38 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
39 ok $v2w, 'v2w loaded';
41 my $mime = PublicInbox::Eml->new(<<'');
42 From: Me <me@example.com>
43 To: You <you@example.com>
45 Date: Thu, 01 Jan 1970 00:00:00 +0000
47 my $old_rotate_bytes = $v2w->{rotate_bytes};
48 $v2w->{rotate_bytes} = 500; # force rotating
50 $mime->header_set('Message-ID', "<$i\@example.com>");
51 $mime->header_set('Subject', "subject = $i");
52 ok($v2w->add($mime), "add msg $i OK");
55 my $epoch_max = $v2w->{epoch_max};
56 ok($epoch_max > 0, "multiple epochs");
59 my $smsg = $ibx->over->get_art(1);
60 like($smsg->{lines}, qr/\A[0-9]+\z/, 'lines is a digit');
61 like($smsg->{bytes}, qr/\A[0-9]+\z/, 'bytes is a digit');
65 my $sock = tcp_server();
66 my @cmd = ('-httpd', '-W0', "--stdout=$tmpdir/out", "--stderr=$tmpdir/err");
67 my $td = start_script(\@cmd, undef, { 3 => $sock });
68 my ($host, $port) = tcp_host_port($sock);
71 @cmd = (qw(-clone -q), "http://$host:$port/v2/", "$tmpdir/m");
72 run_script(\@cmd) or xbail '-clone';
74 for my $i (0..$epoch_max) {
75 ok(-d "$tmpdir/m/git/$i.git", "epoch $i cloned");
78 @cmd = ("-init", '-j1', '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
80 ok(run_script(\@cmd), 'initialized public-inbox -V2');
81 my @shards = glob("$tmpdir/m/xap*/?");
82 is(scalar(@shards), 1, 'got a single shard on init');
84 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'indexed');
86 my $mibx = { inboxdir => "$tmpdir/m", address => 'alt@example.com' };
87 $mibx = PublicInbox::Inbox->new($mibx);
88 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
91 $mime->header_set('Message-ID', "<$i\@example.com>");
92 $mime->header_set('Subject', "subject = $i");
93 ok($v2w->add($mime), "add msg $i OK");
99 my $fetch_each_epoch = sub {
100 my %before = map { $_ => 1 } glob("$tmpdir/m/git/*");
101 run_script([qw(-fetch -q)], undef, {-C => "$tmpdir/m"}) or
103 my @after = grep { !$before{$_} } glob("$tmpdir/m/git/*");
104 push @new_epochs, @after;
107 $fetch_each_epoch->();
109 my $mset = $mibx->search->reopen->mset('m:15@example.com');
110 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
111 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'index updated');
112 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
113 $mset = $mibx->search->reopen->mset('m:15@example.com');
114 is(scalar($mset->items), 1, 'found message in mirror');
117 $mime->header_set('Message-ID', '<10@example.com>');
118 $mime->header_set('Subject', 'subject = 10');
121 local $SIG{__WARN__} = sub { push @warn, @_ };
122 ok($v2w->purge($mime), 'purge a message');
123 my $warn = join('', @warn);
124 like($warn, qr/purge rewriting/);
125 my @subj = ($warn =~ m/^# subject .*$/mg);
126 is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
131 my $msgs = $mibx->over->get_thread('10@example.com');
132 my $to_purge = $msgs->[0]->{blob};
133 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
134 $mset = $ibx->search->reopen->mset('m:10@example.com');
135 is(scalar($mset->items), 0, 'purged message gone from origin');
137 $fetch_each_epoch->();
140 PublicInbox::InboxWritable::cleanup($mibx);
142 my $cmd = [ qw(-index --prune -j0), "$tmpdir/m" ];
143 my ($out, $err) = ('', '');
144 my $opt = { 1 => \$out, 2 => \$err };
145 ok(run_script($cmd, undef, $opt), '-index --prune');
146 like($err, qr/discontiguous range/, 'warned about discontiguous range');
147 unlike($err, qr/fatal/, 'no scary fatal error shown');
150 $mset = $mibx->search->reopen->mset('m:10@example.com');
151 is(scalar($mset->items), 0, 'purged message not found in mirror');
152 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'minmax still synced');
153 for my $i ((1..9),(11..15)) {
154 $mset = $mibx->search->mset("m:$i\@example.com");
155 is(scalar($mset->items), 1, "$i\@example.com remains visible");
157 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
161 local $SIG{__WARN__} = sub { push @warn, @_ };
163 is_deeply(\@warn, [], 'no warnings from index_sync after purge');
166 # deletes happen in a different fetch window
168 $mset = $mibx->search->reopen->mset('m:1@example.com');
169 is(scalar($mset->items), 1, '1@example.com visible in mirror');
170 $mime->header_set('Message-ID', '<1@example.com>');
171 $mime->header_set('Subject', 'subject = 1');
172 ok($v2w->remove($mime), 'removed <1@example.com> from source');
175 $fetch_each_epoch->();
176 PublicInbox::InboxWritable::cleanup($mibx);
178 my $cmd = [ qw(-index -j0), "$tmpdir/m" ];
179 my ($out, $err) = ('', '');
180 my $opt = { 1 => \$out, 2 => \$err };
181 ok(run_script($cmd, undef, $opt), 'index ran');
182 is($err, '', 'no errors reported by index');
183 $mset = $mibx->search->reopen->mset('m:1@example.com');
184 is(scalar($mset->items), 0, '1@example.com no longer visible in mirror');
187 if ('sequential-shard') {
188 $mset = $mibx->search->mset('m:15@example.com');
189 is(scalar($mset->items), 1, 'large message not indexed');
190 remove_tree(glob("$tmpdir/m/xap*"), glob("$tmpdir/m/msgmap.*"));
191 my $cmd = [ qw(-index -j9 --sequential-shard), "$tmpdir/m" ];
192 ok(run_script($cmd), '--sequential-shard works');
193 my @shards = glob("$tmpdir/m/xap*/?");
194 is(scalar(@shards), 8, 'got expected shard count');
195 PublicInbox::InboxWritable::cleanup($mibx);
196 $mset = $mibx->search->mset('m:15@example.com');
197 is(scalar($mset->items), 1, 'search works after --sequential-shard');
201 $mime->header_set('Message-ID', '<2big@a>');
203 $mime->body_str_set("z\n" x 1024);
204 ok($v2w->add($mime), "add big message");
207 $fetch_each_epoch->();
208 PublicInbox::InboxWritable::cleanup($mibx);
209 my $cmd = [qw(-index -j0), "$tmpdir/m", "--max-size=$max" ];
210 my $opt = { 2 => \(my $err) };
211 ok(run_script($cmd, undef, $opt), 'indexed with --max-size');
212 like($err, qr/skipping [a-f0-9]{40,}/, 'warned about skipping message');
213 $mset = $mibx->search->reopen->mset('m:2big@a');
214 is(scalar($mset->items), 0, 'large message not indexed');
217 open my $fh, '>>', $pi_config or die;
218 print $fh <<EOF or die;
224 $cmd = [ qw(-index -j0 --reindex), "$tmpdir/m" ];
225 ok(run_script($cmd, undef, $opt), 'reindexed w/ indexMaxSize in file');
226 like($err, qr/skipping [a-f0-9]{40,}/, 'warned about skipping message');
227 $mset = $mibx->search->reopen->mset('m:2big@a');
228 is(scalar($mset->items), 0, 'large message not re-indexed');
230 ok(scalar(@new_epochs), 'new epochs were created and fetched');
231 for my $d (@new_epochs) {
232 is(xqx(['git', "--git-dir=$d", 'config', qw(include.path)]),
233 "../../all.git/config\n",
237 ok($td->kill, 'killed httpd');