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