]> Sergey Matveev's repositories - public-inbox.git/blob - t/www_listing.t
tests: favor IPv6
[public-inbox.git] / t / www_listing.t
1 # Copyright (C) 2019-2021 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(json URI::Escape Plack::Builder Digest::SHA
11                 IO::Compress::Gzip IO::Uncompress::Gunzip HTTP::Tiny));
12 require PublicInbox::WwwListing;
13 require PublicInbox::ManifestJsGz;
14 use PublicInbox::Config;
15 my $json = PublicInbox::Config::json();
16
17 use_ok 'PublicInbox::Git';
18
19 my ($tmpdir, $for_destroy) = tmpdir();
20 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
21 PublicInbox::Import::init_bare($bare->{git_dir});
22 is($bare->manifest_entry, undef, 'empty repo has no manifest entry');
23 {
24         my $fi_data = './t/git.fast-import-data';
25         open my $fh, '<', $fi_data or die "open $fi_data: $!";
26         my $env = { GIT_DIR => $bare->{git_dir} };
27         is(xsys([qw(git fast-import --quiet)], $env, { 0 => $fh }), 0,
28                 'fast-import');
29 }
30
31 like($bare->manifest_entry->{fingerprint}, qr/\A[a-f0-9]{40}\z/,
32         'got fingerprint with non-empty repo');
33
34 sub tiny_test {
35         my ($json, $host, $port) = @_;
36         my $tmp;
37         my $http = HTTP::Tiny->new;
38         my $res = $http->get("http://$host:$port/");
39         is($res->{status}, 200, 'got HTML listing');
40         like($res->{content}, qr!</html>!si, 'listing looks like HTML');
41
42         $res = $http->get("http://$host:$port/", {'Accept-Encoding'=>'gzip'});
43         is($res->{status}, 200, 'got gzipped HTML listing');
44         IO::Uncompress::Gunzip::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         IO::Uncompress::Gunzip::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 all clones", 'description read');
56         ok(my $bare = $manifest->{'/bare'}, '/bare in manifest');
57         is($bare->{description}, 'Unnamed repository',
58                 'missing $GIT_DIR/description fallback');
59
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');
65
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');
72 }
73
74 my $td;
75 SKIP: {
76         my $err = "$tmpdir/stderr.log";
77         my $out = "$tmpdir/stdout.log";
78         my $alt = "$tmpdir/alt.git";
79         my $cfgfile = "$tmpdir/config";
80         my $v2 = "$tmpdir/v2";
81         my $sock = tcp_server();
82         my ($host, $port) = tcp_host_port($sock);
83         my @clone = qw(git clone -q -s --bare);
84         is(xsys(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
85
86         PublicInbox::Import::init_bare("$v2/all.git");
87         for my $i (0..2) {
88                 is(xsys(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i")
89         }
90         ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
91         open $fh, '>', "$alt/description" or die;
92         print $fh "we're all clones\n" or die;
93         close $fh or die;
94         is(xsys('git', "--git-dir=$alt", qw(config gitweb.owner),
95                 "lorelei \xc4\x80"), 0,
96                 'set gitweb user');
97         ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
98         open $fh, '>', $cfgfile or die;
99         print $fh <<"" or die;
100 [publicinbox]
101         wwwlisting = all
102 [publicinbox "bare"]
103         inboxdir = $bare->{git_dir}
104         url = http://$host/bare
105         address = bare\@example.com
106 [publicinbox "alt"]
107         inboxdir = $alt
108         url = http://$host/alt
109         address = alt\@example.com
110 [publicinbox "v2"]
111         inboxdir = $v2
112         url = http://$host/v2
113         address = v2\@example.com
114
115         close $fh or die;
116         my $env = { PI_CONFIG => $cfgfile };
117         my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
118         $td = start_script($cmd, $env, { 3 => $sock });
119         $sock = undef;
120
121         tiny_test($json, $host, $port);
122
123         my $grok_pull = which('grok-pull') or
124                 skip('skipping grok-pull integration test', 2);
125
126         ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
127         open $fh, '>', "$tmpdir/repos.conf" or die;
128         print $fh <<"" or die;
129 # You can pull from multiple grok mirrors, just create
130 # a separate section for each mirror. The name can be anything.
131 [test]
132 site = http://$host:$port
133 manifest = http://$host:$port/manifest.js.gz
134 toplevel = $tmpdir/mirror
135 mymanifest = $tmpdir/local-manifest.js.gz
136
137         close $fh or die;
138
139         xsys($grok_pull, '-c', "$tmpdir/repos.conf");
140         is($? >> 8, 127, 'grok-pull exit code as expected');
141         for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
142                 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
143         }
144
145         # support per-inbox manifests, handy for v2:
146         # /$INBOX/v2/manifest.js.gz
147         open $fh, '>', "$tmpdir/per-inbox.conf" or die;
148         print $fh <<"" or die;
149 # You can pull from multiple grok mirrors, just create
150 # a separate section for each mirror. The name can be anything.
151 [v2]
152 site = http://$host:$port
153 manifest = http://$host:$port/v2/manifest.js.gz
154 toplevel = $tmpdir/per-inbox
155 mymanifest = $tmpdir/per-inbox-manifest.js.gz
156
157         close $fh or die;
158         ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
159         xsys($grok_pull, '-c', "$tmpdir/per-inbox.conf");
160         is($? >> 8, 127, 'grok-pull exit code as expected');
161         for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
162                 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
163         }
164 }
165
166 done_testing();