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