[PATCH] selftests/zram: emit KTAP output

Song Hu <[email protected]>
Newsgroups org.kernel.vger.linux-kselftest,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
zram.sh, the kselftest target, prints raw banners and runs zram01.sh
and zram02.sh ignoring their exit status: both scripts always exit 0
and report the result with a plain "[PASS]"/"[FAIL]" echo, so the
harness always sees success no matter what the tests did.

Emit the TAP header and plan up front, report each sub-test from its
exit code and fold its output into "#" diagnostics, and skip both
sub-tests through the ktap helpers when not run as root.  zram01.sh
and zram02.sh now exit non-zero on failure, and check_prereqs() prints
a "1..0 # SKIP" line for standalone runs.  No test logic changes.

Tested on x86_64: both sub-tests pass as root, and are reported as
SKIP with the plan and totals lines when not run as root.

Signed-off-by: Song Hu <[email protected]>
---
 tools/testing/selftests/zram/zram.sh | 43 +++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/tools/testing/selftests/zram/zram.sh b/tools/testing/selftests/zram/zram.sh
index b0b91d9b0dc2..e39a84420d18 100755
--- a/tools/testing/selftests/zram/zram.sh
+++ b/tools/testing/selftests/zram/zram.sh
@@ -1,18 +1,39 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
-TCID="zram.sh"
 
-. ./zram_lib.sh
+# KTAP output helpers (ktap_test_pass, ktap_test_fail, ...).
+DIR="$(dirname "$(readlink -f "$0")")"
+# shellcheck source=../kselftest/ktap_helpers.sh
+source "${DIR}"/../kselftest/ktap_helpers.sh
 
-run_zram () {
-echo "--------------------"
-echo "running zram tests"
-echo "--------------------"
-./zram01.sh
-echo ""
-./zram02.sh
+ktap_print_header
+ktap_set_plan 2
+
+if [ "$(id -u)" -ne 0 ]; then
+	ktap_test_skip "zram01.sh: must be run as root"
+	ktap_test_skip "zram02.sh: must be run as root"
+	ktap_finished
+fi
+
+# Run a sub-test, fold its output into "# " diagnostic lines and report its
+# exit code as the KTAP result.
+run_one()
+{
+	local script=$1
+
+	"${DIR}/$script" 2>&1 | sed 's/^/# /'
+	local rc=${PIPESTATUS[0]}
+
+	if [ "$rc" -eq 0 ]; then
+		ktap_test_pass "$script"
+	elif [ "$rc" -eq "$KSFT_SKIP" ]; then
+		ktap_test_skip "$script"
+	else
+		ktap_test_fail "$script"
+	fi
 }
 
-check_prereqs
+run_one zram01.sh
+run_one zram02.sh
 
-run_zram
+ktap_finished
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.