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