]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
t/common: introduce run_script wrapper for t/cgi.t
[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                         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         inboxdir = $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 ($sock, $pid);
64 END { kill 'TERM', $pid if defined $pid };
65
66 $! = 0;
67 $sock = tcp_server();
68 ok($sock, 'sock created');
69 my $httpd = "$script-httpd";
70 my $cmd = [ $httpd, '-W0', "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
71 ok(defined($pid = spawn_listener(undef, $cmd, [ $sock ])),
72         'spawned httpd process successfully');
73 my ($host, $port) = ($sock->sockhost, $sock->sockport);
74 $sock = undef;
75
76 my @cmd;
77 foreach my $i (0..$epoch_max) {
78         my $sfx = $i == 0 ? '.git' : '';
79         @cmd = (qw(git clone --mirror -q),
80                 "http://$host:$port/v2/$i$sfx",
81                 "$tmpdir/m/git/$i.git");
82
83         is(system(@cmd), 0, "cloned $i.git");
84         ok(-d "$tmpdir/m/git/$i.git", "mirror $i OK");
85 }
86
87 @cmd = ("$script-init", '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
88         'alt@example.com');
89 is(system(@cmd), 0, 'initialized public-inbox -V2');
90 is(system("$script-index", "$tmpdir/m"), 0, '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->barrier;
103
104 sub fetch_each_epoch {
105         foreach my $i (0..$epoch_max) {
106                 my $dir = "$tmpdir/m/git/$i.git";
107                 is(system('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 is(system("$script-index", "$tmpdir/m"), 0, '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->barrier;
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         my $cmd = [ "$script-index", '--prune', "$tmpdir/m" ];
145         my ($in, $out, $err) = ('', '', '');
146         ok(IPC::Run::run($cmd, \$in, \$out, \$err), '-index --prune');
147         like($err, qr/discontiguous range/, 'warned about discontiguous range');
148         unlike($err, qr/fatal/, 'no scary fatal error shown');
149 }
150
151 $mset = $mibx->search->reopen->query('m:10@example.com', {mset => 1});
152 is(scalar($mset->items), 0, 'purged message not found in mirror');
153 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'minmax still synced');
154 for my $i ((1..9),(11..15)) {
155         $mset = $mibx->search->query("m:$i\@example.com", {mset => 1});
156         is(scalar($mset->items), 1, "$i\@example.com remains visible");
157 }
158 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
159
160 {
161         my @warn;
162         local $SIG{__WARN__} = sub { push @warn, @_ };
163         $v2w->index_sync;
164         is_deeply(\@warn, [], 'no warnings from index_sync after purge');
165 }
166
167 # deletes happen in a different fetch window
168 {
169         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
170         is(scalar($mset->items), 1, '1@example.com visible in mirror');
171         $mime->header_set('Message-ID', '<1@example.com>');
172         $mime->header_set('Subject', 'subject = 1');
173         ok($v2w->remove($mime), 'removed <1@example.com> from source');
174         $v2w->done;
175         fetch_each_epoch();
176
177         my ($in, $out, $err) = ('', '', '');
178         my $cmd = [ "$script-index", "$tmpdir/m" ];
179         ok(IPC::Run::run($cmd, \$in, \$out, \$err), 'index ran');
180         is($err, '', 'no errors reported by index');
181         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
182         is(scalar($mset->items), 0, '1@example.com no longer visible in mirror');
183 }
184
185 ok(kill('TERM', $pid), 'killed httpd');
186 $pid = undef;
187 waitpid(-1, 0);
188
189 done_testing();
190
191 1;