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 all 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');
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 die;
95 print $fh "we're all clones\n" or die;
97 is(xsys('git', "--git-dir=$alt", qw(config gitweb.owner),
98 "lorelei \xc4\x80"), 0,
100 ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
101 open $fh, '>', $cfgfile or xbail "open $cfgfile: $!";
103 print $fh <<"" or xbail "print $!";
105 inboxdir = $bare->{git_dir}
106 url = http://$host/bare
107 address = bare\@example.com
110 url = http://$host/alt
111 address = alt\@example.com
114 url = http://$host/v2
115 address = v2\@example.com
117 my $env = { PI_CONFIG => $cfgfile };
118 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
119 $td = start_script($cmd, $env, { 3 => $sock });
121 # default publicinboxGrokManifest match=domain default
122 tiny_test($json, $host, $port);
125 print $fh <<"" or xbail "print $!";
129 close $fh or xbail "close $!";
130 $td = start_script($cmd, $env, { 3 => $sock });
131 tiny_test($json, $host, $port, 1);
134 my $grok_pull = which('grok-pull') 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);
141 ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
142 open $fh, '>', "$tmpdir/repos.conf" or die;
143 print $fh <<"" or die;
145 toplevel = $tmpdir/mirror
146 manifest = $tmpdir/local-manifest.js.gz
148 site = http://$host:$port
149 manifest = \${site}/manifest.js.gz
155 xsys($grok_pull, '-c', "$tmpdir/repos.conf");
156 is($? >> 8, 0, 'grok-pull exit code as expected');
157 for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
158 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
161 # support per-inbox manifests, handy for v2:
162 # /$INBOX/v2/manifest.js.gz
163 open $fh, '>', "$tmpdir/per-inbox.conf" or die;
164 print $fh <<"" or die;
166 toplevel = $tmpdir/per-inbox
167 manifest = $tmpdir/per-inbox-manifest.js.gz
169 site = http://$host:$port
170 manifest = \${site}/v2/manifest.js.gz
175 ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
176 xsys($grok_pull, '-c', "$tmpdir/per-inbox.conf");
177 is($? >> 8, 0, 'grok-pull exit code as expected');
178 for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
179 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");