]> Sergey Matveev's repositories - public-inbox.git/blob - t/common.perl
t/common: inline stream_to_string into t/feed.t
[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);
5 use POSIX qw(dup2);
6 use strict;
7 use warnings;
8 use IO::Socket::INET;
9
10 sub tcp_server () {
11         IO::Socket::INET->new(
12                 LocalAddr => '127.0.0.1',
13                 ReuseAddr => 1,
14                 Proto => 'tcp',
15                 Type => Socket::SOCK_STREAM(),
16                 Listen => 1024,
17                 Blocking => 0,
18         )
19 }
20
21 sub unix_server ($) {
22         my $s = IO::Socket::UNIX->new(
23                 Listen => 1024,
24                 Type => Socket::SOCK_STREAM(),
25                 Local => $_[0],
26         );
27         $s->blocking(0);
28         $s;
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 spawn_listener {
44         my ($env, $cmd, $socks) = @_;
45         my $pid = fork;
46         defined $pid or die "fork failed: $!\n";
47         if ($pid == 0) {
48                 # pretend to be systemd (cf. sd_listen_fds(3))
49                 my $fd = 3; # 3 == SD_LISTEN_FDS_START
50                 foreach my $s (@$socks) {
51                         my $fl = fcntl($s, F_GETFD, 0);
52                         if (($fl & FD_CLOEXEC) != FD_CLOEXEC) {
53                                 warn "got FD:".fileno($s)." w/o CLOEXEC\n";
54                         }
55                         fcntl($s, F_SETFD, $fl &= ~FD_CLOEXEC);
56                         dup2(fileno($s), $fd++) or die "dup2 failed: $!\n";
57                 }
58                 $ENV{LISTEN_PID} = $$;
59                 $ENV{LISTEN_FDS} = scalar @$socks;
60                 %ENV = (%ENV, %$env) if $env;
61                 exec @$cmd;
62                 die "FAIL: ",join(' ', @$cmd), ": $!\n";
63         }
64         $pid;
65 }
66
67 sub require_git ($;$) {
68         my ($req, $maybe) = @_;
69         my ($req_maj, $req_min) = split(/\./, $req);
70         my ($cur_maj, $cur_min) = (`git --version` =~ /version (\d+)\.(\d+)/);
71
72         my $req_int = ($req_maj << 24) | ($req_min << 16);
73         my $cur_int = ($cur_maj << 24) | ($cur_min << 16);
74         if ($cur_int < $req_int) {
75                 return 0 if $maybe;
76                 plan skip_all => "git $req+ required, have $cur_maj.$cur_min";
77         }
78         1;
79 }
80
81 1;