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