]> Sergey Matveev's repositories - public-inbox.git/blob - t/indexlevels-mirror.t
searchidx: fix incremental index with indexlevel=basic on v1
[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 File::Temp qw/tempdir/;
9 require './t/common.perl';
10 require_git(2.6);
11 my $this = (split('/', __FILE__))[-1];
12
13 # TODO: remove Search::Xapian as a requirement for basic
14 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
15         eval "require $mod";
16         plan skip_all => "$mod missing for $this" if $@;
17 }
18
19 my $path = 'blib/script';
20 my $index = "$path/public-inbox-index";
21
22 my $mime = PublicInbox::MIME->create(
23         header => [
24                 From => 'a@example.com',
25                 To => 'test@example.com',
26                 Subject => 'this is a subject',
27                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
28         ],
29         body => "hello world\n",
30 );
31
32 sub import_index_incremental {
33         my ($v, $level) = @_;
34         my $tmpdir = tempdir("pi-$this-tmp-XXXXXX", TMPDIR => 1, CLEANUP => 1);
35         my $ibx = PublicInbox::Inbox->new({
36                 mainrepo => "$tmpdir/testbox",
37                 name => "$this-$v",
38                 version => $v,
39                 -primary_address => 'test@example.com',
40                 indexlevel => $level,
41         });
42         my $cls = "PublicInbox::V${v}Writable";
43         use_ok $cls;
44         my $im = $cls->new($ibx, {nproc=>1});
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}), 0, 'index master OK');
51         my $ro_master = PublicInbox::Inbox->new({mainrepo => $ibx->{mainrepo}});
52         my ($nr, $msgs) = $ro_master->recent;
53         is($nr, 1, 'only one message in master, so far');
54         is($msgs->[0]->{mid}, 'm@1', 'first message in master indexed');
55
56         # clone
57         my @cmd = (qw(git clone --mirror -q));
58         my $mirror = "$tmpdir/mirror-$v";
59         if ($v == 1) {
60                 push @cmd, $ibx->{mainrepo}, $mirror;
61         } else {
62                 push @cmd, "$ibx->{mainrepo}/git/0.git", "$mirror/git/0.git";
63         }
64         my $fetch_dir = $cmd[-1];
65         is(system(@cmd), 0, "v$v clone OK");
66
67         # inbox init
68         local $ENV{PI_CONFIG} = "$tmpdir/.picfg";
69         @cmd = ("$path/public-inbox-init", '-L', $level,
70                 'mirror', $mirror, '//example.com/test', 'test@example.com');
71         push @cmd, '-V2' if $v == 2;
72         is(system(@cmd), 0, "v$v init OK");
73
74         # index mirror
75         is(system($index, $mirror), 0, "v$v index mirror OK");
76
77         # read-only access
78         my $ro_mirror = PublicInbox::Inbox->new({mainrepo => $mirror});
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         is(system($index, $mirror), 0, "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         is(system($index, $ibx->{mainrepo}), 0, 'index master OK');
98         ($nr, $msgs) = $ro_master->recent;
99         is($nr, 2, '2nd message seen in master');
100         is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
101                 ['m@1','m@2'], 'got both messages in master');
102
103         # remove message from master
104         ok($im->remove($mime), '2nd message removed');
105         $im->done;
106
107         # sync the mirror
108         is(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
109         is(system($index, $mirror), 0, "v$v index mirror again OK");
110         ($nr, $msgs) = $ro_mirror->recent;
111         is($nr, 1, '2nd message gone from mirror');
112         is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
113                 'message unavailable in mirror');
114 }
115
116 # we can probably cull some other tests and put full/medium tests, here
117 for my $level (qw(basic)) {
118         for my $v (1..2) {
119                 subtest("v$v indexlevel=$level" => sub {
120                         import_index_incremental($v, $level);
121                 })
122         }
123 }
124
125 done_testing();