]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
ef9a5405a0dff95cb631731a26280e9addc73f5b
[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 Danga::Socket
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::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         mainrepo = $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
62 my %opts = (
63         LocalAddr => '127.0.0.1',
64         ReuseAddr => 1,
65         Proto => 'tcp',
66         Listen => 1024,
67 );
68 my ($sock, $pid);
69 END { kill 'TERM', $pid if defined $pid };
70
71 $! = 0;
72 $sock = IO::Socket::INET->new(%opts);
73 ok($sock, 'sock created');
74 my $cmd = [ "$script-httpd", "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
75 ok(defined($pid = spawn_listener(undef, $cmd, [ $sock ])),
76         'spawned httpd process successfully');
77 my ($host, $port) = ($sock->sockhost, $sock->sockport);
78 $sock = undef;
79
80 my @cmd;
81 foreach my $i (0..$epoch_max) {
82         @cmd = (qw(git clone --mirror -q), "http://$host:$port/v2/$i",
83                 "$tmpdir/m/git/$i.git");
84
85         is(system(@cmd), 0, 'cloned OK');
86         ok(-d "$tmpdir/m/git/$i.git", 'mirror OK');
87 }
88
89 @cmd = ("$script-init", '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
90         'alt@example.com');
91 is(system(@cmd), 0, 'initialized public-inbox -V2');
92 is(system("$script-index", "$tmpdir/m"), 0, 'indexed');
93
94 my $mibx = { mainrepo => "$tmpdir/m", address => 'alt@example.com' };
95 $mibx = PublicInbox::Inbox->new($mibx);
96 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
97
98 $v2w->{rotate_bytes} = $old_rotate_bytes;
99 for my $i (10..15) {
100         $mime->header_set('Message-ID', "<$i\@example.com>");
101         $mime->header_set('Subject', "subject = $i");
102         ok($v2w->add($mime), "add msg $i OK");
103 }
104 $v2w->barrier;
105
106 sub fetch_each_epoch {
107         foreach my $i (0..$epoch_max) {
108                 my $dir = "$tmpdir/m/git/$i.git";
109                 is(system('git', "--git-dir=$dir", 'fetch', '-q'), 0,
110                         'fetch successful');
111         }
112 }
113
114 fetch_each_epoch();
115
116 my $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
117 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
118 is(system("$script-index", "$tmpdir/m"), 0, 'index updated');
119 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
120 $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
121 is(scalar($mset->items), 1, 'found message in mirror');
122
123 # purge:
124 $mime->header_set('Message-ID', '<10@example.com>');
125 $mime->header_set('Subject', 'subject = 10');
126 {
127         my @warn;
128         local $SIG{__WARN__} = sub { push @warn, @_ };
129         ok($v2w->purge($mime), 'purge a message');
130         my $warn = join('', @warn);
131         like($warn, qr/purge rewriting/);
132         my @subj = ($warn =~ m/^# subject .*$/mg);
133         is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
134 }
135
136 $v2w->barrier;
137
138 my $msgs = $mibx->search->{over_ro}->get_thread('10@example.com');
139 my $to_purge = $msgs->[0]->{blob};
140 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
141 $mset = $ibx->search->reopen->query('m:10@example.com', {mset => 1});
142 is(scalar($mset->items), 0, 'purged message gone from origin');
143
144 fetch_each_epoch();
145 {
146         my $cmd = [ "$script-index", '--prune', "$tmpdir/m" ];
147         my ($in, $out, $err) = ('', '', '');
148         ok(IPC::Run::run($cmd, \$in, \$out, \$err), '-index --prune');
149         like($err, qr/discontiguous range/, 'warned about discontiguous range');
150         unlike($err, qr/fatal/, 'no scary fatal error shown');
151 }
152
153 $mset = $mibx->search->reopen->query('m:10@example.com', {mset => 1});
154 is(scalar($mset->items), 0, 'purged message not found in mirror');
155 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'minmax still synced');
156 for my $i ((1..9),(11..15)) {
157         $mset = $mibx->search->query("m:$i\@example.com", {mset => 1});
158         is(scalar($mset->items), 1, "$i\@example.com remains visible");
159 }
160 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
161
162 {
163         my @warn;
164         local $SIG{__WARN__} = sub { push @warn, @_ };
165         $v2w->index_sync;
166         is_deeply(\@warn, [], 'no warnings from index_sync after purge');
167 }
168
169 # deletes happen in a different fetch window
170 {
171         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
172         is(scalar($mset->items), 1, '1@example.com visible in mirror');
173         $mime->header_set('Message-ID', '<1@example.com>');
174         $mime->header_set('Subject', 'subject = 1');
175         ok($v2w->remove($mime), 'removed <1@example.com> from source');
176         $v2w->done;
177         fetch_each_epoch();
178
179         my ($in, $out, $err) = ('', '', '');
180         my $cmd = [ "$script-index", "$tmpdir/m" ];
181         ok(IPC::Run::run($cmd, \$in, \$out, \$err), 'index ran');
182         is($err, '', 'no errors reported by index');
183         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
184         is(scalar($mset->items), 0, '1@example.com no longer visible in mirror');
185 }
186
187 ok(kill('TERM', $pid), 'killed httpd');
188 $pid = undef;
189 waitpid(-1, 0);
190
191 done_testing();
192
193 1;