]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / t / v2mirror.t
1 # Copyright (C) 2018-2020 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 require_mods(qw(Plack::Util Plack::Builder
13                 HTTP::Date HTTP::Status Search::Xapian DBD::SQLite));
14 use IO::Socket;
15 use POSIX qw(dup2);
16 use_ok 'PublicInbox::V2Writable';
17 use PublicInbox::InboxWritable;
18 use PublicInbox::MIME;
19 use PublicInbox::Config;
20 # FIXME: too much setup
21 my ($tmpdir, $for_destroy) = tmpdir();
22 my $pi_config = "$tmpdir/config";
23 {
24         open my $fh, '>', $pi_config or die "open($pi_config): $!";
25         print $fh <<"" or die "print $pi_config: $!";
26 [publicinbox "v2"]
27         inboxdir = $tmpdir/in
28         address = test\@example.com
29
30         close $fh or die "close($pi_config): $!";
31 }
32 local $ENV{PI_CONFIG} = $pi_config;
33
34 my $cfg = PublicInbox::Config->new($pi_config);
35 my $ibx = $cfg->lookup('test@example.com');
36 ok($ibx, 'inbox found');
37 $ibx->{version} = 2;
38 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
39 ok $v2w, 'v2w loaded';
40 $v2w->{parallel} = 0;
41 my $mime = PublicInbox::MIME->new(<<'');
42 From: Me <me@example.com>
43 To: You <you@example.com>
44 Subject: a
45 Date: Thu, 01 Jan 1970 00:00:00 +0000
46
47 my $old_rotate_bytes = $v2w->{rotate_bytes};
48 $v2w->{rotate_bytes} = 500; # force rotating
49 for my $i (1..9) {
50         $mime->header_set('Message-ID', "<$i\@example.com>");
51         $mime->header_set('Subject', "subject = $i");
52         ok($v2w->add($mime), "add msg $i OK");
53 }
54
55 my $epoch_max = $v2w->{epoch_max};
56 ok($epoch_max > 0, "multiple epochs");
57 $v2w->done;
58 $ibx->cleanup;
59
60 my $sock = tcp_server();
61 ok($sock, 'sock created');
62 my $cmd = [ '-httpd', '-W0', "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
63 my $td = start_script($cmd, undef, { 3 => $sock });
64 my ($host, $port) = ($sock->sockhost, $sock->sockport);
65 $sock = undef;
66
67 my @cmd;
68 foreach my $i (0..$epoch_max) {
69         my $sfx = $i == 0 ? '.git' : '';
70         @cmd = (qw(git clone --mirror -q),
71                 "http://$host:$port/v2/$i$sfx",
72                 "$tmpdir/m/git/$i.git");
73
74         is(system(@cmd), 0, "cloned $i.git");
75         ok(-d "$tmpdir/m/git/$i.git", "mirror $i OK");
76 }
77
78 @cmd = ("-init", '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
79         'alt@example.com');
80 ok(run_script(\@cmd), 'initialized public-inbox -V2');
81
82 ok(run_script(['-index', "$tmpdir/m"]), 'indexed');
83
84 my $mibx = { inboxdir => "$tmpdir/m", address => 'alt@example.com' };
85 $mibx = PublicInbox::Inbox->new($mibx);
86 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
87
88 $v2w->{rotate_bytes} = $old_rotate_bytes;
89 for my $i (10..15) {
90         $mime->header_set('Message-ID', "<$i\@example.com>");
91         $mime->header_set('Subject', "subject = $i");
92         ok($v2w->add($mime), "add msg $i OK");
93 }
94 $v2w->done;
95 $ibx->cleanup;
96
97 my $fetch_each_epoch = sub {
98         foreach my $i (0..$epoch_max) {
99                 my $dir = "$tmpdir/m/git/$i.git";
100                 is(system('git', "--git-dir=$dir", 'fetch', '-q'), 0,
101                         'fetch successful');
102         }
103 };
104
105 $fetch_each_epoch->();
106
107 my $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
108 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
109 ok(run_script(["-index", "$tmpdir/m"]), 'index updated');
110 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
111 $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
112 is(scalar($mset->items), 1, 'found message in mirror');
113
114 # purge:
115 $mime->header_set('Message-ID', '<10@example.com>');
116 $mime->header_set('Subject', 'subject = 10');
117 {
118         my @warn;
119         local $SIG{__WARN__} = sub { push @warn, @_ };
120         ok($v2w->purge($mime), 'purge a message');
121         my $warn = join('', @warn);
122         like($warn, qr/purge rewriting/);
123         my @subj = ($warn =~ m/^# subject .*$/mg);
124         is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
125 }
126
127 $v2w->done;
128
129 my $msgs = $mibx->search->{over_ro}->get_thread('10@example.com');
130 my $to_purge = $msgs->[0]->{blob};
131 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
132 $mset = $ibx->search->reopen->query('m:10@example.com', {mset => 1});
133 is(scalar($mset->items), 0, 'purged message gone from origin');
134
135 $fetch_each_epoch->();
136 {
137         $ibx->cleanup;
138         PublicInbox::InboxWritable::cleanup($mibx);
139         $v2w->done;
140         my $cmd = [ '-index', '--prune', "$tmpdir/m" ];
141         my ($out, $err) = ('', '');
142         my $opt = { 1 => \$out, 2 => \$err };
143         ok(run_script($cmd, undef, $opt), '-index --prune');
144         like($err, qr/discontiguous range/, 'warned about discontiguous range');
145         unlike($err, qr/fatal/, 'no scary fatal error shown');
146 }
147
148 $mset = $mibx->search->reopen->query('m:10@example.com', {mset => 1});
149 is(scalar($mset->items), 0, 'purged message not found in mirror');
150 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'minmax still synced');
151 for my $i ((1..9),(11..15)) {
152         $mset = $mibx->search->query("m:$i\@example.com", {mset => 1});
153         is(scalar($mset->items), 1, "$i\@example.com remains visible");
154 }
155 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
156
157 {
158         my @warn;
159         local $SIG{__WARN__} = sub { push @warn, @_ };
160         $v2w->index_sync;
161         is_deeply(\@warn, [], 'no warnings from index_sync after purge');
162 }
163
164 # deletes happen in a different fetch window
165 {
166         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
167         is(scalar($mset->items), 1, '1@example.com visible in mirror');
168         $mime->header_set('Message-ID', '<1@example.com>');
169         $mime->header_set('Subject', 'subject = 1');
170         ok($v2w->remove($mime), 'removed <1@example.com> from source');
171         $v2w->done;
172         $ibx->cleanup;
173         $fetch_each_epoch->();
174         PublicInbox::InboxWritable::cleanup($mibx);
175
176         my $cmd = [ "-index", "$tmpdir/m" ];
177         my ($out, $err) = ('', '');
178         my $opt = { 1 => \$out, 2 => \$err };
179         ok(run_script($cmd, undef, $opt), '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($td->kill, 'killed httpd');
186 $td->join;
187
188 done_testing();
189
190 1;