OpenLLS 1.1 - Testsuite
[exim.git] / test / lib / Exim / Runtest.pm
CommitLineData
1f187290 1package Exim::Runtest;
87e93574 2use 5.010;
1f187290
HSHR
3use strict;
4use warnings;
6336058c 5use File::Basename;
b369d470 6use IO::Socket::INET;
fefe59d9 7use Cwd;
1f187290
HSHR
8use Carp;
9
d54f9577
HSHR
10use Exporter;
11our @ISA = qw(Exporter);
12
6336058c 13our @EXPORT_OK = qw(mailgroup dynamic_socket exim_binary flavour flavours);
87e93574
HSHR
14our %EXPORT_TAGS = (
15 all => \@EXPORT_OK,
16);
17
1f187290
HSHR
18use List::Util qw'shuffle';
19
10012250
HSHR
20=head1 NAME
21
22Exim::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
31The B<Exim::Runtest> module provides some simple functions
32for the F<runtest> script. No functions are exported yet.
33
34=cut
1f187290 35
1f187290 36sub mailgroup {
b1227303 37 my $group = shift // croak "Need a default group name.";
1f187290
HSHR
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];
b369d470
HSHR
49}
50
51sub 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}
1f187290 63
fefe59d9
HSHR
64sub 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
6336058c
HSHR
110sub 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;
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
135sub flavours {
136 my %h = map { /\.(\S+)$/, 1 }
137 glob('stdout/*.*'), glob('stderr/*.*');
138 return sort keys %h;
139}
fefe59d9 140
1f187290 1411;
10012250
HSHR
142
143__END__
144
145=head1 FUNCTIONS
146
147=over
148
149=item B<mailgroup>(I<$default>)
150
151Check if the mailgroup I<$default> exists. Return the checked
152group name or some other random but existing group.
153
154=item B<dynamic_socket>()
155
156Return a dynamically allocated listener socket in the range
157between 1024 and 65534;
158
fefe59d9
HSHR
159=item ($binary, @argv) = B<exim_binary>(I<@argv>)
160
161Find the Exim binary. Consider the first element of I<@argv>
162and remove it from I<@argv>, if it is an executable binary.
163Otherwise search the binary (while honouring C<EXIM_BUILD_SUFFIX>,
164C<../scripts/os-type> and C<../os-arch>) and return the
165the path to the binary and the unmodified I<@argv>.
166
6336058c
HSHR
167=item B<flavour>()
168
169Find a hint for the current flavour (Linux distro). It does so by checking
170typical files in the F</etc> directory.
171
172=item B<flavours>()
173
174Return a list of available flavours. It does so by scanning F<stdout/> and
175F<stderr/> for I<flavour> files (extensions after the numerical prefix.
176
10012250
HSHR
177=back
178
179=cut