]> Sergey Matveev's repositories - public-inbox.git/blob - xt/git-http-backend.t
f2ae44fe870c169bb33b1f01e2d793f2d56512cd
[public-inbox.git] / xt / git-http-backend.t
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Ensure buffering behavior in -httpd doesn't cause runaway memory use
5 # or data corruption
6 use strict;
7 use warnings;
8 use Test::More;
9 use POSIX qw(setsid);
10 use PublicInbox::TestCommon;
11 use PublicInbox::Spawn qw(which);
12
13 my $git_dir = $ENV{GIANT_GIT_DIR};
14 plan 'skip_all' => 'GIANT_GIT_DIR not defined' unless $git_dir;
15 require_mods(qw(BSD::Resource Plack::Util Plack::Builder
16                 HTTP::Date HTTP::Status Net::HTTP));
17 my $psgi = "./t/git-http-backend.psgi";
18 my ($tmpdir, $for_destroy) = tmpdir();
19 my $err = "$tmpdir/stderr.log";
20 my $out = "$tmpdir/stdout.log";
21 my $sock = tcp_server();
22 my $host = $sock->sockhost;
23 my $port = $sock->sockport;
24 my $td;
25
26 my $get_maxrss = sub {
27         my $http = Net::HTTP->new(Host => "$host:$port");
28         ok($http, 'Net::HTTP object created for maxrss');
29         $http->write_request(GET => '/');
30         my ($code, $mess, %h) = $http->read_response_headers;
31         is($code, 200, 'success reading maxrss');
32         my $n = $http->read_entity_body(my $buf, 256);
33         ok(defined $n, 'read response body');
34         like($buf, qr/\A\d+\n\z/, 'got memory response');
35         ok(int($buf) > 0, 'got non-zero memory response');
36         int($buf);
37 };
38
39 {
40         ok($sock, 'sock created');
41         my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err", $psgi ];
42         $td = start_script($cmd, undef, { 3 => $sock });
43 }
44 my $mem_a = $get_maxrss->();
45
46 SKIP: {
47         my $max = 0;
48         my $pack;
49         my $glob = "$git_dir/objects/pack/pack-*.pack";
50         foreach my $f (glob($glob)) {
51                 my $n = -s $f;
52                 if ($n > $max) {
53                         $max = $n;
54                         $pack = $f;
55                 }
56         }
57         skip "no packs found in $git_dir" unless defined $pack;
58         if ($pack !~ m!(/objects/pack/pack-[a-f0-9]{40}.pack)\z!) {
59                 skip "bad pack name: $pack";
60         }
61         my $url = $1;
62         my $http = Net::HTTP->new(Host => "$host:$port");
63         ok($http, 'Net::HTTP object created');
64         $http->write_request(GET => $url);
65         my ($code, $mess, %h) = $http->read_response_headers;
66         is(200, $code, 'got 200 success for pack');
67         is($max, $h{'Content-Length'}, 'got expected Content-Length for pack');
68
69         # no $http->read_entity_body, here, since we want to force buffering
70         foreach my $i (1..3) {
71                 sleep 1;
72                 my $diff = $get_maxrss->() - $mem_a;
73                 note "${diff}K memory increase after $i seconds";
74                 ok($diff < 1024, 'no bloating caused by slow dumb client');
75         }
76 }
77
78 SKIP: { # make sure Last-Modified + If-Modified-Since works with curl
79         my $nr = 6;
80         skip 'no description', $nr unless -f "$git_dir/description";
81         my $mtime = (stat(_))[9];
82         my $curl = which('curl');
83         skip 'curl(1) not found', $nr unless $curl;
84         my $url = "http://$host:$port/description";
85         my $dst = "$tmpdir/desc";
86         is(system($curl, qw(-RsSf), '-o', $dst, $url), 0, 'curl -R');
87         is((stat($dst))[9], $mtime, 'curl used remote mtime');
88         is(system($curl, qw(-sSf), '-z', $dst, '-o', "$dst.2", $url), 0,
89                 'curl -z noop');
90         ok(!-e "$dst.2", 'no modification, nothing retrieved');
91         utime(0, 0, $dst) or die "utime failed: $!";
92         is(system($curl, qw(-sSfR), '-z', $dst, '-o', "$dst.2", $url), 0,
93                 'curl -z updates');
94         ok(-e "$dst.2", 'faked modification, got new file retrieved');
95 }
96
97 {
98         my $c = fork;
99         if ($c == 0) {
100                 setsid();
101                 exec qw(git clone -q --mirror), "http://$host:$port/",
102                         "$tmpdir/mirror.git";
103                 die "Failed start git clone: $!\n";
104         }
105         select(undef, undef, undef, 0.1);
106         foreach my $i (1..10) {
107                 is(1, kill('STOP', -$c), 'signaled clone STOP');
108                 sleep 1;
109                 ok(kill('CONT', -$c), 'continued clone');
110                 my $diff = $get_maxrss->() - $mem_a;
111                 note "${diff}K memory increase after $i seconds";
112                 ok($diff < 2048, 'no bloating caused by slow smart client');
113         }
114         ok(kill('CONT', -$c), 'continued clone');
115         is($c, waitpid($c, 0), 'reaped wayward slow clone');
116         is($?, 0, 'clone did not error out');
117         note 'clone done, fsck-ing clone result...';
118         is(0, system("git", "--git-dir=$tmpdir/mirror.git",
119                         qw(fsck --no-progress)),
120                 'fsck did not report corruption');
121
122         my $diff = $get_maxrss->() - $mem_a;
123         note "${diff}K memory increase after smart clone";
124         ok($diff < 2048, 'no bloating caused by slow smart client');
125 }
126
127 {
128         ok($td->kill, 'killed httpd');
129         $td->join;
130 }
131
132 done_testing();