]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Refactor the script that gets/sets the ports for system tests
authorOndřej Surý <ondrej@isc.org>
Mon, 27 Apr 2020 08:47:08 +0000 (10:47 +0200)
committerOndřej Surý <ondrej@isc.org>
Mon, 27 Apr 2020 09:43:30 +0000 (11:43 +0200)
The current script used ephemeral port range which clashed with the
ports used by the tools (dig, ...), and the range always started with
the first port and there was 100 ports allocated for each system test.

In this commit, the first port has been randomized, the get_ports.sh
script outputs the variables (the output has to be eval'ed from run.sh)
and there's less waste in the port range.

bin/tests/system/.gitignore
bin/tests/system/Makefile.am
bin/tests/system/get_base_port.sh [deleted file]
bin/tests/system/get_ports.sh [new file with mode: 0755]
bin/tests/system/run.sh.in
util/copyrights

index 8205de1dbc83823475208dfc8409f2da588dc795..0c66e8d2cc9c291b04ea3c13661cef019eaa67ab 100644 (file)
@@ -12,8 +12,8 @@ named.run
 parallel.mk
 /*.log
 /*.trs
-/get_base_port.state
-/get_base_port.lock
+/get_ports.state
+/get_ports.lock
 /run.sh
 /run.log
 /start.sh
index 43e00b9486764eec21ae1e1bea236c8ff94b3b01..0dda39f679dfb90e8b49ae471bf4fb823a936035 100644 (file)
@@ -170,9 +170,9 @@ LOG_DRIVER = $(top_srcdir)/custom-test-driver
 AM_LOG_DRIVER_FLAGS = $(LOG_DRIVER_V)
 
 LOG_COMPILER = $(builddir)/run.sh
-AM_LOG_FLAGS = -r -p "$$("$(srcdir)/get_base_port.sh")"
+AM_LOG_FLAGS = -r
 
 $(TESTS): run.sh
 
 clean-local:
-       -rm -f get_base_port.state get_base_port.lock
+       -rm -f get_ports.state get_ports.lock
diff --git a/bin/tests/system/get_base_port.sh b/bin/tests/system/get_base_port.sh
deleted file mode 100755 (executable)
index 7c1b2d3..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-#!/bin/sh
-#
-# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
-#
-# This Source Code Form is subject to the terms of the Mozilla Public
-# License, v. 2.0. If a copy of the MPL was not distributed with this
-# file, You can obtain one at http://mozilla.org/MPL/2.0/.
-#
-# See the COPYRIGHT file distributed with this work for additional
-# information regarding copyright ownership.
-
-# This script is a 'port' broker.  It keeps track of ports given to the
-# individual system subtests, so every test is given a unique port range.
-
-lockfile=get_base_port.lock
-statefile=get_base_port.state
-
-ephemeral_port_min=49152
-ephemeral_port_max=65535
-
-get_base_port() {
-    if ( set -o noclobber; echo "$$" > "${lockfile}" ) 2> /dev/null; then
-       trap 'rm -f "${lockfile}"; exit $?' INT TERM EXIT
-
-       base_port=$(cat "${statefile}" 2>/dev/null)
-
-       if [ -z "${base_port}" ]; then
-           base_port="${ephemeral_port_min}"
-       fi
-
-       if [ "$((base_port+100))" -gt "${ephemeral_port_max}" ]; then
-           base_port="${ephemeral_port_min}"
-       fi
-
-       echo $((base_port+100)) > get_base_port.state
-
-       # clean up after yourself, and release your trap
-       rm -f "${lockfile}"
-       trap - INT TERM EXIT
-       echo "${base_port}"
-    else
-       echo 0
-    fi
-}
-
-tries=10
-
-while [ "${tries}" -gt 0 ]; do
-    base_port=$(get_base_port)
-    if [ "${base_port}" -gt 0 ]; then
-       echo "${base_port}"
-       exit 0
-    fi
-    sleep 1
-    tries=$((tries-1))
-done
-
-exit 1
diff --git a/bin/tests/system/get_ports.sh b/bin/tests/system/get_ports.sh
new file mode 100755 (executable)
index 0000000..30c066b
--- /dev/null
@@ -0,0 +1,94 @@
+#!/bin/sh
+#
+# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# See the COPYRIGHT file distributed with this work for additional
+# information regarding copyright ownership.
+
+# This script is a 'port' broker.  It keeps track of ports given to the
+# individual system subtests, so every test is given a unique port range.
+
+lockfile=get_ports.lock
+statefile=get_ports.state
+
+port_min=5001
+port_max=32767
+
+get_random() {
+    dd if=/dev/urandom bs=1 count=2 2>/dev/null | od -tu2 -An
+}
+
+get_port() {
+    tries=10
+    port=0
+    while [ "${tries}" -gt 0 ]; do
+       if ( set -o noclobber; echo "$$" > "${lockfile}" ) 2> /dev/null; then
+           trap 'rm -f "${lockfile}"; exit $?' INT TERM EXIT
+
+           port=$(cat "${statefile}" 2>/dev/null)
+
+           if [ -z "${port}" ]; then
+               if [ "$1" -gt 0 ]; then
+                   port="$1"
+               else
+                   port_range=$((port_max-port_min))
+                   port=$(($(get_random)%port_range+port_range))
+               fi
+           fi
+
+           if [ "$((port+1))" -gt "${port_max}" ]; then
+               port="${port_min}"
+           fi
+
+           echo $((port+1)) > get_ports.state
+
+           # clean up after yourself, and release your trap
+           rm -f "${lockfile}"
+           trap - INT TERM EXIT
+
+           # we have our port
+           break
+       fi
+       sleep 1
+       tries=$((tries-1))
+    done
+    if [ "$port" -eq 0 ]; then
+       exit 1
+    fi
+    echo "$port"
+}
+
+baseport=0
+while getopts "p:-:" OPT; do
+    if [ "$OPT" = "-" ] && [ -n "$OPTARG" ]; then
+       OPT="${OPTARG%%=*}"
+       OPTARG="${OPTARG#$OPT}"
+       OPTARG="${OPTARG#=}"
+    fi
+
+    # shellcheck disable=SC2214
+    case "$OPT" in
+       p | port) baseport=$OPTARG ;;
+       -) break ;;
+       *) echo "invalid option" >&2; exit 1 ;;
+    esac
+done
+
+echo "export PORT=$(get_port "$baseport")"
+echo "export EXTRAPORT1=$(get_port)"
+echo "export EXTRAPORT2=$(get_port)"
+echo "export EXTRAPORT3=$(get_port)"
+echo "export EXTRAPORT4=$(get_port)"
+echo "export EXTRAPORT5=$(get_port)"
+echo "export EXTRAPORT6=$(get_port)"
+echo "export EXTRAPORT7=$(get_port)"
+echo "export EXTRAPORT8=$(get_port)"
+echo "export CONTROLPORT=$(get_port)"
+
+# Local Variables:
+# sh-basic-offset: 4
+# End:
index f7d9735a73d52f1ae043f25c6db582faa8294527..5e4095ea67d4bac07a794b2f6fdc605a92c1c636 100644 (file)
@@ -17,6 +17,7 @@ top_builddir=@top_builddir@
 builddir=@abs_builddir@
 srcdir=@abs_srcdir@
 
+# shellcheck source=conf.sh
 . ${builddir}/conf.sh
 
 SYSTEMTESTTOP="$(cd -P -- "${builddir}" && pwd -P)"
@@ -29,7 +30,8 @@ date_with_args() (
 )
 
 stopservers=true
-baseport=5300
+# baseport == 0 means random
+baseport=0
 
 if [ "${SYSTEMTEST_NO_CLEAN:-0}" -eq 1 ]; then
        clean=false
@@ -39,7 +41,7 @@ fi
 
 do_run=false
 log_flags="-r"
-while getopts "knp:r-:" OPT; do
+while getopts "sknp:r-:" OPT; do
        log_flags="$log_flags -$OPT$OPTARG"
        if [ "$OPT" = "-" ] && [ -n "$OPTARG" ]; then
                OPT="${OPTARG%%=*}"
@@ -47,6 +49,7 @@ while getopts "knp:r-:" OPT; do
                OPTARG="${OPTARG#=}"
        fi
 
+       # shellcheck disable=SC2214
        case "$OPT" in
                k | keep) stopservers=false ;;
                n | noclean) clean=false ;;
@@ -61,8 +64,11 @@ done
 shift $((OPTIND-1))
 
 if ! $do_run; then
-       env - TESTS="$1" TEST_SUITE_LOG=run.log LOG_DRIVER_FLAGS="--verbose yes --color-tests yes" LOG_FLAGS="$log_flags" make -e check
-       exit $?
+    if [ "$baseport" -eq 0 ]; then
+       log_flags="$log_flags -p 5300"
+    fi
+    env - TESTS="$*" TEST_SUITE_LOG=run.log LOG_DRIVER_FLAGS="--verbose yes --color-tests yes" LOG_FLAGS="$log_flags" make -e check
+    exit $?
 fi
 
 if [ $# -eq 0 ]; then
@@ -94,64 +100,18 @@ if [ ! -d "${systest}" ]; then
     exit 1
 fi
 
-# Define the number of ports allocated for each test, and the lowest and
-# highest valid values for the "-p" option.
-#
-# The lowest valid value is one more than the highest privileged port number
-# (1024).
-#
-# The highest valid value is calculated by noting that the value passed on the
-# command line is the lowest port number in a block of "numports" consecutive
-# ports and that the highest valid port number is 65,535.
-numport=100
-minvalid=$((1024 + 1))
-maxvalid=$((65535 - numport + 1))
-
-if ! [ "$baseport" -eq "$baseport" ] > /dev/null 2>&1; then
-    echofail "$0: $systest: must specify a numeric value for the port" >&2
-    exit 1
-elif [ "$baseport" -lt "$minvalid" ] || [ "$baseport" -gt "$maxvalid" ]; then
-    echofail "$0: $systest: the specified port must be in the range $minvalid to $maxvalid" >&2
-    exit 1
-fi
 
-# Name the first 10 ports in the set (it is assumed that each test has access
+# Get the first 10 ports in the set (it is assumed that each test has access
 # to ten or more ports): the query port, the control port and eight extra
 # ports.  Since the lowest numbered port (specified in the command line)
 # will usually be a multiple of 10, the names are chosen so that if this is
 # true, the last digit of EXTRAPORTn is "n".
-PORT=$baseport
-EXTRAPORT1=$((baseport + 1))
-EXTRAPORT2=$((baseport + 2))
-EXTRAPORT3=$((baseport + 3))
-EXTRAPORT4=$((baseport + 4))
-EXTRAPORT5=$((baseport + 5))
-EXTRAPORT6=$((baseport + 6))
-EXTRAPORT7=$((baseport + 7))
-EXTRAPORT8=$((baseport + 8))
-CONTROLPORT=$((baseport + 9))
-
-LOWPORT=$baseport
-HIGHPORT=$((baseport + numport - 1))
-
-export PORT
-export EXTRAPORT1
-export EXTRAPORT2
-export EXTRAPORT3
-export EXTRAPORT4
-export EXTRAPORT5
-export EXTRAPORT6
-export EXTRAPORT7
-export EXTRAPORT8
-export CONTROLPORT
-
-export LOWPORT
-export HIGHPORT
+eval "$(${srcdir}/get_ports.sh -p "$baseport")"
 
 echostart "S:$systest:$(date_with_args)"
 echoinfo  "T:$systest:1:A"
 echoinfo  "A:$systest:System test $systest"
-echoinfo  "I:$systest:PORTRANGE:${LOWPORT} - ${HIGHPORT}"
+echoinfo  "I:$systest:PORTS:${PORT},${EXTRAPORT1},${EXTRAPORT2},${EXTRAPORT3},${EXTRAPORT4},${EXTRAPORT5},${EXTRAPORT6},${EXTRAPORT7},${EXTRAPORT8},${CONTROLPORT}"
 
 $PERL ${srcdir}/testsock.pl -p "$PORT"  || {
     echowarn "I:$systest:Network interface aliases not set up.  Skipping test."
@@ -236,7 +196,7 @@ else
         echofail "R:$systest:FAIL"
         # Do not clean up - we need the evidence.
        find "$systest/" -name 'core*' -or -name '*.core' | while read -r coredump; do
-               SYSTESTDIR="$systest"
+               export SYSTESTDIR="$systest"
                echoinfo "D:$systest:backtrace from $coredump start"
                binary=$(gdb --batch --core="$coredump" | sed -ne "s/Core was generated by \`//;s/ .*'.$//p;")
                "${top_builddir}/libtool" --mode=execute gdb \
index a16a02e5ecee391f5e47df220c5ea145f79bf608..e13441fee792954ce38ff1cbfe68488bdebb8289 100644 (file)
 ./bin/tests/system/geoip2/prereq.sh            SH      2019,2020
 ./bin/tests/system/geoip2/setup.sh             SH      2019,2020
 ./bin/tests/system/geoip2/tests.sh             SH      2019,2020
-./bin/tests/system/get_base_port.sh            SH      2020
+./bin/tests/system/get_ports.sh                        SH      2020
 ./bin/tests/system/glue/clean.sh               SH      2000,2001,2004,2007,2012,2014,2015,2016,2018,2019,2020
 ./bin/tests/system/glue/fi.good                        X       2000,2001,2018,2019,2020
 ./bin/tests/system/glue/noglue.good            X       2000,2001,2018,2019,2020