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