lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / lei-mirror.t
blob76041b7358c76d64bd4c27f37bb3b21d5f4a044f
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12; use PublicInbox::TestCommon;
5 use PublicInbox::Inbox;
6 require_mods(qw(-httpd lei DBD::SQLite));
7 require_cmd('curl');
8 require_git_http_backend;
9 use PublicInbox::Spawn qw(which);
10 require PublicInbox::Msgmap;
11 my $sock = tcp_server();
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $http = 'http://'.tcp_host_port($sock);
14 my ($ro_home, $cfg_path) = setup_public_inboxes;
15 my $cmd = [ qw(-httpd -W0 ./t/lei-mirror.psgi),
16         "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
17 my $td = start_script($cmd, { PI_CONFIG => $cfg_path }, { 3 => $sock });
18 my %created;
19 test_lei({ tmpdir => $tmpdir }, sub {
20         my $home = $ENV{HOME};
21         my $t1 = "$home/t1-mirror";
22         my $mm_orig = "$ro_home/t1/public-inbox/msgmap.sqlite3";
23         $created{v1} = PublicInbox::Msgmap->new_file($mm_orig)->created_at;
24         lei_ok('add-external', $t1, '--mirror', "$http/t1/", \'--mirror v1');
25         my $mm_dup = "$t1/public-inbox/msgmap.sqlite3";
26         ok(-f $mm_dup, 't1-mirror indexed');
27         is(PublicInbox::IO::try_cat("$t1/description"),
28                 "mirror of $http/t1/\n", 'description set');
29         ok(-f "$t1/Makefile", 'convenience Makefile added (v1)');
30         SKIP: {
31                 my $make = require_cmd('make', 1);
32                 delete local @ENV{qw(MFLAGS MAKEFLAGS MAKELEVEL)};
33                 is(xsys([$make, 'help'], undef, { -C => $t1, 1 => \(my $help) }),
34                         0, "$make handled Makefile without errors");
35                 isnt($help, '', 'make help worked');
36         }
37         ok(-f "$t1/inbox.config.example", 'inbox.config.example downloaded');
38         is((stat(_))[9], $created{v1},
39                 'inbox.config.example mtime is ->created_at');
40         is((stat(_))[2] & 0222, 0, 'inbox.config.example not writable');
41         my $tb = PublicInbox::Msgmap->new_file($mm_dup)->created_at;
42         is($tb, $created{v1}, 'created_at matched in mirror');
44         lei_ok('ls-external');
45         like($lei_out, qr!\Q$t1\E!, 't1 added to ls-externals');
47         my $t2 = "$home/t2-mirror";
48         $mm_orig = "$ro_home/t2/msgmap.sqlite3";
49         $created{v2} = PublicInbox::Msgmap->new_file($mm_orig)->created_at;
50         lei_ok('add-external', $t2, '--mirror', "$http/t2/", \'--mirror v2');
51         $mm_dup = "$t2/msgmap.sqlite3";
52         ok(-f $mm_dup, 't2-mirror indexed');
53         ok(-f "$t2/description", 't2 description');
54         ok(-f "$t2/Makefile", 'convenience Makefile added (v2)');
55         is(PublicInbox::IO::try_cat("$t2/description"),
56                 "mirror of $http/t2/\n", 'description set');
57         $tb = PublicInbox::Msgmap->new_file($mm_dup)->created_at;
58         is($tb, $created{v2}, 'created_at matched in v2 mirror');
60         lei_ok('ls-external');
61         like($lei_out, qr!\Q$t2\E!, 't2 added to ls-externals');
63         ok(!lei('add-external', $t2, '--mirror', "$http/t2/"),
64                 '--mirror fails if reused') or diag "$lei_err.$lei_out = $?";
65         like($lei_err, qr/\Q$t2\E' already exists/, 'destination in error');
67         ok(!lei('add-external', "$home/t2\nnewline", '--mirror', "$http/t2/"),
68                 '--mirror fails on newline');
69         like($lei_err, qr/`\\n' not allowed/, 'newline noted in error');
71         lei_ok('ls-external');
72         like($lei_out, qr!\Q$t2\E!, 'still in ls-externals');
73         unlike($lei_out, qr!\Qnewline\E!, 'newline entry not added');
75         ok(!lei('add-external', "$t2-fail", '-Lmedium'), '--mirror v2');
76         like($lei_err, qr/not a directory/, 'non-directory noted');
77         ok(!-d "$t2-fail", 'destination not created on failure');
78         lei_ok('ls-external');
79         unlike($lei_out, qr!\Q$t2-fail\E!, 'not added to ls-external');
81         lei_ok('add-external', "$t1-pfx", '--mirror', "$http/pfx/t1/",
82                         \'--mirror v1 w/ PSGI prefix');
83         ok(!-e "$t1-pfx/mirror.done", 'no leftover mirror.done');
85         my $d = "$home/404";
86         ok(!lei(qw(add-external --mirror), "$http/404", $d), 'mirror 404');
87         unlike($lei_err, qr!unlink.*?404/mirror\.done!,
88                 'no unlink failure message');
89         ok(!-d $d, "`404' dir not created");
90         lei_ok('ls-external');
91         unlike($lei_out, qr!\Q$d\E!s, 'not added to ls-external');
93         $d = "$home/bad-epoch";
94         ok(!lei(qw(add-external -q --epoch=0.. --mirror), "$http/t1/", $d),
95                 'v1 fails on --epoch');
96         ok(!-d $d, 'destination not created on unacceptable --epoch');
97         ok(!lei(qw(add-external -q --epoch=1 --mirror), "$http/t2/", $d),
98                 'v2 fails on bad epoch range');
99         ok(!-d $d, 'destination not created on bad epoch');
101         my %phail = (
102                 HTTPS => 'https://public-inbox.org/' . 'phail',
103                 ONION =>
104 'http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/' .
105 'phail,'
106         );
107         for my $t (qw(HTTPS ONION)) {
108         SKIP: {
109                 my $k = "TEST_LEI_EXTERNAL_$t";
110                 $ENV{$k} or skip "$k unset", 1;
111                 my $url = $phail{$t};
112                 my $dir = "phail-$t";
113                 ok(!lei(qw(add-external -Lmedium --mirror),
114                         $url, $dir), '--mirror non-existent v2');
115                 is($? >> 8, 22, 'curl 404');
116                 ok(!-d $dir, 'directory not created');
117                 unlike($lei_err, qr/# mirrored/, 'no success message');
118                 like($lei_err, qr/curl.*404/, "curl 404 shown for $k");
119         } # SKIP
120         } # for
123 SKIP: {
124         undef $sock;
125         my $d = "$tmpdir/d";
126         mkdir $d or xbail "mkdir $d $!";
127         my $opt = { -C => $d, 2 => \(my $err) };
128         ok(!run_script([qw(-clone -q), "$http/404"], undef, $opt), '404 fails');
129         ok(!-d "$d/404", 'destination not created');
131         ok(run_script([qw(-clone -q -C), $d, "$http/t2"], undef, $opt),
132                 '-clone succeeds on v2');
133         ok(-f "$d/t2/git/0.git/config", 'epoch cloned');
135         # writeBitmaps is the default for bare repos in git 2.22+,
136         # so we may stop setting it ourselves.
137         0 and is(xqx(['git', "--git-dir=$d/t2/git/0.git", 'config',
138                 qw(--bool repack.writeBitmaps)]), "true\n",
139                 'write bitmaps set (via include.path=all.git/config');
141         is(xqx(['git', "--git-dir=$d/t2/git/0.git", 'config',
142                 qw(include.path)]), "../../all.git/config\n",
143                 'include.path set');
145         ok(-s "$d/t2/all.git/objects/info/alternates",
146                 'all.git alternates created');
147         ok(-f "$d/t2/manifest.js.gz", 'manifest saved');
148         ok(!-e "$d/t2/mirror.done", 'no leftover mirror.done');
149         ok(!run_script([qw(-fetch --exit-code -C), "$d/t2"], undef, $opt),
150                 '-fetch succeeds w/ manifest.js.gz');
151         is($? >> 8, 127, '--exit-code gave 127');
152         unlike($err, qr/git --git-dir=\S+ fetch/, 'no fetch done w/ manifest');
153         unlink("$d/t2/manifest.js.gz") or xbail "unlink $!";
154         ok(!run_script([qw(-fetch --exit-code -C), "$d/t2"], undef, $opt),
155                 '-fetch succeeds w/o manifest.js.gz');
156         is($? >> 8, 127, '--exit-code gave 127');
157         like($err, qr/git --git-dir=\S+ fetch/, 'fetch forced w/o manifest');
159         ok(run_script([qw(-clone -q -C), $d, "$http/t1"], undef, $opt),
160                 'cloning v1 works');
161         ok(-d "$d/t1", 'v1 cloned');
162         ok(!-e "$d/t1/mirror.done", 'no leftover file');
163         ok(-f "$d/t1/manifest.js.gz", 'manifest saved');
164         ok(!run_script([qw(-fetch --exit-code -C), "$d/t1"], undef, $opt),
165                 'fetching v1 works');
166         is($? >> 8, 127, '--exit-code gave 127');
167         unlike($err, qr/git --git-dir=\S+ fetch/, 'no fetch done w/ manifest');
168         unlink("$d/t1/manifest.js.gz") or xbail "unlink $!";
169         my $before = [ glob("$d/t1/*") ];
170         ok(!run_script([qw(-fetch --exit-code -C), "$d/t1"], undef, $opt),
171                 'fetching v1 works w/o manifest.js.gz');
172         is($? >> 8, 127, '--exit-code gave 127');
173         unlink("$d/t1/FETCH_HEAD"); # git internal
174         like($err, qr/git --git-dir=\S+ fetch/, 'no fetch done w/ manifest');
175         ok(unlink("$d/t1/manifest.js.gz"), 'manifest created');
176         my $after = [ glob("$d/t1/*") ];
177         is_deeply($before, $after, 'no new files created');
179         local $ENV{HOME} = $tmpdir;
180         ok(run_script([qw(-index -Lbasic), "$d/t1"]), 'index v1');
181         ok(run_script([qw(-index -Lbasic), "$d/t2"]), 'index v2');
183         SKIP: {
184                 join('', sort(keys %created)) eq 'v1v2' or
185                         skip "lei didn't run", 2;
186                 my $f = "$d/t1/public-inbox/msgmap.sqlite3";
187                 my $ca = PublicInbox::Msgmap->new_file($f)->created_at;
188                 is($ca, $created{v1}, 'clone + index v1 synced ->created_at');
190                 $f = "$d/t2/msgmap.sqlite3";
191                 $ca = PublicInbox::Msgmap->new_file($f)->created_at;
192                 is($ca, $created{v2}, 'clone + index v2 synced ->created_at');
193         }
194         test_lei(sub {
195                 lei_ok qw(inspect num:1 --dir), "$d/t1";
196                 ok(ref(json_utf8->decode($lei_out)), 'inspect num: on v1');
197                 lei_ok qw(inspect num:1 --dir), "$d/t2";
198                 ok(ref(json_utf8->decode($lei_out)), 'inspect num: on v2');
199         });
202 ok($td->kill, 'killed -httpd');
203 $td->join;
206         require_ok 'PublicInbox::LeiMirror';
207         my $mrr = { src => 'https://example.com/src/', dst => $tmpdir };
208         my $exp = "mirror of https://example.com/src/\n";
209         my $f = "$tmpdir/description";
210         PublicInbox::LeiMirror::set_description($mrr);
211         is(PublicInbox::IO::try_cat($f), $exp, 'description set on ENOENT');
213         my $fh;
214         (open($fh, '>', $f) and close($fh)) or xbail $!;
215         PublicInbox::LeiMirror::set_description($mrr);
216         is(PublicInbox::IO::try_cat($f), $exp, 'description set on empty');
217         (open($fh, '>', $f) and print $fh "x\n" and close($fh)) or xbail $!;
218         is(PublicInbox::IO::try_cat($f), "x\n",
219                 'description preserved if non-default');
222 done_testing;