]> Sergey Matveev's repositories - public-inbox.git/blob - t/www_listing.t
e6bb1bda27baf66db53bee30622c2355a6ee6343
[public-inbox.git] / t / www_listing.t
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 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();
13
14 use_ok 'PublicInbox::Git';
15
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');
20 {
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,
25                 'fast-import');
26 }
27
28 like($bare->manifest_entry->{fingerprint}, qr/\A[a-f0-9]{40}\z/,
29         'got fingerprint with non-empty repo');
30
31 sub tiny_test {
32         my ($json, $host, $port, $html) = @_;
33         my ($tmp, $res);
34         my $http = HTTP::Tiny->new;
35         if ($html) {
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');
39
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');
45         }
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');
58
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');
64
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');
71
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);
75 }
76
77 my $td;
78 SKIP: {
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');
88
89         PublicInbox::Import::init_bare("$v2/all.git");
90         for my $i (0..2) {
91                 is(xsys(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i")
92         }
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,
99                 'set gitweb user');
100         open $fh, '>', $cfgfile or xbail "open $cfgfile: $!";
101         $fh->autoflush(1);
102         print $fh <<"" or xbail "print $!";
103 [publicinbox "bare"]
104         inboxdir = $bare->{git_dir}
105         url = http://$host/bare
106         address = bare\@example.com
107 [publicinbox "alt"]
108         inboxdir = $alt
109         url = http://$host/alt
110         address = alt\@example.com
111 [publicinbox "v2"]
112         inboxdir = $v2
113         url = http://$host/v2
114         address = v2\@example.com
115
116         my $env = { PI_CONFIG => $cfgfile };
117         my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
118         my $psgi = "$tmpdir/pfx.psgi";
119         {
120                 open my $psgi_fh, '>', $psgi or xbail "open: $!";
121                 print $psgi_fh <<'EOM' or xbail "print $!";
122 use PublicInbox::WWW;
123 use Plack::Builder;
124 my $www = PublicInbox::WWW->new;
125 builder {
126         enable 'Head';
127         mount '/pfx/' => sub { $www->call(@_) }
128 }
129 EOM
130                 close $psgi_fh or xbail "close: $!";
131         }
132
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";
138
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) ],
144                 'manifest saved');
145         for (keys %$mf) { ok(-d "$tmpdir/pfx$_", "pfx/$_ cloned") }
146
147         $clone_err = '';
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
154         undef $td;
155
156         open $mh, '<', "$tmpdir/incl/manifest.js.gz" or xbail "open: $!";
157         gunzip(\(do { local $/; <$mh> }) => \($mjs = ''));
158         $mf = $json->decode($mjs);
159         is_deeply([keys %$mf], [ '/alt' ], 'excluded keys skipped in manifest');
160
161         $td = start_script($cmd, $env, { 3 => $sock });
162
163         # default publicinboxGrokManifest match=domain default
164         tiny_test($json, $host, $port);
165
166         # normal full clone on /
167         $clone_err = '';
168         ok(run_script(['-clone', "http://$host:$port/", "$tmpdir/full" ],
169                 undef, $opt), 'full clone') or diag "clone_err=$clone_err";
170         ok(-d "$tmpdir/full/$_", "$_ cloned") for qw(alt v2 bare);
171
172         undef $td;
173
174         print $fh <<"" or xbail "print $!";
175 [publicinbox]
176         wwwlisting = all
177
178         close $fh or xbail "close $!";
179         $td = start_script($cmd, $env, { 3 => $sock });
180         undef $sock;
181         tiny_test($json, $host, $port, 1);
182
183         # grok-pull sleeps a long while some places:
184         # https://lore.kernel.org/tools/20211013110344.GA10632@dcvr/
185         skip 'TEST_GROK unset', 12 unless $ENV{TEST_GROK};
186         my $grok_pull = require_cmd('grok-pull', 1) or
187                 skip('grok-pull not available', 12);
188         my ($grok_version) = (xqx([$grok_pull, "--version"])
189                         =~ /(\d+)\.(?:\d+)(?:\.(\d+))?/);
190         $grok_version >= 2 or
191                 skip('grok-pull v2 or later not available', 12);
192         my $grok_loglevel = $ENV{TEST_GROK_LOGLEVEL} // 'info';
193
194         ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
195         my $tail = tail_f("$tmpdir/grok.log");
196         open $fh, '>', "$tmpdir/repos.conf" or xbail $!;
197         print $fh <<"" or xbail $!;
198 [core]
199 toplevel = $tmpdir/mirror
200 manifest = $tmpdir/local-manifest.js.gz
201 log = $tmpdir/grok.log
202 loglevel = $grok_loglevel
203 [remote]
204 site = http://$host:$port
205 manifest = \${site}/manifest.js.gz
206 [pull]
207 [fsck]
208
209         close $fh or xbail $!;
210         xsys($grok_pull, '-c', "$tmpdir/repos.conf");
211         is($? >> 8, 0, 'grok-pull exit code as expected');
212         for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
213                 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
214         }
215
216         # support per-inbox manifests, handy for v2:
217         # /$INBOX/v2/manifest.js.gz
218         open $fh, '>', "$tmpdir/per-inbox.conf" or xbail $!;
219         print $fh <<"" or xbail $!;
220 [core]
221 toplevel = $tmpdir/per-inbox
222 manifest = $tmpdir/per-inbox-manifest.js.gz
223 log = $tmpdir/grok.log
224 loglevel = $grok_loglevel
225 [remote]
226 site = http://$host:$port
227 manifest = \${site}/v2/manifest.js.gz
228 [pull]
229 [fsck]
230
231         close $fh or xbail $!;
232         ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
233         xsys($grok_pull, '-c', "$tmpdir/per-inbox.conf");
234         is($? >> 8, 0, 'grok-pull exit code as expected');
235         for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
236                 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
237         }
238         $td->kill;
239         $td->join;
240         is($?, 0, 'no error in exited process');
241         open $fh, '<', $err or BAIL_OUT("open $err failed: $!");
242         my $eout = do { local $/; <$fh> };
243         unlike($eout, qr/wide/i, 'no Wide character warnings');
244         unlike($eout, qr/uninitialized/i, 'no uninitialized warnings');
245 }
246
247 done_testing();