]> Sergey Matveev's repositories - public-inbox.git/blob - t/common.perl
288a0a1928b6c62f34aa1c15f37f023f6508c0fc
[public-inbox.git] / t / common.perl
1 # Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek);
5 use POSIX qw(dup2);
6 use strict;
7 use warnings;
8 use IO::Socket::INET;
9
10 sub tmpdir (;$) {
11         my ($base) = @_;
12         require File::Temp;
13         unless (defined $base) {
14                 ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!);
15         }
16         my $tmpdir = File::Temp->newdir("pi-$base-$$-XXXXXX", TMPDIR => 1);
17         ($tmpdir->dirname, $tmpdir);
18 }
19
20 sub tcp_server () {
21         IO::Socket::INET->new(
22                 LocalAddr => '127.0.0.1',
23                 ReuseAddr => 1,
24                 Proto => 'tcp',
25                 Type => Socket::SOCK_STREAM(),
26                 Listen => 1024,
27                 Blocking => 0,
28         )
29 }
30
31 sub tcp_connect {
32         my ($dest, %opt) = @_;
33         my $s = IO::Socket::INET->new(
34                 Proto => 'tcp',
35                 Type => Socket::SOCK_STREAM(),
36                 PeerAddr => $dest->sockhost . ':' . $dest->sockport,
37                 %opt,
38         );
39         $s->autoflush(1);
40         $s;
41 }
42
43 sub require_git ($;$) {
44         my ($req, $maybe) = @_;
45         my ($req_maj, $req_min) = split(/\./, $req);
46         my ($cur_maj, $cur_min) = (`git --version` =~ /version (\d+)\.(\d+)/);
47
48         my $req_int = ($req_maj << 24) | ($req_min << 16);
49         my $cur_int = ($cur_maj << 24) | ($cur_min << 16);
50         if ($cur_int < $req_int) {
51                 return 0 if $maybe;
52                 plan skip_all => "git $req+ required, have $cur_maj.$cur_min";
53         }
54         1;
55 }
56
57 sub key2script ($) {
58         my ($key) = @_;
59         return $key if $key =~ m!\A/!;
60         # n.b. we may have scripts which don't start with "public-inbox" in
61         # the future:
62         $key =~ s/\A([-\.])/public-inbox$1/;
63         'blib/script/'.$key;
64 }
65
66 sub _prepare_redirects ($) {
67         my ($fhref) = @_;
68         my @x = ([ \*STDIN, '<&' ], [ \*STDOUT, '>&' ], [ \*STDERR, '>&' ]);
69         for (my $fd = 0; $fd <= $#x; $fd++) {
70                 my $fh = $fhref->[$fd] or next;
71                 my ($oldfh, $mode) = @{$x[$fd]};
72                 open $oldfh, $mode, $fh or die "$$oldfh $mode redirect: $!";
73         }
74 }
75
76 # $opt->{run_mode} (or $ENV{TEST_RUN_MODE}) allows chosing between
77 # three ways to spawn our own short-lived Perl scripts for testing:
78 #
79 # 0 - (fork|vfork) + execve, the most realistic but slowest
80 # 1 - preloading and running in a forked subprocess (fast)
81 # 2 - preloading and running in current process (slightly faster than 1)
82 #
83 # 2 is not compatible with scripts which use "exit" (which we'll try to
84 # avoid in the future).
85 # The default is 2.
86 our $run_script_exit_code;
87 sub RUN_SCRIPT_EXIT () { "RUN_SCRIPT_EXIT\n" };
88 sub run_script_exit (;$) {
89         $run_script_exit_code = $_[0] // 0;
90         die RUN_SCRIPT_EXIT;
91 }
92
93 my %cached_scripts;
94 sub key2sub ($) {
95         my ($key) = @_;
96         $cached_scripts{$key} //= do {
97                 my $f = key2script($key);
98                 open my $fh, '<', $f or die "open $f: $!";
99                 my $str = do { local $/; <$fh> };
100                 my ($fc, $rest) = ($key =~ m/([a-z])([a-z0-9]+)\z/);
101                 $fc = uc($fc);
102                 my $pkg = "PublicInbox::TestScript::$fc$rest";
103                 eval <<EOF;
104 package $pkg;
105 use strict;
106 use subs qw(exit);
107
108 *exit = *::run_script_exit;
109 sub main {
110 $str
111         0;
112 }
113 1;
114 EOF
115                 $pkg->can('main');
116         }
117 }
118
119 sub _run_sub ($$$) {
120         my ($sub, $key, $argv) = @_;
121         local @ARGV = @$argv;
122         $run_script_exit_code = undef;
123         my $exit_code = eval { $sub->(@$argv) };
124         if ($@ eq RUN_SCRIPT_EXIT) {
125                 $@ = '';
126                 $exit_code = $run_script_exit_code;
127                 $? = ($exit_code << 8);
128         } elsif (defined($exit_code)) {
129                 $? = ($exit_code << 8);
130         } elsif ($@) { # mimic die() behavior when uncaught
131                 warn "E: eval-ed $key: $@\n";
132                 $? = ($! << 8) if $!;
133                 $? = (255 << 8) if $? == 0;
134         } else {
135                 die "BUG: eval-ed $key: no exit code or \$@\n";
136         }
137 }
138
139 sub run_script ($;$$) {
140         my ($cmd, $env, $opt) = @_;
141         my ($key, @argv) = @$cmd;
142         my $run_mode = $ENV{TEST_RUN_MODE} // $opt->{run_mode} // 1;
143         my $sub = $run_mode == 0 ? undef : key2sub($key);
144         my $fhref = [];
145         my $spawn_opt = {};
146         for my $fd (0..2) {
147                 my $redir = $opt->{$fd};
148                 next unless ref($redir);
149                 open my $fh, '+>', undef or die "open: $!";
150                 $fhref->[$fd] = $fh;
151                 $spawn_opt->{$fd} = fileno($fh);
152                 next if $fd > 0;
153                 $fh->autoflush(1);
154                 print $fh $$redir or die "print: $!";
155                 seek($fh, 0, SEEK_SET) or die "seek: $!";
156         }
157         if ($run_mode == 0) {
158                 # spawn an independent new process, like real-world use cases:
159                 require PublicInbox::Spawn;
160                 my $cmd = [ key2script($key), @argv ];
161                 my $pid = PublicInbox::Spawn::spawn($cmd, $env, $spawn_opt);
162                 defined($pid) or die "spawn: $!";
163                 if (defined $pid) {
164                         my $r = waitpid($pid, 0);
165                         defined($r) or die "waitpid: $!";
166                         $r == $pid or die "waitpid: expected $pid, got $r";
167                 }
168         } else { # localize and run everything in the same process:
169                 local *STDIN = *STDIN;
170                 local *STDOUT = *STDOUT;
171                 local *STDERR = *STDERR;
172                 local %ENV = $env ? (%ENV, %$env) : %ENV;
173                 local %SIG = %SIG;
174                 local $0 = join(' ', @$cmd);
175                 _prepare_redirects($fhref);
176                 _run_sub($sub, $key, \@argv);
177         }
178
179         # slurp the redirects back into user-supplied strings
180         for my $fd (1..2) {
181                 my $fh = $fhref->[$fd] or next;
182                 seek($fh, 0, SEEK_SET) or die "seek: $!";
183                 my $redir = $opt->{$fd};
184                 local $/;
185                 $$redir = <$fh>;
186         }
187         $? == 0;
188 }
189
190 sub wait_for_tail () { sleep(2) }
191
192 sub start_script {
193         my ($cmd, $env, $opt) = @_;
194         my ($key, @argv) = @$cmd;
195         my $run_mode = $ENV{TEST_RUN_MODE} // $opt->{run_mode} // 1;
196         my $sub = $run_mode == 0 ? undef : key2sub($key);
197         my $tail_pid;
198         if (my $tail_cmd = $ENV{TAIL}) {
199                 my @paths;
200                 for (@argv) {
201                         next unless /\A--std(?:err|out)=(.+)\z/;
202                         push @paths, $1;
203                 }
204                 if (@paths) {
205                         defined($tail_pid = fork) or die "fork: $!\n";
206                         if ($tail_pid == 0) {
207                                 # make sure files exist, first
208                                 open my $fh, '>>', $_ for @paths;
209                                 open(STDOUT, '>&STDERR') or die "1>&2: $!";
210                                 exec(split(' ', $tail_cmd), @paths);
211                                 die "$tail_cmd failed: $!";
212                         }
213                         wait_for_tail();
214                 }
215         }
216         defined(my $pid = fork) or die "fork: $!\n";
217         if ($pid == 0) {
218                 # pretend to be systemd (cf. sd_listen_fds(3))
219                 # 3 == SD_LISTEN_FDS_START
220                 my $fd;
221                 for ($fd = 0; 1; $fd++) {
222                         my $s = $opt->{$fd};
223                         last if $fd >= 3 && !defined($s);
224                         next unless $s;
225                         my $fl = fcntl($s, F_GETFD, 0);
226                         if (($fl & FD_CLOEXEC) != FD_CLOEXEC) {
227                                 warn "got FD:".fileno($s)." w/o CLOEXEC\n";
228                         }
229                         fcntl($s, F_SETFD, $fl &= ~FD_CLOEXEC);
230                         dup2(fileno($s), $fd) or die "dup2 failed: $!\n";
231                 }
232                 %ENV = (%ENV, %$env) if $env;
233                 my $fds = $fd - 3;
234                 if ($fds > 0) {
235                         $ENV{LISTEN_PID} = $$;
236                         $ENV{LISTEN_FDS} = $fds;
237                 }
238                 $0 = join(' ', @$cmd);
239                 if ($sub) {
240                         _run_sub($sub, $key, \@argv);
241                         POSIX::_exit($? >> 8);
242                 } else {
243                         exec(key2script($key), @argv);
244                         die "FAIL: ",join(' ', $key, @argv), ": $!\n";
245                 }
246         }
247         TestProcess->new($pid, $tail_pid);
248 }
249
250 package TestProcess;
251 use strict;
252
253 # prevent new threads from inheriting these objects
254 sub CLONE_SKIP { 1 }
255
256 sub new {
257         my ($klass, $pid, $tail_pid) = @_;
258         bless { pid => $pid, tail_pid => $tail_pid, owner => $$ }, $klass;
259 }
260
261 sub kill {
262         my ($self, $sig) = @_;
263         CORE::kill($sig // 'TERM', $self->{pid});
264 }
265
266 sub join {
267         my ($self) = @_;
268         my $pid = delete $self->{pid} or return;
269         my $ret = waitpid($pid, 0);
270         defined($ret) or die "waitpid($pid): $!";
271         $ret == $pid or die "waitpid($pid) != $ret";
272 }
273
274 sub DESTROY {
275         my ($self) = @_;
276         return if $self->{owner} != $$;
277         if (my $tail = delete $self->{tail_pid}) {
278                 ::wait_for_tail();
279                 CORE::kill('TERM', $tail);
280         }
281         my $pid = delete $self->{pid} or return;
282         CORE::kill('TERM', $pid);
283 }
284
285 1;