]> Sergey Matveev's repositories - public-inbox.git/blob - t/www_listing.t
update copyrights for 2021
[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         ok($sock, 'sock created');
83         my ($host, $port) = ($sock->sockhost, $sock->sockport);
84         my @clone = qw(git clone -q -s --bare);
85         is(xsys(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
86
87         PublicInbox::Import::init_bare("$v2/all.git");
88         for my $i (0..2) {
89                 is(xsys(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i")
90         }
91         ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
92         open $fh, '>', "$alt/description" or die;
93         print $fh "we're all clones\n" or die;
94         close $fh or die;
95         is(xsys('git', "--git-dir=$alt", qw(config gitweb.owner),
96                 "lorelei \xc4\x80"), 0,
97                 'set gitweb user');
98         ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
99         open $fh, '>', $cfgfile or die;
100         print $fh <<"" or die;
101 [publicinbox]
102         wwwlisting = all
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         close $fh or die;
117         my $env = { PI_CONFIG => $cfgfile };
118         my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
119         $td = start_script($cmd, $env, { 3 => $sock });
120         $sock = undef;
121
122         tiny_test($json, $host, $port);
123
124         my $grok_pull = which('grok-pull') or
125                 skip('skipping grok-pull integration test', 2);
126
127         ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
128         open $fh, '>', "$tmpdir/repos.conf" or die;
129         print $fh <<"" or die;
130 # You can pull from multiple grok mirrors, just create
131 # a separate section for each mirror. The name can be anything.
132 [test]
133 site = http://$host:$port
134 manifest = http://$host:$port/manifest.js.gz
135 toplevel = $tmpdir/mirror
136 mymanifest = $tmpdir/local-manifest.js.gz
137
138         close $fh or die;
139
140         xsys($grok_pull, '-c', "$tmpdir/repos.conf");
141         is($? >> 8, 127, 'grok-pull exit code as expected');
142         for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
143                 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
144         }
145
146         # support per-inbox manifests, handy for v2:
147         # /$INBOX/v2/manifest.js.gz
148         open $fh, '>', "$tmpdir/per-inbox.conf" or die;
149         print $fh <<"" or die;
150 # You can pull from multiple grok mirrors, just create
151 # a separate section for each mirror. The name can be anything.
152 [v2]
153 site = http://$host:$port
154 manifest = http://$host:$port/v2/manifest.js.gz
155 toplevel = $tmpdir/per-inbox
156 mymanifest = $tmpdir/per-inbox-manifest.js.gz
157
158         close $fh or die;
159         ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
160         xsys($grok_pull, '-c', "$tmpdir/per-inbox.conf");
161         is($? >> 8, 127, 'grok-pull exit code as expected');
162         for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
163                 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
164         }
165 }
166
167 done_testing();