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>
6 use PublicInbox::TestCommon;
9 local $ENV{HOME} = abs_path('t');
11 # Integration tests for HTTP cloning + mirroring
12 require_mods(qw(Plack::Util Plack::Builder
13 HTTP::Date HTTP::Status Search::Xapian DBD::SQLite));
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";
24 open my $fh, '>', $pi_config or die "open($pi_config): $!";
25 print $fh <<"" or die "print $pi_config: $!";
28 address = test\@example.com
30 close $fh or die "close($pi_config): $!";
32 local $ENV{PI_CONFIG} = $pi_config;
34 my $cfg = PublicInbox::Config->new($pi_config);
35 my $ibx = $cfg->lookup('test@example.com');
36 ok($ibx, 'inbox found');
38 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
39 ok $v2w, 'v2w loaded';
41 my $mime = PublicInbox::MIME->new(<<'');
42 From: Me <me@example.com>
43 To: You <you@example.com>
45 Date: Thu, 01 Jan 1970 00:00:00 +0000
47 my $old_rotate_bytes = $v2w->{rotate_bytes};
48 $v2w->{rotate_bytes} = 500; # force rotating
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");
55 my $epoch_max = $v2w->{epoch_max};
56 ok($epoch_max > 0, "multiple epochs");
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);
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");
74 is(system(@cmd), 0, "cloned $i.git");
75 ok(-d "$tmpdir/m/git/$i.git", "mirror $i OK");
78 @cmd = ("-init", '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
80 ok(run_script(\@cmd), 'initialized public-inbox -V2');
82 ok(run_script(['-index', "$tmpdir/m"]), 'indexed');
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');
88 $v2w->{rotate_bytes} = $old_rotate_bytes;
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");
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,
105 $fetch_each_epoch->();
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');
115 $mime->header_set('Message-ID', '<10@example.com>');
116 $mime->header_set('Subject', 'subject = 10');
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");
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');
135 $fetch_each_epoch->();
138 PublicInbox::InboxWritable::cleanup($mibx);
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');
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");
155 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
159 local $SIG{__WARN__} = sub { push @warn, @_ };
161 is_deeply(\@warn, [], 'no warnings from index_sync after purge');
164 # deletes happen in a different fetch window
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');
173 $fetch_each_epoch->();
174 PublicInbox::InboxWritable::cleanup($mibx);
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');
185 ok($td->kill, 'killed httpd');