]> Sergey Matveev's repositories - public-inbox.git/blob - t/www_listing.t
www: manifest.js.gz generation no longer hogs event loop
[public-inbox.git] / t / www_listing.t
1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # manifest.js.gz generation and grok-pull integration test
4 use strict;
5 use warnings;
6 use Test::More;
7 use PublicInbox::Spawn qw(which);
8 use PublicInbox::TestCommon;
9 use PublicInbox::Import;
10 require_mods(qw(URI::Escape Plack::Builder Digest::SHA
11                 IO::Compress::Gzip IO::Uncompress::Gunzip HTTP::Tiny));
12 require PublicInbox::WwwListing;
13 require PublicInbox::ManifestJsGz;
14 my $json = do {
15         no warnings 'once';
16         $PublicInbox::ManifestJsGz::json;
17 } or plan skip_all => "JSON module missing";
18
19 use_ok 'PublicInbox::Git';
20
21 my ($tmpdir, $for_destroy) = tmpdir();
22 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
23 PublicInbox::Import::init_bare($bare->{git_dir});
24 is(PublicInbox::ManifestJsGz::fingerprint($bare), undef,
25         'empty repo has no fingerprint');
26 {
27         my $fi_data = './t/git.fast-import-data';
28         open my $fh, '<', $fi_data or die "open $fi_data: $!";
29         my $env = { GIT_DIR => $bare->{git_dir} };
30         is(xsys([qw(git fast-import --quiet)], $env, { 0 => $fh }), 0,
31                 'fast-import');
32 }
33
34 like(PublicInbox::ManifestJsGz::fingerprint($bare), qr/\A[a-f0-9]{40}\z/,
35         'got fingerprint with non-empty repo');
36
37 sub tiny_test {
38         my ($json, $host, $port) = @_;
39         my $tmp;
40         my $http = HTTP::Tiny->new;
41         my $res = $http->get("http://$host:$port/");
42         is($res->{status}, 200, 'got HTML listing');
43         like($res->{content}, qr!</html>!si, 'listing looks like HTML');
44
45         $res = $http->get("http://$host:$port/", {'Accept-Encoding'=>'gzip'});
46         is($res->{status}, 200, 'got gzipped HTML listing');
47         IO::Uncompress::Gunzip::gunzip(\(delete $res->{content}) => \$tmp);
48         like($tmp, qr!</html>!si, 'unzipped listing looks like HTML');
49
50         $res = $http->get("http://$host:$port/manifest.js.gz");
51         is($res->{status}, 200, 'got manifest');
52         IO::Uncompress::Gunzip::gunzip(\(delete $res->{content}) => \$tmp);
53         unlike($tmp, qr/"modified":\s*"/, 'modified is an integer');
54         my $manifest = $json->decode($tmp);
55         ok(my $clone = $manifest->{'/alt'}, '/alt in manifest');
56         is($clone->{owner}, "lorelei \x{100}", 'owner set');
57         is($clone->{reference}, '/bare', 'reference detected');
58         is($clone->{description}, "we're all clones", 'description read');
59         ok(my $bare = $manifest->{'/bare'}, '/bare in manifest');
60         is($bare->{description}, 'Unnamed repository',
61                 'missing $GIT_DIR/description fallback');
62
63         like($bare->{fingerprint}, qr/\A[a-f0-9]{40}\z/, 'fingerprint');
64         is($clone->{fingerprint}, $bare->{fingerprint}, 'fingerprint matches');
65         is(HTTP::Date::time2str($bare->{modified}),
66                 $res->{headers}->{'last-modified'},
67                 'modified field and Last-Modified header match');
68
69         ok(my $v2epoch0 = $manifest->{'/v2/git/0.git'}, 'v2 epoch 0 appeared');
70         like($v2epoch0->{description}, qr/ \[epoch 0\]\z/,
71                 'epoch 0 in description');
72         ok(my $v2epoch1 = $manifest->{'/v2/git/1.git'}, 'v2 epoch 1 appeared');
73         like($v2epoch1->{description}, qr/ \[epoch 1\]\z/,
74                 'epoch 1 in description');
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         ok($sock, 'sock created');
86         my ($host, $port) = ($sock->sockhost, $sock->sockport);
87         my @clone = qw(git clone -q -s --bare);
88         is(xsys(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
89
90         PublicInbox::Import::init_bare("$v2/all.git");
91         for my $i (0..2) {
92                 is(xsys(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i")
93         }
94         ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
95         open $fh, '>', "$alt/description" or die;
96         print $fh "we're all clones\n" or die;
97         close $fh or die;
98         is(xsys('git', "--git-dir=$alt", qw(config gitweb.owner),
99                 "lorelei \xc4\x80"), 0,
100                 'set gitweb user');
101         ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
102         open $fh, '>', $cfgfile or die;
103         print $fh <<"" or die;
104 [publicinbox]
105         wwwlisting = all
106 [publicinbox "bare"]
107         inboxdir = $bare->{git_dir}
108         url = http://$host/bare
109         address = bare\@example.com
110 [publicinbox "alt"]
111         inboxdir = $alt
112         url = http://$host/alt
113         address = alt\@example.com
114 [publicinbox "v2"]
115         inboxdir = $v2
116         url = http://$host/v2
117         address = v2\@example.com
118
119         close $fh or die;
120         my $env = { PI_CONFIG => $cfgfile };
121         my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
122         $td = start_script($cmd, $env, { 3 => $sock });
123         $sock = undef;
124
125         tiny_test($json, $host, $port);
126
127         my $grok_pull = which('grok-pull') or
128                 skip('skipping grok-pull integration test', 2);
129
130         ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
131         open $fh, '>', "$tmpdir/repos.conf" or die;
132         print $fh <<"" or die;
133 # You can pull from multiple grok mirrors, just create
134 # a separate section for each mirror. The name can be anything.
135 [test]
136 site = http://$host:$port
137 manifest = http://$host:$port/manifest.js.gz
138 toplevel = $tmpdir/mirror
139 mymanifest = $tmpdir/local-manifest.js.gz
140
141         close $fh or die;
142
143         xsys($grok_pull, '-c', "$tmpdir/repos.conf");
144         is($? >> 8, 127, 'grok-pull exit code as expected');
145         for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
146                 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
147         }
148
149         # support per-inbox manifests, handy for v2:
150         # /$INBOX/v2/manifest.js.gz
151         open $fh, '>', "$tmpdir/per-inbox.conf" or die;
152         print $fh <<"" or die;
153 # You can pull from multiple grok mirrors, just create
154 # a separate section for each mirror. The name can be anything.
155 [v2]
156 site = http://$host:$port
157 manifest = http://$host:$port/v2/manifest.js.gz
158 toplevel = $tmpdir/per-inbox
159 mymanifest = $tmpdir/per-inbox-manifest.js.gz
160
161         close $fh or die;
162         ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
163         xsys($grok_pull, '-c', "$tmpdir/per-inbox.conf");
164         is($? >> 8, 127, 'grok-pull exit code as expected');
165         for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
166                 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
167         }
168 }
169
170 done_testing();