Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 d9e6269e3303
("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>

authored by

Hangbin Liu and committed by
Shuah Khan
2964f6b8 132618c5

+73 -43
+60 -35
tools/testing/selftests/kselftest/runner.sh
··· 1 - #!/bin/sh 1 + #!/bin/bash 2 2 # SPDX-License-Identifier: GPL-2.0 3 3 # 4 4 # Runs a set of tests in a given subdirectory. 5 - export skip_rc=4 5 + . $(dirname "$(readlink -e "${BASH_SOURCE[0]}")")/ktap_helpers.sh 6 6 export timeout_rc=124 7 7 export logfile=/dev/stdout 8 8 export per_test_logging= ··· 44 44 fi 45 45 } 46 46 47 - report_failure() 48 - { 49 - echo "not ok $*" 50 - echo "$*" >> "$kselftest_failures_file" 51 - } 52 - 53 47 run_one() 54 48 { 55 49 DIR="$1" 56 50 TEST="$2" 57 - local test_num="$3" 51 + local rc test_num="$3" 58 52 59 53 BASENAME_TEST=$(basename $TEST) 60 54 ··· 96 102 # Command line timeout overrides the settings file 97 103 if [ -n "$kselftest_override_timeout" ]; then 98 104 kselftest_timeout="$kselftest_override_timeout" 99 - echo "# overriding timeout to $kselftest_timeout" >> "$logfile" 105 + ktap_print_msg "overriding timeout to $kselftest_timeout" >> "$logfile" 100 106 else 101 - echo "# timeout set to $kselftest_timeout" >> "$logfile" 107 + ktap_print_msg "timeout set to $kselftest_timeout" >> "$logfile" 102 108 fi 103 109 104 110 TEST_HDR_MSG="selftests: $DIR: $BASENAME_TEST" 105 111 echo "# $TEST_HDR_MSG" 106 112 if [ ! -e "$TEST" ]; then 107 - echo "# Warning: file $TEST is missing!" 108 - report_failure "$test_num $TEST_HDR_MSG" 113 + ktap_print_msg "Warning: file $TEST is missing!" 114 + ktap_test_fail "$test_num $TEST_HDR_MSG" 115 + rc=$KSFT_FAIL 109 116 else 110 117 if [ -x /usr/bin/stdbuf ]; then 111 118 stdbuf="/usr/bin/stdbuf --output=L " ··· 117 122 elif [ -x "./ksft_runner.sh" ]; then 118 123 cmd="$stdbuf ./ksft_runner.sh ./$BASENAME_TEST" 119 124 else 120 - echo "# Warning: file $TEST is not executable" 125 + ktap_print_msg "Warning: file $TEST is not executable" 121 126 122 127 if [ $(head -n 1 "$TEST" | cut -c -2) = "#!" ] 123 128 then 124 129 interpreter=$(head -n 1 "$TEST" | cut -c 3-) 125 130 cmd="$stdbuf $interpreter ./$BASENAME_TEST" 126 131 else 127 - report_failure "$test_num $TEST_HDR_MSG" 128 - return 132 + ktap_test_fail "$test_num $TEST_HDR_MSG" 133 + return $KSFT_FAIL 129 134 fi 130 135 fi 131 136 cd `dirname $TEST` > /dev/null 132 - ((((( tap_timeout "$cmd" 2>&1; echo $? >&3) | 137 + (((( tap_timeout "$cmd" 2>&1; echo $? >&3) | 133 138 tap_prefix >&4) 3>&1) | 134 - (read xs; exit $xs)) 4>>"$logfile" && 135 - echo "ok $test_num $TEST_HDR_MSG") || 136 - (rc=$?; \ 137 - if [ $rc -eq $skip_rc ]; then \ 138 - echo "ok $test_num $TEST_HDR_MSG # SKIP" 139 - elif [ $rc -eq $timeout_rc ]; then \ 140 - echo "#" 141 - report_failure "$test_num $TEST_HDR_MSG # TIMEOUT $kselftest_timeout seconds" 142 - else 143 - report_failure "$test_num $TEST_HDR_MSG # exit=$rc" 144 - fi) 139 + (read xs; exit $xs)) 4>>"$logfile" 140 + rc=$? 141 + case "$rc" in 142 + "$KSFT_PASS") 143 + ktap_test_pass "$test_num $TEST_HDR_MSG";; 144 + "$KSFT_SKIP") 145 + ktap_test_skip "$test_num $TEST_HDR_MSG";; 146 + "$KSFT_XFAIL") 147 + ktap_test_xfail "$test_num $TEST_HDR_MSG";; 148 + "$timeout_rc") 149 + ktap_test_fail "$test_num $TEST_HDR_MSG # TIMEOUT $kselftest_timeout seconds";; 150 + *) 151 + ktap_test_fail "$test_num $TEST_HDR_MSG # exit=$rc";; 152 + esac 145 153 cd - >/dev/null 146 154 fi 155 + 156 + return $rc 147 157 } 148 158 149 159 in_netns() ··· 164 164 165 165 run_in_netns() 166 166 { 167 - local netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX) 168 167 local tmplog="/tmp/$(mktemp -u ${BASENAME_TEST}-XXXXXX)" 168 + local netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX) 169 + local rc 170 + 169 171 ip netns add $netns 170 172 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" 173 175 fi 174 176 ip -n $netns link set lo up 177 + 175 178 in_netns $netns &> $tmplog 179 + rc=$? 180 + 176 181 ip netns del $netns &> /dev/null 182 + # Cat the log at once to avoid parallel netns logs. 177 183 cat $tmplog 178 184 rm -f $tmplog 185 + return $rc 179 186 } 180 187 181 188 run_many() 182 189 { 183 - echo "TAP version 13" 184 190 DIR="${PWD#${BASE_DIR}/}" 185 191 test_num=0 186 - total=$(echo "$@" | wc -w) 187 - echo "1..$total" 192 + local rc 193 + pids=() 194 + 188 195 for TEST in "$@"; do 189 196 BASENAME_TEST=$(basename $TEST) 190 197 test_num=$(( test_num + 1 )) ··· 201 194 fi 202 195 if [ -n "$RUN_IN_NETNS" ]; then 203 196 run_in_netns & 197 + pids+=($!) 204 198 else 205 199 run_one "$DIR" "$TEST" "$test_num" 206 200 fi 207 201 done 208 202 209 - wait 203 + # These variables are outputs of ktap_helpers.sh but since we've 204 + # run the test in a subprocess we need to update them manually 205 + for pid in "${pids[@]}"; do 206 + wait "$pid" 207 + rc=$? 208 + case "$rc" in 209 + "$KSFT_PASS") 210 + KTAP_CNT_PASS=$((KTAP_CNT_PASS + 1));; 211 + "$KSFT_FAIL") 212 + KTAP_CNT_FAIL=$((KTAP_CNT_FAIL + 1));; 213 + "$KSFT_SKIP") 214 + KTAP_CNT_SKIP=$((KTAP_CNT_SKIP + 1));; 215 + "$KSFT_XFAIL") 216 + KTAP_CNT_XFAIL=$((KTAP_CNT_XFAIL + 1));; 217 + *) 218 + KTAP_CNT_FAIL=$((KTAP_CNT_FAIL + 1));; 219 + esac 220 + done 210 221 }
+13 -8
tools/testing/selftests/run_kselftest.sh
··· 121 121 done 122 122 fi 123 123 124 - kselftest_failures_file="$(mktemp --tmpdir kselftest-failures-XXXXXX)" 125 - export kselftest_failures_file 126 - 124 + curdir=$(pwd) 125 + total=$(echo "$available" | wc -w) 127 126 collections=$(echo "$available" | cut -d: -f1 | sort | uniq) 127 + 128 + ktap_print_header 129 + ktap_set_plan "$total" 130 + 128 131 for collection in $collections ; do 129 132 [ -w /dev/kmsg ] && echo "kselftest: Running tests in $collection" >> /dev/kmsg 130 133 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" 132 136 done 133 137 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" 138 143 fi