198 lines · bash
1#!/bin/sh2# SPDX-License-Identifier: GPL-2.03#4# Runs a set of tests in a given subdirectory.5export skip_rc=46export timeout_rc=1247export logfile=/dev/stdout8export per_test_logging=9export RUN_IN_NETNS=10 11# Defaults for "settings" file fields:12# "timeout" how many seconds to let each test run before running13# over our soft timeout limit.14export kselftest_default_timeout=4515 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 find other tools.18if [ -z "$BASE_DIR" ]; then19 echo "Error: BASE_DIR must be set before sourcing." >&220 exit 121fi22 23TR_CMD=$(command -v tr)24 25# If Perl is unavailable, we must fall back to line-at-a-time prefixing26# with sed instead of unbuffered output.27tap_prefix()28{29 if [ ! -x /usr/bin/perl ]; then30 sed -e 's/^/# /'31 else32 "$BASE_DIR"/kselftest/prefix.pl33 fi34}35 36tap_timeout()37{38 # Make sure tests will time out if utility is available.39 if [ -x /usr/bin/timeout ] ; then40 /usr/bin/timeout --foreground "$kselftest_timeout" \41 /usr/bin/timeout "$kselftest_timeout" $142 else43 $144 fi45}46 47run_one()48{49 DIR="$1"50 TEST="$2"51 local test_num="$3"52 53 BASENAME_TEST=$(basename $TEST)54 55 # Reset any "settings"-file variables.56 export kselftest_timeout="$kselftest_default_timeout"57 58 # Safe default if tr not available59 kselftest_cmd_args_ref="KSELFTEST_ARGS"60 61 # Optional arguments for this command, possibly defined as an62 # environment variable built using the test executable in all63 # uppercase and sanitized substituting non acceptable shell64 # variable name characters with "_" as in:65 #66 # KSELFTEST_<UPPERCASE_SANITIZED_TESTNAME>_ARGS="<options>"67 #68 # e.g.69 #70 # rtctest --> KSELFTEST_RTCTEST_ARGS="/dev/rtc1"71 #72 # cpu-on-off-test.sh --> KSELFTEST_CPU_ON_OFF_TEST_SH_ARGS="-a -p 10"73 #74 if [ -n "$TR_CMD" ]; then75 BASENAME_SANITIZED=$(echo "$BASENAME_TEST" | \76 $TR_CMD -d "[:blank:][:cntrl:]" | \77 $TR_CMD -c "[:alnum:]_" "_" | \78 $TR_CMD [:lower:] [:upper:])79 kselftest_cmd_args_ref="KSELFTEST_${BASENAME_SANITIZED}_ARGS"80 fi81 82 # Load per-test-directory kselftest "settings" file.83 settings="$BASE_DIR/$DIR/settings"84 if [ -r "$settings" ] ; then85 while read line ; do86 # Skip comments.87 if echo "$line" | grep -q '^#'; then88 continue89 fi90 field=$(echo "$line" | cut -d= -f1)91 value=$(echo "$line" | cut -d= -f2-)92 eval "kselftest_$field"="$value"93 done < "$settings"94 fi95 96 # Command line timeout overrides the settings file97 if [ -n "$kselftest_override_timeout" ]; then98 kselftest_timeout="$kselftest_override_timeout"99 echo "# overriding timeout to $kselftest_timeout" >> "$logfile"100 else101 echo "# timeout set to $kselftest_timeout" >> "$logfile"102 fi103 104 TEST_HDR_MSG="selftests: $DIR: $BASENAME_TEST"105 echo "# $TEST_HDR_MSG"106 if [ ! -e "$TEST" ]; then107 echo "# Warning: file $TEST is missing!"108 echo "not ok $test_num $TEST_HDR_MSG"109 else110 if [ -x /usr/bin/stdbuf ]; then111 stdbuf="/usr/bin/stdbuf --output=L "112 fi113 eval kselftest_cmd_args="\$${kselftest_cmd_args_ref:-}"114 if [ -x "$TEST" ]; then115 cmd="$stdbuf ./$BASENAME_TEST $kselftest_cmd_args"116 elif [ -x "./ksft_runner.sh" ]; then117 cmd="$stdbuf ./ksft_runner.sh ./$BASENAME_TEST"118 else119 echo "# Warning: file $TEST is not executable"120 121 if [ $(head -n 1 "$TEST" | cut -c -2) = "#!" ]122 then123 interpreter=$(head -n 1 "$TEST" | cut -c 3-)124 cmd="$stdbuf $interpreter ./$BASENAME_TEST"125 else126 echo "not ok $test_num $TEST_HDR_MSG"127 return128 fi129 fi130 cd `dirname $TEST` > /dev/null131 ((((( tap_timeout "$cmd" 2>&1; echo $? >&3) |132 tap_prefix >&4) 3>&1) |133 (read xs; exit $xs)) 4>>"$logfile" &&134 echo "ok $test_num $TEST_HDR_MSG") ||135 (rc=$?; \136 if [ $rc -eq $skip_rc ]; then \137 echo "ok $test_num $TEST_HDR_MSG # SKIP"138 elif [ $rc -eq $timeout_rc ]; then \139 echo "#"140 echo "not ok $test_num $TEST_HDR_MSG # TIMEOUT $kselftest_timeout seconds"141 else142 echo "not ok $test_num $TEST_HDR_MSG # exit=$rc"143 fi)144 cd - >/dev/null145 fi146}147 148in_netns()149{150 local name=$1151 ip netns exec $name bash <<-EOF152 BASE_DIR=$BASE_DIR153 source $BASE_DIR/kselftest/runner.sh154 logfile=$logfile155 run_one $DIR $TEST $test_num156 EOF157}158 159run_in_netns()160{161 local netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX)162 local tmplog="/tmp/$(mktemp -u ${BASENAME_TEST}-XXXXXX)"163 ip netns add $netns164 if [ $? -ne 0 ]; then165 echo "# Warning: Create namespace failed for $BASENAME_TEST"166 echo "not ok $test_num selftests: $DIR: $BASENAME_TEST # Create NS failed"167 fi168 ip -n $netns link set lo up169 in_netns $netns &> $tmplog170 ip netns del $netns &> /dev/null171 cat $tmplog172 rm -f $tmplog173}174 175run_many()176{177 echo "TAP version 13"178 DIR="${PWD#${BASE_DIR}/}"179 test_num=0180 total=$(echo "$@" | wc -w)181 echo "1..$total"182 for TEST in "$@"; do183 BASENAME_TEST=$(basename $TEST)184 test_num=$(( test_num + 1 ))185 if [ -n "$per_test_logging" ]; then186 logfile="/tmp/$BASENAME_TEST"187 cat /dev/null > "$logfile"188 fi189 if [ -n "$RUN_IN_NETNS" ]; then190 run_in_netns &191 else192 run_one "$DIR" "$TEST" "$test_num"193 fi194 done195 196 wait197}198