Skip to content

Commit 2964f6b

Browse files
liuhangbinshuahkh
authored andcommitted
selftests: Use ktap helpers for runner.sh
Instead of manually writing ktap messages, we should use the formal ktap helpers in runner.sh. Brendan did some work in commit d9e6269 ("selftests/run_kselftest.sh: exit with error if tests fail") to make run_kselftest.sh exit with the correct return value. However, the output does not include the total results, such as how many tests passed or failed. Let’s convert all manually printed messages in runner.sh to use the formal ktap helpers. Here are what I changed: 1. Move TAP header from runner.sh to run_kselftest.sh, since run_kselftest.sh is the only caller of run_many(). 2. In run_kselftest.sh, call run_many() in main process to count the pass/fail numbers. 3. In run_kselftest.sh, do not generate kselftest_failures_file. Just use ktap_print_totals to report the result. 4. In runner.sh run_one(), get the return value and use ktap helpers for all pass/fail reporting. This allows counting pass/fail numbers in the main process. 5. In runner.sh run_in_netns(), also return the correct rc, so we can count results during wait. After the change, the printed result looks like: not ok 4 4 selftests: clone3: clone3_cap_checkpoint_restore # exit=1 # Totals: pass:3 fail:1 xfail:0 xpass:0 skip:0 error:0 ]# echo $? 1 Fixed change log commit description errors and long lines: Shuah Khan <skhan@linuxfoundation.org> Tested-by: Brendan Jackman <jackmanb@google.com> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com> Reviewed-by: Brendan Jackman <jackmanb@google.com> Link: https://lore.kernel.org/r/20260225010833.11301-1-liuhangbin@gmail.com Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 132618c commit 2964f6b

2 files changed

Lines changed: 73 additions & 43 deletions

File tree

tools/testing/selftests/kselftest/runner.sh

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
# SPDX-License-Identifier: GPL-2.0
33
#
44
# Runs a set of tests in a given subdirectory.
5-
export skip_rc=4
5+
. $(dirname "$(readlink -e "${BASH_SOURCE[0]}")")/ktap_helpers.sh
66
export timeout_rc=124
77
export logfile=/dev/stdout
88
export per_test_logging=
@@ -44,17 +44,11 @@ tap_timeout()
4444
fi
4545
}
4646

47-
report_failure()
48-
{
49-
echo "not ok $*"
50-
echo "$*" >> "$kselftest_failures_file"
51-
}
52-
5347
run_one()
5448
{
5549
DIR="$1"
5650
TEST="$2"
57-
local test_num="$3"
51+
local rc test_num="$3"
5852

5953
BASENAME_TEST=$(basename $TEST)
6054

@@ -102,16 +96,17 @@ run_one()
10296
# Command line timeout overrides the settings file
10397
if [ -n "$kselftest_override_timeout" ]; then
10498
kselftest_timeout="$kselftest_override_timeout"
105-
echo "# overriding timeout to $kselftest_timeout" >> "$logfile"
99+
ktap_print_msg "overriding timeout to $kselftest_timeout" >> "$logfile"
106100
else
107-
echo "# timeout set to $kselftest_timeout" >> "$logfile"
101+
ktap_print_msg "timeout set to $kselftest_timeout" >> "$logfile"
108102
fi
109103

110104
TEST_HDR_MSG="selftests: $DIR: $BASENAME_TEST"
111105
echo "# $TEST_HDR_MSG"
112106
if [ ! -e "$TEST" ]; then
113-
echo "# Warning: file $TEST is missing!"
114-
report_failure "$test_num $TEST_HDR_MSG"
107+
ktap_print_msg "Warning: file $TEST is missing!"
108+
ktap_test_fail "$test_num $TEST_HDR_MSG"
109+
rc=$KSFT_FAIL
115110
else
116111
if [ -x /usr/bin/stdbuf ]; then
117112
stdbuf="/usr/bin/stdbuf --output=L "
@@ -122,33 +117,38 @@ run_one()
122117
elif [ -x "./ksft_runner.sh" ]; then
123118
cmd="$stdbuf ./ksft_runner.sh ./$BASENAME_TEST"
124119
else
125-
echo "# Warning: file $TEST is not executable"
120+
ktap_print_msg "Warning: file $TEST is not executable"
126121

127122
if [ $(head -n 1 "$TEST" | cut -c -2) = "#!" ]
128123
then
129124
interpreter=$(head -n 1 "$TEST" | cut -c 3-)
130125
cmd="$stdbuf $interpreter ./$BASENAME_TEST"
131126
else
132-
report_failure "$test_num $TEST_HDR_MSG"
133-
return
127+
ktap_test_fail "$test_num $TEST_HDR_MSG"
128+
return $KSFT_FAIL
134129
fi
135130
fi
136131
cd `dirname $TEST` > /dev/null
137-
((((( tap_timeout "$cmd" 2>&1; echo $? >&3) |
132+
(((( tap_timeout "$cmd" 2>&1; echo $? >&3) |
138133
tap_prefix >&4) 3>&1) |
139-
(read xs; exit $xs)) 4>>"$logfile" &&
140-
echo "ok $test_num $TEST_HDR_MSG") ||
141-
(rc=$?; \
142-
if [ $rc -eq $skip_rc ]; then \
143-
echo "ok $test_num $TEST_HDR_MSG # SKIP"
144-
elif [ $rc -eq $timeout_rc ]; then \
145-
echo "#"
146-
report_failure "$test_num $TEST_HDR_MSG # TIMEOUT $kselftest_timeout seconds"
147-
else
148-
report_failure "$test_num $TEST_HDR_MSG # exit=$rc"
149-
fi)
134+
(read xs; exit $xs)) 4>>"$logfile"
135+
rc=$?
136+
case "$rc" in
137+
"$KSFT_PASS")
138+
ktap_test_pass "$test_num $TEST_HDR_MSG";;
139+
"$KSFT_SKIP")
140+
ktap_test_skip "$test_num $TEST_HDR_MSG";;
141+
"$KSFT_XFAIL")
142+
ktap_test_xfail "$test_num $TEST_HDR_MSG";;
143+
"$timeout_rc")
144+
ktap_test_fail "$test_num $TEST_HDR_MSG # TIMEOUT $kselftest_timeout seconds";;
145+
*)
146+
ktap_test_fail "$test_num $TEST_HDR_MSG # exit=$rc";;
147+
esac
150148
cd - >/dev/null
151149
fi
150+
151+
return $rc
152152
}
153153

