2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # manifest.js.gz generation and grok-pull integration test
5 use strict; use v5.10.1; use PublicInbox::TestCommon;
6 use PublicInbox::Import;
7 use IO::Uncompress::Gunzip qw(gunzip);
8 require_mods(qw(json URI::Escape Plack::Builder Digest::SHA HTTP::Tiny));
9 require PublicInbox::WwwListing;
10 require PublicInbox::ManifestJsGz;
11 use PublicInbox::Config;
12 my $json = PublicInbox::Config::json();
14 use_ok 'PublicInbox::Git';
16 my ($tmpdir, $for_destroy) = tmpdir();
17 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
18 PublicInbox::Import::init_bare($bare->{git_dir});
19 is($bare->manifest_entry, undef, 'empty repo has no manifest entry');
21 my $fi_data = './t/git.fast-import-data';
22 open my $fh, '<', $fi_data or die "open $fi_data: $!";
23 my $env = { GIT_DIR => $bare->{git_dir} };
24 is(xsys([qw(git fast-import --quiet)], $env, { 0 => $fh }), 0,
28 like($bare->manifest_entry->{fingerprint}, qr/\A[a-f0-9]{40}\z/,
29 'got fingerprint with non-empty repo');
32 my ($json, $host, $port, $html) = @_;
34 my $http = HTTP::Tiny->new;
36 $res = $http->get("http://$host:$port/");
37 is($res->{status}, 200, 'got HTML listing');
38 like($res->{content}, qr!</html>!si, 'listing looks like HTML');
40 $res = $http->get("http://$host:$port/",
41 {'Accept-Encoding'=>'gzip'});
42 is($res->{status}, 200, 'got gzipped HTML listing');
43 gunzip(\(delete $res->{content}) => \$tmp);
44 like($tmp, qr!</html>!si, 'unzipped listing looks like HTML');
46 $res = $http->get("http://$host:$port/manifest.js.gz");
47 is($res->{status}, 200, 'got manifest');
48 gunzip(\(delete $res->{content}) => \$tmp);
49 unlike($tmp, qr/"modified":\s*"/, 'modified is an integer');
50 my $manifest = $json->decode($tmp);
51 ok(my $clone = $manifest->{'/alt'}, '/alt in manifest');
52 is($clone->{owner}, "lorelei \x{100}", 'owner set');
53 is($clone->{reference}, '/bare', 'reference detected');
54 is($clone->{description}, "we're \x{100}ll clones", 'description read');
55 ok(my $bare = $manifest->{'/bare'}, '/bare in manifest');
56 is($bare->{description}, 'Unnamed repository',
57 'missing $GIT_DIR/description fallback');
59 like($bare->{fingerprint}, qr/\A[a-f0-9]{40}\z/, 'fingerprint');
60 is($clone->{fingerprint}, $bare->{fingerprint}, 'fingerprint matches');
61 is(HTTP::Date::time2str($bare->{modified}),
62 $res->{headers}->{'last-modified'},
63 'modified field and Last-Modified header match');
65 ok(my $v2epoch0 = $manifest->{'/v2/git/0.git'}, 'v2 epoch 0 appeared');
66 like($v2epoch0->{description}, qr/ \[epoch 0\]\z/,
67 'epoch 0 in description');
68 ok(my $v2epoch1 = $manifest->{'/v2/git/1.git'}, 'v2 epoch 1 appeared');
69 like($v2epoch1->{description}, qr/ \[epoch 1\]\z/,
70 'epoch 1 in description');
72 $res = $http->get("http://$host:$port/alt/description");
73 is($res->{content}, "we're \xc4\x80ll clones\n", 'UTF-8 description')
74 or diag explain($res);
79 my $err = "$tmpdir/stderr.log";
80 my $out = "$tmpdir/stdout.log";
81 my $alt = "$tmpdir/alt.git";
82 my $cfgfile = "$tmpdir/config";
83 my $v2 = "$tmpdir/v2";
84 my $sock = tcp_server();
85 my ($host, $port) = tcp_host_port($sock);
86 my @clone = qw(git clone -q -s --bare);
87 is(xsys(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
89 PublicInbox::Import::init_bare("$v2/all.git");
91 is(xsys(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i")
93 ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
94 open $fh, '>', "$alt/description" or xbail "open $alt/description $!";
95 print $fh "we're \xc4\x80ll clones\n" or xbail "print $!";
96 close $fh or xbail "write: $alt/description $!";
97 is(xsys('git', "--git-dir=$alt", qw(config gitweb.owner),
98 "lorelei \xc4\x80"), 0,
100 open $fh, '>', $cfgfile or xbail "open $cfgfile: $!";
102 print $fh <<"" or xbail "print $!";
104 inboxdir = $bare->{git_dir}
105 url = http://$host/bare
106 address = bare\@example.com
109 url = http://$host/alt
110 address = alt\@example.com
113 url = http://$host/v2
114 address = v2\@example.com
116 my $env = { PI_CONFIG => $cfgfile };
117 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
118 my $psgi = "$tmpdir/pfx.psgi";
120 open my $psgi_fh, '>', $psgi or xbail "open: $!";
121 print $psgi_fh <<'EOM' or xbail "print $!";
122 use PublicInbox::WWW;
124 my $www = PublicInbox::WWW->new;
127 mount '/pfx/' => sub { $www->call(@_) }
130 close $psgi_fh or xbail "close: $!";
133 # ensure prefixed mount full clones work:
134 $td = start_script([@$cmd, $psgi], $env, { 3 => $sock });
135 my $opt = { 2 => \(my $clone_err = '') };
136 ok(run_script(['-clone', "http://$host:$port/pfx", "$tmpdir/pfx" ],
137 undef, $opt), 'pfx clone w/pfx') or diag "clone_err=$clone_err";
139 open my $mh, '<', "$tmpdir/pfx/manifest.js.gz" or xbail "open: $!";
140 gunzip(\(do { local $/; <$mh> }) => \(my $mjs = ''));
141 my $mf = $json->decode($mjs);
142 is_deeply([sort keys %$mf], [ qw(/alt /bare /v2/git/0.git
143 /v2/git/1.git /v2/git/2.git) ],
145 for (keys %$mf) { ok(-d "$tmpdir/pfx$_", "pfx/$_ cloned") }
148 ok(run_script(['-clone', '--include=*/alt',
149 "http://$host:$port/pfx", "$tmpdir/incl" ],
150 undef, $opt), 'clone w/include') or diag "clone_err=$clone_err";
151 ok(-d "$tmpdir/incl/alt", 'alt cloned');
152 ok(!-d "$tmpdir/incl/v2" && !-d "$tmpdir/incl/bare", 'only alt cloned');
153 is(xqx([qw(git config -f), "$tmpdir/incl/alt/config", 'gitweb.owner']),
154 "lorelei \xc4\x80\n", 'gitweb.owner set by -clone');
157 ok(run_script(['-clone', '--dry-run',
158 "http://$host:$port/pfx", "$tmpdir/dry-run" ],
159 undef, $opt), 'clone --dry-run') or diag "clone_err=$clone_err";
160 ok(!-d "$tmpdir/dry-run", 'nothing cloned with --dry-run');
164 open $mh, '<', "$tmpdir/incl/manifest.js.gz" or xbail "open: $!";
165 gunzip(\(do { local $/; <$mh> }) => \($mjs = ''));
166 $mf = $json->decode($mjs);
167 is_deeply([keys %$mf], [ '/alt' ], 'excluded keys skipped in manifest');
169 $td = start_script($cmd, $env, { 3 => $sock });
171 # default publicinboxGrokManifest match=domain default
172 tiny_test($json, $host, $port);
174 # normal full clone on /
176 ok(run_script(['-clone', "http://$host:$port/", "$tmpdir/full" ],
177 undef, $opt), 'full clone') or diag "clone_err=$clone_err";
178 ok(-d "$tmpdir/full/$_", "$_ cloned") for qw(alt v2 bare);
182 print $fh <<"" or xbail "print $!";
186 close $fh or xbail "close $!";
187 $td = start_script($cmd, $env, { 3 => $sock });
189 tiny_test($json, $host, $port, 1);
191 # grok-pull sleeps a long while some places:
192 # https://lore.kernel.org/tools/20211013110344.GA10632@dcvr/
193 skip 'TEST_GROK unset', 12 unless $ENV{TEST_GROK};
194 my $grok_pull = require_cmd('grok-pull', 1) or
195 skip('grok-pull not available', 12);
196 my ($grok_version) = (xqx([$grok_pull, "--version"])
197 =~ /(\d+)\.(?:\d+)(?:\.(\d+))?/);
198 $grok_version >= 2 or
199 skip('grok-pull v2 or later not available', 12);
200 my $grok_loglevel = $ENV{TEST_GROK_LOGLEVEL} // 'info';
202 ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
203 my $tail = tail_f("$tmpdir/grok.log");
204 open $fh, '>', "$tmpdir/repos.conf" or xbail $!;
205 print $fh <<"" or xbail $!;
207 toplevel = $tmpdir/mirror
208 manifest = $tmpdir/local-manifest.js.gz
209 log = $tmpdir/grok.log
210 loglevel = $grok_loglevel
212 site = http://$host:$port
213 manifest = \${site}/manifest.js.gz
217 close $fh or xbail $!;
218 xsys($grok_pull, '-c', "$tmpdir/repos.conf");
219 is($? >> 8, 0, 'grok-pull exit code as expected');
220 for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
221 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
224 # support per-inbox manifests, handy for v2:
225 # /$INBOX/v2/manifest.js.gz
226 open $fh, '>', "$tmpdir/per-inbox.conf" or xbail $!;
227 print $fh <<"" or xbail $!;
229 toplevel = $tmpdir/per-inbox
230 manifest = $tmpdir/per-inbox-manifest.js.gz
231 log = $tmpdir/grok.log
232 loglevel = $grok_loglevel
234 site = http://$host:$port
235 manifest = \${site}/v2/manifest.js.gz
239 close $fh or xbail $!;
240 ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
241 xsys($grok_pull, '-c', "$tmpdir/per-inbox.conf");
242 is($? >> 8, 0, 'grok-pull exit code as expected');
243 for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
244 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
248 is($?, 0, 'no error in exited process');
249 open $fh, '<', $err or BAIL_OUT("open $err failed: $!");
250 my $eout = do { local $/; <$fh> };
251 unlike($eout, qr/wide/i, 'no Wide character warnings');
252 unlike($eout, qr/uninitialized/i, 'no uninitialized warnings');