]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2mirror.t
ensure Xapian and SQLite are still optional for v1 tests
[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 for my $i (1..9) {
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");
52 }
53 $v2w->barrier;
54
55 my %opts = (
56         LocalAddr => '127.0.0.1',
57         ReuseAddr => 1,
58         Proto => 'tcp',
59         Listen => 1024,
60 );
61 my ($sock, $pid);
62 END { kill 'TERM', $pid if defined $pid };
63
64 $! = 0;
65 $sock = IO::Socket::INET->new(%opts);
66 ok($sock, 'sock created');
67 my $fl = fcntl($sock, F_GETFD, 0);
68 $pid = fork;
69 if ($pid == 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} = $$;
74         $ENV{LISTEN_FDS} = 1;
75         exec "$script-httpd", "--stdout=$tmpdir/out", "--stderr=$tmpdir/err";
76         die "FAIL: $!\n";
77 }
78 ok(defined $pid, 'forked httpd process successfully');
79 my ($host, $port) = ($sock->sockhost, $sock->sockport);
80 $sock = undef;
81
82 my @cmd = (qw(git clone --mirror -q), "http://$host:$port/v2/0",
83         "$tmpdir/m/git/0.git");
84
85 is(system(@cmd), 0, 'cloned OK');
86 ok(-d "$tmpdir/m/git/0.git", 'mirror OK');;
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 for my $i (10..15) {
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");
101 }
102 $v2w->barrier;
103 is(system('git', "--git-dir=$tmpdir/m/git/0.git", 'fetch', '-q'), 0,
104         'fetch successful');
105
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');
112
113 # purge:
114 $mime->header_set('Message-ID', '<10@example.com>');
115 $mime->header_set('Subject', 'subject = 10');
116 {
117         my @warn;
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");
124 }
125
126 $v2w->barrier;
127
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');
133
134 is(system('git', "--git-dir=$tmpdir/m/git/0.git", 'fetch', '-q'), 0,
135         'fetch successful');
136 {
137         open my $err, '+>', "$tmpdir/index-err" or die "open: $!";
138         my $ipid = fork;
139         if ($ipid == 0) {
140                 dup2(fileno($err), 2) or die "dup2 failed: $!";
141                 exec("$script-index", '--prune', "$tmpdir/m");
142                 die "exec fail: $!";
143         }
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');
151 }
152
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");
159 }
160 is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
161
162 {
163         my @warn;
164         local $SIG{__WARN__} = sub { push @warn, @_ };
165         $v2w->index_sync;
166         is_deeply(\@warn, [], 'no warnings from index_sync after purge');
167 }
168
169 $v2w->done;
170 ok(kill('TERM', $pid), 'killed httpd');
171 $pid = undef;
172 waitpid(-1, 0);
173
174 done_testing();
175
176 1;