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