2 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # manifest.js.gz generation and grok-pull integration test
5 use strict; use v5.10.1; use PublicInbox::TestCommon;
6 use PublicInbox::Import;
7 use IO::Uncompress::Gunzip qw(gunzip);
8 require_mods(qw(json URI::Escape Plack::Builder Digest::SHA HTTP::Tiny));
9 require PublicInbox::WwwListing;
10 require PublicInbox::ManifestJsGz;
11 use PublicInbox::Config;
12 my $json = PublicInbox::Config::json();
14 use_ok 'PublicInbox::Git';
16 my ($tmpdir, $for_destroy) = tmpdir();
17 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
18 PublicInbox::Import::init_bare($bare->{git_dir});
19 is($bare->manifest_entry, undef, 'empty repo has no manifest entry');
21 my $fi_data = './t/git.fast-import-data';
22 open my $fh, '<', $fi_data or die "open $fi_data: $!";
23 my $env = { GIT_DIR => $bare->{git_dir} };
24 is(xsys([qw(git fast-import --quiet)], $env, { 0 => $fh }), 0,
28 like($bare->manifest_entry->{fingerprint}, qr/\A[a-f0-9]{40}\z/,
29 'got fingerprint with non-empty repo');
32 my ($json, $host, $port, $html) = @_;
34 my $http = HTTP::Tiny->new;
36 $res = $http->get("http://$host:$port/");
37 is($res->{status}, 200, 'got HTML listing');
38 like($res->{content}, qr!</html>!si, 'listing looks like HTML');
40 $res = $http->get("http://$host:$port/",
41 {'Accept-Encoding'=>'gzip'});
42 is($res->{status}, 200, 'got gzipped HTML listing');
43 gunzip(\(delete $res->{content}) => \$tmp);
44 like($tmp, qr!</html>!si, 'unzipped listing looks like HTML');
46 $res = $http->get("http://$host:$port/manifest.js.gz");
47 is($res->{status}, 200, 'got manifest');
48 gunzip(\(delete $res->{content}) => \$tmp);
49 unlike($tmp, qr/"modified":\s*"/, 'modified is an integer');
50 my $manifest = $json->decode($tmp);
51 ok(my $clone = $manifest->{'/alt'}, '/alt in manifest');
52 is($clone->{owner}, "lorelei \x{100}", 'owner set');
53 is($clone->{reference}, '/bare', 'reference detected');
54 is($clone->{description}, "we're \x{100}ll clones", 'description read');
55 ok(my $bare = $manifest->{'/bare'}, '/bare in manifest');
56 is($bare->{description}, 'Unnamed repository',
57 'missing $GIT_DIR/description fallback');
59 like($bare->{fingerprint}, qr/\A[a-f0-9]{40}\z/, 'fingerprint');
60 is($clone->{fingerprint}, $bare->{fingerprint}, 'fingerprint matches');
61 is(HTTP::Date::time2str($bare->{modified}),
62 $res->{headers}->{'last-modified'},
63 'modified field and Last-Modified header match');
65 ok(my $v2epoch0 = $manifest->{'/v2/git/0.git'}, 'v2 epoch 0 appeared');
66 like($v2epoch0->{description}, qr/ \[epoch 0\]\z/,
67 'epoch 0 in description');
68 ok(my $v2epoch1 = $manifest->{'/v2/git/1.git'}, 'v2 epoch 1 appeared');
69 like($v2epoch1->{description}, qr/ \[epoch 1\]\z/,
70 'epoch 1 in description');
72 $res = $http->get("http://$host:$port/alt/description");
73 is($res->{content}, "we're \xc4\x80ll clones\n", 'UTF-8 description')
74 or diag explain($res);
79 my $err = "$tmpdir/stderr.log";
80 my $out = "$tmpdir/stdout.log";
81 my $alt = "$tmpdir/alt.git";
82 my $cfgfile = "$tmpdir/config";
83 my $v2 = "$tmpdir/v2";
84 my $sock = tcp_server();
85 my ($host, $port) = tcp_host_port($sock);
86 my @clone = qw(git clone -q -s --bare);
87 is(xsys(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
89 PublicInbox::Import::init_bare("$v2/all.git");
91 is(xsys(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i")
93 ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
94 open $fh, '>', "$alt/description" or xbail "open $alt/description $!";
95 print $fh "we're \xc4\x80ll clones\n" or xbail "print $!";
96 close $fh or xbail "write: $alt/description $!";
97 is(xsys('git', "--git-dir=$alt", qw(config gitweb.owner),
98 "lorelei \xc4\x80"), 0,
100 open $fh, '>', $cfgfile or xbail "open $cfgfile: $!";
102 print $fh <<"" or xbail "print $!";
104 inboxdir = $bare->{git_dir}
105 url = http://$host/bare
106 address = bare\@example.com
109 url = http://$host/alt
110 address = alt\@example.com
113 url = http://$host/v2
114 address = v2\@example.com
116 my $env = { PI_CONFIG => $cfgfile };
117 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
118 $td = start_script($cmd, $env, { 3 => $sock });
120 # default publicinboxGrokManifest match=domain default
121 tiny_test($json, $host, $port);
124 print $fh <<"" or xbail "print $!";
128 close $fh or xbail "close $!";
129 $td = start_script($cmd, $env, { 3 => $sock });
130 tiny_test($json, $host, $port, 1);
133 skip 'TEST_GROK unset', 12 unless $ENV{TEST_GROK};
134 my $grok_pull = require_cmd('grok-pull', 1) or
135 skip('grok-pull not available', 12);
136 my ($grok_version) = (xqx([$grok_pull, "--version"])
137 =~ /(\d+)\.(?:\d+)(?:\.(\d+))?/);
138 $grok_version >= 2 or
139 skip('grok-pull v2 or later not available', 12);
140 my $grok_loglevel = $ENV{TEST_GROK_LOGLEVEL} // 'info';
142 ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
143 my $tail = tail_f("$tmpdir/grok.log");
144 open $fh, '>', "$tmpdir/repos.conf" or xbail $!;
145 print $fh <<"" or xbail $!;
147 toplevel = $tmpdir/mirror
148 manifest = $tmpdir/local-manifest.js.gz
149 log = $tmpdir/grok.log
150 loglevel = $grok_loglevel
152 site = http://$host:$port
153 manifest = \${site}/manifest.js.gz
157 close $fh or xbail $!;
158 xsys($grok_pull, '-c', "$tmpdir/repos.conf");
159 is($? >> 8, 0, 'grok-pull exit code as expected');
160 for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
161 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
164 # support per-inbox manifests, handy for v2:
165 # /$INBOX/v2/manifest.js.gz
166 open $fh, '>', "$tmpdir/per-inbox.conf" or xbail $!;
167 print $fh <<"" or xbail $!;
169 toplevel = $tmpdir/per-inbox
170 manifest = $tmpdir/per-inbox-manifest.js.gz
171 log = $tmpdir/grok.log
172 loglevel = $grok_loglevel
174 site = http://$host:$port
175 manifest = \${site}/v2/manifest.js.gz
179 close $fh or xbail $!;
180 ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
181 xsys($grok_pull, '-c', "$tmpdir/per-inbox.conf");
182 is($? >> 8, 0, 'grok-pull exit code as expected');
183 for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
184 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
188 is($?, 0, 'no error in exited process');
189 open $fh, '<', $err or BAIL_OUT("open $err failed: $!");
190 my $eout = do { local $/; <$fh> };
191 unlike($eout, qr/wide/i, 'no Wide character warnings');
192 unlike($eout, qr/uninitialized/i, 'no uninitialized warnings');