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/manifest.js.gz");
35 is($res->{status}, 200, 'got manifest');
37 IO::Uncompress::Gunzip::gunzip(\(delete $res->{content}) => \$tmp);
38 unlike($tmp, qr/"modified":\s*"/, 'modified is an integer');
39 my $manifest = $json->decode($tmp);
40 ok(my $clone = $manifest->{'/alt'}, '/alt in manifest');
41 is($clone->{owner}, 'lorelei', 'owner set');
42 is($clone->{reference}, '/bare', 'reference detected');
43 is($clone->{description}, "we're all clones", 'description read');
44 ok(my $bare = $manifest->{'/bare'}, '/bare in manifest');
45 is($bare->{description}, 'Unnamed repository',
46 'missing $GIT_DIR/description fallback');
48 like($bare->{fingerprint}, qr/\A[a-f0-9]{40}\z/, 'fingerprint');
49 is($clone->{fingerprint}, $bare->{fingerprint}, 'fingerprint matches');
50 is(HTTP::Date::time2str($bare->{modified}),
51 $res->{headers}->{'last-modified'},
52 'modified field and Last-Modified header match');
54 ok(my $v2epoch0 = $manifest->{'/v2/git/0.git'}, 'v2 epoch 0 appeared');
55 like($v2epoch0->{description}, qr/ \[epoch 0\]\z/,
56 'epoch 0 in description');
57 ok(my $v2epoch1 = $manifest->{'/v2/git/1.git'}, 'v2 epoch 1 appeared');
58 like($v2epoch1->{description}, qr/ \[epoch 1\]\z/,
59 'epoch 1 in description');
64 my $err = "$tmpdir/stderr.log";
65 my $out = "$tmpdir/stdout.log";
66 my $alt = "$tmpdir/alt.git";
67 my $cfgfile = "$tmpdir/config";
68 my $v2 = "$tmpdir/v2";
69 my $sock = tcp_server();
70 ok($sock, 'sock created');
71 my ($host, $port) = ($sock->sockhost, $sock->sockport);
72 my @clone = qw(git clone -q -s --bare);
73 is(system(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
75 system(qw(git init --bare -q), "$v2/all.git") == 0 or die;
77 is(system(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i");
79 ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
80 open $fh, '>', "$alt/description" or die;
81 print $fh "we're all clones\n" or die;
83 is(system('git', "--git-dir=$alt", qw(config gitweb.owner lorelei)), 0,
85 ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
86 open $fh, '>', $cfgfile or die;
87 print $fh <<"" or die;
89 inboxdir = $bare->{git_dir}
90 url = http://$host/bare
91 address = bare\@example.com
94 url = http://$host/alt
95 address = alt\@example.com
99 address = v2\@example.com
102 my $env = { PI_CONFIG => $cfgfile };
103 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
104 $td = start_script($cmd, $env, { 3 => $sock });
107 tiny_test($json, $host, $port);
109 skip 'skipping grok-pull integration test', 2 if !which('grok-pull');
111 ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
112 open $fh, '>', "$tmpdir/repos.conf" or die;
113 print $fh <<"" or die;
114 # You can pull from multiple grok mirrors, just create
115 # a separate section for each mirror. The name can be anything.
117 site = http://$host:$port
118 manifest = http://$host:$port/manifest.js.gz
119 toplevel = $tmpdir/mirror
120 mymanifest = $tmpdir/local-manifest.js.gz
124 system(qw(grok-pull -c), "$tmpdir/repos.conf");
125 is($? >> 8, 127, 'grok-pull exit code as expected');
126 for (qw(alt bare v2/git/0.git v2/git/1.git v2/git/2.git)) {
127 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");
130 # support per-inbox manifests, handy for v2:
131 # /$INBOX/v2/manifest.js.gz
132 open $fh, '>', "$tmpdir/per-inbox.conf" or die;
133 print $fh <<"" or die;
134 # You can pull from multiple grok mirrors, just create
135 # a separate section for each mirror. The name can be anything.
137 site = http://$host:$port
138 manifest = http://$host:$port/v2/manifest.js.gz
139 toplevel = $tmpdir/per-inbox
140 mymanifest = $tmpdir/per-inbox-manifest.js.gz
143 ok(mkdir("$tmpdir/per-inbox"), 'prepare single-v2-inbox mirror');
144 system(qw(grok-pull -c), "$tmpdir/per-inbox.conf");
145 is($? >> 8, 127, 'grok-pull exit code as expected');
146 for (qw(v2/git/0.git v2/git/1.git v2/git/2.git)) {
147 ok(-d "$tmpdir/per-inbox/$_", "grok-pull created $_");