]> Sergey Matveev's repositories - public-inbox.git/blob - t/indexlevels-mirror.t
lazy load Xapian and make it optional for v2
[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 foreach my $mod (qw(DBD::SQLite)) {
14         eval "require $mod";
15         plan skip_all => "$mod missing for $this" if $@;
16 }
17
18 my $path = 'blib/script';
19 my $index = "$path/public-inbox-index";
20
21 my $mime = PublicInbox::MIME->create(
22         header => [
23                 From => 'a@example.com',
24                 To => 'test@example.com',
25                 Subject => 'this is a subject',
26                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
27         ],
28         body => "hello world\n",
29 );
30
31 sub import_index_incremental {
32         my ($v, $level) = @_;
33         my $tmpdir = tempdir("pi-$this-tmp-XXXXXX", TMPDIR => 1, CLEANUP => 1);
34         my $ibx = PublicInbox::Inbox->new({
35                 mainrepo => "$tmpdir/testbox",
36                 name => "$this-$v",
37                 version => $v,
38                 -primary_address => 'test@example.com',
39                 indexlevel => $level,
40         });
41         my $cls = "PublicInbox::V${v}Writable";
42         use_ok $cls;
43         my $im = $cls->new($ibx, {nproc=>1});
44         $mime->header_set('Message-ID', '<m@1>');
45         ok($im->add($mime), 'first message added');
46         $im->done;
47
48         # index master (required for v1)
49         is(system($index, $ibx->{mainrepo}, "-L$level"), 0, 'index master OK');
50         my $ro_master = PublicInbox::Inbox->new({
51                 mainrepo => $ibx->{mainrepo},
52                 indexlevel => $level
53         });
54         my ($nr, $msgs) = $ro_master->recent;
55         is($nr, 1, 'only one message in master, so far');
56         is($msgs->[0]->{mid}, 'm@1', 'first message in master indexed');
57
58         # clone
59         my @cmd = (qw(git clone --mirror -q));
60         my $mirror = "$tmpdir/mirror-$v";
61         if ($v == 1) {
62                 push @cmd, $ibx->{mainrepo}, $mirror;
63         } else {
64                 push @cmd, "$ibx->{mainrepo}/git/0.git", "$mirror/git/0.git";
65         }
66         my $fetch_dir = $cmd[-1];
67         is(system(@cmd), 0, "v$v clone OK");
68
69         # inbox init
70         local $ENV{PI_CONFIG} = "$tmpdir/.picfg";
71         @cmd = ("$path/public-inbox-init", '-L', $level,
72                 'mirror', $mirror, '//example.com/test', 'test@example.com');
73         push @cmd, '-V2' if $v == 2;
74         is(system(@cmd), 0, "v$v init OK");
75
76         # index mirror
77         is(system($index, $mirror), 0, "v$v index mirror OK");
78
79         # read-only access
80         my $ro_mirror = PublicInbox::Inbox->new({
81                 mainrepo => $mirror,
82                 indexlevel => 'basic'
83         });
84         ($nr, $msgs) = $ro_mirror->recent;
85         is($nr, 1, 'only one message, so far');
86         is($msgs->[0]->{mid}, 'm@1', 'read first message');
87
88         # update master
89         $mime->header_set('Message-ID', '<m@2>');
90         ok($im->add($mime), '2nd message added');
91         $im->done;
92
93         # mirror updates
94         is(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
95         is(system($index, $mirror), 0, "v$v index mirror again OK");
96         ($nr, $msgs) = $ro_mirror->recent;
97         is($nr, 2, '2nd message seen in mirror');
98         is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
99                 ['m@1','m@2'], 'got both messages in mirror');
100
101         # incremental index master (required for v1)
102         is(system($index, $ibx->{mainrepo}, "-L$level"), 0, '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         # remove message from master
109         ok($im->remove($mime), '2nd message removed');
110         $im->done;
111
112         # sync the mirror
113         is(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
114         is(system($index, $mirror), 0, "v$v index mirror again OK");
115         ($nr, $msgs) = $ro_mirror->recent;
116         is($nr, 1, '2nd message gone from mirror');
117         is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
118                 'message unavailable in mirror');
119 }
120
121 # we can probably cull some other tests and put full/medium tests, here
122 for my $level (qw(basic)) {
123         for my $v (1..2) {
124                 subtest("v$v indexlevel=$level" => sub {
125                         import_index_incremental($v, $level);
126                 })
127         }
128 }
129
130 done_testing();