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 File::Temp qw/tempdir/;
9 require './t/common.perl';
10 my @mods = qw(URI::Escape Plack::Builder IPC::Run Digest::SHA HTTP::Tiny
11 IO::Compress::Gzip IO::Uncompress::Gunzip Net::HTTP);
12 foreach my $mod (@mods) {
13 eval("require $mod") or plan skip_all => "$mod missing for $0";
15 use_ok 'PublicInbox::WwwListing';
16 use_ok 'PublicInbox::Git';
18 my $fi_data = './t/git.fast-import-data';
19 my $tmpdir = tempdir('www_listing-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
20 my $bare = PublicInbox::Git->new("$tmpdir/bare.git");
21 is(system(qw(git init -q --bare), $bare->{git_dir}), 0, 'git init --bare');
22 is(PublicInbox::WwwListing::fingerprint($bare), undef,
23 'empty repo has no fingerprint');
25 my $cmd = [ 'git', "--git-dir=$bare->{git_dir}", qw(fast-import --quiet) ];
26 ok(IPC::Run::run($cmd, '<', $fi_data), 'fast-import');
28 like(PublicInbox::WwwListing::fingerprint($bare), qr/\A[a-f0-9]{40}\z/,
29 'got fingerprint with non-empty repo');
32 END { kill 'TERM', $pid if defined $pid };
34 my $json = eval { PublicInbox::WwwListing::_json() };
35 skip "JSON module missing: $@", 1 if $@;
36 my $err = "$tmpdir/stderr.log";
37 my $out = "$tmpdir/stdout.log";
38 my $alt = "$tmpdir/alt.git";
39 my $cfgfile = "$tmpdir/config";
40 my $v2 = "$tmpdir/v2";
41 my $httpd = 'blib/script/public-inbox-httpd';
44 LocalAddr => '127.0.0.1',
50 my $sock = IO::Socket::INET->new(%opts);
51 ok($sock, 'sock created');
52 my ($host, $port) = ($sock->sockhost, $sock->sockport);
53 my @clone = qw(git clone -q -s --bare);
54 is(system(@clone, $bare->{git_dir}, $alt), 0, 'clone shared repo');
57 is(system(@clone, $alt, "$v2/git/$i.git"), 0, "clone epoch $i");
59 ok(open(my $fh, '>', "$v2/inbox.lock"), 'mock a v2 inbox');
60 open $fh, '>', "$alt/description" or die;
61 print $fh "we're all clones\n" or die;
63 is(system('git', "--git-dir=$alt", qw(config gitweb.owner lorelei)), 0,
65 ok(unlink("$bare->{git_dir}/description"), 'removed bare/description');
66 open $fh, '>', $cfgfile or die;
67 print $fh <<"" or die;
69 mainrepo = $bare->{git_dir}
70 url = http://$host/bare
71 address = bare\@example.com
74 url = http://$host/alt
75 address = alt\@example.com
79 address = v2\@example.com
82 my $env = { PI_CONFIG => $cfgfile };
83 my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err" ];
84 $pid = spawn_listener($env, $cmd, [$sock]);
86 my $http = Net::HTTP->new(Host => "$host:$port");
87 $http->write_request(GET => '/manifest.js.gz');
88 my ($code, undef, %h) = $http->read_response_headers;
89 is($code, 200, 'got manifest');
93 my $n = $http->read_entity_body(my $buf, 65536);
94 die unless defined $n;
98 IO::Uncompress::Gunzip::gunzip(\$body => \$tmp);
99 my $manifest = $json->decode($tmp);
100 ok(my $clone = $manifest->{'/alt'}, '/alt in manifest');
101 is($clone->{owner}, 'lorelei', 'owner set');
102 is($clone->{reference}, '/bare', 'reference detected');
103 is($clone->{description}, "we're all clones", 'description read');
104 ok(my $bare = $manifest->{'/bare'}, '/bare in manifest');
105 is($bare->{description}, 'Unnamed repository',
106 'missing $GIT_DIR/description fallback');
108 like($bare->{fingerprint}, qr/\A[a-f0-9]{40}\z/, 'fingerprint');
109 is($clone->{fingerprint}, $bare->{fingerprint}, 'fingerprint matches');
111 is(HTTP::Date::time2str($bare->{modified}), $h{'Last-Modified'},
112 'modified field and Last-Modified header match');
114 ok($manifest->{'/v2/0'}, 'v2 epoch appeared');
116 skip 'skipping grok-pull integration test', 2 if !which('grok-pull');
118 ok(mkdir("$tmpdir/mirror"), 'prepare grok mirror dest');
119 open $fh, '>', "$tmpdir/repos.conf" or die;
120 print $fh <<"" or die;
121 # You can pull from multiple grok mirrors, just create
122 # a separate section for each mirror. The name can be anything.
124 site = http://$host:$port
125 manifest = http://$host:$port/manifest.js.gz
126 toplevel = $tmpdir/mirror
127 mymanifest = $tmpdir/local-manifest.js.gz
131 system(qw(grok-pull -c), "$tmpdir/repos.conf");
132 is($? >> 8, 127, 'grok-pull exit code as expected');
133 for (qw(alt bare v2/0 v2/1 v2/2)) {
134 ok(-d "$tmpdir/mirror/$_", "grok-pull created $_");