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