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 v5.12; use PublicInbox::TestCommon;
6 use PublicInbox::Import;
7 use IO::Uncompress::Gunzip qw(gunzip);
8 require_mods qw(json URI::Escape psgi -httpd HTTP::Tiny);
9 my $curl = require_cmd 'curl';
10 require PublicInbox::WwwListing;
11 require PublicInbox::ManifestJsGz;
12 require PublicInbox::Git;
13 require PublicInbox::Config;
14 my $json = PublicInbox::Config::json();
15 use autodie qw(open close mkdir);
18 my ($tmpdir, $for_destroy) = tmpdir();
19 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
20 PublicInbox::Import::init_bare($bare->{git_dir});
21 is($bare->manifest_entry, undef, 'empty repo has no manifest entry');
23 my $fi_data = './t/git.fast-import-data';
24 open my $fh, '<', $fi_data;
25 my $env = { GIT_DIR => $bare->{git_dir} };
26 xsys_e [qw(git fast-import --quiet)], $env, { 0 => $fh };
29 like($bare->manifest_entry->{fingerprint}, qr/\A[a-f0-9]{40}\z/,
30 'got fingerprint with non-empty repo');
33 my ($json, $host, $port, $html) = @_;
35 my $http = HTTP::Tiny->new;
37 $res = $http->get("http://$host:$port/");
38 is($res->{status}, 200, 'got HTML listing');
39 like($res->{content}, qr!</html>!si, 'listing looks like HTML');
41 $res = $http->get("http://$host:$port/",
42 {'Accept-Encoding'=>'gzip'});
43 is($res->{status}, 200, 'got gzipped HTML listing');
44 gunzip(\(delete $res->{content}) => \$tmp);
45 like($tmp, qr!</html>!si, 'unzipped listing looks like HTML');
47 $res = $http->get("http://$host:$port/manifest.js.gz");
48 is($res->{status}, 200, 'got manifest');
49 gunzip(\(delete $res->{content}) => \$tmp);
50 unlike($tmp, qr/"modified":\s*"/, 'modified is an integer');
51 my $manifest = $json->decode($tmp);
52 ok(my $clone = $manifest->{'/alt'}, '/alt in manifest');
53 is($clone->{owner}, "lorelei \x{100}", 'owner set');
54 is($clone->{reference}, '/bare', 'reference detected');
55 is($clone->{description}, "we're \x{100}ll clones", 'description read');
56 ok(my $bare = $manifest->{'/bare'}, '/bare in manifest');
57 is($bare->{description}, 'Unnamed repository',
58 'missing $GIT_DIR/description fallback');
60 like($bare->{fingerprint}, qr/\A[a-f0-9]{40}\z/, 'fingerprint');
61 is($clone->{fingerprint}, $bare->{fingerprint}, 'fingerprint matches');
62 is(HTTP::Date::time2str($bare->{modified}),
63 $res->{headers}->{'last-modified'},
64 'modified field and Last-Modified header match');
66 ok(my $v2epoch0 = $manifest->{'/v2/git/0.git'}, 'v2 epoch 0 appeared');
67 like($v2epoch0->{description}, qr/ \[epoch 0\]\z/,
68 'epoch 0 in description');
69 ok(my $v2epoch1 = $manifest->{'/v2/git/1.git'}, 'v2 epoch 1 appeared');
70 like($v2epoch1->{description}, qr/ \[epoch 1\]\z/,
71 'epoch 1 in description');
73 $res = $http->get("http://$host:$port/alt/description");
74 is($res->{content}, "we're \xc4\x80ll clones\n", 'UTF-8 description')
75 or diag explain($res);
80 require_git_http_backend 1;
81 my $err = "$tmpdir/stderr.log";
82 my $out = "$tmpdir/stdout.log";
83 my $alt = "$tmpdir/alt.git";
84 my $cfgfile = "$tmpdir/config";
85 my $v2 = "$tmpdir/v2";
86 my $sock = tcp_server();
87 my ($host, $port) = tcp_host_port($sock);
88 my @clone = qw(git clone -q -s --bare);
89 xsys_e @clone, $bare->{git_dir}, $alt;
91 PublicInbox::Import::init_bare("$v2/all.git");
93 xsys_e @clone, $alt, "$v2/git/$i.git";
95 open my $fh, '>', "$v2/inbox.lock";
96 open $fh, '>', "$v2/description";
97 print $fh "a v2 inbox\n";
100 open $fh, '>', "$alt/description";
101 print $fh "we're \xc4\x80ll clones\n";
103 xsys_e 'git', "--git-dir=$alt", qw(config gitweb.owner),
105 open $fh, '>', $cfgfile;
108 inboxdir = $bare->{git_dir}
109 url = http://$host/bare
110 address = bare\@example.com
113 url = http://$host/alt
114 address = alt\@example.com
117 url = http://$host/v2
118 address = v2\@example.com
122 my $env = { PI_CONFIG => $cfgfile };
123 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
124 my $psgi = "$tmpdir/pfx.psgi";
126 open my $psgi_fh, '>', $psgi;
127 print $psgi_fh <<'EOM';
128 use PublicInbox::WWW;
130 my $www = PublicInbox::WWW->new;
133 mount '/pfx/' => sub { $www->call(@_) }
139 # ensure prefixed mount full clones work:
140 $td = start_script([@$cmd, $psgi], $env, { 3 => $sock });
141 my $opt = { 2 => \(my $clone_err = '') };
142 ok(run_script(['-clone', "http://$host:$port/pfx", "$tmpdir/pfx" ],
143 undef, $opt), 'pfx clone w/pfx') or diag "clone_err=$clone_err";
145 open my $mh, '<', "$tmpdir/pfx/manifest.js.gz";
146 gunzip(\(do { local $/; <$mh> }) => \(my $mjs = ''));
147 my $mf = $json->decode($mjs);
148 is_deeply([sort keys %$mf], [ qw(/alt /bare /v2/git/0.git
149 /v2/git/1.git /v2/git/2.git) ],
151 for (keys %$mf) { ok(-d "$tmpdir/pfx$_", "pfx/$_ cloned") }
152 open my $desc, '<', "$tmpdir/pfx/v2/description";
154 is($desc, "a v2 inbox\n", 'v2 description retrieved');
157 ok(run_script(['-clone', '--include=*/alt',
158 "http://$host:$port/pfx", "$tmpdir/incl" ],
159 undef, $opt), 'clone w/include') or diag "clone_err=$clone_err";
160 ok(-d "$tmpdir/incl/alt", 'alt cloned');
161 ok(!-d "$tmpdir/incl/v2" && !-d "$tmpdir/incl/bare", 'only alt cloned');
162 is(xqx([qw(git config -f), "$tmpdir/incl/alt/config", 'gitweb.owner']),
163 "lorelei \xc4\x80\n", 'gitweb.owner set by -clone');
166 ok(run_script(['-clone', '--dry-run',
167 "http://$host:$port/pfx", "$tmpdir/dry-run" ],
168 undef, $opt), 'clone --dry-run') or diag "clone_err=$clone_err";
169 ok(!-d "$tmpdir/dry-run", 'nothing cloned with --dry-run');
173 open $mh, '<', "$tmpdir/incl/manifest.js.gz";
174 gunzip(\(do { local $/; <$mh> }) => \($mjs = ''));
175 $mf = $json->decode($mjs);
176 is_deeply([keys %$mf], [ '/alt' ], 'excluded keys skipped in manifest');
178 $td = start_script($cmd, $env, { 3 => $sock });
180 my $local_mfest = "$tmpdir/local.manifest.js.gz";
181 xsys_e [$curl, '-gsSfR', '-o', $local_mfest,
182 "http://$host:$port/manifest.js.gz" ];
183 xsys_e [$curl, '-vgsSfR', '-o', "$tmpdir/again.js.gz",
184 '-z', $local_mfest, "http://$host:$port/manifest.js.gz" ],
185 undef, { 2 => \(my $curl_err) };
186 like $curl_err, qr! HTTP/1\.[012] 304 !sm,
187 'got 304 response w/ If-Modified-Since';
189 # default publicinboxGrokManifest match=domain default
190 tiny_test($json, $host, $port);
192 # normal full clone on /
194 ok(run_script(['-clone', "http://$host:$port/", "$tmpdir/full" ],
195 undef, $opt), 'full clone') or diag "clone_err=$clone_err";
196 ok(-d "$tmpdir/full/$_", "$_ cloned") for qw(alt v2 bare);
200 open $fh, '>>', $cfgfile;
206 $td = start_script($cmd, $env, { 3 => $sock });
208 tiny_test($json, $host, $port, 1);
210 # grok-pull sleeps a long while some places:
211 # https://lore.kernel.org/tools/20211013110344.GA10632@dcvr/
212 skip 'TEST_GROK unset', 12 unless $ENV{TEST_GROK};
213 my $grok_pull = require_cmd('grok-pull', 1) or
214 skip('grok-pull not available', 12);
215 my ($grok_version) = (xqx([$grok_pull, "--version"])
216 =~ /(\d+)\.(?:\d+)(?:\.(\d+))?/);
217 $grok_version >= 2 or
218 skip('grok-pull v2 or later not available', 12);
219 my $grok_loglevel = $ENV{TEST_GROK_LOGLEVEL} // 'info';
221 mkdir "$tmpdir/mirror";
222 my $tail = tail_f("$tmpdir/grok.log");
223 open $fh, '>', "$tmpdir/repos.conf";
226 toplevel = $tmpdir/mirror
227 manifest = $tmpdir/local-manifest.js.gz
228 log = $tmpdir/grok.log
229 loglevel = $grok_loglevel
231 site = http://$host:$port
232 manifest = \${site}/manifest.js.gz
237 xsys($grok_pull, '-c', "$tmpdir/repos.conf");
238 is($? >> 8, 0, 'grok-pull exit code as expected');
239 for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
240 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
243 # support per-inbox manifests, handy for v2:
244 # /$INBOX/v2/manifest.js.gz
245 open $fh, '>', "$tmpdir/per-inbox.conf";
248 toplevel = $tmpdir/per-inbox
249 manifest = $tmpdir/per-inbox-manifest.js.gz
250 log = $tmpdir/grok.log
251 loglevel = $grok_loglevel
253 site = http://$host:$port
254 manifest = \${site}/v2/manifest.js.gz
259 mkdir "$tmpdir/per-inbox";
260 xsys($grok_pull, '-c', "$tmpdir/per-inbox.conf");
261 is($? >> 8, 0, 'grok-pull exit code as expected');
262 for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
263 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
267 is($?, 0, 'no error in exited process');
269 my $eout = do { local $/; <$fh> };
270 unlike($eout, qr/wide/i, 'no Wide character warnings');
271 unlike($eout, qr/uninitialized/i, 'no uninitialized warnings');