ftracetest 7.5 KB

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