]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
e3c384fa77a1f546741e153a59492d2d96b49c20
[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 use PublicInbox::TestCommon;
7 use Cwd qw(abs_path);
8 require_git(2.6);
9 local $ENV{HOME} = abs_path('t');
10
11 # Integration tests for HTTP cloning + mirroring
12 foreach my $mod (qw(Plack::Util Plack::Builder
13                         HTTP::Date HTTP::Status Search::Xapian DBD::SQLite)) {
14         eval "require $mod";
15         plan skip_all => "$mod missing for v2mirror.t" if $@;
16 }
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, $for_destroy) = tmpdir();
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         inboxdir = $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 $ibx->cleanup;
62
63 my $sock = tcp_server();
64 ok($sock, 'sock created');
65 my $cmd = [ '-httpd', '-W0', "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
66 my $td = start_script($cmd, undef, { 3 => $sock });
67 my ($host, $port) = ($sock->sockhost, $sock->sockport);
68 $sock = undef;
69
70 my @cmd;
71 foreach my $i (0..$epoch_max) {
72         my $sfx = $i == 0 ? '.git' : '';
73         @cmd = (qw(git clone --mirror -q),
74                 "http://$host:$port/v2/$i$sfx",
75                 "$tmpdir/m/git/$i.git");
76
77         is(system(@cmd), 0, "cloned $i.git");
78         ok(-d "$tmpdir/m/git/$i.git", "mirror $i OK");
79 }
80
81 @cmd = ("-init", '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
82         'alt@example.com');
83 ok(run_script(\@cmd), 'initialized public-inbox -V2');
84
85 ok(run_script(['-index', "$tmpdir/m"]), 'indexed');
86
87 my $mibx = { inboxdir => "$tmpdir/m", address => 'alt@example.com' };
88 $mibx = PublicInbox::Inbox->new($mibx);
89 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
90
91 $v2w->{rotate_bytes} = $old_rotate_bytes;
92 for my $i (10..15) {
93         $mime->header_set('Message-ID', "<$i\@example.com>");
94         $mime->header_set('Subject', "subject = $i");
95         ok($v2w->add($mime), "add msg $i OK");
96 }
97 $v2w->done;
98 $ibx->cleanup;
99
100 my $fetch_each_epoch = sub {
101         foreach my $i (0..$epoch_max) {
102                 my $dir = "$tmpdir/m/git/$i.git";
103                 is(system('git', "--git-dir=$dir", 'fetch', '-q'), 0,
104                         'fetch successful');
105         }
106 };
107
108 $fetch_each_epoch->();
109
110 my $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
111 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
112 ok(run_script(["-index", "$tmpdir/m"]), 'index updated');
113 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
114 $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
115 is(scalar($mset->items), 1, 'found message in mirror');
116
117 # purge:
118 $mime->header_set('Message-ID', '<10@example.com>');
119 $mime->header_set('Subject', 'subject = 10');
120 {
121         my @warn;
122         local $SIG{__WARN__} = sub { push @warn, @_ };
123         ok($v2w->purge($mime), 'purge a message');
124         my $warn = join('', @warn);
125         like($warn, qr/purge rewriting/);
126         my @subj = ($warn =~ m/^# subject .*$/mg);
127         is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
128 }
129
130 $v2w->done;
131
132 my $msgs = $mibx->search->{over_ro}->get_thread('10@example.com');
133 my $to_purge = $msgs->[0]->{blob};
134 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
135 $mset = $ibx->search->reopen->query('m:10@example.com', {mset => 1});
136 is(scalar($mset->items), 0, 'purged message gone from origin');
137
138 $fetch_each_epoch->();
139 {
140         $ibx->cleanup;
141         PublicInbox::InboxWritable::cleanup($mibx);
142         $v2w->done;
143         my $cmd = [ '-index', '--prune', "$tmpdir/m" ];
144         my ($out, $err) = ('', '');
145         my $opt = { 1 => \$out, 2 => \$err };
146         ok(run_script($cmd, undef, $opt), '-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         $ibx->cleanup;
176         $fetch_each_epoch->();
177         PublicInbox::InboxWritable::cleanup($mibx);
178
179         my $cmd = [ "-index", "$tmpdir/m" ];
180         my ($out, $err) = ('', '');
181         my $opt = { 1 => \$out, 2 => \$err };
182         ok(run_script($cmd, undef, $opt), '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($td->kill, 'killed httpd');
189 $td->join;
190
191 done_testing();
192
193 1;