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