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