1 #!/bin/sh 1 #!/bin/sh 2 # SPDX-License-Identifier: GPL-2.0 2 # SPDX-License-Identifier: GPL-2.0 3 # 3 # 4 # Runs a set of tests in a given subdirectory. 4 # Runs a set of tests in a given subdirectory. 5 export skip_rc=4 5 export skip_rc=4 6 export timeout_rc=124 6 export timeout_rc=124 7 export logfile=/dev/stdout 7 export logfile=/dev/stdout 8 export per_test_logging= 8 export per_test_logging= 9 export RUN_IN_NETNS= 9 export RUN_IN_NETNS= 10 10 11 # Defaults for "settings" file fields: 11 # Defaults for "settings" file fields: 12 # "timeout" how many seconds to let each test 12 # "timeout" how many seconds to let each test run before running 13 # over our soft timeout limit. 13 # over our soft timeout limit. 14 export kselftest_default_timeout=45 14 export kselftest_default_timeout=45 15 15 16 # There isn't a shell-agnostic way to find the 16 # There isn't a shell-agnostic way to find the path of a sourced file, 17 # so we must rely on BASE_DIR being set to fin 17 # so we must rely on BASE_DIR being set to find other tools. 18 if [ -z "$BASE_DIR" ]; then 18 if [ -z "$BASE_DIR" ]; then 19 echo "Error: BASE_DIR must be set befo 19 echo "Error: BASE_DIR must be set before sourcing." >&2 20 exit 1 20 exit 1 21 fi 21 fi 22 22 23 TR_CMD=$(command -v tr) 23 TR_CMD=$(command -v tr) 24 24 25 # If Perl is unavailable, we must fall back to 25 # If Perl is unavailable, we must fall back to line-at-a-time prefixing 26 # with sed instead of unbuffered output. 26 # with sed instead of unbuffered output. 27 tap_prefix() 27 tap_prefix() 28 { 28 { 29 if [ ! -x /usr/bin/perl ]; then 29 if [ ! -x /usr/bin/perl ]; then 30 sed -e 's/^/# /' 30 sed -e 's/^/# /' 31 else 31 else 32 "$BASE_DIR"/kselftest/prefix.p 32 "$BASE_DIR"/kselftest/prefix.pl 33 fi 33 fi 34 } 34 } 35 35 36 tap_timeout() 36 tap_timeout() 37 { 37 { 38 # Make sure tests will time out if uti 38 # Make sure tests will time out if utility is available. 39 if [ -x /usr/bin/timeout ] ; then 39 if [ -x /usr/bin/timeout ] ; then 40 /usr/bin/timeout --foreground 40 /usr/bin/timeout --foreground "$kselftest_timeout" \ 41 /usr/bin/timeout "$kse 41 /usr/bin/timeout "$kselftest_timeout" $1 42 else 42 else 43 $1 43 $1 44 fi 44 fi 45 } 45 } 46 46 47 run_one() 47 run_one() 48 { 48 { 49 DIR="$1" 49 DIR="$1" 50 TEST="$2" 50 TEST="$2" 51 local test_num="$3" 51 local test_num="$3" 52 52 53 BASENAME_TEST=$(basename $TEST) 53 BASENAME_TEST=$(basename $TEST) 54 54 55 # Reset any "settings"-file variables. 55 # Reset any "settings"-file variables. 56 export kselftest_timeout="$kselftest_d 56 export kselftest_timeout="$kselftest_default_timeout" 57 57 58 # Safe default if tr not available 58 # Safe default if tr not available 59 kselftest_cmd_args_ref="KSELFTEST_ARGS 59 kselftest_cmd_args_ref="KSELFTEST_ARGS" 60 60 61 # Optional arguments for this command, 61 # Optional arguments for this command, possibly defined as an 62 # environment variable built using the 62 # environment variable built using the test executable in all 63 # uppercase and sanitized substituting 63 # uppercase and sanitized substituting non acceptable shell 64 # variable name characters with "_" as 64 # variable name characters with "_" as in: 65 # 65 # 66 # KSELFTEST_<UPPERCASE_SANITIZED 66 # KSELFTEST_<UPPERCASE_SANITIZED_TESTNAME>_ARGS="<options>" 67 # 67 # 68 # e.g. 68 # e.g. 69 # 69 # 70 # rtctest --> KSELFTEST_RTCTEST_ 70 # rtctest --> KSELFTEST_RTCTEST_ARGS="/dev/rtc1" 71 # 71 # 72 # cpu-on-off-test.sh --> KSELFTE 72 # cpu-on-off-test.sh --> KSELFTEST_CPU_ON_OFF_TEST_SH_ARGS="-a -p 10" 73 # 73 # 74 if [ -n "$TR_CMD" ]; then 74 if [ -n "$TR_CMD" ]; then 75 BASENAME_SANITIZED=$(echo "$BA 75 BASENAME_SANITIZED=$(echo "$BASENAME_TEST" | \ 76 $TR_CM 76 $TR_CMD -d "[:blank:][:cntrl:]" | \ 77 $TR_CM 77 $TR_CMD -c "[:alnum:]_" "_" | \ 78 $TR_CM 78 $TR_CMD [:lower:] [:upper:]) 79 kselftest_cmd_args_ref="KSELFT 79 kselftest_cmd_args_ref="KSELFTEST_${BASENAME_SANITIZED}_ARGS" 80 fi 80 fi 81 81 82 # Load per-test-directory kselftest "s 82 # Load per-test-directory kselftest "settings" file. 83 settings="$BASE_DIR/$DIR/settings" 83 settings="$BASE_DIR/$DIR/settings" 84 if [ -r "$settings" ] ; then 84 if [ -r "$settings" ] ; then 85 while read line ; do 85 while read line ; do 86 # Skip comments. 86 # Skip comments. 87 if echo "$line" | grep 87 if echo "$line" | grep -q '^#'; then 88 continue 88 continue 89 fi 89 fi 90 field=$(echo "$line" | 90 field=$(echo "$line" | cut -d= -f1) 91 value=$(echo "$line" | 91 value=$(echo "$line" | cut -d= -f2-) 92 eval "kselftest_$field 92 eval "kselftest_$field"="$value" 93 done < "$settings" 93 done < "$settings" 94 fi 94 fi 95 95 96 # Command line timeout overrides the s 96 # Command line timeout overrides the settings file 97 if [ -n "$kselftest_override_timeout" 97 if [ -n "$kselftest_override_timeout" ]; then 98 kselftest_timeout="$kselftest_ 98 kselftest_timeout="$kselftest_override_timeout" 99 echo "# overriding timeout to 99 echo "# overriding timeout to $kselftest_timeout" >> "$logfile" 100 else 100 else 101 echo "# timeout set to $kselft 101 echo "# timeout set to $kselftest_timeout" >> "$logfile" 102 fi 102 fi 103 103 104 TEST_HDR_MSG="selftests: $DIR: $BASENA 104 TEST_HDR_MSG="selftests: $DIR: $BASENAME_TEST" 105 echo "# $TEST_HDR_MSG" 105 echo "# $TEST_HDR_MSG" 106 if [ ! -e "$TEST" ]; then 106 if [ ! -e "$TEST" ]; then 107 echo "# Warning: file $TEST is 107 echo "# Warning: file $TEST is missing!" 108 echo "not ok $test_num $TEST_H 108 echo "not ok $test_num $TEST_HDR_MSG" 109 else 109 else 110 if [ -x /usr/bin/stdbuf ]; the 110 if [ -x /usr/bin/stdbuf ]; then 111 stdbuf="/usr/bin/stdbu 111 stdbuf="/usr/bin/stdbuf --output=L " 112 fi 112 fi 113 eval kselftest_cmd_args="\$${k 113 eval kselftest_cmd_args="\$${kselftest_cmd_args_ref:-}" 114 if [ -x "$TEST" ]; then 114 if [ -x "$TEST" ]; then 115 cmd="$stdbuf ./$BASENA 115 cmd="$stdbuf ./$BASENAME_TEST $kselftest_cmd_args" 116 elif [ -x "./ksft_runner.sh" ] 116 elif [ -x "./ksft_runner.sh" ]; then 117 cmd="$stdbuf ./ksft_ru 117 cmd="$stdbuf ./ksft_runner.sh ./$BASENAME_TEST" 118 else 118 else 119 echo "# Warning: file 119 echo "# Warning: file $TEST is not executable" 120 120 121 if [ $(head -n 1 "$TES 121 if [ $(head -n 1 "$TEST" | cut -c -2) = "#!" ] 122 then 122 then 123 interpreter=$( 123 interpreter=$(head -n 1 "$TEST" | cut -c 3-) 124 cmd="$stdbuf $ 124 cmd="$stdbuf $interpreter ./$BASENAME_TEST" 125 else 125 else 126 echo "not ok $ 126 echo "not ok $test_num $TEST_HDR_MSG" 127 return 127 return 128 fi 128 fi 129 fi 129 fi 130 cd `dirname $TEST` > /dev/null 130 cd `dirname $TEST` > /dev/null 131 ((((( tap_timeout "$cmd" 2>&1; 131 ((((( tap_timeout "$cmd" 2>&1; echo $? >&3) | 132 tap_prefix >&4) 3>&1) 132 tap_prefix >&4) 3>&1) | 133 (read xs; exit $xs)) 4 133 (read xs; exit $xs)) 4>>"$logfile" && 134 echo "ok $test_num $TEST_HDR_M 134 echo "ok $test_num $TEST_HDR_MSG") || 135 (rc=$?; \ 135 (rc=$?; \ 136 if [ $rc -eq $skip_rc ]; then 136 if [ $rc -eq $skip_rc ]; then \ 137 echo "ok $test_num $TE 137 echo "ok $test_num $TEST_HDR_MSG # SKIP" 138 elif [ $rc -eq $timeout_rc ]; 138 elif [ $rc -eq $timeout_rc ]; then \ 139 echo "#" 139 echo "#" 140 echo "not ok $test_num 140 echo "not ok $test_num $TEST_HDR_MSG # TIMEOUT $kselftest_timeout seconds" 141 else 141 else 142 echo "not ok $test_num 142 echo "not ok $test_num $TEST_HDR_MSG # exit=$rc" 143 fi) 143 fi) 144 cd - >/dev/null 144 cd - >/dev/null 145 fi 145 fi 146 } 146 } 147 147 148 in_netns() 148 in_netns() 149 { 149 { 150 local name=$1 150 local name=$1 151 ip netns exec $name bash <<-EOF 151 ip netns exec $name bash <<-EOF 152 BASE_DIR=$BASE_DIR 152 BASE_DIR=$BASE_DIR 153 source $BASE_DIR/kselftest/run 153 source $BASE_DIR/kselftest/runner.sh 154 logfile=$logfile 154 logfile=$logfile 155 run_one $DIR $TEST $test_num 155 run_one $DIR $TEST $test_num 156 EOF 156 EOF 157 } 157 } 158 158 159 run_in_netns() 159 run_in_netns() 160 { 160 { 161 local netns=$(mktemp -u ${BASENAME_TES 161 local netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX) 162 local tmplog="/tmp/$(mktemp -u ${BASEN 162 local tmplog="/tmp/$(mktemp -u ${BASENAME_TEST}-XXXXXX)" 163 ip netns add $netns 163 ip netns add $netns 164 if [ $? -ne 0 ]; then 164 if [ $? -ne 0 ]; then 165 echo "# Warning: Create namesp 165 echo "# Warning: Create namespace failed for $BASENAME_TEST" 166 echo "not ok $test_num selftes 166 echo "not ok $test_num selftests: $DIR: $BASENAME_TEST # Create NS failed" 167 fi 167 fi 168 ip -n $netns link set lo up 168 ip -n $netns link set lo up 169 in_netns $netns &> $tmplog 169 in_netns $netns &> $tmplog 170 ip netns del $netns &> /dev/null 170 ip netns del $netns &> /dev/null 171 cat $tmplog 171 cat $tmplog 172 rm -f $tmplog 172 rm -f $tmplog 173 } 173 } 174 174 175 run_many() 175 run_many() 176 { 176 { 177 echo "TAP version 13" 177 echo "TAP version 13" 178 DIR="${PWD#${BASE_DIR}/}" 178 DIR="${PWD#${BASE_DIR}/}" 179 test_num=0 179 test_num=0 180 total=$(echo "$@" | wc -w) 180 total=$(echo "$@" | wc -w) 181 echo "1..$total" 181 echo "1..$total" 182 for TEST in "$@"; do 182 for TEST in "$@"; do 183 BASENAME_TEST=$(basename $TEST 183 BASENAME_TEST=$(basename $TEST) 184 test_num=$(( test_num + 1 )) 184 test_num=$(( test_num + 1 )) 185 if [ -n "$per_test_logging" ]; 185 if [ -n "$per_test_logging" ]; then 186 logfile="/tmp/$BASENAM 186 logfile="/tmp/$BASENAME_TEST" 187 cat /dev/null > "$logf 187 cat /dev/null > "$logfile" 188 fi 188 fi 189 if [ -n "$RUN_IN_NETNS" ]; the 189 if [ -n "$RUN_IN_NETNS" ]; then 190 run_in_netns & 190 run_in_netns & 191 else 191 else 192 run_one "$DIR" "$TEST" 192 run_one "$DIR" "$TEST" "$test_num" 193 fi 193 fi 194 done 194 done 195 195 196 wait 196 wait 197 } 197 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.