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
8 use PublicInbox::Spawn qw(which);
9 use PublicInbox::TestCommon;
10 use PublicInbox::Import;
11 use IO::Uncompress::Gunzip qw(gunzip);
12 require_mods(qw(json URI::Escape Plack::Builder Digest::SHA HTTP::Tiny));
13 require PublicInbox::WwwListing;
14 require PublicInbox::ManifestJsGz;
15 use PublicInbox::Config;
16 my $json = PublicInbox::Config::json();
18 use_ok 'PublicInbox::Git';
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($bare->manifest_entry, undef, 'empty repo has no manifest entry');
25 my $fi_data = './t/git.fast-import-data';
26 open my $fh, '<', $fi_data or die "open $fi_data: $!";
27 my $env = { GIT_DIR => $bare->{git_dir} };
28 is(xsys([qw(git fast-import --quiet)], $env, { 0 => $fh }), 0,
32 like($bare->manifest_entry->{fingerprint}, qr/\A[a-f0-9]{40}\z/,
33 'got fingerprint with non-empty repo');
36 my ($json, $host, $port, $html) = @_;
38 my $http = HTTP::Tiny->new;
40 $res = $http->get("http://$host:$port/");
41 is($res->{status}, 200, 'got HTML listing');
42 like($res->{content}, qr!</html>!si, 'listing looks like HTML');
44 $res = $http->get("http://$host:$port/",
45 {'Accept-Encoding'=>'gzip'});
46 is($res->{status}, 200, 'got gzipped HTML listing');
47 gunzip(\(delete $res->{content}) => \$tmp);
48 like($tmp, qr!</html>!si, 'unzipped listing looks like HTML');
50 $res = $http->get("http://$host:$port/manifest.js.gz");
51 is($res->{status}, 200, 'got manifest');
52 gunzip(\(delete $res->{content}) => \$tmp);
53 unlike($tmp, qr/"modified":\s*"/, 'modified is an integer');
54 my $manifest = $json->decode($tmp);
55 ok(my $clone = $manifest->{'/alt'}, '/alt in manifest');
56 is($clone->{owner}, "lorelei \x{100}", 'owner set');
57 is($clone->{reference}, '/bare', 'reference detected');
58 is($clone->{description}, "we're \x{100}ll clones", 'description read');
59 ok(my $bare = $manifest->{'/bare'}, '/bare in manifest');
60 is($bare->{description}, 'Unnamed repository',
61 'missing $GIT_DIR/description fallback');
63 like($bare->{fingerprint}, qr/\A[a-f0-9]{40}\z/, 'fingerprint');
64 is($clone->{fingerprint}, $bare->{fingerprint}, 'fingerprint matches');
65 is(HTTP::Date::time2str($bare->{modified}),
66 $res->{headers}->{'last-modified'},
67 'modified field and Last-Modified header match');
69 ok(my $v2epoch0 = $manifest->{'/v2/git/0.git'}, 'v2 epoch 0 appeared');
70 like($v2epoch0->{description}, qr/ \[epoch 0\]\z/,
71 'epoch 0 in description');
72 ok(my $v2epoch1 = $manifest->{'/v2/git/1.git'}, 'v2 epoch 1 appeared');
73 like($v2epoch1->{description}, qr/ \[epoch 1\]\z/,
74 'epoch 1 in description');
76 $res = $http->get("http://$host:$port/alt/description");
77 is($res->{content}, "we're \xc4\x80ll clones\n", 'UTF-8 description')
78 or diag explain($res);
83 my $err = "$tmpdir/stderr.log";
84 my $out = "$tmpdir/stdout.log";
85 my $alt = "$tmpdir/alt.git";
86 my $cfgfile = "$tmpdir/config";
87 my $v2 = "$tmpdir/v2";
88 my $sock = tcp_server();
89 my ($host, $port) = tcp_host_port($sock);
90 my @clone = qw(git clone -q -s --bare);
91 is(xsys(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
93 PublicInbox::Import::init_bare("$v2/all.git");
95 is(xsys(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i")
97 ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
98 open $fh, '>', "$alt/description" or xbail "open $alt/description $!";
99 print $fh "we're \xc4\x80ll clones\n" or xbail "print $!";
100 close $fh or xbail "write: $alt/description $!";
101 is(xsys('git', "--git-dir=$alt", qw(config gitweb.owner),
102 "lorelei \xc4\x80"), 0,
104 ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
105 open $fh, '>', $cfgfile or xbail "open $cfgfile: $!";
107 print $fh <<"" or xbail "print $!";
109 inboxdir = $bare->{git_dir}
110 url = http://$host/bare
111 address = bare\@example.com
114 url = http://$host/alt
115 address = alt\@example.com
118 url = http://$host/v2
119 address = v2\@example.com
121 my $env = { PI_CONFIG => $cfgfile };
122 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
123 $td = start_script($cmd, $env, { 3 => $sock });
125 # default publicinboxGrokManifest match=domain default
126 tiny_test($json, $host, $port);
129 print $fh <<"" or xbail "print $!";
133 close $fh or xbail "close $!";
134 $td = start_script($cmd, $env, { 3 => $sock });
135 tiny_test($json, $host, $port, 1);
138 my $grok_pull = which('grok-pull') or
139 skip('grok-pull not available', 12);
140 my ($grok_version) = (xqx([$grok_pull, "--version"])
141 =~ /(\d+)\.(?:\d+)(?:\.(\d+))?/);
142 $grok_version >= 2 or
143 skip('grok-pull v2 or later not available', 12);
145 ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
146 open $fh, '>', "$tmpdir/repos.conf" or die;
147 print $fh <<"" or die;
149 toplevel = $tmpdir/mirror
150 manifest = $tmpdir/local-manifest.js.gz
152 site = http://$host:$port
153 manifest = \${site}/manifest.js.gz
159 xsys($grok_pull, '-c', "$tmpdir/repos.conf");
160 is($? >> 8, 0, 'grok-pull exit code as expected');
161 for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
162 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
165 # support per-inbox manifests, handy for v2:
166 # /$INBOX/v2/manifest.js.gz
167 open $fh, '>', "$tmpdir/per-inbox.conf" or die;
168 print $fh <<"" or die;
170 toplevel = $tmpdir/per-inbox
171 manifest = $tmpdir/per-inbox-manifest.js.gz
173 site = http://$host:$port
174 manifest = \${site}/v2/manifest.js.gz
179 ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
180 xsys($grok_pull, '-c', "$tmpdir/per-inbox.conf");
181 is($? >> 8, 0, 'grok-pull exit code as expected');
182 for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
183 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");
187 is($?, 0, 'no error in exited process');
188 open $fh, '<', $err or BAIL_OUT("open $err failed: $!");
189 my $eout = do { local $/; <$fh> };
190 unlike($eout, qr/wide/i, 'no Wide character warnings');
191 unlike($eout, qr/uninitialized/i, 'no uninitialized warnings');