]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
t/common: start_script replaces spawn_listener
[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 $pi_config = "$tmpdir/config";
25 {
26         open my $fh, '>', $pi_config or die "open($pi_config): $!";
27         print $fh <<"" or die "print $pi_config: $!";
28 [publicinbox "v2"]
29         inboxdir = $tmpdir/in
30         address = test\@example.com
31
32         close $fh or die "close($pi_config): $!";
33 }
34 local $ENV{PI_CONFIG} = $pi_config;
35
36 my $cfg = PublicInbox::Config->new($pi_config);
37 my $ibx = $cfg->lookup('test@example.com');
38 ok($ibx, 'inbox found');
39 $ibx->{version} = 2;
40 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
41 ok $v2w, 'v2w loaded';
42 $v2w->{parallel} = 0;
43 my $mime = PublicInbox::MIME->new(<<'');
44 From: Me <me@example.com>
45 To: You <you@example.com>
46 Subject: a
47 Date: Thu, 01 Jan 1970 00:00:00 +0000
48
49 my $old_rotate_bytes = $v2w->{rotate_bytes};
50 $v2w->{rotate_bytes} = 500; # force rotating
51 for my $i (1..9) {
52         $mime->header_set('Message-ID', "<$i\@example.com>");
53         $mime->header_set('Subject', "subject = $i");
54         ok($v2w->add($mime), "add msg $i OK");
55 }
56
57 my $epoch_max = $v2w->{epoch_max};
58 ok($epoch_max > 0, "multiple epochs");
59 $v2w->done;
60 $ibx->cleanup;
61
62 my $sock = tcp_server();
63 ok($sock, 'sock created');
64 my $cmd = [ '-httpd', '-W0', "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
65 my $td = start_script($cmd, undef, { 3 => $sock });
66 my ($host, $port) = ($sock->sockhost, $sock->sockport);
67 $sock = undef;
68
69 my @cmd;
70 foreach my $i (0..$epoch_max) {
71         my $sfx = $i == 0 ? '.git' : '';
72         @cmd = (qw(git clone --mirror -q),
73                 "http://$host:$port/v2/$i$sfx",
74                 "$tmpdir/m/git/$i.git");
75
76         is(system(@cmd), 0, "cloned $i.git");
77         ok(-d "$tmpdir/m/git/$i.git", "mirror $i OK");
78 }
79
80 @cmd = ("-init", '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
81         'alt@example.com');
82 ok(run_script(\@cmd), 'initialized public-inbox -V2');
83
84 ok(run_script(['-index', "$tmpdir/m"]), 'indexed');
85
86 my $mibx = { inboxdir => "$tmpdir/m", address => 'alt@example.com' };
87 $mibx = PublicInbox::Inbox->new($mibx);
88 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
89
90 $v2w->{rotate_bytes} = $old_rotate_bytes;
91 for my $i (10..15) {
92         $mime->header_set('Message-ID', "<$i\@example.com>");
93         $mime->header_set('Subject', "subject = $i");
94         ok($v2w->add($mime), "add msg $i OK");
95 }
96 $v2w->done;
97 $ibx->cleanup;
98
99 sub fetch_each_epoch {
100         foreach my $i (0..$epoch_max) {
101                 my $dir = "$tmpdir/m/git/$i.git";
102                 is(system('git', "--git-dir=$dir", 'fetch', '-q'), 0,
103                         'fetch successful');
104         }
105 }
106
107 fetch_each_epoch();
108
109 my $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
110 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
111 ok(run_script(["-index", "$tmpdir/m"]), 'index updated');
112 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
113 $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
114 is(scalar($mset->items), 1, 'found message in mirror');
115
116 # purge:
117 $mime->header_set('Message-ID', '<10@example.com>');
118 $mime->header_set('Subject', 'subject = 10');
119 {
120         my @warn;
121         local $SIG{__WARN__} = sub { push @warn, @_ };
122         ok($v2w->purge($mime), 'purge a message');
123         my $warn = join('', @warn);
124         like($warn, qr/purge rewriting/);
125         my @subj = ($warn =~ m/^# subject .*$/mg);
126         is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
127 }
128
129 $v2w->done;
130
131 my $msgs = $mibx->search->{over_ro}->get_thread('10@example.com');
132 my $to_purge = $msgs->[0]->{blob};
133 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
134 $mset = $ibx->search->reopen->query('m:10@example.com', {mset => 1});
135 is(scalar($mset->items), 0, 'purged message gone from origin');
136
137 fetch_each_epoch();
138 {
139         $ibx->cleanup;
140         PublicInbox::InboxWritable::cleanup($mibx);
141         $v2w->done;
142         my $cmd = [ '-index', '--prune', "$tmpdir/m" ];
143         my ($out, $err) = ('', '');
144         my $opt = { 1 => \$out, 2 => \$err };
145         ok(run_script($cmd, undef, $opt), '-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         $ibx->cleanup;
175         fetch_each_epoch();
176         PublicInbox::InboxWritable::cleanup($mibx);
177
178         my $cmd = [ "-index", "$tmpdir/m" ];
179         my ($out, $err) = ('', '');
180         my $opt = { 1 => \$out, 2 => \$err };
181         ok(run_script($cmd, undef, $opt), '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($td->kill, 'killed httpd');
188 $td->join;
189
190 done_testing();
191
192 1;