]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
t/v2mirror: switch to default run_mode for speedup
[public-inbox.git] / t / v2mirror.t
1 # Copyright (C) 2018-2019 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 require './t/common.perl';
7 require_git(2.6);
8
9 # Integration tests for HTTP cloning + mirroring
10 foreach my $mod (qw(Plack::Util Plack::Builder
11                         HTTP::Date HTTP::Status Search::Xapian DBD::SQLite)) {
12         eval "require $mod";
13         plan skip_all => "$mod missing for v2mirror.t" if $@;
14 }
15 use File::Temp qw/tempdir/;
16 use IO::Socket;
17 use POSIX qw(dup2);
18 use_ok 'PublicInbox::V2Writable';
19 use PublicInbox::InboxWritable;
20 use PublicInbox::MIME;
21 use PublicInbox::Config;
22 # FIXME: too much setup
23 my $tmpdir = tempdir('pi-v2mirror-XXXXXX', TMPDIR => 1, CLEANUP => 1);
24 my $script = 'blib/script/public-inbox';
25 my $pi_config = "$tmpdir/config";
26 {
27         open my $fh, '>', $pi_config or die "open($pi_config): $!";
28         print $fh <<"" or die "print $pi_config: $!";
29 [publicinbox "v2"]
30         inboxdir = $tmpdir/in
31         address = test\@example.com
32
33         close $fh or die "close($pi_config): $!";
34 }
35 local $ENV{PI_CONFIG} = $pi_config;
36
37 my $cfg = PublicInbox::Config->new($pi_config);
38 my $ibx = $cfg->lookup('test@example.com');
39 ok($ibx, 'inbox found');
40 $ibx->{version} = 2;
41 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
42 ok $v2w, 'v2w loaded';
43 $v2w->{parallel} = 0;
44 my $mime = PublicInbox::MIME->new(<<'');
45 From: Me <me@example.com>
46 To: You <you@example.com>
47 Subject: a
48 Date: Thu, 01 Jan 1970 00:00:00 +0000
49
50 my $old_rotate_bytes = $v2w->{rotate_bytes};
51 $v2w->{rotate_bytes} = 500; # force rotating
52 for my $i (1..9) {
53         $mime->header_set('Message-ID', "<$i\@example.com>");
54         $mime->header_set('Subject', "subject = $i");
55         ok($v2w->add($mime), "add msg $i OK");
56 }
57
58 my $epoch_max = $v2w->{epoch_max};
59 ok($epoch_max > 0, "multiple epochs");
60 $v2w->done;
61 $ibx->cleanup;
62
63 my ($sock, $pid);
64
65 # TODO: replace this with ->DESTROY:
66 my $owner_pid = $$;
67 END { kill('TERM', $pid) if defined($pid) && $owner_pid == $$ };
68
69 $! = 0;
70 $sock = tcp_server();
71 ok($sock, 'sock created');
72 my $httpd = "$script-httpd";
73 my $cmd = [ $httpd, '-W0', "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
74 ok(defined($pid = spawn_listener(undef, $cmd, [ $sock ])),
75         'spawned httpd process successfully');
76 my ($host, $port) = ($sock->sockhost, $sock->sockport);
77 $sock = undef;
78
79 my @cmd;
80 foreach my $i (0..$epoch_max) {
81         my $sfx = $i == 0 ? '.git' : '';
82         @cmd = (qw(git clone --mirror -q),
83                 "http://$host:$port/v2/$i$sfx",
84                 "$tmpdir/m/git/$i.git");
85
86         is(system(@cmd), 0, "cloned $i.git");
87         ok(-d "$tmpdir/m/git/$i.git", "mirror $i OK");
88 }
89
90 @cmd = ("-init", '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
91         'alt@example.com');
92 ok(run_script(\@cmd), 'initialized public-inbox -V2');
93
94 ok(run_script(['-index', "$tmpdir/m"]), 'indexed');
95
96 my $mibx = { inboxdir => "$tmpdir/m", address => 'alt@example.com' };
97 $mibx = PublicInbox::Inbox->new($mibx);
98 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
99
100 $v2w->{rotate_bytes} = $old_rotate_bytes;
101 for my $i (10..15) {
102         $mime->header_set('Message-ID', "<$i\@example.com>");
103         $mime->header_set('Subject', "subject = $i");
104         ok($v2w->add($mime), "add msg $i OK");
105 }
106 $v2w->done;
107 $ibx->cleanup;
108
109 sub fetch_each_epoch {
110         foreach my $i (0..$epoch_max) {
111                 my $dir = "$tmpdir/m/git/$i.git";
112                 is(system('git', "--git-dir=$dir", 'fetch', '-q'), 0,
113                         'fetch successful');
114         }
115 }
116
117 fetch_each_epoch();
118
119 my $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
120 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
121 ok(run_script(["-index", "$tmpdir/m"]), 'index updated');
122 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
123 $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
124 is(scalar($mset->items), 1, 'found message in mirror');
125
126 # purge:
127 $mime->header_set('Message-ID', '<10@example.com>');
128 $mime->header_set('Subject', 'subject = 10');
129 {
130         my @warn;
131         local $SIG{__WARN__} = sub { push @warn, @_ };
132         ok($v2w->purge($mime), 'purge a message');
133         my $warn = join('', @warn);
134         like($warn, qr/purge rewriting/);
135         my @subj = ($warn =~ m/^# subject .*$/mg);
136         is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
137 }
138
139 $v2w->done;
140
141 my $msgs = $mibx->search->{over_ro}->get_thread('10@example.com');
142 my $to_purge = $msgs->[0]->{blob};
143 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
144 $mset = $ibx->search->reopen->query('m:10@example.com', {mset => 1});
145 is(scalar($mset->items), 0, 'purged message gone from origin');
146
147 fetch_each_epoch();
148 {
149         $ibx->cleanup;
150         PublicInbox::InboxWritable::cleanup($mibx);
151         $v2w->done;
152         my $cmd = [ '-index', '--prune', "$tmpdir/m" ];
153         my ($out, $err) = ('', '');
154         my $opt = { 1 => \$out, 2 => \$err };
155         ok(run_script($cmd, undef, $opt), '-index --prune');
156         like($err, qr/discontiguous range/, 'warned about discontiguous range');
157         unlike($err, qr/fatal/, 'no scary fatal error shown');
158 }
159
160 $mset = $mibx->search->reopen->query('m:10@example.com', {mset => 1});
161 is(scalar($mset->items), 0, 'purged message not found in mirror');
162 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'minmax still synced');
163 for my $i ((1..9),(11..15)) {
164         $mset = $mibx->search->query("m:$i\@example.com", {mset => 1});
165         is(scalar($mset->items), 1, "$i\@example.com remains visible");
166 }
167 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
168
169 {
170         my @warn;
171         local $SIG{__WARN__} = sub { push @warn, @_ };
172         $v2w->index_sync;
173         is_deeply(\@warn, [], 'no warnings from index_sync after purge');
174 }
175
176 # deletes happen in a different fetch window
177 {
178         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
179         is(scalar($mset->items), 1, '1@example.com visible in mirror');
180         $mime->header_set('Message-ID', '<1@example.com>');
181         $mime->header_set('Subject', 'subject = 1');
182         ok($v2w->remove($mime), 'removed <1@example.com> from source');
183         $v2w->done;
184         $ibx->cleanup;
185         fetch_each_epoch();
186         PublicInbox::InboxWritable::cleanup($mibx);
187
188         my $cmd = [ "-index", "$tmpdir/m" ];
189         my ($out, $err) = ('', '');
190         my $opt = { 1 => \$out, 2 => \$err };
191         ok(run_script($cmd, undef, $opt), 'index ran');
192         is($err, '', 'no errors reported by index');
193         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
194         is(scalar($mset->items), 0, '1@example.com no longer visible in mirror');
195 }
196
197 ok(kill('TERM', $pid), 'killed httpd');
198 $pid = undef;
199 waitpid(-1, 0);
200
201 done_testing();
202
203 1;