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>
4 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek);
13 unless (defined $base) {
14 ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!);
16 my $tmpdir = File::Temp->newdir("pi-$base-$$-XXXXXX", TMPDIR => 1);
17 ($tmpdir->dirname, $tmpdir);
21 IO::Socket::INET->new(
22 LocalAddr => '127.0.0.1',
25 Type => Socket::SOCK_STREAM(),
32 my ($dest, %opt) = @_;
33 my $s = IO::Socket::INET->new(
35 Type => Socket::SOCK_STREAM(),
36 PeerAddr => $dest->sockhost . ':' . $dest->sockport,
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+)/);
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) {
52 plan skip_all => "git $req+ required, have $cur_maj.$cur_min";
59 return $key if $key =~ m!\A/!;
60 # n.b. we may have scripts which don't start with "public-inbox" in
62 $key =~ s/\A([-\.])/public-inbox$1/;
66 sub _prepare_redirects ($) {
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: $!";
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:
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)
83 # 2 is not compatible with scripts which use "exit" (which we'll try to
84 # avoid in the future).
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;
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/);
102 my $pkg = "PublicInbox::TestScript::$fc$rest";
108 *exit = *::run_script_exit;
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) {
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;
135 die "BUG: eval-ed $key: no exit code or \$@\n";
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);
147 my $redir = $opt->{$fd};
148 next unless ref($redir);
149 open my $fh, '+>', undef or die "open: $!";
151 $spawn_opt->{$fd} = fileno($fh);
154 print $fh $$redir or die "print: $!";
155 seek($fh, 0, SEEK_SET) or die "seek: $!";
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: $!";
164 my $r = waitpid($pid, 0);
165 defined($r) or die "waitpid: $!";
166 $r == $pid or die "waitpid: expected $pid, got $r";
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;
174 local $0 = join(' ', @$cmd);
175 _prepare_redirects($fhref);
176 _run_sub($sub, $key, \@argv);
179 # slurp the redirects back into user-supplied strings
181 my $fh = $fhref->[$fd] or next;
182 seek($fh, 0, SEEK_SET) or die "seek: $!";
183 my $redir = $opt->{$fd};
190 sub wait_for_tail () { sleep(2) }
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);
198 if (my $tail_cmd = $ENV{TAIL}) {
201 next unless /\A--std(?:err|out)=(.+)\z/;
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: $!";
216 defined(my $pid = fork) or die "fork: $!\n";
218 # pretend to be systemd (cf. sd_listen_fds(3))
219 # 3 == SD_LISTEN_FDS_START
221 for ($fd = 0; 1; $fd++) {
223 last if $fd >= 3 && !defined($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";
229 fcntl($s, F_SETFD, $fl &= ~FD_CLOEXEC);
230 dup2(fileno($s), $fd) or die "dup2 failed: $!\n";
232 %ENV = (%ENV, %$env) if $env;
235 $ENV{LISTEN_PID} = $$;
236 $ENV{LISTEN_FDS} = $fds;
238 $0 = join(' ', @$cmd);
240 _run_sub($sub, $key, \@argv);
241 POSIX::_exit($? >> 8);
243 exec(key2script($key), @argv);
244 die "FAIL: ",join(' ', $key, @argv), ": $!\n";
247 TestProcess->new($pid, $tail_pid);
253 # prevent new threads from inheriting these objects
257 my ($klass, $pid, $tail_pid) = @_;
258 bless { pid => $pid, tail_pid => $tail_pid, owner => $$ }, $klass;
262 my ($self, $sig) = @_;
263 CORE::kill($sig // 'TERM', $self->{pid});
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";
276 return if $self->{owner} != $$;
277 if (my $tail = delete $self->{tail_pid}) {
279 CORE::kill('TERM', $tail);
281 my $pid = delete $self->{pid} or return;
282 CORE::kill('TERM', $pid);