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));
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)');
24 lei_ok('ls-external');
25 like($lei_out, qr!\Q$t1\E!, 't1 added to ls-externals');
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');
35 lei_ok('ls-external');
36 like($lei_out, qr!\Q$t2\E!, 't2 added to ls-externals');
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');
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');
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');
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');
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');
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');
69 HTTPS => 'https://public-inbox.org/' . 'phail',
71 'http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/' .
74 for my $t (qw(HTTPS ONION)) {
76 my $k = "TEST_LEI_EXTERNAL_$t";
77 $ENV{$k} or skip "$k unset", 1;
80 ok(!lei(qw(add-external -Lmedium --mirror),
81 $url, $dir), '--mirror non-existent v2');
82 is($? >> 8, 22, 'curl 404');
83 ok(!-d $dir, 'directory not created');
84 unlike($lei_err, qr/# mirrored/, 'no success message');
85 like($lei_err, qr/curl.*404/, "curl 404 shown for $k");
93 mkdir $d or xbail "mkdir $d $!";
94 my $opt = { -C => $d, 2 => \(my $err) };
95 ok(!run_script([qw(-clone -q), "$http/404"], undef, $opt), '404 fails');
96 ok(!-d "$d/404", 'destination not created');
98 ok(run_script([qw(-clone -q -C), $d, "$http/t2"], undef, $opt),
99 '-clone succeeds on v2');
100 ok(-f "$d/t2/git/0.git/config", 'epoch cloned');
102 # writeBitmaps is the default for bare repos in git 2.22+,
103 # so we may stop setting it ourselves.
104 0 and is(xqx(['git', "--git-dir=$d/t2/git/0.git", 'config',
105 qw(--bool repack.writeBitmaps)]), "true\n",
106 'write bitmaps set (via include.path=all.git/config');
108 is(xqx(['git', "--git-dir=$d/t2/git/0.git", 'config',
109 qw(include.path)]), "../../all.git/config\n",
112 ok(-s "$d/t2/all.git/objects/info/alternates",
113 'all.git alternates created');
114 ok(-f "$d/t2/manifest.js.gz", 'manifest saved');
115 ok(!-e "$d/t2/mirror.done", 'no leftover mirror.done');
116 ok(!run_script([qw(-fetch --exit-code -C), "$d/t2"], undef, $opt),
117 '-fetch succeeds w/ manifest.js.gz');
118 is($? >> 8, 127, '--exit-code gave 127');
119 unlike($err, qr/git --git-dir=\S+ fetch/, 'no fetch done w/ manifest');
120 unlink("$d/t2/manifest.js.gz") or xbail "unlink $!";
121 ok(!run_script([qw(-fetch --exit-code -C), "$d/t2"], undef, $opt),
122 '-fetch succeeds w/o manifest.js.gz');
123 is($? >> 8, 127, '--exit-code gave 127');
124 like($err, qr/git --git-dir=\S+ fetch/, 'fetch forced w/o manifest');
126 ok(run_script([qw(-clone -q -C), $d, "$http/t1"], undef, $opt),
128 ok(-d "$d/t1", 'v1 cloned');
129 ok(!-e "$d/t1/mirror.done", 'no leftover file');
130 ok(-f "$d/t1/manifest.js.gz", 'manifest saved');
131 ok(!run_script([qw(-fetch --exit-code -C), "$d/t1"], undef, $opt),
132 'fetching v1 works');
133 is($? >> 8, 127, '--exit-code gave 127');
134 unlike($err, qr/git --git-dir=\S+ fetch/, 'no fetch done w/ manifest');
135 unlink("$d/t1/manifest.js.gz") or xbail "unlink $!";
136 my $before = [ glob("$d/t1/*") ];
137 ok(!run_script([qw(-fetch --exit-code -C), "$d/t1"], undef, $opt),
138 'fetching v1 works w/o manifest.js.gz');
139 is($? >> 8, 127, '--exit-code gave 127');
140 unlink("$d/t1/FETCH_HEAD"); # git internal
141 like($err, qr/git --git-dir=\S+ fetch/, 'no fetch done w/ manifest');
142 ok(unlink("$d/t1/manifest.js.gz"), 'manifest created');
143 my $after = [ glob("$d/t1/*") ];
144 is_deeply($before, $after, 'no new files created');
147 ok($td->kill, 'killed -httpd');
151 require_ok 'PublicInbox::LeiMirror';
152 my $mrr = { src => 'https://example.com/src/', dst => $tmpdir };
153 my $exp = "mirror of https://example.com/src/\n";
154 my $f = "$tmpdir/description";
155 PublicInbox::LeiMirror::set_description($mrr);
156 is(PublicInbox::Inbox::try_cat($f), $exp, 'description set on ENOENT');
159 (open($fh, '>', $f) and close($fh)) or xbail $!;
160 PublicInbox::LeiMirror::set_description($mrr);
161 is(PublicInbox::Inbox::try_cat($f), $exp, 'description set on empty');
162 (open($fh, '>', $f) and print $fh "x\n" and close($fh)) or xbail $!;
163 is(PublicInbox::Inbox::try_cat($f), "x\n",
164 'description preserved if non-default');