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