ftracetest 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #!/bin/sh
  2. # ftracetest - Ftrace test shell scripts
  3. #
  4. # Copyright (C) Hitachi Ltd., 2014
  5. # Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
  6. #
  7. # Released under the terms of the GPL v2.
  8. usage() { # errno [message]
  9. [ "$2" ] && echo $2
  10. echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]"
  11. echo " Options:"
  12. echo " -h|--help Show help message"
  13. echo " -k|--keep Keep passed test logs"
  14. echo " -v|--verbose Increase verbosity of test messages"
  15. echo " -vv Alias of -v -v (Show all results in stdout)"
  16. echo " -d|--debug Debug mode (trace all shell commands)"
  17. exit $1
  18. }
  19. errexit() { # message
  20. echo "Error: $1" 1>&2
  21. exit 1
  22. }
  23. # Ensuring user privilege
  24. if [ `id -u` -ne 0 ]; then
  25. errexit "this must be run by root user"
  26. fi
  27. # Utilities
  28. absdir() { # file_path
  29. (cd `dirname $1`; pwd)
  30. }
  31. abspath() {
  32. echo `absdir $1`/`basename $1`
  33. }
  34. find_testcases() { #directory
  35. echo `find $1 -name \*.tc | sort`
  36. }
  37. parse_opts() { # opts
  38. local OPT_TEST_CASES=
  39. local OPT_TEST_DIR=
  40. while [ "$1" ]; do
  41. case "$1" in
  42. --help|-h)
  43. usage 0
  44. ;;
  45. --keep|-k)
  46. KEEP_LOG=1
  47. shift 1
  48. ;;
  49. --verbose|-v|-vv)
  50. VERBOSE=$((VERBOSE + 1))
  51. [ $1 == '-vv' ] && VERBOSE=$((VERBOSE + 1))
  52. shift 1
  53. ;;
  54. --debug|-d)
  55. DEBUG=1
  56. shift 1
  57. ;;
  58. *.tc)
  59. if [ -f "$1" ]; then
  60. OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"
  61. shift 1
  62. else
  63. usage 1 "$1 is not a testcase"
  64. fi
  65. ;;
  66. *)
  67. if [ -d "$1" ]; then
  68. OPT_TEST_DIR=`abspath $1`
  69. OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
  70. shift 1
  71. else
  72. usage 1 "Invalid option ($1)"
  73. fi
  74. ;;
  75. esac
  76. done
  77. if [ "$OPT_TEST_CASES" ]; then
  78. TEST_CASES=$OPT_TEST_CASES
  79. fi
  80. }
  81. # Parameters
  82. DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
  83. if [ -z "$DEBUGFS_DIR" ]; then
  84. TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
  85. else
  86. TRACING_DIR=$DEBUGFS_DIR/tracing
  87. fi
  88. TOP_DIR=`absdir $0`
  89. TEST_DIR=$TOP_DIR/test.d
  90. TEST_CASES=`find_testcases $TEST_DIR`
  91. LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
  92. KEEP_LOG=0
  93. DEBUG=0
  94. VERBOSE=0
  95. # Parse command-line options
  96. parse_opts $*
  97. [ $DEBUG -ne 0 ] && set -x
  98. # Verify parameters
  99. if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
  100. errexit "No ftrace directory found"
  101. fi
  102. # Preparing logs
  103. LOG_FILE=$LOG_DIR/ftracetest.log
  104. mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
  105. date > $LOG_FILE
  106. prlog() { # messages
  107. echo "$@" | tee -a $LOG_FILE
  108. }
  109. catlog() { #file
  110. cat $1 | tee -a $LOG_FILE
  111. }
  112. prlog "=== Ftrace unit tests ==="
  113. # Testcase management
  114. # Test result codes - Dejagnu extended code
  115. PASS=0 # The test succeeded.
  116. FAIL=1 # The test failed, but was expected to succeed.
  117. UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted)
  118. UNTESTED=3 # The test was not run, currently just a placeholder.
  119. UNSUPPORTED=4 # The test failed because of lack of feature.
  120. XFAIL=5 # The test failed, and was expected to fail.
  121. # Accumulations
  122. PASSED_CASES=
  123. FAILED_CASES=
  124. UNRESOLVED_CASES=
  125. UNTESTED_CASES=
  126. UNSUPPORTED_CASES=
  127. XFAILED_CASES=
  128. UNDEFINED_CASES=
  129. TOTAL_RESULT=0
  130. CASENO=0
  131. testcase() { # testfile
  132. CASENO=$((CASENO+1))
  133. desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
  134. prlog -n "[$CASENO]$desc"
  135. }
  136. eval_result() { # sigval
  137. case $1 in
  138. $PASS)
  139. prlog " [PASS]"
  140. PASSED_CASES="$PASSED_CASES $CASENO"
  141. return 0
  142. ;;
  143. $FAIL)
  144. prlog " [FAIL]"
  145. FAILED_CASES="$FAILED_CASES $CASENO"
  146. return 1 # this is a bug.
  147. ;;
  148. $UNRESOLVED)
  149. prlog " [UNRESOLVED]"
  150. UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
  151. return 1 # this is a kind of bug.. something happened.
  152. ;;
  153. $UNTESTED)
  154. prlog " [UNTESTED]"
  155. UNTESTED_CASES="$UNTESTED_CASES $CASENO"
  156. return 0
  157. ;;
  158. $UNSUPPORTED)
  159. prlog " [UNSUPPORTED]"
  160. UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
  161. return 1 # this is not a bug, but the result should be reported.
  162. ;;
  163. $XFAIL)
  164. prlog " [XFAIL]"
  165. XFAILED_CASES="$XFAILED_CASES $CASENO"
  166. return 0
  167. ;;
  168. *)
  169. prlog " [UNDEFINED]"
  170. UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
  171. return 1 # this must be a test bug
  172. ;;
  173. esac
  174. }
  175. # Signal handling for result codes
  176. SIG_RESULT=
  177. SIG_BASE=36 # Use realtime signals
  178. SIG_PID=$$
  179. SIG_FAIL=$((SIG_BASE + FAIL))
  180. trap 'SIG_RESULT=$FAIL' $SIG_FAIL
  181. SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
  182. exit_unresolved () {
  183. kill -s $SIG_UNRESOLVED $SIG_PID
  184. exit 0
  185. }
  186. trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
  187. SIG_UNTESTED=$((SIG_BASE + UNTESTED))
  188. exit_untested () {
  189. kill -s $SIG_UNTESTED $SIG_PID
  190. exit 0
  191. }
  192. trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
  193. SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
  194. exit_unsupported () {
  195. kill -s $SIG_UNSUPPORTED $SIG_PID
  196. exit 0
  197. }
  198. trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
  199. SIG_XFAIL=$((SIG_BASE + XFAIL))
  200. exit_xfail () {
  201. kill -s $SIG_XFAIL $SIG_PID
  202. exit 0
  203. }
  204. trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
  205. __run_test() { # testfile
  206. # setup PID and PPID, $$ is not updated.
  207. (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1)
  208. [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
  209. }
  210. # Run one test case
  211. run_test() { # testfile
  212. local testname=`basename $1`
  213. local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
  214. export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
  215. testcase $1
  216. echo "execute: "$1 > $testlog
  217. SIG_RESULT=0
  218. if [ $VERBOSE -ge 2 ]; then
  219. __run_test $1 2>> $testlog | tee -a $testlog
  220. else
  221. __run_test $1 >> $testlog 2>&1
  222. fi
  223. eval_result $SIG_RESULT
  224. if [ $? -eq 0 ]; then
  225. # Remove test log if the test was done as it was expected.
  226. [ $KEEP_LOG -eq 0 ] && rm $testlog
  227. else
  228. [ $VERBOSE -ge 1 ] && catlog $testlog
  229. TOTAL_RESULT=1
  230. fi
  231. rm -rf $TMPDIR
  232. }
  233. # load in the helper functions
  234. . $TEST_DIR/functions
  235. # Main loop
  236. for t in $TEST_CASES; do
  237. run_test $t
  238. done
  239. prlog ""
  240. prlog "# of passed: " `echo $PASSED_CASES | wc -w`
  241. prlog "# of failed: " `echo $FAILED_CASES | wc -w`
  242. prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
  243. prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
  244. prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
  245. prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
  246. prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
  247. # if no error, return 0
  248. exit $TOTAL_RESULT