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