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