]> Sergey Matveev's repositories - public-inbox.git/blob - t/indexlevels-mirror.t
v1writable: retire in favor of InboxWritable
[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 './t/common.perl';
11 require_git(2.6);
12 my $this = (split('/', __FILE__))[-1];
13
14 foreach my $mod (qw(DBD::SQLite)) {
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 $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer;
43         $mime->header_set('Message-ID', '<m@1>');
44         ok($im->add($mime), 'first message added');
45         $im->done;
46
47         # index master (required for v1)
48         is(system($index, $ibx->{mainrepo}, "-L$level"), 0, 'index master OK');
49         my $ro_master = PublicInbox::Inbox->new({
50                 mainrepo => $ibx->{mainrepo},
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->{mainrepo}, $mirror;
62         } else {
63                 push @cmd, "$ibx->{mainrepo}/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 = ("$path/public-inbox-init", '-L', $level,
71                 'mirror', $mirror, '//example.com/test', 'test@example.com');
72         push @cmd, '-V2' if $v == 2;
73         is(system(@cmd), 0, "v$v init OK");
74
75         # index mirror
76         is(system($index, $mirror), 0, "v$v index mirror OK");
77
78         # read-only access
79         my $ro_mirror = PublicInbox::Inbox->new({
80                 mainrepo => $mirror,
81                 indexlevel => 'basic'
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         is(system($index, $mirror), 0, "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         is(system($index, $ibx->{mainrepo}, "-L$level"), 0, 'index master OK');
102         ($nr, $msgs) = $ro_master->recent;
103         is($nr, 2, '2nd message seen in master');
104         is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
105                 ['m@1','m@2'], 'got both messages in master');
106
107         # remove message from master
108         ok($im->remove($mime), '2nd message removed');
109         $im->done;
110
111         # sync the mirror
112         is(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
113         is(system($index, $mirror), 0, "v$v index mirror again OK");
114         ($nr, $msgs) = $ro_mirror->recent;
115         is($nr, 1, '2nd message gone from mirror');
116         is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
117                 'message unavailable in mirror');
118
119         if ($v == 2 && $level eq 'basic') {
120                 is_deeply([glob("$ibx->{mainrepo}/xap*/?/")], [],
121                          'no Xapian partition directories for v2 basic');
122         }
123 }
124
125 # we can probably cull some other tests and put full/medium tests, here
126 for my $level (qw(basic)) {
127         for my $v (1..2) {
128                 subtest("v$v indexlevel=$level" => sub {
129                         import_index_incremental($v, $level);
130                 })
131         }
132 }
133
134 done_testing();