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