]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-mirror.t
daemon: unconditionally close Xapian shards on cleanup
[public-inbox.git] / t / lei-mirror.t
1 #!perl -w
2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict; use v5.10.1; use PublicInbox::TestCommon;
5 use PublicInbox::Inbox;
6 require_mods(qw(-httpd lei));
7 require_cmd('curl');
8 my $sock = tcp_server();
9 my ($tmpdir, $for_destroy) = tmpdir();
10 my $http = 'http://'.tcp_host_port($sock);
11 my ($ro_home, $cfg_path) = setup_public_inboxes;
12 my $cmd = [ qw(-httpd -W0 ./t/lei-mirror.psgi),
13         "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
14 my $td = start_script($cmd, { PI_CONFIG => $cfg_path }, { 3 => $sock });
15 test_lei({ tmpdir => $tmpdir }, sub {
16         my $home = $ENV{HOME};
17         my $t1 = "$home/t1-mirror";
18         lei_ok('add-external', $t1, '--mirror', "$http/t1/", \'--mirror v1');
19         ok(-f "$t1/public-inbox/msgmap.sqlite3", 't1-mirror indexed');
20         is(PublicInbox::Inbox::try_cat("$t1/description"),
21                 "mirror of $http/t1/\n", 'description set');
22         ok(-f "$t1/Makefile", 'convenience Makefile added (v1)');
23
24         lei_ok('ls-external');
25         like($lei_out, qr!\Q$t1\E!, 't1 added to ls-externals');
26
27         my $t2 = "$home/t2-mirror";
28         lei_ok('add-external', $t2, '--mirror', "$http/t2/", \'--mirror v2');
29         ok(-f "$t2/msgmap.sqlite3", 't2-mirror indexed');
30         ok(-f "$t2/description", 't2 description');
31         ok(-f "$t2/Makefile", 'convenience Makefile added (v2)');
32         is(PublicInbox::Inbox::try_cat("$t2/description"),
33                 "mirror of $http/t2/\n", 'description set');
34
35         lei_ok('ls-external');
36         like($lei_out, qr!\Q$t2\E!, 't2 added to ls-externals');
37
38         ok(!lei('add-external', $t2, '--mirror', "$http/t2/"),
39                 '--mirror fails if reused') or diag "$lei_err.$lei_out = $?";
40         like($lei_err, qr/\Q$t2\E' already exists/, 'destination in error');
41
42         ok(!lei('add-external', "$home/t2\nnewline", '--mirror', "$http/t2/"),
43                 '--mirror fails on newline');
44         like($lei_err, qr/`\\n' not allowed/, 'newline noted in error');
45
46         lei_ok('ls-external');
47         like($lei_out, qr!\Q$t2\E!, 'still in ls-externals');
48         unlike($lei_out, qr!\Qnewline\E!, 'newline entry not added');
49
50         ok(!lei('add-external', "$t2-fail", '-Lmedium'), '--mirror v2');
51         like($lei_err, qr/not a directory/, 'non-directory noted');
52         ok(!-d "$t2-fail", 'destination not created on failure');
53         lei_ok('ls-external');
54         unlike($lei_out, qr!\Q$t2-fail\E!, 'not added to ls-external');
55
56         lei_ok('add-external', "$t1-pfx", '--mirror', "$http/pfx/t1/",
57                         \'--mirror v1 w/ PSGI prefix');
58         ok(!-e "$t1-pfx/mirror.done", 'no leftover mirror.done');
59
60         my $d = "$home/404";
61         ok(!lei(qw(add-external --mirror), "$http/404", $d), 'mirror 404');
62         unlike($lei_err, qr!unlink.*?404/mirror\.done!,
63                 'no unlink failure message');
64         ok(!-d $d, "`404' dir not created");
65         lei_ok('ls-external');
66         unlike($lei_out, qr!\Q$d\E!s, 'not added to ls-external');
67
68         $d = "$home/bad-epoch";
69         ok(!lei(qw(add-external -q --epoch=0.. --mirror), "$http/t1/", $d),
70                 'v1 fails on --epoch');
71         ok(!-d $d, 'destination not created on unacceptable --epoch');
72         ok(!lei(qw(add-external -q --epoch=1 --mirror), "$http/t2/", $d),
73                 'v2 fails on bad epoch range');
74         ok(!-d $d, 'destination not created on bad epoch');
75
76         my %phail = (
77                 HTTPS => 'https://public-inbox.org/' . 'phail',
78                 ONION =>
79 'http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/' .
80 'phail,'
81         );
82         for my $t (qw(HTTPS ONION)) {
83         SKIP: {
84                 my $k = "TEST_LEI_EXTERNAL_$t";
85                 $ENV{$k} or skip "$k unset", 1;
86                 my $url = $phail{$t};
87                 my $dir = "phail-$t";
88                 ok(!lei(qw(add-external -Lmedium --mirror),
89                         $url, $dir), '--mirror non-existent v2');
90                 is($? >> 8, 22, 'curl 404');
91                 ok(!-d $dir, 'directory not created');
92                 unlike($lei_err, qr/# mirrored/, 'no success message');
93                 like($lei_err, qr/curl.*404/, "curl 404 shown for $k");
94         } # SKIP
95         } # for
96 });
97
98 SKIP: {
99         undef $sock;
100         my $d = "$tmpdir/d";
101         mkdir $d or xbail "mkdir $d $!";
102         my $opt = { -C => $d, 2 => \(my $err) };
103         ok(!run_script([qw(-clone -q), "$http/404"], undef, $opt), '404 fails');
104         ok(!-d "$d/404", 'destination not created');
105
106         ok(run_script([qw(-clone -q -C), $d, "$http/t2"], undef, $opt),
107                 '-clone succeeds on v2');
108         ok(-f "$d/t2/git/0.git/config", 'epoch cloned');
109
110         # writeBitmaps is the default for bare repos in git 2.22+,
111         # so we may stop setting it ourselves.
112         0 and is(xqx(['git', "--git-dir=$d/t2/git/0.git", 'config',
113                 qw(--bool repack.writeBitmaps)]), "true\n",
114                 'write bitmaps set (via include.path=all.git/config');
115
116         is(xqx(['git', "--git-dir=$d/t2/git/0.git", 'config',
117                 qw(include.path)]), "../../all.git/config\n",
118                 'include.path set');
119
120         ok(-s "$d/t2/all.git/objects/info/alternates",
121                 'all.git alternates created');
122         ok(-f "$d/t2/manifest.js.gz", 'manifest saved');
123         ok(!-e "$d/t2/mirror.done", 'no leftover mirror.done');
124         ok(!run_script([qw(-fetch --exit-code -C), "$d/t2"], undef, $opt),
125                 '-fetch succeeds w/ manifest.js.gz');
126         is($? >> 8, 127, '--exit-code gave 127');
127         unlike($err, qr/git --git-dir=\S+ fetch/, 'no fetch done w/ manifest');
128         unlink("$d/t2/manifest.js.gz") or xbail "unlink $!";
129         ok(!run_script([qw(-fetch --exit-code -C), "$d/t2"], undef, $opt),
130                 '-fetch succeeds w/o manifest.js.gz');
131         is($? >> 8, 127, '--exit-code gave 127');
132         like($err, qr/git --git-dir=\S+ fetch/, 'fetch forced w/o manifest');
133
134         ok(run_script([qw(-clone -q -C), $d, "$http/t1"], undef, $opt),
135                 'cloning v1 works');
136         ok(-d "$d/t1", 'v1 cloned');
137         ok(!-e "$d/t1/mirror.done", 'no leftover file');
138         ok(-f "$d/t1/manifest.js.gz", 'manifest saved');
139         ok(!run_script([qw(-fetch --exit-code -C), "$d/t1"], undef, $opt),
140                 'fetching v1 works');
141         is($? >> 8, 127, '--exit-code gave 127');
142         unlike($err, qr/git --git-dir=\S+ fetch/, 'no fetch done w/ manifest');
143         unlink("$d/t1/manifest.js.gz") or xbail "unlink $!";
144         my $before = [ glob("$d/t1/*") ];
145         ok(!run_script([qw(-fetch --exit-code -C), "$d/t1"], undef, $opt),
146                 'fetching v1 works w/o manifest.js.gz');
147         is($? >> 8, 127, '--exit-code gave 127');
148         unlink("$d/t1/FETCH_HEAD"); # git internal
149         like($err, qr/git --git-dir=\S+ fetch/, 'no fetch done w/ manifest');
150         ok(unlink("$d/t1/manifest.js.gz"), 'manifest created');
151         my $after = [ glob("$d/t1/*") ];
152         is_deeply($before, $after, 'no new files created');
153 }
154
155 ok($td->kill, 'killed -httpd');
156 $td->join;
157
158 {
159         require_ok 'PublicInbox::LeiMirror';
160         my $mrr = { src => 'https://example.com/src/', dst => $tmpdir };
161         my $exp = "mirror of https://example.com/src/\n";
162         my $f = "$tmpdir/description";
163         PublicInbox::LeiMirror::set_description($mrr);
164         is(PublicInbox::Inbox::try_cat($f), $exp, 'description set on ENOENT');
165
166         my $fh;
167         (open($fh, '>', $f) and close($fh)) or xbail $!;
168         PublicInbox::LeiMirror::set_description($mrr);
169         is(PublicInbox::Inbox::try_cat($f), $exp, 'description set on empty');
170         (open($fh, '>', $f) and print $fh "x\n" and close($fh)) or xbail $!;
171         is(PublicInbox::Inbox::try_cat($f), "x\n",
172                 'description preserved if non-default');
173 }
174
175 done_testing;