Skip to content

Commit d9e6269

Browse files
bjackmanshuahkh
authored andcommitted
selftests/run_kselftest.sh: exit with error if tests fail
Parsing KTAP is quite an inconvenience, but most of the time the thing you really want to know is "did anything fail"? Let's give the user the his information without them needing to parse anything. Because of the use of subshells and namespaces, this needs to be communicated via a file. Just write arbitrary data into the file and treat non-empty content as a signal that something failed. In case any user depends on the current behaviour, such as running this from a script with `set -e` and parsing the result for failures afterwards, add a flag they can set to get the old behaviour, namely --no-error-on-fail. Link: https://lore.kernel.org/r/20251111-b4-ksft-error-on-fail-v3-1-0951a51135f6@google.com Signed-off-by: Brendan Jackman <jackmanb@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 26347f8 commit d9e6269

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

tools/testing/selftests/kselftest/runner.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ tap_timeout()
4444
fi
4545
}
4646

47+
report_failure()
48+
{
49+
echo "not ok $*"
50+
echo "$*" >> "$kselftest_failures_file"
51+
}
52+
4753
run_one()
4854
{
4955
DIR="$1"
@@ -105,7 +111,7 @@ run_one()
105111
echo "# $TEST_HDR_MSG"
106112
if [ ! -e "$TEST" ]; then
107113
echo "# Warning: file $TEST is missing!"
108-
echo "not ok $test_num $TEST_HDR_MSG"
114+
report_failure "$test_num $TEST_HDR_MSG"
109115
else
110116
if [ -x /usr/bin/stdbuf ]; then
111117
stdbuf="/usr/bin/stdbuf --output=L "
@@ -123,7 +129,7 @@ run_one()
123129
interpreter=$(head -n 1 "$TEST" | cut -c 3-)
124130
cmd="$stdbuf $interpreter ./$BASENAME_TEST"
125131
else
126-
echo "not ok $test_num $TEST_HDR_MSG"
132+
report_failure "$test_num $TEST_HDR_MSG"
127133
return
128134
fi
129135
fi
@@ -137,9 +143,9 @@ run_one()
137143
echo "ok $test_num $TEST_HDR_MSG # SKIP"
138144
elif [ $rc -eq $timeout_rc ]; then \
139145
echo "#"
140-
echo "not ok $test_num $TEST_HDR_MSG # TIMEOUT $kselftest_timeout seconds"
146+
report_failure "$test_num $TEST_HDR_MSG # TIMEOUT $kselftest_timeout seconds"
141147
else
142-
echo "not ok $test_num $TEST_HDR_MSG # exit=$rc"
148+
report_failure "$test_num $TEST_HDR_MSG # exit=$rc"
143149
fi)
144150
cd - >/dev/null
145151
fi

tools/testing/selftests/run_kselftest.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Usage: $0 [OPTIONS]
3333
-c | --collection COLLECTION Run all tests from COLLECTION
3434
-l | --list List the available collection:test entries
3535
-d | --dry-run Don't actually run any tests
36+
-f | --no-error-on-fail Don't exit with an error just because tests failed
3637
-n | --netns Run each test in namespace
3738
-h | --help Show this usage info
3839
-o | --override-timeout Number of seconds after which we timeout
@@ -44,6 +45,7 @@ COLLECTIONS=""
4445
TESTS=""
4546
dryrun=""
4647
kselftest_override_timeout=""
48+
ERROR_ON_FAIL=true
4749
while true; do
4850
case "$1" in
4951
-s | --summary)
@@ -65,6 +67,9 @@ while true; do
6567
-d | --dry-run)
6668
dryrun="echo"
6769
shift ;;
70+
-f | --no-error-on-fail)
71+
ERROR_ON_FAIL=false
72+
shift ;;
6873
-n | --netns)
6974
RUN_IN_NETNS=1
7075
shift ;;
@@ -105,9 +110,18 @@ if [ -n "$TESTS" ]; then
105110
available="$(echo "$valid" | sed -e 's/ /\n/g')"
106111
fi
107112

113+
kselftest_failures_file="$(mktemp --tmpdir kselftest-failures-XXXXXX)"
114+
export kselftest_failures_file
115+
108116
collections=$(echo "$available" | cut -d: -f1 | sort | uniq)
109117
for collection in $collections ; do
110118
[ -w /dev/kmsg ] && echo "kselftest: Running tests in $collection" >> /dev/kmsg
111119
tests=$(echo "$available" | grep "^$collection:" | cut -d: -f2)
112120
($dryrun cd "$collection" && $dryrun run_many $tests)
113121
done
122+
123+
failures="$(cat "$kselftest_failures_file")"
124+
rm "$kselftest_failures_file"
125+
if "$ERROR_ON_FAIL" && [ "$failures" ]; then
126+
exit 1
127+
fi

0 commit comments

Comments
 (0)