runner.sh 5.0 KB

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