]> Sergey Matveev's repositories - public-inbox.git/blob - t/www_listing.t
testcommon: add require_mods method and use it
[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         for my $i (0..2) {
76                 is(system(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i");
77         }
78         ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
79         open $fh, '>', "$alt/description" or die;
80         print $fh "we're all clones\n" or die;
81         close $fh or die;
82         is(system('git', "--git-dir=$alt", qw(config gitweb.owner lorelei)), 0,
83                 'set gitweb user');
84         ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
85         open $fh, '>', $cfgfile or die;
86         print $fh <<"" or die;
87 [publicinbox "bare"]
88         inboxdir = $bare->{git_dir}
89         url = http://$host/bare
90         address = bare\@example.com
91 [publicinbox "alt"]
92         inboxdir = $alt
93         url = http://$host/alt
94         address = alt\@example.com
95 [publicinbox "v2"]
96         inboxdir = $v2
97         url = http://$host/v2
98         address = v2\@example.com
99
100         close $fh or die;
101         my $env = { PI_CONFIG => $cfgfile };
102         my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
103         $td = start_script($cmd, $env, { 3 => $sock });
104         $sock = undef;
105
106         tiny_test($json, $host, $port);
107
108         skip 'skipping grok-pull integration test', 2 if !which('grok-pull');
109
110         ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
111         open $fh, '>', "$tmpdir/repos.conf" or die;
112         print $fh <<"" or die;
113 # You can pull from multiple grok mirrors, just create
114 # a separate section for each mirror. The name can be anything.
115 [test]
116 site = http://$host:$port
117 manifest = http://$host:$port/manifest.js.gz
118 toplevel = $tmpdir/mirror
119 mymanifest = $tmpdir/local-manifest.js.gz
120
121         close $fh or die;
122
123         system(qw(grok-pull -c), "$tmpdir/repos.conf");
124         is($? >> 8, 127, 'grok-pull exit code as expected');
125         for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
126                 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
127         }
128
129         # support per-inbox manifests, handy for v2:
130         # /$INBOX/v2/manifest.js.gz
131         open $fh, '>', "$tmpdir/per-inbox.conf" or die;
132         print $fh <<"" or die;
133 # You can pull from multiple grok mirrors, just create
134 # a separate section for each mirror. The name can be anything.
135 [v2]
136 site = http://$host:$port
137 manifest = http://$host:$port/v2/manifest.js.gz
138 toplevel = $tmpdir/per-inbox
139 mymanifest = $tmpdir/per-inbox-manifest.js.gz
140
141         close $fh or die;
142         ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
143         system(qw(grok-pull -c), "$tmpdir/per-inbox.conf");
144         is($? >> 8, 127, 'grok-pull exit code as expected');
145         for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
146                 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
147         }
148 }
149
150 done_testing();