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