]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: abstract the common test parts into a utility script 27603/head
authorFrantisek Sumsal <frantisek@sumsal.cz>
Wed, 10 May 2023 19:12:01 +0000 (21:12 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Wed, 10 May 2023 19:26:26 +0000 (21:26 +0200)
Also, instead of bailing out on the first failed subtest, always run all
subtests and print a summary at the end (with an appropriate exit code).

test/units/test-control.sh [new file with mode: 0644]
test/units/testsuite-07.sh
test/units/testsuite-17.sh
test/units/testsuite-22.sh
test/units/testsuite-23.sh
test/units/testsuite-74.sh
test/units/testsuite-81.sh

diff --git a/test/units/test-control.sh b/test/units/test-control.sh
new file mode 100644 (file)
index 0000000..dd28939
--- /dev/null
@@ -0,0 +1,126 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# shellcheck shell=bash
+
+if [[ "${BASH_SOURCE[0]}" -ef "$0" ]]; then
+    echo >&2 "This file should not be executed directly"
+    exit 1
+fi
+
+declare -i CHILD_PID=0
+PASSED_TESTS=()
+FAILED_TESTS=()
+
+# Like trap, but passes the signal name as the first argument
+trap_with_sig() {
+    local fun="${1:?}"
+    local sig
+    shift
+
+    for sig in "$@"; do
+        # shellcheck disable=SC2064
+        trap "$fun $sig" "$sig"
+    done
+}
+
+# Propagate the caught signal to the current child process
+handle_signal() {
+    local sig="${1:?}"
+
+    if [[ $CHILD_PID -gt 0 ]]; then
+        echo "Propagating signal $sig to child process $CHILD_PID"
+        kill -s "$sig" "$CHILD_PID"
+    fi
+}
+
+# In order to make the handle_signal() stuff above work, we have to execute
+# each script asynchronously, since bash won't execute traps until the currently
+# executed command finishes. This, however, introduces another issue regarding
+# how bash's wait works. Quoting:
+#
+#   When bash is waiting for an asynchronous command via the wait builtin,
+#   the reception of a signal for which a trap has been set will cause the wait
+#   builtin to return immediately with an exit status greater than 128,
+#   immediately after which the trap is executed.
+#
+# In other words - every time we propagate a signal, wait returns with
+# 128+signal, so we have to wait again - repeat until the process dies.
+wait_harder() {
+    local pid="${1:?}"
+
+    while kill -0 "$pid" &>/dev/null; do
+        wait "$pid" || :
+    done
+
+    wait "$pid"
+}
+
+# Like run_subtests, but propagate specified signals to the subtest script
+run_subtests_with_signals() {
+    local subtests=("${0%.sh}".*.sh)
+    local subtest
+
+    if [[ "${#subtests[@]}" -eq 0 ]]; then
+        echo >&2 "No subtests found for file $0"
+        exit 1
+    fi
+
+    if [[ "$#" -eq 0 ]]; then
+        echo >&2 "No signals to propagate were specified"
+        exit 1
+    fi
+
+    trap_with_sig handle_signal "$@"
+
+    for subtest in "${subtests[@]}"; do
+        : "--- $subtest BEGIN ---"
+        "./$subtest" &
+        CHILD_PID=$!
+        wait_harder "$CHILD_PID" && PASSED_TESTS+=("$subtest") || FAILED_TESTS+=("$subtest")
+        : "--- $subtest END ---"
+    done
+
+    show_summary
+}
+
+run_subtests() {
+    local subtests=("${0%.sh}".*.sh)
+    local subtest
+
+    if [[ "${#subtests[@]}" -eq 0 ]]; then
+        echo >&2 "No subtests found for file $0"
+        exit 1
+    fi
+
+    for subtest in "${subtests[@]}"; do
+        : "--- $subtest BEGIN ---"
+        "./$subtest" && PASSED_TESTS+=("$subtest") || FAILED_TESTS+=("$subtest")
+        : "--- $subtest END ---"
+    done
+
+    show_summary
+}
+
+show_summary() {(
+    set +x
+
+    if [[ ${#PASSED_TESTS[@]} -eq 0 && ${#FAILED_TESTS[@]} -eq 0 ]]; then
+        echo >&2 "No tests were executed, this is most likely an error"
+        exit 1
+    fi
+
+    printf "PASSED TESTS: %3d:\n" "${#PASSED_TESTS[@]}"
+    echo   "------------------"
+    for t in "${PASSED_TESTS[@]}"; do
+        echo "$t"
+    done
+
+    if [[ "${#FAILED_TESTS[@]}" -ne 0 ]]; then
+        printf "FAILED TESTS: %3d:\n" "${#FAILED_TESTS[@]}"
+        echo   "------------------"
+        for t in "${FAILED_TESTS[@]}"; do
+            echo "$t"
+        done
+    fi
+
+    [[ "${#FAILED_TESTS[@]}" -eq 0 ]]
+)}
index 8c004a72e520f58c6a6c57d87313f5aca52a2aa7..58d278e1f1f26e07bbba2694fd53d1c99d7f4df9 100755 (executable)
@@ -3,16 +3,16 @@
 set -eux
 set -o pipefail
 
+# shellcheck source=test/units/test-control.sh
+. "$(dirname "$0")"/test-control.sh
+
 : >/failed
 
 # Issue: https://github.com/systemd/systemd/issues/2730
 # See TEST-07-PID1/test.sh for the first "half" of the test
 mountpoint /issue2730
 
-for script in "${0%.sh}".*.sh; do
-    echo "Running $script"
-    "./$script"
-done
+run_subtests
 
 touch /testok
 rm /failed
index b389875ef1ab551c67d34747c01f8d1865760880..72040f69d87f7c4e28be2fc3045369efc35c8d75 100755 (executable)
@@ -3,13 +3,14 @@
 set -eux
 set -o pipefail
 
+# shellcheck source=test/units/test-control.sh
+. "$(dirname "$0")"/test-control.sh
+
 : >/failed
 
 udevadm settle
 
-for t in "${0%.sh}".*.sh; do
-    echo "Running $t"; ./"$t"
-done
+run_subtests
 
 touch /testok
 rm /failed
index 43823f1d466fd85663d760b9eb6d0a3dabbfeffd..5a07e7b78c272b42fe2a322dc024425882bff668 100755 (executable)
@@ -3,11 +3,12 @@
 set -eux
 set -o pipefail
 
+# shellcheck source=test/units/test-control.sh
+. "$(dirname "$0")"/test-control.sh
+
 : >/failed
 
-for t in "${0%.sh}".*.sh; do
-    echo "Running $t"; ./"$t"
-done
+run_subtests
 
 touch /testok
 rm /failed
index 34899070f1790acb7ec00951ac2c58e67f7989eb..3be645e20ae319fc619568e87b7c26b2c2a97ff5 100755 (executable)
@@ -5,62 +5,11 @@ set -o pipefail
 
 : >/failed
 
-declare -i CHILD_PID=0
+# shellcheck source=test/units/test-control.sh
+. "$(dirname "$0")"/test-control.sh
 
-# Note: all the signal shenanigans are necessary for the Upholds= tests
-
-# Like trap, but passes the signal name as the first argument
-trap_with_sig() {
-    local fun="${1:?}"
-    local sig
-    shift
-
-    for sig in "$@"; do
-        # shellcheck disable=SC2064
-        trap "$fun $sig" "$sig"
-    done
-}
-
-# Propagate the caught signal to the current child process
-handle_signal() {
-    local sig="${1:?}"
-
-    if [[ $CHILD_PID -gt 0 ]]; then
-        echo "Propagating signal $sig to child process $CHILD_PID"
-        kill -s "$sig" "$CHILD_PID"
-    fi
-}
-
-# In order to make the handle_signal() stuff above work, we have to execute
-# each script asynchronously, since bash won't execute traps until the currently
-# executed command finishes. This, however, introduces another issue regarding
-# how bash's wait works. Quoting:
-#
-#   When bash is waiting for an asynchronous command via the wait builtin,
-#   the reception of a signal for which a trap has been set will cause the wait
-#   builtin to return immediately with an exit status greater than 128,
-#   immediately after which the trap is executed.
-#
-# In other words - every time we propagate a signal, wait returns with
-# 128+signal, so we have to wait again - repeat until the process dies.
-wait_harder() {
-    local pid="${1:?}"
-
-    while kill -0 "$pid" &>/dev/null; do
-        wait "$pid" || :
-    done
-
-    wait "$pid"
-}
-
-trap_with_sig handle_signal SIGUSR1 SIGUSR2 SIGRTMIN+1
-
-for script in "${0%.sh}".*.sh; do
-    echo "Running $script"
-    "./$script" &
-    CHILD_PID=$!
-    wait_harder "$CHILD_PID"
-done
+# Note: the signal shenanigans are necessary for the Upholds= tests
+run_subtests_with_signals SIGUSR1 SIGUSR2 SIGRTMIN+1
 
 touch /testok
 rm /failed
index 13c767e490e96182a0481745e269ee4068769bd1..5a07e7b78c272b42fe2a322dc024425882bff668 100755 (executable)
@@ -3,12 +3,12 @@
 set -eux
 set -o pipefail
 
+# shellcheck source=test/units/test-control.sh
+. "$(dirname "$0")"/test-control.sh
+
 : >/failed
 
-for script in "${0%.sh}".*.sh; do
-    echo "Running $script"
-    "./$script"
-done
+run_subtests
 
 touch /testok
 rm /failed
index 13c767e490e96182a0481745e269ee4068769bd1..5a07e7b78c272b42fe2a322dc024425882bff668 100755 (executable)
@@ -3,12 +3,12 @@
 set -eux
 set -o pipefail
 
+# shellcheck source=test/units/test-control.sh
+. "$(dirname "$0")"/test-control.sh
+
 : >/failed
 
-for script in "${0%.sh}".*.sh; do
-    echo "Running $script"
-    "./$script"
-done
+run_subtests
 
 touch /testok
 rm /failed