Testsuite: Assume '' for missing VERSION_ID
[exim.git] / test / lib / Exim / Runtest.pm
1 package Exim::Runtest;
2 use 5.010;
3 use strict;
4 use warnings;
5 use File::Basename;
6 use IO::Socket::INET;
7 use Cwd;
8 use Carp;
9
10 use Exporter;
11 our @ISA = qw(Exporter);
12
13 our @EXPORT_OK = qw(mailgroup dynamic_socket exim_binary flavour flavours);
14 our %EXPORT_TAGS = (
15     all => \@EXPORT_OK,
16 );
17
18 use List::Util qw'shuffle';
19
20 =head1 NAME
21
22 Exim::Runtest - helper functions for the runtest script
23
24 =head1 SYNOPSIS
25
26  use Exim::Runtest;
27  my $foo = Exim::Runtest::foo('foo');
28
29 =head1 DESCRIPTION
30
31 The B<Exim::Runtest> module provides some simple functions
32 for the F<runtest> script. No functions are exported yet.
33
34 =cut
35
36 sub mailgroup {
37     my $group = shift // croak "Need a default group name.";
38
39     croak "Need a group *name*, not a numeric group id."
40         if $group =~ /^\d+$/;
41
42     return $group if getgrnam $group;
43
44     my @groups;
45     setgrent or die "setgrent: $!\n";
46     push @groups, $_ while defined($_ = getgrent);
47     endgrent;
48     return (shuffle @groups)[0];
49 }
50
51 sub dynamic_socket {
52     my $socket;
53     for (my $port = 1024; $port < 65000; $port++) {
54         $socket = IO::Socket::INET->new(
55             LocalHost => '127.0.0.1',
56             LocalPort => $port,
57             Listen => 10,
58             ReuseAddr => 1,
59         ) and return $socket;
60     }
61     croak 'Can not allocate a free port.';
62 }
63
64 sub exim_binary {
65
66     # two simple cases, absolute path or relative path and executable
67     return @_ if $_[0] =~ /^\//;
68     return Cwd::abs_path(shift), @_ if -x $_[0];
69
70     # so we're still here, if the simple approach didn't help.
71
72     # if there is '../exim-snapshot/<build-dir>/exim', use this
73     # if there is '../exim4/<build-dir>/exim', use this
74     # if there is '../exim-*.*/<build-dir>/exim', use the one with the highest version
75     #   4.84 < 4.85RC1 < 4.85RC2 < 4.85 < 4.86RC1 < … < 4.86
76     # if there is '../src/<build-dir>', use this
77     #
78
79     my $prefix = '..';  # was intended for testing.
80
81     # get a list of directories having the "scripts/{os,arch}-type"
82     # scripts
83     my @candidates = grep { -x "$_/scripts/os-type" and -x "$_/scripts/arch-type" }
84         "$prefix/exim-snapshot", "$prefix/exim4", # highest priority
85         (reverse sort {                           # list of exim-*.* directories
86         # split version number from RC number
87         my @a = ($a =~ /(\d+\.\d+)(?:RC(\d+))?/);
88         my @b = ($b =~ /(\d+\.\d+)(?:RC(\d+))?/);
89         # if the versions are not equal, we're fine,
90         # but otherwise we've to compare the RC number, where an
91         # empty RC number is better than a non-empty
92         ($a[0] cmp $b[0]) || (defined $a[1] ? defined $b[1] ? $a[1] cmp $b[1] : -1 : 1)
93         } glob "$prefix/exim-*.*"),
94         "$prefix/src";                       # the "normal" source
95
96     # binaries should be found now depending on the os-type and
97     # arch-type in the directories we got above
98     my @binaries = grep { -x }
99         map { ("$_/exim", "$_/exim4") }
100         map {
101             my $os = `$_/scripts/os-type`;
102             my $arch = `$_/scripts/arch-type`;
103             chomp($os, $arch);
104             "$_/build-$os-$arch" . ($ENV{EXIM_BUILD_SUFFIX} ? ".$ENV{EXIM_BUILD_SUFFIX}" : '');
105         } @candidates;
106
107     return $binaries[0], @_;
108 }
109
110 sub flavour {
111     my $etc = '/etc';
112
113     if (@_) {
114         croak "do not pass a directory, it's for testing only"
115             unless $ENV{HARNESS_ACTIVE};
116         $etc = shift;
117     }
118
119     if (open(my $f, '<', "$etc/os-release")) {
120         local $_ = join '', <$f>;
121         my ($id) = /^ID="?(.*?)"?\s*$/m;
122         my $version = /^VERSION_ID="?(.*?)"?\s*$/m ? $1 : '';
123         return "$id$version";
124     }
125
126     if (open(my $f, '<', "$etc/debian_version")) {
127         chomp(local $_ = <$f>);
128         $_ = int $_;
129         return "debian$_";
130     }
131
132     undef;
133 }
134
135 sub flavours {
136     my %h = map { /\.(\S+)$/, 1 }
137             glob('stdout/*.*'), glob('stderr/*.*');
138     return sort keys %h;
139 }
140
141 1;
142
143 __END__
144
145 =head1 FUNCTIONS
146
147 =over
148
149 =item B<mailgroup>(I<$default>)
150
151 Check if the mailgroup I<$default> exists. Return the checked
152 group name or some other random but existing group.
153
154 =item B<dynamic_socket>()
155
156 Return a dynamically allocated listener socket in the range
157 between 1024 and 65534;
158
159 =item ($binary, @argv) = B<exim_binary>(I<@argv>)
160
161 Find the Exim binary. Consider the first element of I<@argv>
162 and remove it from I<@argv>, if it is an executable binary.
163 Otherwise search the binary (while honouring C<EXIM_BUILD_SUFFIX>,
164 C<../scripts/os-type> and C<../os-arch>) and return the
165 the path to the binary and the unmodified I<@argv>.
166
167 =item B<flavour>()
168
169 Find a hint for the current flavour (Linux distro). It does so by checking
170 typical files in the F</etc> directory.
171
172 =item B<flavours>()
173
174 Return a list of available flavours. It does so by scanning F<stdout/> and
175 F<stderr/> for I<flavour> files (extensions after the numerical prefix.
176
177 =back
178
179 =cut