ftracetest 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. if [ -z "$DEBUGFS_DIR" ]; then
  82. TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
  83. else
  84. TRACING_DIR=$DEBUGFS_DIR/tracing
  85. fi
  86. TOP_DIR=`absdir $0`
  87. TEST_DIR=$TOP_DIR/test.d
  88. TEST_CASES=`find_testcases $TEST_DIR`
  89. LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
  90. KEEP_LOG=0
  91. DEBUG=0
  92. VERBOSE=0
  93. # Parse command-line options
  94. parse_opts $*
  95. [ $DEBUG -ne 0 ] && set -x
  96. # Verify parameters
  97. if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
  98. errexit "No ftrace directory found"
  99. fi
  100. # Preparing logs
  101. LOG_FILE=$LOG_DIR/ftracetest.log
  102. mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
  103. date > $LOG_FILE
  104. prlog() { # messages
  105. echo "$@" | tee -a $LOG_FILE
  106. }
  107. catlog() { #file
  108. cat $1 | tee -a $LOG_FILE
  109. }
  110. prlog "=== Ftrace unit tests ==="
  111. # Testcase management
  112. # Test result codes - Dejagnu extended code
  113. PASS=0 # The test succeeded.
  114. FAIL=1 # The test failed, but was expected to succeed.
  115. UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted)
  116. UNTESTED=3 # The test was not run, currently just a placeholder.
  117. UNSUPPORTED=4 # The test failed because of lack of feature.
  118. XFAIL=5 # The test failed, and was expected to fail.
  119. # Accumulations
  120. PASSED_CASES=
  121. FAILED_CASES=
  122. UNRESOLVED_CASES=
  123. UNTESTED_CASES=
  124. UNSUPPORTED_CASES=
  125. XFAILED_CASES=
  126. UNDEFINED_CASES=
  127. TOTAL_RESULT=0
  128. CASENO=0
  129. testcase() { # testfile
  130. CASENO=$((CASENO+1))
  131. desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
  132. prlog -n "[$CASENO]$desc"
  133. }
  134. eval_result() { # sigval
  135. case $1 in
  136. $PASS)
  137. prlog " [PASS]"
  138. PASSED_CASES="$PASSED_CASES $CASENO"
  139. return 0
  140. ;;
  141. $FAIL)
  142. prlog " [FAIL]"
  143. FAILED_CASES="$FAILED_CASES $CASENO"
  144. return 1 # this is a bug.
  145. ;;
  146. $UNRESOLVED)
  147. prlog " [UNRESOLVED]"
  148. UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
  149. return 1 # this is a kind of bug.. something happened.
  150. ;;
  151. $UNTESTED)
  152. prlog " [UNTESTED]"
  153. UNTESTED_CASES="$UNTESTED_CASES $CASENO"
  154. return 0
  155. ;;
  156. $UNSUPPORTED)
  157. prlog " [UNSUPPORTED]"
  158. UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
  159. return 1 # this is not a bug, but the result should be reported.
  160. ;;
  161. $XFAIL)
  162. prlog " [XFAIL]"
  163. XFAILED_CASES="$XFAILED_CASES $CASENO"
  164. return 0
  165. ;;
  166. *)
  167. prlog " [UNDEFINED]"
  168. UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
  169. return 1 # this must be a test bug
  170. ;;
  171. esac
  172. }
  173. # Signal handling for result codes
  174. SIG_RESULT=
  175. SIG_BASE=36 # Use realtime signals
  176. SIG_PID=$$
  177. SIG_FAIL=$((SIG_BASE + FAIL))
  178. trap 'SIG_RESULT=$FAIL' $SIG_FAIL
  179. SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
  180. exit_unresolved () {
  181. kill -s $SIG_UNRESOLVED $SIG_PID
  182. exit 0
  183. }
  184. trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
  185. SIG_UNTESTED=$((SIG_BASE + UNTESTED))
  186. exit_untested () {
  187. kill -s $SIG_UNTESTED $SIG_PID
  188. exit 0
  189. }
  190. trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
  191. SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
  192. exit_unsupported () {
  193. kill -s $SIG_UNSUPPORTED $SIG_PID
  194. exit 0
  195. }
  196. trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
  197. SIG_XFAIL=$((SIG_BASE + XFAIL))
  198. exit_xfail () {
  199. kill -s $SIG_XFAIL $SIG_PID
  200. exit 0
  201. }
  202. trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
  203. __run_test() { # testfile
  204. # setup PID and PPID, $$ is not updated.
  205. (cd $TRACING_DIR; read PID _ < /proc/self/stat ; set -e; set -x; . $1)
  206. [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
  207. }
  208. # Run one test case
  209. run_test() { # testfile
  210. local testname=`basename $1`
  211. local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
  212. testcase $1
  213. echo "execute: "$1 > $testlog
  214. SIG_RESULT=0
  215. if [ $VERBOSE -ne 0 ]; then
  216. __run_test $1 2>> $testlog | tee -a $testlog
  217. else
  218. __run_test $1 >> $testlog 2>&1
  219. fi
  220. eval_result $SIG_RESULT
  221. if [ $? -eq 0 ]; then
  222. # Remove test log if the test was done as it was expected.
  223. [ $KEEP_LOG -eq 0 ] && rm $testlog
  224. else
  225. catlog $testlog
  226. TOTAL_RESULT=1
  227. fi
  228. }
  229. # load in the helper functions
  230. . $TEST_DIR/functions
  231. # Main loop
  232. for t in $TEST_CASES; do
  233. run_test $t
  234. done
  235. prlog ""
  236. prlog "# of passed: " `echo $PASSED_CASES | wc -w`
  237. prlog "# of failed: " `echo $FAILED_CASES | wc -w`
  238. prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
  239. prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
  240. prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
  241. prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
  242. prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
  243. # if no error, return 0
  244. exit $TOTAL_RESULT