]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
treewide: fix %SIG localization, harder
[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 require_git(2.6);
9 require_cmd('curl');
10 local $ENV{HOME} = abs_path('t');
11
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;
17 use PublicInbox::Eml;
18 use PublicInbox::Config;
19 # FIXME: too much setup
20 my ($tmpdir, $for_destroy) = tmpdir();
21 my $pi_config = "$tmpdir/config";
22 {
23         open my $fh, '>', $pi_config or die "open($pi_config): $!";
24         print $fh <<"" or die "print $pi_config: $!";
25 [publicinbox "v2"]
26         inboxdir = $tmpdir/in
27         address = test\@example.com
28
29         close $fh or die "close($pi_config): $!";
30 }
31 local $ENV{PI_CONFIG} = $pi_config;
32
33 my $cfg = PublicInbox::Config->new($pi_config);
34 my $ibx = $cfg->lookup('test@example.com');
35 ok($ibx, 'inbox found');
36 $ibx->{version} = 2;
37 $ibx->{-no_fsync} = 1;
38 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
39 ok $v2w, 'v2w loaded';
40 $v2w->{parallel} = 0;
41 my $mime = PublicInbox::Eml->new(<<'');
42 From: Me <me@example.com>
43 To: You <you@example.com>
44 Subject: a
45 Date: Thu, 01 Jan 1970 00:00:00 +0000
46
47 my $old_rotate_bytes = $v2w->{rotate_bytes};
48 $v2w->{rotate_bytes} = 500; # force rotating
49 for my $i (1..9) {
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");
53 }
54
55 my $epoch_max = $v2w->{epoch_max};
56 ok($epoch_max > 0, "multiple epochs");
57 $v2w->done;
58 {
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');
62 }
63 $ibx->cleanup;
64
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);
69 $sock = undef;
70
71 @cmd = (qw(-clone -q), "http://$host:$port/v2/", "$tmpdir/m");
72 run_script(\@cmd) or xbail '-clone';
73
74 for my $i (0..$epoch_max) {
75         ok(-d "$tmpdir/m/git/$i.git", "epoch $i cloned");
76 }
77
78 @cmd = ("-init", '-j1', '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
79         'alt@example.com');
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');
83
84 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'indexed');
85
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');
89
90 for my $i (10..15) {
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");
94 }
95 $v2w->done;
96 $ibx->cleanup;
97
98 my @new_epochs;
99 my $fetch_each_epoch = sub {
100         my %before = map { $_ => 1 } glob("$tmpdir/m/git/*");
101         run_script([qw(-fetch --exit-code -q)], undef, {-C => "$tmpdir/m"}) or
102                 xbail '-fetch fail';
103         is($?, 0, '--exit-code 0 after fetch updated');
104         my @after = grep { !$before{$_} } glob("$tmpdir/m/git/*");
105         push @new_epochs, @after;
106 };
107
108 $fetch_each_epoch->();
109
110 my $mset = $mibx->search->reopen->mset('m:15@example.com');
111 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
112 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'index updated');
113 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
114 $mset = $mibx->search->reopen->mset('m:15@example.com');
115 is(scalar($mset->items), 1, 'found message in mirror');
116
117 # purge:
118 $mime->header_set('Message-ID', '<10@example.com>');
119 $mime->header_set('Subject', 'subject = 10');
120 {
121         my @warn;
122         local $SIG{__WARN__} = sub { push @warn, @_ };
123         ok($v2w->purge($mime), 'purge a message');
124         my $warn = join('', @warn);
125         like($warn, qr/purge rewriting/);
126         my @subj = ($warn =~ m/^# subject .*$/mg);
127         is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
128 }
129
130 $v2w->done;
131
132 my $msgs = $mibx->over->get_thread('10@example.com');
133 my $to_purge = $msgs->[0]->{blob};
134 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
135 $mset = $ibx->search->reopen->mset('m:10@example.com');
136 is(scalar($mset->items), 0, 'purged message gone from origin');
137
138 $fetch_each_epoch->();
139 {
140         $ibx->cleanup;
141         PublicInbox::InboxWritable::cleanup($mibx);
142         $v2w->done;
143         my $cmd = [ qw(-index --prune -j0), "$tmpdir/m" ];
144         my ($out, $err) = ('', '');
145         my $opt = { 1 => \$out, 2 => \$err };
146         ok(run_script($cmd, undef, $opt), '-index --prune');
147         like($err, qr/discontiguous range/, 'warned about discontiguous range');
148         unlike($err, qr/fatal/, 'no scary fatal error shown');
149 }
150
151 $mset = $mibx->search->reopen->mset('m:10@example.com');
152 is(scalar($mset->items), 0, 'purged message not found in mirror');
153 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'minmax still synced');
154 for my $i ((1..9),(11..15)) {
155         $mset = $mibx->search->mset("m:$i\@example.com");
156         is(scalar($mset->items), 1, "$i\@example.com remains visible");
157 }
158 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
159
160 {
161         my @warn;
162         local $SIG{__WARN__} = sub { push @warn, @_ };
163         $v2w->index_sync;
164         is_deeply(\@warn, [], 'no warnings from index_sync after purge');
165 }
166
167 # deletes happen in a different fetch window
168 {
169         $mset = $mibx->search->reopen->mset('m:1@example.com');
170         is(scalar($mset->items), 1, '1@example.com visible in mirror');
171         $mime->header_set('Message-ID', '<1@example.com>');
172         $mime->header_set('Subject', 'subject = 1');
173         ok($v2w->remove($mime), 'removed <1@example.com> from source');
174         $v2w->done;
175         $ibx->cleanup;
176         $fetch_each_epoch->();
177         PublicInbox::InboxWritable::cleanup($mibx);
178
179         my $cmd = [ qw(-index -j0), "$tmpdir/m" ];
180         my ($out, $err) = ('', '');
181         my $opt = { 1 => \$out, 2 => \$err };
182         ok(run_script($cmd, undef, $opt), 'index ran');
183         is($err, '', 'no errors reported by index');
184         $mset = $mibx->search->reopen->mset('m:1@example.com');
185         is(scalar($mset->items), 0, '1@example.com no longer visible in mirror');
186 }
187
188 if ('sequential-shard') {
189         $mset = $mibx->search->mset('m:15@example.com');
190         is(scalar($mset->items), 1, 'large message not indexed');
191         remove_tree(glob("$tmpdir/m/xap*"), glob("$tmpdir/m/msgmap.*"));
192         my $cmd = [ qw(-index -j9 --sequential-shard), "$tmpdir/m" ];
193         ok(run_script($cmd), '--sequential-shard works');
194         my @shards = glob("$tmpdir/m/xap*/?");
195         is(scalar(@shards), 8, 'got expected shard count');
196         PublicInbox::InboxWritable::cleanup($mibx);
197         $mset = $mibx->search->mset('m:15@example.com');
198         is(scalar($mset->items), 1, 'search works after --sequential-shard');
199 }
200
201 if ('max size') {
202         $mime->header_set('Message-ID', '<2big@a>');
203         my $max = '2k';
204         $mime->body_str_set("z\n" x 1024);
205         ok($v2w->add($mime), "add big message");
206         $v2w->done;
207         $ibx->cleanup;
208         $fetch_each_epoch->();
209         PublicInbox::InboxWritable::cleanup($mibx);
210         my $cmd = [qw(-index -j0), "$tmpdir/m", "--max-size=$max" ];
211         my $opt = { 2 => \(my $err) };
212         ok(run_script($cmd, undef, $opt), 'indexed with --max-size');
213         like($err, qr/skipping [a-f0-9]{40,}/, 'warned about skipping message');
214         $mset = $mibx->search->reopen->mset('m:2big@a');
215         is(scalar($mset->items), 0, 'large message not indexed');
216
217         {
218                 open my $fh, '>>', $pi_config or die;
219                 print $fh <<EOF or die;
220 [publicinbox]
221         indexMaxSize = 2k
222 EOF
223                 close $fh or die;
224         }
225         $cmd = [ qw(-index -j0 --reindex), "$tmpdir/m" ];
226         ok(run_script($cmd, undef, $opt), 'reindexed w/ indexMaxSize in file');
227         like($err, qr/skipping [a-f0-9]{40,}/, 'warned about skipping message');
228         $mset = $mibx->search->reopen->mset('m:2big@a');
229         is(scalar($mset->items), 0, 'large message not re-indexed');
230 }
231 ok(scalar(@new_epochs), 'new epochs were created and fetched');
232 for my $d (@new_epochs) {
233         is(xqx(['git', "--git-dir=$d", 'config', qw(include.path)]),
234                 "../../all.git/config\n",
235                 'include.path set');
236 }
237
238 if ('test read-only epoch dirs') {
239         my @git = ('git', "--git-dir=$new_epochs[0]");
240         my $get_objs = [@git,
241                 qw(cat-file --buffer --batch-check --batch-all-objects)];
242         my $before = [sort xqx($get_objs)];
243
244         remove_tree(map { "$new_epochs[0]/$_" } qw(objects refs/heads));
245         chmod(0555, $new_epochs[0]) or xbail "chmod: $!";
246
247         # force a refetch
248         unlink("$tmpdir/m/manifest.js.gz") or xbail "unlink: $!";
249
250         run_script([qw(-fetch -q)], undef, {-C => "$tmpdir/m"}) or
251                 xbail '-fetch failed';
252
253         ok(!-d "$new_epochs[0]/objects", 'no objects after fetch to R/O dir');
254
255         chmod(0755, $new_epochs[0]) or xbail "chmod: $!";
256         mkdir("$new_epochs[0]/objects") or xbail "mkdir: $!";
257         mkdir("$new_epochs[0]/refs/heads") or xbail "mkdir: $!";
258
259         my $err = '';
260         run_script([qw(-fetch -q)], undef, {-C => "$tmpdir/m", 2 => \$err}) or
261                 xbail '-fetch failed '.$err;
262         is_deeply([ sort xqx($get_objs) ], $before,
263                 'fetch restored objects once GIT_DIR became writable');
264 }
265
266 ok($td->kill, 'killed httpd');
267 $td->join;
268
269 done_testing;