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