]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
tests: consolidate process spawning code.
[public-inbox.git] / t / v2mirror.t
1 # Copyright (C) 2018 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
8 # Integration tests for HTTP cloning + mirroring
9 foreach my $mod (qw(Plack::Util Plack::Builder Danga::Socket
10                         HTTP::Date HTTP::Status Search::Xapian DBD::SQLite
11                         IPC::Run)) {
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::MIME;
20 use PublicInbox::Config;
21 # FIXME: too much setup
22 my $tmpdir = tempdir('pi-v2mirror-XXXXXX', TMPDIR => 1, CLEANUP => 1);
23 my $script = 'blib/script/public-inbox';
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         mainrepo = $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
61 my %opts = (
62         LocalAddr => '127.0.0.1',
63         ReuseAddr => 1,
64         Proto => 'tcp',
65         Listen => 1024,
66 );
67 my ($sock, $pid);
68 END { kill 'TERM', $pid if defined $pid };
69
70 $! = 0;
71 $sock = IO::Socket::INET->new(%opts);
72 ok($sock, 'sock created');
73 my $cmd = [ "$script-httpd", "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
74 ok(defined($pid = spawn_listener(undef, $cmd, [ $sock ])),
75         'spawned httpd process successfully');
76 my ($host, $port) = ($sock->sockhost, $sock->sockport);
77 $sock = undef;
78
79 my @cmd;
80 foreach my $i (0..$epoch_max) {
81         @cmd = (qw(git clone --mirror -q), "http://$host:$port/v2/$i",
82                 "$tmpdir/m/git/$i.git");
83
84         is(system(@cmd), 0, 'cloned OK');
85         ok(-d "$tmpdir/m/git/$i.git", 'mirror OK');
86 }
87
88 @cmd = ("$script-init", '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
89         'alt@example.com');
90 is(system(@cmd), 0, 'initialized public-inbox -V2');
91 is(system("$script-index", "$tmpdir/m"), 0, 'indexed');
92
93 my $mibx = { mainrepo => "$tmpdir/m", address => 'alt@example.com' };
94 $mibx = PublicInbox::Inbox->new($mibx);
95 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
96
97 $v2w->{rotate_bytes} = $old_rotate_bytes;
98 for my $i (10..15) {
99         $mime->header_set('Message-ID', "<$i\@example.com>");
100         $mime->header_set('Subject', "subject = $i");
101         ok($v2w->add($mime), "add msg $i OK");
102 }
103 $v2w->barrier;
104
105 sub fetch_each_epoch {
106         foreach my $i (0..$epoch_max) {
107                 my $dir = "$tmpdir/m/git/$i.git";
108                 is(system('git', "--git-dir=$dir", 'fetch', '-q'), 0,
109                         'fetch successful');
110         }
111 }
112
113 fetch_each_epoch();
114
115 my $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
116 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
117 is(system("$script-index", "$tmpdir/m"), 0, 'index updated');
118 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
119 $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
120 is(scalar($mset->items), 1, 'found message in mirror');
121
122 # purge:
123 $mime->header_set('Message-ID', '<10@example.com>');
124 $mime->header_set('Subject', 'subject = 10');
125 {
126         my @warn;
127         local $SIG{__WARN__} = sub { push @warn, @_ };
128         ok($v2w->purge($mime), 'purge a message');
129         my $warn = join('', @warn);
130         like($warn, qr/purge rewriting/);
131         my @subj = ($warn =~ m/^# subject .*$/mg);
132         is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
133 }
134
135 $v2w->barrier;
136
137 my $msgs = $mibx->search->{over_ro}->get_thread('10@example.com');
138 my $to_purge = $msgs->[0]->{blob};
139 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
140 $mset = $ibx->search->reopen->query('m:10@example.com', {mset => 1});
141 is(scalar($mset->items), 0, 'purged message gone from origin');
142
143 fetch_each_epoch();
144 {
145         my $cmd = [ "$script-index", '--prune', "$tmpdir/m" ];
146         my ($in, $out, $err) = ('', '', '');
147         ok(IPC::Run::run($cmd, \$in, \$out, \$err), '-index --prune');
148         like($err, qr/discontiguous range/, 'warned about discontiguous range');
149         unlike($err, qr/fatal/, 'no scary fatal error shown');
150 }
151
152 $mset = $mibx->search->reopen->query('m:10@example.com', {mset => 1});
153 is(scalar($mset->items), 0, 'purged message not found in mirror');
154 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'minmax still synced');
155 for my $i ((1..9),(11..15)) {
156         $mset = $mibx->search->query("m:$i\@example.com", {mset => 1});
157         is(scalar($mset->items), 1, "$i\@example.com remains visible");
158 }
159 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
160
161 {
162         my @warn;
163         local $SIG{__WARN__} = sub { push @warn, @_ };
164         $v2w->index_sync;
165         is_deeply(\@warn, [], 'no warnings from index_sync after purge');
166 }
167
168 # deletes happen in a different fetch window
169 {
170         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
171         is(scalar($mset->items), 1, '1@example.com visible in mirror');
172         $mime->header_set('Message-ID', '<1@example.com>');
173         $mime->header_set('Subject', 'subject = 1');
174         ok($v2w->remove($mime), 'removed <1@example.com> from source');
175         $v2w->done;
176         fetch_each_epoch();
177
178         my ($in, $out, $err) = ('', '', '');
179         my $cmd = [ "$script-index", "$tmpdir/m" ];
180         ok(IPC::Run::run($cmd, \$in, \$out, \$err), '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(kill('TERM', $pid), 'killed httpd');
187 $pid = undef;
188 waitpid(-1, 0);
189
190 done_testing();
191
192 1;