154154
in_netns()
@@ -164,27 +164,34 @@ in_netns()
164164

165165
run_in_netns()
166166
{
167-
local netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX)
168167
local tmplog="/tmp/$(mktemp -u ${BASENAME_TEST}-XXXXXX)"
168+
local netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX)
169+
local rc
170+
169171
ip netns add $netns
170172
if [ $? -ne 0 ]; then
171-
echo "# Warning: Create namespace failed for $BASENAME_TEST"
172-
echo "not ok $test_num selftests: $DIR: $BASENAME_TEST # Create NS failed"
173+
ktap_print_msg "Warning: Create namespace failed for $BASENAME_TEST"
174+
ktap_test_fail "$test_num selftests: $DIR: $BASENAME_TEST # Create NS failed"
173175
fi
174176
ip -n $netns link set lo up
177+
175178
in_netns $netns &> $tmplog
179+
rc=$?
180+
176181
ip netns del $netns &> /dev/null
182+
# Cat the log at once to avoid parallel netns logs.
177183
cat $tmplog
178184
rm -f $tmplog
185+
return $rc
179186
}
180187

181188
run_many()
182189
{
183-
echo "TAP version 13"
184190
DIR="${PWD#${BASE_DIR}/}"
185191
test_num=0
186-
total=$(echo "$@" | wc -w)
187-
echo "1..$total"
192+
local rc
193+
pids=()
194+
188195
for TEST in "$@"; do
189196
BASENAME_TEST=$(basename $TEST)
190197
test_num=$(( test_num + 1 ))
@@ -194,10 +201,28 @@ run_many()
194201
fi
195202
if [ -n "$RUN_IN_NETNS" ]; then
196203
run_in_netns &
204+
pids+=($!)
197205
else
198206
run_one "$DIR" "$TEST" "$test_num"
199207
fi
200208
done
201209

202-
wait
210+
# These variables are outputs of ktap_helpers.sh but since we've
211+
# run the test in a subprocess we need to update them manually
212+
for pid in "${pids[@]}"; do
213+
wait "$pid"
214+
rc=$?
215+
case "$rc" in
216+
"$KSFT_PASS")
217+
KTAP_CNT_PASS=$((KTAP_CNT_PASS + 1));;
218+
"$KSFT_FAIL")
219+
KTAP_CNT_FAIL=$((KTAP_CNT_FAIL + 1));;
220+
"$KSFT_SKIP")
221+
KTAP_CNT_SKIP=$((KTAP_CNT_SKIP + 1));;
222+
"$KSFT_XFAIL")
223+
KTAP_CNT_XFAIL=$((KTAP_CNT_XFAIL + 1));;
224+
*)
225+
KTAP_CNT_FAIL=$((KTAP_CNT_FAIL + 1));;
226+
esac
227+
done
203228
}

tools/testing/selftests/run_kselftest.sh

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,23 @@ if [ -n "$SKIP" ]; then
121121
done
122122
fi
123123

124-
kselftest_failures_file="$(mktemp --tmpdir kselftest-failures-XXXXXX)"
125-
export kselftest_failures_file
126-
124+
curdir=$(pwd)
125+
total=$(echo "$available" | wc -w)
127126
collections=$(echo "$available" | cut -d: -f1 | sort | uniq)
127+
128+
ktap_print_header
129+
ktap_set_plan "$total"
130+
128131
for collection in $collections ; do
129132
[ -w /dev/kmsg ] && echo "kselftest: Running tests in $collection" >> /dev/kmsg
130133
tests=$(echo "$available" | grep "^$collection:" | cut -d: -f2)
131-
($dryrun cd "$collection" && $dryrun run_many $tests)
134+
$dryrun cd "$collection" && $dryrun run_many $tests
135+
$dryrun cd "$curdir"
132136
done
133137

134-
failures="$(cat "$kselftest_failures_file")"
135-
rm "$kselftest_failures_file"
136-
if "$ERROR_ON_FAIL" && [ "$failures" ]; then
137-
exit 1
138+
ktap_print_totals
139+
if "$ERROR_ON_FAIL" && [ "$KTAP_CNT_FAIL" -ne 0 ]; then
140+
exit "$KSFT_FAIL"
141+
else
142+
exit "$KSFT_PASS"
138143
fi

0 commit comments

Comments
 (0)