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_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";
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
48 my $old_rotate_bytes = $v2w->{rotate_bytes};
49 $v2w->{rotate_bytes} = 500; # force rotating
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");
56 my $epoch_max = $v2w->{epoch_max};
57 ok($epoch_max > 0, "multiple epochs");
61 LocalAddr => '127.0.0.1',
67 END { kill 'TERM', $pid if defined $pid };
70 $sock = IO::Socket::INET->new(%opts);
71 ok($sock, 'sock created');
72 my $fl = fcntl($sock, F_GETFD, 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} = $$;
80 exec "$script-httpd", "--stdout=$tmpdir/out", "--stderr=$tmpdir/err";
83 ok(defined $pid, 'forked httpd process successfully');
84 my ($host, $port) = ($sock->sockhost, $sock->sockport);
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");
92 is(system(@cmd), 0, 'cloned OK');
93 ok(-d "$tmpdir/m/git/$i.git", 'mirror OK');
96 @cmd = ("$script-init", '-V2', 'm', "$tmpdir/m", 'http://example.com/m',
98 is(system(@cmd), 0, 'initialized public-inbox -V2');
99 is(system("$script-index", "$tmpdir/m"), 0, 'indexed');
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');
105 $v2w->{rotate_bytes} = $old_rotate_bytes;
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");
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,
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');
131 $mime->header_set('Message-ID', '<10@example.com>');
132 $mime->header_set('Subject', 'subject = 10');
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");
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');
153 open my $err, '+>', "$tmpdir/index-err" or die "open: $!";
156 dup2(fileno($err), 2) or die "dup2 failed: $!";
157 exec("$script-index", '--prune', "$tmpdir/m");
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');
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");
176 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
180 local $SIG{__WARN__} = sub { push @warn, @_ };
182 is_deeply(\@warn, [], 'no warnings from index_sync after purge');
186 ok(kill('TERM', $pid), 'killed httpd');