test: abstract the test case logic into a shared function

This commit is contained in:
Frantisek Sumsal
2023-05-22 12:39:25 +02:00
parent 7942811255
commit b60d910d12
4 changed files with 37 additions and 34 deletions

View File

@@ -82,6 +82,7 @@ run_subtests_with_signals() {
show_summary
}
# Run all subtests (i.e. files named as testsuite-<testid>.<subtest_name>.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

View File

@@ -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

View File

@@ -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

View File

@@ -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