]> Sergey Matveev's repositories - public-inbox.git/blob - t/www_listing.t
www: wire up /$INBOX/manifest.js.gz, too
[public-inbox.git] / t / www_listing.t
1 # Copyright (C) 2019 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 File::Temp qw/tempdir/;
9 require './t/common.perl';
10 my @mods = qw(URI::Escape Plack::Builder IPC::Run Digest::SHA HTTP::Tiny
11                 IO::Compress::Gzip IO::Uncompress::Gunzip Net::HTTP);
12 foreach my $mod (@mods) {
13         eval("require $mod") or plan skip_all => "$mod missing for $0";
14 }
15 use_ok 'PublicInbox::WwwListing';
16 use_ok 'PublicInbox::Git';
17
18 my $fi_data = './t/git.fast-import-data';
19 my $tmpdir = tempdir('www_listing-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
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 $cmd = [ 'git', "--git-dir=$bare->{git_dir}", qw(fast-import --quiet) ];
26 ok(IPC::Run::run($cmd, '<', $fi_data), 'fast-import');
27
28 like(PublicInbox::WwwListing::fingerprint($bare), qr/\A[a-f0-9]{40}\z/,
29         'got fingerprint with non-empty repo');
30
31 my $pid;
32 END { kill 'TERM', $pid if defined $pid };
33 SKIP: {
34         my $json = eval { PublicInbox::WwwListing::_json() };
35         skip "JSON module missing: $@", 1 if $@;
36         my $err = "$tmpdir/stderr.log";
37         my $out = "$tmpdir/stdout.log";
38         my $alt = "$tmpdir/alt.git";
39         my $cfgfile = "$tmpdir/config";
40         my $v2 = "$tmpdir/v2";
41         my $httpd = 'blib/script/public-inbox-httpd';
42         use IO::Socket::INET;
43         my %opts = (
44                 LocalAddr => '127.0.0.1',
45                 ReuseAddr => 1,
46                 Proto => 'tcp',
47                 Type => SOCK_STREAM,
48                 Listen => 1024,
49         );
50         my $sock = IO::Socket::INET->new(%opts);
51         ok($sock, 'sock created');
52         my ($host, $port) = ($sock->sockhost, $sock->sockport);
53         my @clone = qw(git clone -q -s --bare);
54         is(system(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
55
56         for my $i (0..2) {
57                 is(system(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i");
58         }
59         ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
60         open $fh, '>', "$alt/description" or die;
61         print $fh "we're all clones\n" or die;
62         close $fh or die;
63         is(system('git', "--git-dir=$alt", qw(config gitweb.owner lorelei)), 0,
64                 'set gitweb user');
65         ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
66         open $fh, '>', $cfgfile or die;
67         print $fh <<"" or die;
68 [publicinbox "bare"]
69         mainrepo = $bare->{git_dir}
70         url = http://$host/bare
71         address = bare\@example.com
72 [publicinbox "alt"]
73         mainrepo = $alt
74         url = http://$host/alt
75         address = alt\@example.com
76 [publicinbox "v2"]
77         mainrepo = $v2
78         url = http://$host/v2
79         address = v2\@example.com
80
81         close $fh or die;
82         my $env = { PI_CONFIG => $cfgfile };
83         my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err" ];
84         $pid = spawn_listener($env, $cmd, [$sock]);
85         $sock = undef;
86         my $http = Net::HTTP->new(Host => "$host:$port");
87         $http->write_request(GET => '/manifest.js.gz');
88         my ($code, undef, %h) = $http->read_response_headers;
89         is($code, 200, 'got manifest');
90         my $tmp;
91         my $body = '';
92         while (1) {
93                 my $n = $http->read_entity_body(my $buf, 65536);
94                 die unless defined $n;
95                 last if $n == 0;
96                 $body .= $buf;
97         }
98         IO::Uncompress::Gunzip::gunzip(\$body => \$tmp);
99         my $manifest = $json->decode($tmp);
100         ok(my $clone = $manifest->{'/alt'}, '/alt in manifest');
101         is($clone->{owner}, 'lorelei', 'owner set');
102         is($clone->{reference}, '/bare', 'reference detected');
103         is($clone->{description}, "we're all clones", 'description read');
104         ok(my $bare = $manifest->{'/bare'}, '/bare in manifest');
105         is($bare->{description}, 'Unnamed repository',
106                 'missing $GIT_DIR/description fallback');
107
108         like($bare->{fingerprint}, qr/\A[a-f0-9]{40}\z/, 'fingerprint');
109         is($clone->{fingerprint}, $bare->{fingerprint}, 'fingerprint matches');
110
111         is(HTTP::Date::time2str($bare->{modified}), $h{'Last-Modified'},
112                 'modified field and Last-Modified header match');
113
114         ok($manifest->{'/v2/0'}, 'v2 epoch appeared');
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/0 v2/1 v2/2)) {
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/0 v2/1 v2/2)) {
154                 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
155         }
156 }
157
158 done_testing();