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