]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
init: add -j / --jobs parameter
[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 Cwd qw(abs_path);
8 require_git(2.6);
9 local $ENV{HOME} = abs_path('t');
10
11 # Integration tests for HTTP cloning + mirroring
12 require_mods(qw(Plack::Util Plack::Builder
13                 HTTP::Date HTTP::Status Search::Xapian DBD::SQLite));
14 use IO::Socket;
15 use POSIX qw(dup2);
16 use_ok 'PublicInbox::V2Writable';
17 use PublicInbox::InboxWritable;
18 use PublicInbox::Eml;
19 use PublicInbox::Config;
20 # FIXME: too much setup
21 my ($tmpdir, $for_destroy) = tmpdir();
22 my $pi_config = "$tmpdir/config";
23 {
24         open my $fh, '>', $pi_config or die "open($pi_config): $!";
25         print $fh <<"" or die "print $pi_config: $!";
26 [publicinbox "v2"]
27         inboxdir = $tmpdir/in
28         address = test\@example.com
29
30         close $fh or die "close($pi_config): $!";
31 }
32 local $ENV{PI_CONFIG} = $pi_config;
33
34 my $cfg = PublicInbox::Config->new($pi_config);
35 my $ibx = $cfg->lookup('test@example.com');
36 ok($ibx, 'inbox found');
37 $ibx->{version} = 2;
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 ok($sock, 'sock created');
67 my $cmd = [ '-httpd', '-W0', "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
68 my $td = start_script($cmd, undef, { 3 => $sock });
69 my ($host, $port) = ($sock->sockhost, $sock->sockport);
70 $sock = undef;
71
72 my @cmd;
73 foreach my $i (0..$epoch_max) {
74         my $sfx = $i == 0 ? '.git' : '';
75         @cmd = (qw(git clone --mirror -q),
76                 "http://$host:$port/v2/$i$sfx",
77                 "$tmpdir/m/git/$i.git");
78
79         is(xsys(@cmd), 0, "cloned $i.git");
80         ok(-d "$tmpdir/m/git/$i.git", "mirror $i OK");
81 }
82
83 @cmd = ("-init", '-j1', '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
84         'alt@example.com');
85 ok(run_script(\@cmd), 'initialized public-inbox -V2');
86 my @shards = glob("$tmpdir/m/xap*/?");
87 is(scalar(@shards), 1, 'got a single shard on init');
88
89 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'indexed');
90
91 my $mibx = { inboxdir => "$tmpdir/m", address => 'alt@example.com' };
92 $mibx = PublicInbox::Inbox->new($mibx);
93 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
94
95 $v2w->{rotate_bytes} = $old_rotate_bytes;
96 for my $i (10..15) {
97         $mime->header_set('Message-ID', "<$i\@example.com>");
98         $mime->header_set('Subject', "subject = $i");
99         ok($v2w->add($mime), "add msg $i OK");
100 }
101 $v2w->done;
102 $ibx->cleanup;
103
104 my $fetch_each_epoch = sub {
105         foreach my $i (0..$epoch_max) {
106                 my $dir = "$tmpdir/m/git/$i.git";
107                 is(xsys('git', "--git-dir=$dir", 'fetch', '-q'), 0,
108                         'fetch successful');
109         }
110 };
111
112 $fetch_each_epoch->();
113
114 my $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
115 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
116 ok(run_script([qw(-index -j0), "$tmpdir/m"]), 'index updated');
117 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
118 $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
119 is(scalar($mset->items), 1, 'found message in mirror');
120
121 # purge:
122 $mime->header_set('Message-ID', '<10@example.com>');
123 $mime->header_set('Subject', 'subject = 10');
124 {
125         my @warn;
126         local $SIG{__WARN__} = sub { push @warn, @_ };
127         ok($v2w->purge($mime), 'purge a message');
128         my $warn = join('', @warn);
129         like($warn, qr/purge rewriting/);
130         my @subj = ($warn =~ m/^# subject .*$/mg);
131         is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
132 }
133
134 $v2w->done;
135
136 my $msgs = $mibx->search->{over_ro}->get_thread('10@example.com');
137 my $to_purge = $msgs->[0]->{blob};
138 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
139 $mset = $ibx->search->reopen->query('m:10@example.com', {mset => 1});
140 is(scalar($mset->items), 0, 'purged message gone from origin');
141
142 $fetch_each_epoch->();
143 {
144         $ibx->cleanup;
145         PublicInbox::InboxWritable::cleanup($mibx);
146         $v2w->done;
147         my $cmd = [ qw(-index --prune -j0), "$tmpdir/m" ];
148         my ($out, $err) = ('', '');
149         my $opt = { 1 => \$out, 2 => \$err };
150         ok(run_script($cmd, undef, $opt), '-index --prune');
151         like($err, qr/discontiguous range/, 'warned about discontiguous range');
152         unlike($err, qr/fatal/, 'no scary fatal error shown');
153 }
154
155 $mset = $mibx->search->reopen->query('m:10@example.com', {mset => 1});
156 is(scalar($mset->items), 0, 'purged message not found in mirror');
157 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'minmax still synced');
158 for my $i ((1..9),(11..15)) {
159         $mset = $mibx->search->query("m:$i\@example.com", {mset => 1});
160         is(scalar($mset->items), 1, "$i\@example.com remains visible");
161 }
162 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
163
164 {
165         my @warn;
166         local $SIG{__WARN__} = sub { push @warn, @_ };
167         $v2w->index_sync;
168         is_deeply(\@warn, [], 'no warnings from index_sync after purge');
169 }
170
171 # deletes happen in a different fetch window
172 {
173         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
174         is(scalar($mset->items), 1, '1@example.com visible in mirror');
175         $mime->header_set('Message-ID', '<1@example.com>');
176         $mime->header_set('Subject', 'subject = 1');
177         ok($v2w->remove($mime), 'removed <1@example.com> from source');
178         $v2w->done;
179         $ibx->cleanup;
180         $fetch_each_epoch->();
181         PublicInbox::InboxWritable::cleanup($mibx);
182
183         my $cmd = [ qw(-index -j0), "$tmpdir/m" ];
184         my ($out, $err) = ('', '');
185         my $opt = { 1 => \$out, 2 => \$err };
186         ok(run_script($cmd, undef, $opt), 'index ran');
187         is($err, '', 'no errors reported by index');
188         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
189         is(scalar($mset->items), 0, '1@example.com no longer visible in mirror');
190 }
191
192 if ('max size') {
193         $mime->header_set('Message-ID', '<2big@a>');
194         my $max = '2k';
195         $mime->body_str_set("z\n" x 1024);
196         ok($v2w->add($mime), "add big message");
197         $v2w->done;
198         $ibx->cleanup;
199         $fetch_each_epoch->();
200         PublicInbox::InboxWritable::cleanup($mibx);
201         my $cmd = [qw(-index -j0), "$tmpdir/m", "--max-size=$max" ];
202         my $opt = { 2 => \(my $err) };
203         ok(run_script($cmd, undef, $opt), 'indexed with --max-size');
204         like($err, qr/skipping [a-f0-9]{40,}/, 'warned about skipping message');
205         $mset = $mibx->search->reopen->query('m:2big@a', {mset =>1});
206         is(scalar($mset->items), 0, 'large message not indexed');
207
208         {
209                 open my $fh, '>>', $pi_config or die;
210                 print $fh <<EOF or die;
211 [publicinbox]
212         indexMaxSize = 2k
213 EOF
214                 close $fh or die;
215         }
216         $cmd = [ qw(-index -j0 --reindex), "$tmpdir/m" ];
217         ok(run_script($cmd, undef, $opt), 'reindexed w/ indexMaxSize in file');
218         like($err, qr/skipping [a-f0-9]{40,}/, 'warned about skipping message');
219         $mset = $mibx->search->reopen->query('m:2big@a', {mset =>1});
220         is(scalar($mset->items), 0, 'large message not re-indexed');
221 }
222
223 ok($td->kill, 'killed httpd');
224 $td->join;
225
226 done_testing();
227
228 1;