]> Sergey Matveev's repositories - public-inbox.git/blob - t/convert-compact.t
tests: consistently check for xapian-compact
[public-inbox.git] / t / convert-compact.t
1 # Copyright (C) 2018-2020 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::Eml;
7 use PublicInbox::TestCommon;
8 require_git(2.6);
9 require_mods(qw(DBD::SQLite Search::Xapian));
10 have_xapian_compact or
11         plan skip_all => 'xapian-compact missing for '.__FILE__;
12
13 use_ok 'PublicInbox::V2Writable';
14 use PublicInbox::Import;
15 my ($tmpdir, $for_destroy) = tmpdir();
16 my $ibx = {
17         inboxdir => "$tmpdir/v1",
18         name => 'test-v1',
19         -primary_address => 'test@example.com',
20 };
21
22 PublicInbox::Import::init_bare($ibx->{inboxdir});
23 ok(umask(077), 'set restrictive umask');
24 ok(PublicInbox::Import::run_die([qw(git) , "--git-dir=$ibx->{inboxdir}",
25         qw(config core.sharedRepository 0644)]), 'set sharedRepository');
26 $ibx = PublicInbox::Inbox->new($ibx);
27 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
28 my $mime = PublicInbox::Eml->new(<<'EOF');
29 From: a@example.com
30 To: b@example.com
31 Subject: this is a subject
32 Message-ID: <a-mid@b>
33 Date: Fri, 02 Oct 1993 00:00:00 +0000
34
35 hello world
36 EOF
37
38 ok($im->add($mime), 'added one message');
39 ok($im->remove($mime), 'remove message');
40 ok($im->add($mime), 'added message again');
41 $im->done;
42 for (1..2) {
43         eval { PublicInbox::SearchIdx->new($ibx, 1)->index_sync; };
44         is($@, '', 'no errors syncing');
45 }
46
47 is(((stat("$ibx->{inboxdir}/public-inbox"))[2]) & 07777, 0755,
48         'sharedRepository respected for v1');
49 is(((stat("$ibx->{inboxdir}/public-inbox/msgmap.sqlite3"))[2]) & 07777, 0644,
50         'sharedRepository respected for v1 msgmap');
51 my @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*/*");
52 foreach (@xdir) {
53         my @st = stat($_);
54         is($st[2] & 07777, -f _ ? 0644 : 0755,
55                 'sharedRepository respected on file after convert');
56 }
57
58 local $ENV{PI_CONFIG} = '/dev/null';
59 my ($out, $err) = ('', '');
60 my $rdr = { 1 => \$out, 2 => \$err };
61
62 my $cmd = [ '-compact', $ibx->{inboxdir} ];
63 ok(run_script($cmd, undef, $rdr), 'v1 compact works');
64
65 @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*");
66 is(scalar(@xdir), 1, 'got one xapian directory after compact');
67 is(((stat($xdir[0]))[2]) & 07777, 0755,
68         'sharedRepository respected on v1 compact');
69
70 my $hwm = do {
71         my $mm = $ibx->mm;
72         $ibx->cleanup;
73         $mm->num_highwater;
74 };
75 ok(defined($hwm) && $hwm > 0, "highwater mark set #$hwm");
76
77 $cmd = [ '-convert', '--no-index', $ibx->{inboxdir}, "$tmpdir/no-index" ];
78 ok(run_script($cmd, undef, $rdr), 'convert --no-index works');
79
80 $cmd = [ '-convert', $ibx->{inboxdir}, "$tmpdir/x/v2" ];
81 ok(run_script($cmd, undef, $rdr), 'convert works');
82 @xdir = glob("$tmpdir/x/v2/xap*/*");
83 foreach (@xdir) {
84         my @st = stat($_);
85         is($st[2] & 07777, -f _ ? 0644 : 0755,
86                 'sharedRepository respected after convert');
87 }
88
89 $cmd = [ '-compact', "$tmpdir/x/v2" ];
90 my $env = { NPROC => 2 };
91 ok(run_script($cmd, $env, $rdr), 'v2 compact works');
92 $ibx->{inboxdir} = "$tmpdir/x/v2";
93 $ibx->{version} = 2;
94 is($ibx->mm->num_highwater, $hwm, 'highwater mark unchanged in v2 inbox');
95
96 @xdir = glob("$tmpdir/x/v2/xap*/*");
97 foreach (@xdir) {
98         my @st = stat($_);
99         is($st[2] & 07777, -f _ ? 0644 : 0755,
100                 'sharedRepository respected after v2 compact');
101 }
102 is(((stat("$tmpdir/x/v2/msgmap.sqlite3"))[2]) & 07777, 0644,
103         'sharedRepository respected for v2 msgmap');
104
105 @xdir = (glob("$tmpdir/x/v2/git/*.git/objects/*/*"),
106          glob("$tmpdir/x/v2/git/*.git/objects/pack/*"));
107 foreach (@xdir) {
108         my @st = stat($_);
109         is($st[2] & 07777, -f _ ? 0444 : 0755,
110                 'sharedRepository respected after v2 compact');
111 }
112 my $msgs = $ibx->recent({limit => 1000});
113 is($msgs->[0]->{mid}, 'a-mid@b', 'message exists in history');
114 is(scalar @$msgs, 1, 'only one message in history');
115
116 $ibx = undef;
117 $err = '';
118 $cmd = [ qw(-index -j0 --reindex -c), "$tmpdir/x/v2" ];
119 ok(run_script($cmd, undef, $rdr), '--reindex -c');
120 like($err, qr/xapian-compact/, 'xapian-compact ran (-c)');
121
122 $rdr->{2} = \(my $err2 = '');
123 $cmd = [ qw(-index -j0 --reindex -cc), "$tmpdir/x/v2" ];
124 ok(run_script($cmd, undef, $rdr), '--reindex -c -c');
125 like($err2, qr/xapian-compact/, 'xapian-compact ran (-c -c)');
126 ok(($err2 =~ tr/\n/\n/) > ($err =~ tr/\n/\n/), '-compacted twice');
127
128 done_testing();