diff --git a/test/units/test-control.sh b/test/units/test-control.sh index dd28939cbf5..3e2549d0b30 100644 --- a/test/units/test-control.sh +++ b/test/units/test-control.sh @@ -82,6 +82,7 @@ run_subtests_with_signals() { show_summary } +# Run all subtests (i.e. files named as testsuite-..sh) run_subtests() { local subtests=("${0%.sh}".*.sh) local subtest @@ -100,6 +101,29 @@ run_subtests() { show_summary } +# Run all test cases (i.e. functions prefixed with testcase_ in the current namespace) +run_testcases() { + local testcase testcases + + # Create a list of all functions prefixed with testcase_ + mapfile -t testcases < <(declare -F | awk '$3 ~ /^testcase_/ {print $3;}') + + if [[ "${#testcases[@]}" -eq 0 ]]; then + echo >&2 "No test cases found, this is most likely an error" + exit 1 + fi + + for testcase in "${testcases[@]}"; do + : "+++ $testcase BEGIN +++" + # Note: the subshell here is used purposefully, otherwise we might + # unexpectedly inherit a RETURN trap handler from the called + # function and call it for the second time once we return, + # causing a "double-free" + ("$testcase") + : "+++ $testcase END +++" + done +} + show_summary() {( set +x diff --git a/test/units/testsuite-13.nspawn.sh b/test/units/testsuite-13.nspawn.sh index 0cc96424e63..682f6777356 100755 --- a/test/units/testsuite-13.nspawn.sh +++ b/test/units/testsuite-13.nspawn.sh @@ -28,9 +28,12 @@ set -eux set -o pipefail +# shellcheck source=test/units/test-control.sh +. "$(dirname "$0")"/test-control.sh # shellcheck source=test/units/util.sh . "$(dirname "$0")"/util.sh + export SYSTEMD_LOG_LEVEL=debug export SYSTEMD_LOG_TARGET=journal @@ -838,17 +841,7 @@ matrix_run_one() { return 0 } -# Create a list of all functions prefixed with testcase_ -mapfile -t TESTCASES < <(declare -F | awk '$3 ~ /^testcase_/ {print $3;}') - -if [[ "${#TESTCASES[@]}" -eq 0 ]]; then - echo >&2 "No test cases found, this is most likely an error" - exit 1 -fi - -for testcase in "${TESTCASES[@]}"; do - "$testcase" -done +run_testcases for api_vfs_writable in yes no network; do matrix_run_one no no $api_vfs_writable diff --git a/test/units/testsuite-73.sh b/test/units/testsuite-73.sh index 523acd8d06f..022a70862f0 100755 --- a/test/units/testsuite-73.sh +++ b/test/units/testsuite-73.sh @@ -4,6 +4,8 @@ set -eux set -o pipefail +# shellcheck source=test/units/test-control.sh +. "$(dirname "$0")"/test-control.sh # shellcheck source=test/units/util.sh . "$(dirname "$0")"/util.sh @@ -688,18 +690,7 @@ testcase_locale_gen_leading_space() { export SYSTEMD_KBD_MODEL_MAP=/usr/lib/systemd/tests/testdata/test-keymap-util/kbd-model-map enable_debug - -# Create a list of all functions prefixed with testcase_ -mapfile -t TESTCASES < <(declare -F | awk '$3 ~ /^testcase_/ {print $3;}') - -if [[ "${#TESTCASES[@]}" -eq 0 ]]; then - echo >&2 "No test cases found, this is most likely an error" - exit 1 -fi - -for testcase in "${TESTCASES[@]}"; do - "$testcase" -done +run_testcases touch /testok rm /failed diff --git a/test/units/testsuite-74.machine-id-setup.sh b/test/units/testsuite-74.machine-id-setup.sh index a24f9d299e2..c2b9db51782 100755 --- a/test/units/testsuite-74.machine-id-setup.sh +++ b/test/units/testsuite-74.machine-id-setup.sh @@ -4,6 +4,11 @@ set -eux set -o pipefail +# shellcheck source=test/units/test-control.sh +. "$(dirname "$0")"/test-control.sh +# shellcheck source=test/units/util.sh +. "$(dirname "$0")"/util.sh + root_mock() { local root="${1:?}" @@ -69,14 +74,4 @@ testcase_transient() { systemctl --state=failed --no-legend --no-pager >/failed test ! -s /failed -# Create a list of all functions prefixed with testcase_ -mapfile -t TESTCASES < <(declare -F | awk '$3 ~ /^testcase_/ {print $3;}') - -if [[ "${#TESTCASES[@]}" -eq 0 ]]; then - echo >&2 "No test cases found, this is most likely an error" - exit 1 -fi - -for testcase in "${TESTCASES[@]}"; do - "$testcase" -done +run_testcases