]> Sergey Matveev's repositories - public-inbox.git/blob - t/www_listing.t
Merge remote-tracking branch 'origin/nntp-tls'
[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
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 $fi_data = './t/git.fast-import-data';
23 my $tmpdir = tempdir('www_listing-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
24 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
25 is(system(qw(git init -q --bare), $bare->{git_dir}), 0, 'git init --bare');
26 is(PublicInbox::WwwListing::fingerprint($bare), undef,
27         'empty repo has no fingerprint');
28
29 my $cmd = [ 'git', "--git-dir=$bare->{git_dir}", qw(fast-import --quiet) ];
30 ok(IPC::Run::run($cmd, '<', $fi_data), 'fast-import');
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 ($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 $pid;
67 END { kill 'TERM', $pid if defined $pid };
68 SKIP: {
69         my $err = "$tmpdir/stderr.log";
70         my $out = "$tmpdir/stdout.log";
71         my $alt = "$tmpdir/alt.git";
72         my $cfgfile = "$tmpdir/config";
73         my $v2 = "$tmpdir/v2";
74         my $httpd = 'blib/script/public-inbox-httpd';
75         use IO::Socket::INET;
76         my %opts = (
77                 LocalAddr => '127.0.0.1',
78                 ReuseAddr => 1,
79                 Proto => 'tcp',
80                 Type => SOCK_STREAM,
81                 Listen => 1024,
82         );
83         my $sock = IO::Socket::INET->new(%opts);
84         ok($sock, 'sock created');
85         my ($host, $port) = ($sock->sockhost, $sock->sockport);
86         my @clone = qw(git clone -q -s --bare);
87         is(system(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
88
89         for my $i (0..2) {
90                 is(system(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i");
91         }
92         ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
93         open $fh, '>', "$alt/description" or die;
94         print $fh "we're all clones\n" or die;
95         close $fh or die;
96         is(system('git', "--git-dir=$alt", qw(config gitweb.owner lorelei)), 0,
97                 'set gitweb user');
98         ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
99         open $fh, '>', $cfgfile or die;
100         print $fh <<"" or die;
101 [publicinbox "bare"]
102         mainrepo = $bare->{git_dir}
103         url = http://$host/bare
104         address = bare\@example.com
105 [publicinbox "alt"]
106         mainrepo = $alt
107         url = http://$host/alt
108         address = alt\@example.com
109 [publicinbox "v2"]
110         mainrepo = $v2
111         url = http://$host/v2
112         address = v2\@example.com
113
114         close $fh or die;
115         my $env = { PI_CONFIG => $cfgfile };
116         my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err" ];
117         $pid = spawn_listener($env, $cmd, [$sock]);
118         $sock = undef;
119
120         tiny_test($host, $port);
121
122         skip 'skipping grok-pull integration test', 2 if !which('grok-pull');
123
124         ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
125         open $fh, '>', "$tmpdir/repos.conf" or die;
126         print $fh <<"" or die;
127 # You can pull from multiple grok mirrors, just create
128 # a separate section for each mirror. The name can be anything.
129 [test]
130 site = http://$host:$port
131 manifest = http://$host:$port/manifest.js.gz
132 toplevel = $tmpdir/mirror
133 mymanifest = $tmpdir/local-manifest.js.gz
134
135         close $fh or die;
136
137         system(qw(grok-pull -c), "$tmpdir/repos.conf");
138         is($? >> 8, 127, 'grok-pull exit code as expected');
139         for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
140                 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
141         }
142
143         # support per-inbox manifests, handy for v2:
144         # /$INBOX/v2/manifest.js.gz
145         open $fh, '>', "$tmpdir/per-inbox.conf" or die;
146         print $fh <<"" or die;
147 # You can pull from multiple grok mirrors, just create
148 # a separate section for each mirror. The name can be anything.
149 [v2]
150 site = http://$host:$port
151 manifest = http://$host:$port/v2/manifest.js.gz
152 toplevel = $tmpdir/per-inbox
153 mymanifest = $tmpdir/per-inbox-manifest.js.gz
154
155         close $fh or die;
156         ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
157         system(qw(grok-pull -c), "$tmpdir/per-inbox.conf");
158         is($? >> 8, 127, 'grok-pull exit code as expected');
159         for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
160                 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
161         }
162 }
163
164 done_testing();