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
7 use PublicInbox::Spawn qw(which);
8 use PublicInbox::TestCommon;
9 require_mods(qw(URI::Escape Plack::Builder Digest::SHA
10 IO::Compress::Gzip IO::Uncompress::Gunzip HTTP::Tiny));
11 require PublicInbox::WwwListing;
12 my $json = eval { PublicInbox::WwwListing::_json() };
13 plan skip_all => "JSON module missing: $@" if $@;
15 use_ok 'PublicInbox::Git';
17 my ($tmpdir, $for_destroy) = tmpdir();
18 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
19 is(system(qw(git init -q --bare), $bare->{git_dir}), 0, 'git init --bare');
20 is(PublicInbox::WwwListing::fingerprint($bare), undef,
21 'empty repo has no fingerprint');
23 my $fi_data = './t/git.fast-import-data';
24 local $ENV{GIT_DIR} = $bare->{git_dir};
25 is(system("git fast-import --quiet <$fi_data"), 0, 'fast-import');
28 like(PublicInbox::WwwListing::fingerprint($bare), qr/\A[a-f0-9]{40}\z/,
29 'got fingerprint with non-empty repo');
32 my ($json, $host, $port) = @_;
33 my $http = HTTP::Tiny->new;
34 my $res = $http->get("http://$host:$port/");
35 is($res->{status}, 200, 'got HTML listing');
36 like($res->{content}, qr!</html>!si, 'listing looks like HTML');
37 $res = $http->get("http://$host:$port/manifest.js.gz");
38 is($res->{status}, 200, 'got manifest');
40 IO::Uncompress::Gunzip::gunzip(\(delete $res->{content}) => \$tmp);
41 unlike($tmp, qr/"modified":\s*"/, 'modified is an integer');
42 my $manifest = $json->decode($tmp);
43 ok(my $clone = $manifest->{'/alt'}, '/alt in manifest');
44 is($clone->{owner}, 'lorelei', 'owner set');
45 is($clone->{reference}, '/bare', 'reference detected');
46 is($clone->{description}, "we're all clones", 'description read');
47 ok(my $bare = $manifest->{'/bare'}, '/bare in manifest');
48 is($bare->{description}, 'Unnamed repository',
49 'missing $GIT_DIR/description fallback');
51 like($bare->{fingerprint}, qr/\A[a-f0-9]{40}\z/, 'fingerprint');
52 is($clone->{fingerprint}, $bare->{fingerprint}, 'fingerprint matches');
53 is(HTTP::Date::time2str($bare->{modified}),
54 $res->{headers}->{'last-modified'},
55 'modified field and Last-Modified header match');
57 ok(my $v2epoch0 = $manifest->{'/v2/git/0.git'}, 'v2 epoch 0 appeared');
58 like($v2epoch0->{description}, qr/ \[epoch 0\]\z/,
59 'epoch 0 in description');
60 ok(my $v2epoch1 = $manifest->{'/v2/git/1.git'}, 'v2 epoch 1 appeared');
61 like($v2epoch1->{description}, qr/ \[epoch 1\]\z/,
62 'epoch 1 in description');
67 my $err = "$tmpdir/stderr.log";
68 my $out = "$tmpdir/stdout.log";
69 my $alt = "$tmpdir/alt.git";
70 my $cfgfile = "$tmpdir/config";
71 my $v2 = "$tmpdir/v2";
72 my $sock = tcp_server();
73 ok($sock, 'sock created');
74 my ($host, $port) = ($sock->sockhost, $sock->sockport);
75 my @clone = qw(git clone -q -s --bare);
76 is(system(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
78 system(qw(git init --bare -q), "$v2/all.git") == 0 or die;
80 is(system(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i");
82 ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
83 open $fh, '>', "$alt/description" or die;
84 print $fh "we're all clones\n" or die;
86 is(system('git', "--git-dir=$alt", qw(config gitweb.owner lorelei)), 0,
88 ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
89 open $fh, '>', $cfgfile or die;
90 print $fh <<"" or die;
94 inboxdir = $bare->{git_dir}
95 url = http://$host/bare
96 address = bare\@example.com
99 url = http://$host/alt
100 address = alt\@example.com
103 url = http://$host/v2
104 address = v2\@example.com
107 my $env = { PI_CONFIG => $cfgfile };
108 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
109 $td = start_script($cmd, $env, { 3 => $sock });
112 tiny_test($json, $host, $port);
114 skip 'skipping grok-pull integration test', 2 if !which('grok-pull');
116 ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
117 open $fh, '>', "$tmpdir/repos.conf" or die;
118 print $fh <<"" or die;
119 # You can pull from multiple grok mirrors, just create
120 # a separate section for each mirror. The name can be anything.
122 site = http://$host:$port
123 manifest = http://$host:$port/manifest.js.gz
124 toplevel = $tmpdir/mirror
125 mymanifest = $tmpdir/local-manifest.js.gz
129 system(qw(grok-pull -c), "$tmpdir/repos.conf");
130 is($? >> 8, 127, 'grok-pull exit code as expected');
131 for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
132 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
135 # support per-inbox manifests, handy for v2:
136 # /$INBOX/v2/manifest.js.gz
137 open $fh, '>', "$tmpdir/per-inbox.conf" or die;
138 print $fh <<"" or die;
139 # You can pull from multiple grok mirrors, just create
140 # a separate section for each mirror. The name can be anything.
142 site = http://$host:$port
143 manifest = http://$host:$port/v2/manifest.js.gz
144 toplevel = $tmpdir/per-inbox
145 mymanifest = $tmpdir/per-inbox-manifest.js.gz
148 ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
149 system(qw(grok-pull -c), "$tmpdir/per-inbox.conf");
150 is($? >> 8, 127, 'grok-pull exit code as expected');
151 for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
152 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");