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