run_kselftest.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Run installed kselftest tests.
  5. #
  6. # Fallback to readlink if realpath is not available
  7. if which realpath > /dev/null; then
  8. BASE_DIR=$(realpath $(dirname $0))
  9. else
  10. BASE_DIR=$(readlink -f $(dirname $0))
  11. fi
  12. cd $BASE_DIR
  13. TESTS="$BASE_DIR"/kselftest-list.txt
  14. if [ ! -r "$TESTS" ] ; then
  15. echo "$0: Could not find list of tests to run ($TESTS)" >&2
  16. available=""
  17. else
  18. available="$(cat "$TESTS")"
  19. fi
  20. . ./kselftest/runner.sh
  21. ROOT=$PWD
  22. usage()
  23. {
  24. cat <<EOF
  25. Usage: $0 [OPTIONS]
  26. -s | --summary Print summary with detailed log in output.log (conflict with -p)
  27. -p | --per-test-log Print test log in /tmp with each test name (conflict with -s)
  28. -t | --test COLLECTION:TEST Run TEST from COLLECTION
  29. -S | --skip COLLECTION:TEST Skip TEST from COLLECTION
  30. -c | --collection COLLECTION Run all tests from COLLECTION
  31. -l | --list List the available collection:test entries
  32. -d | --dry-run Don't actually run any tests
  33. -f | --no-error-on-fail Don't exit with an error just because tests failed
  34. -n | --netns Run each test in namespace
  35. -h | --help Show this usage info
  36. -o | --override-timeout Number of seconds after which we timeout
  37. EOF
  38. exit $1
  39. }
  40. COLLECTIONS=""
  41. TESTS=""
  42. SKIP=""
  43. dryrun=""
  44. kselftest_override_timeout=""
  45. ERROR_ON_FAIL=true
  46. while true; do
  47. case "$1" in
  48. -s | --summary)
  49. logfile="$BASE_DIR"/output.log
  50. cat /dev/null > $logfile
  51. shift ;;
  52. -p | --per-test-log)
  53. per_test_logging=1
  54. shift ;;
  55. -t | --test)
  56. TESTS="$TESTS $2"
  57. shift 2 ;;
  58. -S | --skip)
  59. SKIP="$SKIP $2"
  60. shift 2 ;;
  61. -c | --collection)
  62. COLLECTIONS="$COLLECTIONS $2"
  63. shift 2 ;;
  64. -l | --list)
  65. echo "$available"
  66. exit 0 ;;
  67. -d | --dry-run)
  68. dryrun="echo"
  69. shift ;;
  70. -f | --no-error-on-fail)
  71. ERROR_ON_FAIL=false
  72. shift ;;
  73. -n | --netns)
  74. RUN_IN_NETNS=1
  75. shift ;;
  76. -o | --override-timeout)
  77. kselftest_override_timeout="$2"
  78. shift 2 ;;
  79. -h | --help)
  80. usage 0 ;;
  81. "")
  82. break ;;
  83. *)
  84. usage 1 ;;
  85. esac
  86. done
  87. # Add all selected collections to the explicit test list.
  88. if [ -n "$COLLECTIONS" ]; then
  89. for collection in $COLLECTIONS ; do
  90. found="$(echo "$available" | grep "^$collection:")"
  91. if [ -z "$found" ] ; then
  92. echo "No such collection '$collection'" >&2
  93. exit 1
  94. fi
  95. TESTS="$TESTS $found"
  96. done
  97. fi
  98. # Replace available test list with explicitly selected tests.
  99. if [ -n "$TESTS" ]; then
  100. valid=""
  101. for test in $TESTS ; do
  102. found="$(echo "$available" | grep "^${test}$")"
  103. if [ -z "$found" ] ; then
  104. echo "No such test '$test'" >&2
  105. exit 1
  106. fi
  107. valid="$valid $found"
  108. done
  109. available="$(echo "$valid" | sed -e 's/ /\n/g')"
  110. fi
  111. # Remove tests to be skipped from available list
  112. if [ -n "$SKIP" ]; then
  113. for skipped in $SKIP ; do
  114. available="$(echo "$available" | grep -v "^${skipped}$")"
  115. done
  116. fi
  117. kselftest_failures_file="$(mktemp --tmpdir kselftest-failures-XXXXXX)"
  118. export kselftest_failures_file
  119. collections=$(echo "$available" | cut -d: -f1 | sort | uniq)
  120. for collection in $collections ; do
  121. [ -w /dev/kmsg ] && echo "kselftest: Running tests in $collection" >> /dev/kmsg
  122. tests=$(echo "$available" | grep "^$collection:" | cut -d: -f2)
  123. ($dryrun cd "$collection" && $dryrun run_many $tests)
  124. done
  125. failures="$(cat "$kselftest_failures_file")"
  126. rm "$kselftest_failures_file"
  127. if "$ERROR_ON_FAIL" && [ "$failures" ]; then
  128. exit 1
  129. fi