]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Improve test discovery logic in get_ports.sh
authorMichał Kępień <michal@isc.org>
Mon, 14 Mar 2022 07:59:32 +0000 (08:59 +0100)
committerMichał Kępień <michal@isc.org>
Mon, 14 Mar 2022 07:59:32 +0000 (08:59 +0100)
The find invocation used by the bin/tests/system/get_ports.sh script
("find . -maxdepth 1 -mindepth 1 -type d") assumes the list of
directories in bin/tests/system/ remains unchanged throughout the run
time of a single system test suite.  With pytest in use and the
conftest.py file now present in bin/tests/system/, that assumption is no
longer true as a __pycache__ directory may be created when the first
pytest-based test is started.  Since the list of names returned by the
above find invocation serves as a fixed-size array of "port range
slots", any changes to that list during a system test suite run may lead
to port assignment collisions [1].

Fix by making the find invocation more nuanced, so that it only returns
names of directories containing test code.  Squash a grep / cut pipeline
into a single awk invocation.

[1] see commit 31e5ca4bd9f50b9e0ad5a88685225afacb073a8a

bin/tests/system/get_ports.sh

index b1d4a8169afa9392618170fa643d8b3c67284a92..b44e3ef6f716c7c703d6528fb777349b4c291f15 100755 (executable)
 # 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.
 
-total_tests=$(find . -maxdepth 1 -mindepth 1 -type d | wc -l)
+get_sorted_test_names() {
+       find . -maxdepth 2 -mindepth 2 -type f \( -name "tests.sh" -o -name "tests*.py" \) | cut -d/ -f2 | sort -u
+}
+
+total_tests=$(get_sorted_test_names | wc -l)
 ports_per_test=20
 
 port_min=5001
@@ -33,7 +37,7 @@ while getopts "p:t:-:" OPT; do
     case "$OPT" in
        p | port) baseport=$OPTARG ;;
        t | test)
-               test_index=$(find . -maxdepth 1 -mindepth 1 -type d | sort | grep -F -x -n "./${OPTARG}" | cut -d: -f1)
+               test_index=$(get_sorted_test_names | awk "/^${OPTARG}\$/ { print NR }")
                if [ -z "${test_index}" ]; then
                        echo "Test '${OPTARG}' not found" >&2
                        exit 1