Browse Source

[tests] use ephemeral ports in tests

avoid spurious test failures due to conflicts with ports in use by
other processes, which might occur when tests use hard-coded ports
master
Glenn Strauss 5 months ago
parent
commit
5b1b9f7824
  1. 8
      tests/404-handler.conf
  2. 65
      tests/LightyTest.pm
  3. 9
      tests/condition.conf
  4. 21
      tests/fastcgi-responder.conf
  5. 8
      tests/lighttpd.conf
  6. 7
      tests/lowercase.conf
  7. 7
      tests/mod-auth.conf
  8. 7
      tests/mod-deflate.conf
  9. 7
      tests/mod-extforward.conf
  10. 5
      tests/mod-fastcgi.t
  11. 4
      tests/mod-proxy.t
  12. 6
      tests/mod-scgi.t
  13. 7
      tests/mod-secdownload.conf
  14. 9
      tests/proxy.conf
  15. 14
      tests/scgi-responder.conf
  16. 9
      tests/var-include.conf

8
tests/404-handler.conf

@ -2,19 +2,13 @@ debug.log-request-handling = "enable"
debug.log-response-header = "enable"
debug.log-request-header = "enable"
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"
server.tag = "Apache 1.3.29"
server.compat-module-load = "disable"
server.modules = (
"mod_cgi",

65
tests/LightyTest.pm

@ -1,12 +1,10 @@
package LightyTest;
use strict;
use IO::Socket;
use Test::More;
use IO::Socket ();
use Test::More; # diag()
use Socket;
use Cwd 'abs_path';
use POSIX qw(:sys_wait_h dup2);
use Errno qw(EADDRINUSE);
sub find_program {
my @DEFAULT_PATHS = ('/usr/bin/', '/usr/local/bin/');
@ -80,7 +78,6 @@ sub new {
if (exists $ENV{LIGHTTPD_EXE_PATH}) {
$self->{LIGHTTPD_PATH} = $ENV{LIGHTTPD_EXE_PATH};
}
$self->{PORT} = 2048;
my ($name, $aliases, $addrtype, $net) = gethostbyaddr(inet_aton("127.0.0.1"), AF_INET);
@ -131,7 +128,8 @@ sub wait_for_port_with_proc {
$timeout--;
# the process is gone, we failed
if (0 != waitpid($child, WNOHANG)) {
require POSIX;
if (0 != waitpid($child, POSIX::WNOHANG)) {
return -1;
}
if (0 >= $timeout) {
@ -144,15 +142,32 @@ sub wait_for_port_with_proc {
return 0;
}
sub bind_ephemeral_tcp_socket {
my $SOCK;
socket($SOCK, PF_INET, SOCK_STREAM, 0) || die "socket: $!";
setsockopt($SOCK, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
bind($SOCK, sockaddr_in(0, INADDR_LOOPBACK)) || die "bind: $!";
my($port) = sockaddr_in(getsockname($SOCK));
return ($SOCK, $port);
}
sub get_ephemeral_tcp_port {
# bind to an ephemeral port, close() it, and return port that was used
# (While there is a race condition before caller may reuse the port,
# the port is likely to remain available for the serialized tests)
my $port;
(undef, $port) = bind_ephemeral_tcp_socket();
return $port;
}
sub start_proc {
my $self = shift;
# kill old proc if necessary
#$self->stop_proc;
if ($self->listening_on($self->{PORT})) {
diag("\nPort ".$self->{PORT}." already in use");
return -1;
}
# listen on localhost and kernel-assigned ephemeral port
my $SOCK;
($SOCK, $self->{PORT}) = bind_ephemeral_tcp_socket();
# pre-process configfile if necessary
#
@ -175,11 +190,26 @@ sub start_proc {
my $child = fork();
if (not defined $child) {
diag("\nFork failed");
close($SOCK);
return -1;
}
if ($child == 0) {
# set up systemd socket activation env vars
$ENV{LISTEN_FDS} = "1";
$ENV{LISTEN_PID} = $$;
listen($SOCK, 1024) || die "listen: $!";
if (fileno($SOCK) != 3) { # SD_LISTEN_FDS_START 3
require POSIX;
POSIX::dup2(fileno($SOCK), 3) || die "dup2: $!";
close($SOCK);
}
else {
require Fcntl;
fcntl($SOCK, Fcntl::F_SETFD, 0); # clr FD_CLOEXEC
}
exec @cmdline or die($?);
}
close($SOCK);
if (0 != $self->wait_for_port_with_proc($self->{PORT}, $child)) {
diag(sprintf('\nThe process %i is not up', $child));
@ -237,15 +267,15 @@ sub handle_http {
print $remote $_;
diag("<< ".$_."\n") if $is_debug;
select(undef, undef, undef, 0.001);
select(undef, undef, undef, 0.0001);
print $remote "\015";
select(undef, undef, undef, 0.001);
select(undef, undef, undef, 0.0001);
print $remote "\012";
select(undef, undef, undef, 0.001);
select(undef, undef, undef, 0.0001);
print $remote "\015";
select(undef, undef, undef, 0.001);
select(undef, undef, undef, 0.0001);
print $remote "\012";
select(undef, undef, undef, 0.001);
select(undef, undef, undef, 0.0001);
}
}
@ -428,7 +458,8 @@ sub spawnfcgi {
setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
bind(SOCK, sockaddr_in($port, $iaddr)) || die "bind: $!";
listen(SOCK, 1024) || die "listen: $!";
dup2(fileno(SOCK), 0) || die "dup2: $!";
require POSIX;
POSIX::dup2(fileno(SOCK), 0) || die "dup2: $!";
exec { $binary } ($binary) or die($?);
} else {
if (0 != $self->wait_for_port_with_proc($port, $child)) {
@ -467,7 +498,7 @@ sub has_crypto {
my $FH;
open($FH, "-|",$self->{LIGHTTPD_PATH}, "-V") || return 0;
while (<$FH>) {
return 1 if (/[+] (?i:OpenSSL|mbedTLS|GnuTLS|WolfSSL|Nettle) support/);
return 1 if (/[+] (?i:OpenSSL|mbedTLS|GnuTLS|WolfSSL|Nettle|NSS crypto) support/);
}
close $FH;
return 0;

9
tests/condition.conf

@ -1,20 +1,13 @@
debug.log-request-handling = "enable"
debug.log-condition-handling = "enable"
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"
server.tag = "Apache 1.3.29"
server.compat-module-load = "disable"
server.modules = (
"mod_redirect",

21
tests/fastcgi-responder.conf

@ -1,14 +1,9 @@
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
debug.log-request-header = "enable"
debug.log-response-header = "enable"
debug.log-request-handling = "enable"
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"
@ -45,7 +40,7 @@ $HTTP["host"] == "auth.example.org" {
"/" => (
"grisu-auth" => (
"host" => "127.0.0.1",
"port" => 10000,
"port" => env.EPHEMERAL_PORT,
"bin-path" => env.SRCDIR + "/fcgi-responder",
"bin-copy-environment" => ( "PATH", "SHELL", "USER", ),
"check-local" => "disable",
@ -54,7 +49,7 @@ $HTTP["host"] == "auth.example.org" {
),
"grisu-resp" => (
"host" => "127.0.0.1",
"port" => 10000,
"port" => env.EPHEMERAL_PORT,
"bin-path" => env.SRCDIR + "/fcgi-responder",
"bin-copy-environment" => ( "PATH", "SHELL", "USER", ),
"check-local" => "disable",
@ -70,7 +65,7 @@ else {
fastcgi.server = (
".php" => ( (
"host" => "127.0.0.1",
"port" => 10000,
"port" => env.EPHEMERAL_PORT,
"bin-path" => env.SRCDIR + "/fcgi-responder",
"bin-copy-environment" => ( "PATH", "SHELL", "USER", ),
"check-local" => "disable",
@ -78,14 +73,14 @@ else {
) ),
"/prefix.fcgi" => ( (
"host" => "127.0.0.1",
"port" => 10000,
"port" => env.EPHEMERAL_PORT,
"bin-path" => env.SRCDIR + "/fcgi-responder",
"max-procs" => 1,
) ),
".fcgi" => (
"grisu" => (
"host" => "127.0.0.1",
"port" => 10000,
"port" => env.EPHEMERAL_PORT,
"bin-path" => env.SRCDIR + "/fcgi-responder",
"bin-copy-environment" => ( "PATH", "SHELL", "USER", ),
"check-local" => "disable",
@ -100,7 +95,7 @@ $HTTP["host"] == "wsgi.example.org" {
fastcgi.server = (
"/" => ( (
"host" => "127.0.0.1",
"port" => 10000,
"port" => env.EPHEMERAL_PORT,
"bin-path" => env.SRCDIR + "/fcgi-responder",
"bin-copy-environment" => ( "PATH", "SHELL", "USER", ),
"check-local" => "disable",

8
tests/lighttpd.conf

@ -2,16 +2,12 @@ debug.log-request-handling = "enable"
debug.log-request-header = "enable"
debug.log-response-header = "enable"
#debug.log-condition-handling = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
## 64 Mbyte ... nice limit
server.max-request-size = 65000
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"

7
tests/lowercase.conf

@ -1,10 +1,5 @@
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"

7
tests/mod-auth.conf

@ -2,13 +2,8 @@ debug.log-request-handling = "enable"
debug.log-request-header = "enable"
debug.log-response-header = "enable"
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"

7
tests/mod-deflate.conf

@ -2,14 +2,9 @@ debug.log-request-handling = "enable"
debug.log-response-header = "disable"
debug.log-request-header = "disable"
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
server.pid-file = env.SRCDIR + "/tmp/lighttpd/lighttpd.pid"
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"

7
tests/mod-extforward.conf

@ -2,13 +2,8 @@ debug.log-request-handling = "enable"
debug.log-response-header = "disable"
debug.log-request-header = "disable"
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"

5
tests/mod-fastcgi.t

@ -19,6 +19,9 @@ SKIP: {
unless ( -x $tf->{BASEDIR}."/tests/fcgi-responder"
|| -x $tf->{BASEDIR}."/tests/fcgi-responder.exe");
my $ephemeral_port = LightyTest->get_ephemeral_tcp_port();
$ENV{EPHEMERAL_PORT} = $ephemeral_port;
$tf->{CONFIGFILE} = 'fastcgi-responder.conf';
ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
@ -196,7 +199,7 @@ EOF
# (might take lighttpd 1 sec to detect backend exit)
select(undef, undef, undef, .5);
for (my $c = 2*20; $c && 0 == $tf->listening_on(10000); --$c) {
for (my $c = 2*20; $c && 0 == $tf->listening_on($ephemeral_port); --$c) {
select(undef, undef, undef, 0.05);
}
$t->{REQUEST} = ( <<EOF

4
tests/mod-proxy.t

@ -20,14 +20,12 @@ my $t;
## 1. the real webserver
## 2. the proxy server
$tf_real->{PORT} = 2048;
$tf_real->{CONFIGFILE} = 'lighttpd.conf';
$tf_proxy->{PORT} = 2050;
$tf_proxy->{CONFIGFILE} = 'proxy.conf';
ok($tf_real->start_proc == 0, "Starting lighttpd") or goto cleanup;
$ENV{EPHEMERAL_PORT} = $tf_real->{PORT};
ok($tf_proxy->start_proc == 0, "Starting lighttpd as proxy") or goto cleanup;
$t->{REQUEST} = ( <<EOF

6
tests/mod-scgi.t

@ -16,8 +16,12 @@ my $t;
SKIP: {
skip "no scgi-responder found", 10 unless -x $tf->{BASEDIR}."/tests/scgi-responder" || -x $tf->{BASEDIR}."/tests/scgi-responder.exe";
my $ephemeral_port = LightyTest->get_ephemeral_tcp_port();
$ENV{EPHEMERAL_PORT} = $ephemeral_port;
$tf->{CONFIGFILE} = 'scgi-responder.conf';
ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
$t->{REQUEST} = ( <<EOF
GET /index.scgi?lf HTTP/1.0
Host: www.example.org
@ -77,7 +81,7 @@ EOF
# (might take lighttpd 1 sec to detect backend exit)
select(undef, undef, undef, .5);
for (my $c = 2*20; $c && 0 == $tf->listening_on(10000); --$c) {
for (my $c = 2*20; $c && 0 == $tf->listening_on($ephemeral_port); --$c) {
select(undef, undef, undef, 0.05);
}
$t->{REQUEST} = ( <<EOF

7
tests/mod-secdownload.conf

@ -2,13 +2,8 @@ debug.log-request-handling = "enable"
debug.log-request-header = "enable"
debug.log-response-header = "enable"
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"

9
tests/proxy.conf

@ -1,10 +1,5 @@
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
## bind to port (default: 80)
server.port = 2050
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"
@ -23,7 +18,7 @@ proxy.debug = 1
proxy.server = ( "" => (
"grisu" => (
"host" => "127.0.0.1",
"port" => 2048,
"port" => env.EPHEMERAL_PORT,
),
))

14
tests/scgi-responder.conf

@ -1,14 +1,9 @@
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
#debug.log-request-header = "enable"
#debug.log-response-header = "enable"
#debug.log-request-handling = "enable"
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"
@ -27,7 +22,7 @@ scgi.server = (
".scgi" => (
"grisu" => (
"host" => "127.0.0.1",
"port" => 10000,
"port" => env.EPHEMERAL_PORT,
"bin-path" => env.SRCDIR + "/scgi-responder",
"check-local" => "disable",
"max-procs" => 1,
@ -39,7 +34,8 @@ scgi.server = (
$HTTP["host"] == "wsgi.example.org" {
scgi.server = (
"/" => ( (
"host" => "127.0.0.1", "port" => 10000,
"host" => "127.0.0.1",
"port" => env.EPHEMERAL_PORT,
"fix-root-scriptname" => "enable",
"check-local" => "disable",
"bin-path" => env.SRCDIR + "/scgi-responder",

9
tests/var-include.conf

@ -1,20 +1,13 @@
debug.log-request-handling = "enable"
#debug.log-condition-handling = "enable"
server.systemd-socket-activation = "enable"
server.document-root = env.SRCDIR + "/tmp/lighttpd/servers/www.example.org/pages/"
## bind to port (default: 80)
server.port = 2048
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.errorlog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.error.log"
server.breakagelog = env.SRCDIR + "/tmp/lighttpd/logs/lighttpd.breakage.log"
server.name = "www.example.org"
server.tag = "Apache 1.3.29"
server.compat-module-load = "disable"
server.modules = (
"mod_redirect",

Loading…
Cancel
Save