lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / www_listing.t
blob0a4c79e8f58acfbaf009de3c4e7015dc2a36ece1
1 #!perl -w
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 Plack::Builder HTTP::Tiny));
9 require_cmd 'curl';
10 require PublicInbox::WwwListing;
11 require PublicInbox::ManifestJsGz;
12 use PublicInbox::Config;
13 my $json = PublicInbox::Config::json();
15 use_ok 'PublicInbox::Git';
17 my ($tmpdir, $for_destroy) = tmpdir();
18 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
19 PublicInbox::Import::init_bare($bare->{git_dir});
20 is($bare->manifest_entry, undef, 'empty repo has no manifest entry');
22         my $fi_data = './t/git.fast-import-data';
23         open my $fh, '<', $fi_data or die "open $fi_data: $!";
24         my $env = { GIT_DIR => $bare->{git_dir} };
25         is(xsys([qw(git fast-import --quiet)], $env, { 0 => $fh }), 0,
26                 'fast-import');
29 like($bare->manifest_entry->{fingerprint}, qr/\A[a-f0-9]{40}\z/,
30         'got fingerprint with non-empty repo');
32 sub tiny_test {
33         my ($json, $host, $port, $html) = @_;
34         my ($tmp, $res);
35         my $http = HTTP::Tiny->new;
36         if ($html) {
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');
46         }
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);
78 my $td;
79 SKIP: {
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         is(xsys(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
91         PublicInbox::Import::init_bare("$v2/all.git");
92         for my $i (0..2) {
93                 is(xsys(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i")
94         }
95         ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
96         open($fh, '>', "$v2/description") or xbail "open $v2/description: $!";
97         print $fh "a v2 inbox\n" or xbail "print $!";
98         close $fh or xbail "write: $v2/description $!";
100         open $fh, '>', "$alt/description" or xbail "open $alt/description $!";
101         print $fh "we're \xc4\x80ll clones\n" or xbail "print $!";
102         close $fh or xbail "write: $alt/description $!";
103         is(xsys('git', "--git-dir=$alt", qw(config gitweb.owner),
104                 "lorelei \xc4\x80"), 0,
105                 'set gitweb user');
106         open $fh, '>', $cfgfile or xbail "open $cfgfile: $!";
107         $fh->autoflush(1);
108         print $fh <<"" or xbail "print $!";
109 [publicinbox "bare"]
110         inboxdir = $bare->{git_dir}
111         url = http://$host/bare
112         address = bare\@example.com
113 [publicinbox "alt"]
114         inboxdir = $alt
115         url = http://$host/alt
116         address = alt\@example.com
117 [publicinbox "v2"]
118         inboxdir = $v2
119         url = http://$host/v2
120         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";
125         {
126                 open my $psgi_fh, '>', $psgi or xbail "open: $!";
127                 print $psgi_fh <<'EOM' or xbail "print $!";
128 use PublicInbox::WWW;
129 use Plack::Builder;
130 my $www = PublicInbox::WWW->new;
131 builder {
132         enable 'Head';
133         mount '/pfx/' => sub { $www->call(@_) }
136                 close $psgi_fh or xbail "close: $!";
137         }
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" or xbail "open: $!";
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) ],
150                 'manifest saved');
151         for (keys %$mf) { ok(-d "$tmpdir/pfx$_", "pfx/$_ cloned") }
152         open my $desc, '<', "$tmpdir/pfx/v2/description" or xbail "open: $!";
153         $desc = <$desc>;
154         is($desc, "a v2 inbox\n", 'v2 description retrieved');
156         $clone_err = '';
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');
165         $clone_err = '';
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');
171         undef $td;
173         open $mh, '<', "$tmpdir/incl/manifest.js.gz" or xbail "open: $!";
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         # default publicinboxGrokManifest match=domain default
181         tiny_test($json, $host, $port);
183         # normal full clone on /
184         $clone_err = '';
185         ok(run_script(['-clone', "http://$host:$port/", "$tmpdir/full" ],
186                 undef, $opt), 'full clone') or diag "clone_err=$clone_err";
187         ok(-d "$tmpdir/full/$_", "$_ cloned") for qw(alt v2 bare);
189         undef $td;
191         print $fh <<"" or xbail "print $!";
192 [publicinbox]
193         wwwlisting = all
195         close $fh or xbail "close $!";
196         $td = start_script($cmd, $env, { 3 => $sock });
197         undef $sock;
198         tiny_test($json, $host, $port, 1);
200         # grok-pull sleeps a long while some places:
201         # https://lore.kernel.org/tools/20211013110344.GA10632@dcvr/
202         skip 'TEST_GROK unset', 12 unless $ENV{TEST_GROK};
203         my $grok_pull = require_cmd('grok-pull', 1) or
204                 skip('grok-pull not available', 12);
205         my ($grok_version) = (xqx([$grok_pull, "--version"])
206                         =~ /(\d+)\.(?:\d+)(?:\.(\d+))?/);
207         $grok_version >= 2 or
208                 skip('grok-pull v2 or later not available', 12);
209         my $grok_loglevel = $ENV{TEST_GROK_LOGLEVEL} // 'info';
211         ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
212         my $tail = tail_f("$tmpdir/grok.log");
213         open $fh, '>', "$tmpdir/repos.conf" or xbail $!;
214         print $fh <<"" or xbail $!;
215 [core]
216 toplevel = $tmpdir/mirror
217 manifest = $tmpdir/local-manifest.js.gz
218 log = $tmpdir/grok.log
219 loglevel = $grok_loglevel
220 [remote]
221 site = http://$host:$port
222 manifest = \${site}/manifest.js.gz
223 [pull]
224 [fsck]
226         close $fh or xbail $!;
227         xsys($grok_pull, '-c', "$tmpdir/repos.conf");
228         is($? >> 8, 0, 'grok-pull exit code as expected');
229         for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
230                 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
231         }
233         # support per-inbox manifests, handy for v2:
234         # /$INBOX/v2/manifest.js.gz
235         open $fh, '>', "$tmpdir/per-inbox.conf" or xbail $!;
236         print $fh <<"" or xbail $!;
237 [core]
238 toplevel = $tmpdir/per-inbox
239 manifest = $tmpdir/per-inbox-manifest.js.gz
240 log = $tmpdir/grok.log
241 loglevel = $grok_loglevel
242 [remote]
243 site = http://$host:$port
244 manifest = \${site}/v2/manifest.js.gz
245 [pull]
246 [fsck]
248         close $fh or xbail $!;
249         ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
250         xsys($grok_pull, '-c', "$tmpdir/per-inbox.conf");
251         is($? >> 8, 0, 'grok-pull exit code as expected');
252         for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
253                 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
254         }
255         $td->kill;
256         $td->join;
257         is($?, 0, 'no error in exited process');
258         open $fh, '<', $err or BAIL_OUT("open $err failed: $!");
259         my $eout = do { local $/; <$fh> };
260         unlike($eout, qr/wide/i, 'no Wide character warnings');
261         unlike($eout, qr/uninitialized/i, 'no uninitialized warnings');
264 done_testing();