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