From 4847de9de10af60201b594cdc9970df4ad6ba5ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= Date: Mon, 14 Mar 2022 08:59:32 +0100 Subject: [PATCH] Improve test discovery logic in get_ports.sh 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 (cherry picked from commit 4e0d576858fafb79f191e85cc810ec4b611b00d3) --- bin/tests/system/get_ports.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/tests/system/get_ports.sh b/bin/tests/system/get_ports.sh index b1d4a8169a..b44e3ef6f7 100755 --- a/bin/tests/system/get_ports.sh +++ b/bin/tests/system/get_ports.sh @@ -14,7 +14,11 @@ # 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