]> Sergey Matveev's repositories - public-inbox.git/blob - t/indexlevels-mirror.t
tests: consistently check for xapian-compact
[public-inbox.git] / t / indexlevels-mirror.t
1 # Copyright (C) 2019-2020 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 use PublicInbox::Eml;
7 use PublicInbox::Inbox;
8 use PublicInbox::InboxWritable;
9 require PublicInbox::Admin;
10 use PublicInbox::TestCommon;
11 my $PI_TEST_VERSION = $ENV{PI_TEST_VERSION} || 2;
12 require_git('2.6') if $PI_TEST_VERSION == 2;
13 require_mods(qw(DBD::SQLite));
14
15 my $mime = PublicInbox::Eml->new(<<'EOF');
16 From: a@example.com
17 To: test@example.com
18 Subject: this is a subject
19 Date: Fri, 02 Oct 1993 00:00:00 +0000
20
21 hello world
22 EOF
23
24 my $import_index_incremental = sub {
25         my ($v, $level, $mime) = @_;
26         my $err = '';
27         my $this = "pi-$v-$level-indexlevels";
28         my ($tmpdir, $for_destroy) = tmpdir();
29         local $ENV{PI_CONFIG} = "$tmpdir/config";
30         my $ibx = PublicInbox::Inbox->new({
31                 inboxdir => "$tmpdir/testbox",
32                 name => $this,
33                 version => $v,
34                 -primary_address => 'test@example.com',
35                 indexlevel => $level,
36         });
37         my $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer(0);
38         $mime->header_set('Message-ID', '<m@1>');
39         ok($im->add($mime), 'first message added');
40         $im->done;
41
42         # index master (required for v1)
43         my @cmd = (qw(-index -j0), $ibx->{inboxdir}, "-L$level");
44         push @cmd, '-c' if have_xapian_compact;
45         ok(run_script(\@cmd, undef, { 2 => \$err }), 'index master');
46         my $ro_master = PublicInbox::Inbox->new({
47                 inboxdir => $ibx->{inboxdir},
48                 indexlevel => $level
49         });
50         my $msgs = $ro_master->recent;
51         is(scalar(@$msgs), 1, 'only one message in master, so far');
52         is($msgs->[0]->{mid}, 'm@1', 'first message in master indexed');
53
54         # clone
55         @cmd = (qw(git clone --mirror -q));
56         my $mirror = "$tmpdir/mirror-$v";
57         if ($v == 1) {
58                 push @cmd, $ibx->{inboxdir}, $mirror;
59         } else {
60                 push @cmd, "$ibx->{inboxdir}/git/0.git", "$mirror/git/0.git";
61         }
62         my $fetch_dir = $cmd[-1];
63         is(xsys(@cmd), 0, "v$v clone OK");
64
65         # inbox init
66         local $ENV{PI_CONFIG} = "$tmpdir/.picfg";
67         @cmd = ('-init', '-L', $level,
68                 'mirror', $mirror, '//example.com/test', 'test@example.com');
69         push @cmd, '-V2' if $v == 2;
70         ok(run_script(\@cmd), "v$v init OK");
71
72         # index mirror
73         ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror OK");
74
75         # read-only access
76         my $ro_mirror = PublicInbox::Inbox->new({
77                 inboxdir => $mirror,
78                 indexlevel => $level,
79         });
80         $msgs = $ro_mirror->recent;
81         is(scalar(@$msgs), 1, 'only one message, so far');
82         is($msgs->[0]->{mid}, 'm@1', 'read first message');
83
84         # update master
85         $mime->header_set('Message-ID', '<m@2>');
86         ok($im->add($mime), '2nd message added');
87         $im->done;
88
89         # mirror updates
90         is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
91         ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror again OK");
92         $msgs = $ro_mirror->recent;
93         is(scalar(@$msgs), 2, '2nd message seen in mirror');
94         is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
95                 ['m@1','m@2'], 'got both messages in mirror');
96
97         # incremental index master (required for v1)
98         ok(run_script([qw(-index -j0), $ibx->{inboxdir}, "-L$level"]),
99                 'index master OK');
100         $msgs = $ro_master->recent;
101         is(scalar(@$msgs), 2, '2nd message seen in master');
102         is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
103                 ['m@1','m@2'], 'got both messages in master');
104
105         my @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
106         is_deeply(\@rw_nums, [1, 2], 'master has expected NNTP articles');
107
108         my @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
109         is_deeply(\@ro_nums, [1, 2], 'mirror has expected NNTP articles');
110
111         # remove message from master
112         ok($im->remove($mime), '2nd message removed');
113         $im->done;
114         @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
115         is_deeply(\@rw_nums, [1], 'unindex NNTP article'.$v.$level);
116
117         if ($level ne 'basic') {
118                 ok(run_script(['-xcpdb', '-q', $mirror]), "v$v xcpdb OK");
119                 is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
120                    'indexlevel detectable by Admin after xcpdb v' .$v.$level);
121                 delete $ro_mirror->{$_} for (qw(over search));
122                 my $mset = $ro_mirror->search->mset('m:m@2');
123                 is($mset->size, 1, "v$v found m\@2 via Xapian on $level");
124         }
125
126         # sync the mirror
127         is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
128         ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror again OK");
129         $msgs = $ro_mirror->recent;
130         is(scalar(@$msgs), 1, '2nd message gone from mirror');
131         is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
132                 'message unavailable in mirror');
133
134         if ($v == 2 && $level eq 'basic') {
135                 is_deeply([glob("$ibx->{inboxdir}/xap*/?/")], [],
136                          'no Xapian shard directories for v2 basic');
137         }
138         if ($level ne 'basic') {
139                 my $mset = $ro_mirror->search->reopen->mset('m:m@2');
140                 is($mset->size, 0,
141                         "v$v m\@2 gone from Xapian in mirror on $level");
142         }
143
144         # add another message to master and have the mirror
145         # sync and reindex it
146         my @expect = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
147         foreach my $i (3..5) {
148                 $mime->header_set('Message-ID', "<m\@$i>");
149                 ok($im->add($mime), "#$i message added");
150                 push @expect, $i;
151         }
152         $im->done;
153         is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
154         ok(run_script([qw(-index -j0 --reindex), $mirror]),
155                 "v$v index --reindex mirror OK");
156         @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
157         @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
158         is_deeply(\@rw_nums, \@expect, "v$v master has expected NNTP articles");
159         is_deeply(\@ro_nums, \@expect, "v$v mirror matches master articles");
160
161         is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
162            'indexlevel detectable by Admin '.$v.$level);
163
164         SKIP: {
165                 skip 'xapian-compact missing', 1 if have_xapian_compact;
166                 my $cmd = [ qw(-compact), $mirror ];
167                 ok(run_script($cmd, undef, { 2 => \$err}), "compact $level");
168         }
169 };
170
171 # we can probably cull some other tests
172 $import_index_incremental->($PI_TEST_VERSION, 'basic', $mime);
173
174 SKIP: {
175         require PublicInbox::Search;
176         PublicInbox::Search::load_xapian() or
177                 skip('Xapian perl binding missing', 2);
178         foreach my $l (qw(medium full)) {
179                 $import_index_incremental->($PI_TEST_VERSION, $l, $mime);
180         }
181 }
182
183 done_testing();