ftracetest 5.6 KB

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