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