ftracetest 5.9 KB

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