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