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