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