]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
search: replace ->query with ->mset
[public-inbox.git] / t / v2mirror.t
1 # Copyright (C) 2018-2020 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 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
40 ok $v2w, 'v2w loaded';
41 $v2w->{parallel} = 0;
42 my $mime = PublicInbox::Eml->new(<<'');
43 From: Me <me@example.com>
44 To: You <you@example.com>
45 Subject: a
46 Date: Thu, 01 Jan 1970 00:00:00 +0000
47
48 my $old_rotate_bytes = $v2w->{rotate_bytes};
49 $v2w->{rotate_bytes} = 500; # force rotating
50 for my $i (1..9) {
51         $mime->header_set('Message-ID', "<$i\@example.com>");
52         $mime->header_set('Subject', "subject = $i");
53         ok($v2w->add($mime), "add msg $i OK");
54 }
55
56 my $epoch_max = $v2w->{epoch_max};
57 ok($epoch_max > 0, "multiple epochs");
58 $v2w->done;
59 {
60         my $smsg = $ibx->over->get_art(1);
61         like($smsg->{lines}, qr/\A[0-9]+\z/, 'lines is a digit');
62         like($smsg->{bytes}, qr/\A[0-9]+\z/, 'bytes is a digit');
63 }
64 $ibx->cleanup;
65
66 my $sock = tcp_server();
67 ok($sock, 'sock created');
68 my $cmd = [ '-httpd', '-W0', "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
69 my $td = start_script($cmd, undef, { 3 => $sock });
70 my ($host, $port) = ($sock->sockhost, $sock->sockport);
71 $sock = undef;
72
73 my @cmd;
74 foreach my $i (0..$epoch_max) {
75         my $sfx = $i == 0 ? '.git' : '';
76         @cmd = (qw(git clone --mirror -q),
77                 "http://$host:$port/v2/$i$sfx",
78                 "$tmpdir/m/git/$i.git");
79
80         is(xsys(@cmd), 0, "cloned $i.git");
81         ok(-d "$tmpdir/m/git/$i.git", "mirror $i OK");
82 }
83
84 @cmd = ("-init", '-j1', '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
85         'alt@example.com');
86 ok(run_script(\@cmd), 'initialized public-inbox -V2');
87 my @shards = glob("$tmpdir/m/xap*/?");
88 is(scalar(@shards), 1, 'got a single shard on init');
89
90 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'indexed');
91
92 my $mibx = { inboxdir => "$tmpdir/m", address => 'alt@example.com' };
93 $mibx = PublicInbox::Inbox->new($mibx);
94 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
95
96 $v2w->{rotate_bytes} = $old_rotate_bytes;
97 for my $i (10..15) {
98         $mime->header_set('Message-ID', "<$i\@example.com>");
99         $mime->header_set('Subject', "subject = $i");
100         ok($v2w->add($mime), "add msg $i OK");
101 }
102 $v2w->done;
103 $ibx->cleanup;
104
105 my $fetch_each_epoch = sub {
106         foreach my $i (0..$epoch_max) {
107                 my $dir = "$tmpdir/m/git/$i.git";
108                 is(xsys('git', "--git-dir=$dir", 'fetch', '-q'), 0,
109                         'fetch successful');
110         }
111 };
112
113 $fetch_each_epoch->();
114
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');
121
122 # purge:
123 $mime->header_set('Message-ID', '<10@example.com>');
124 $mime->header_set('Subject', 'subject = 10');
125 {
126         my @warn;
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");
133 }
134
135 $v2w->done;
136
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');
142
143 $fetch_each_epoch->();
144 {
145         $ibx->cleanup;
146         PublicInbox::InboxWritable::cleanup($mibx);
147         $v2w->done;
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');
154 }
155
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");
162 }
163 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
164
165 {
166         my @warn;
167         local $SIG{__WARN__} = sub { push @warn, @_ };
168         $v2w->index_sync;
169         is_deeply(\@warn, [], 'no warnings from index_sync after purge');
170 }
171
172 # deletes happen in a different fetch window
173 {
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');
179         $v2w->done;
180         $ibx->cleanup;
181         $fetch_each_epoch->();
182         PublicInbox::InboxWritable::cleanup($mibx);
183
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');
191 }
192
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');
204 }
205
206 if ('max size') {
207         $mime->header_set('Message-ID', '<2big@a>');
208         my $max = '2k';
209         $mime->body_str_set("z\n" x 1024);
210         ok($v2w->add($mime), "add big message");
211         $v2w->done;
212         $ibx->cleanup;
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');
221
222         {
223                 open my $fh, '>>', $pi_config or die;
224                 print $fh <<EOF or die;
225 [publicinbox]
226         indexMaxSize = 2k
227 EOF
228                 close $fh or die;
229         }
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');
235 }
236
237 ok($td->kill, 'killed httpd');
238 $td->join;
239
240 done_testing();
241
242 1;