1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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)) {
11 plan skip_all => "$mod missing for v2mirror.t" if $@;
13 use File::Temp qw/tempdir/;
16 use 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";
25 open my $fh, '>', $pi_config or die "open($pi_config): $!";
26 print $fh <<"" or die "print $pi_config: $!";
29 address = test\@example.com
31 close $fh or die "close($pi_config): $!";
33 local $ENV{PI_CONFIG} = $pi_config;
35 my $cfg = PublicInbox::Config->new($pi_config);
36 my $ibx = $cfg->lookup('test@example.com');
37 ok($ibx, 'inbox found');
39 my $v2w = PublicInbox::V2Writable->new($ibx, 1);
40 ok $v2w, 'v2w loaded';
42 my $mime = PublicInbox::MIME->new(<<'');
43 From: Me <me@example.com>
44 To: You <you@example.com>
46 Date: Thu, 01 Jan 1970 00:00:00 +0000
49 $mime->header_set('Message-ID', "<$i\@example.com>");
50 $mime->header_set('Subject', "subject = $i");
51 ok($v2w->add($mime), "add msg $i OK");
56 LocalAddr => '127.0.0.1',
62 END { kill 'TERM', $pid if defined $pid };
65 $sock = IO::Socket::INET->new(%opts);
66 ok($sock, 'sock created');
67 my $fl = fcntl($sock, F_GETFD, 0);
70 # pretend to be systemd
71 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
72 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
73 $ENV{LISTEN_PID} = $$;
75 exec "$script-httpd", "--stdout=$tmpdir/out", "--stderr=$tmpdir/err";
78 ok(defined $pid, 'forked httpd process successfully');
79 my ($host, $port) = ($sock->sockhost, $sock->sockport);
82 my @cmd = (qw(git clone --mirror -q), "http://$host:$port/v2/0",
83 "$tmpdir/m/git/0.git");
85 is(system(@cmd), 0, 'cloned OK');
86 ok(-d "$tmpdir/m/git/0.git", 'mirror OK');;
88 @cmd = ("$script-init", '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
90 is(system(@cmd), 0, 'initialized public-inbox -V2');
91 is(system("$script-index", "$tmpdir/m"), 0, 'indexed');
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');
98 $mime->header_set('Message-ID', "<$i\@example.com>");
99 $mime->header_set('Subject', "subject = $i");
100 ok($v2w->add($mime), "add msg $i OK");
103 is(system('git', "--git-dir=$tmpdir/m/git/0.git", 'fetch', '-q'), 0,
106 my $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
107 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
108 is(system("$script-index", "$tmpdir/m"), 0, 'index updated');
109 is_deeply([$mibx->mm->minmax], [$ibx->mm->minmax], 'index synched minmax');
110 $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
111 is(scalar($mset->items), 1, 'found message in mirror');
114 $mime->header_set('Message-ID', '<10@example.com>');
115 $mime->header_set('Subject', 'subject = 10');
118 local $SIG{__WARN__} = sub { push @warn, @_ };
119 ok($v2w->purge($mime), 'purge a message');
120 my $warn = join('', @warn);
121 like($warn, qr/purge rewriting/);
122 my @subj = ($warn =~ m/^# subject .*$/mg);
123 is_deeply(\@subj, ["# subject = 10"], "only rewrote one");
128 my $msgs = $mibx->search->{over_ro}->get_thread('10@example.com');
129 my $to_purge = $msgs->[0]->{blob};
130 like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
131 $mset = $ibx->search->reopen->query('m:10@example.com', {mset => 1});
132 is(scalar($mset->items), 0, 'purged message gone from origin');
134 is(system('git', "--git-dir=$tmpdir/m/git/0.git", 'fetch', '-q'), 0,
137 open my $err, '+>', "$tmpdir/index-err" or die "open: $!";
140 dup2(fileno($err), 2) or die "dup2 failed: $!";
141 exec("$script-index", '--prune', "$tmpdir/m");
144 ok($ipid, 'running index..');
145 is(waitpid($ipid, 0), $ipid, 'index --prune done');
146 is($?, 0, 'no error from index');
147 ok(seek($err, 0, 0), 'rewound stderr');
148 $err = eval { local $/; <$err> };
149 like($err, qr/discontiguous range/, 'warned about discontiguous range');
150 unlike($err, qr/fatal/, 'no scary fatal error shown');
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");
160 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
164 local $SIG{__WARN__} = sub { push @warn, @_ };
166 is_deeply(\@warn, [], 'no warnings from index_sync after purge');
170 ok(kill('TERM', $pid), 'killed httpd');