3 # ftracetest - Ftrace test shell scripts
5 # Copyright (C) Hitachi Ltd., 2014
6 # Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
8 # Released under the terms of the GPL v2.
10 usage() { # errno [message]
12 echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]"
14 echo " -h|--help Show help message"
15 echo " -k|--keep Keep passed test logs"
16 echo " -v|--verbose Show all stdout messages in testcases"
17 echo " -d|--debug Debug mode (trace all shell commands)"
26 # Ensuring user privilege
27 if [ `id -u` -ne 0 ]; then
28 errexit "this must be run by root user"
32 absdir() { # file_path
33 (cd `dirname $1`; pwd)
37 echo `absdir $1`/`basename $1`
40 find_testcases() { #directory
41 echo `find $1 -name \*.tc | sort`
67 OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"
70 usage 1 "$1 is not a testcase"
75 OPT_TEST_DIR=`abspath $1`
76 OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
79 usage 1 "Invalid option ($1)"
84 if [ "$OPT_TEST_CASES" ]; then
85 TEST_CASES=$OPT_TEST_CASES
90 DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
91 TRACING_DIR=$DEBUGFS_DIR/tracing
93 TEST_DIR=$TOP_DIR/test.d
94 TEST_CASES=`find_testcases $TEST_DIR`
95 LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
99 # Parse command-line options
102 [ $DEBUG -ne 0 ] && set -x
105 if [ -z "$DEBUGFS_DIR" -o ! -d "$TRACING_DIR" ]; then
106 errexit "No ftrace directory found"
110 LOG_FILE=$LOG_DIR/ftracetest.log
111 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
114 echo "$@" | tee -a $LOG_FILE
117 cat $1 | tee -a $LOG_FILE
119 prlog "=== Ftrace unit tests ==="
122 # Testcase management
123 # Test result codes - Dejagnu extended code
124 PASS=0 # The test succeeded.
125 FAIL=1 # The test failed, but was expected to succeed.
126 UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted)
127 UNTESTED=3 # The test was not run, currently just a placeholder.
128 UNSUPPORTED=4 # The test failed because of lack of feature.
129 XFAIL=5 # The test failed, and was expected to fail.
142 testcase() { # testfile
144 desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
145 prlog -n "[$CASENO]$desc"
148 eval_result() { # sigval
152 PASSED_CASES="$PASSED_CASES $CASENO"
157 FAILED_CASES="$FAILED_CASES $CASENO"
158 return 1 # this is a bug.
161 prlog " [UNRESOLVED]"
162 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
163 return 1 # this is a kind of bug.. something happened.
167 UNTESTED_CASES="$UNTESTED_CASES $CASENO"
171 prlog " [UNSUPPORTED]"
172 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
173 return 1 # this is not a bug, but the result should be reported.
177 XFAILED_CASES="$XFAILED_CASES $CASENO"
182 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
183 return 1 # this must be a test bug
188 # Signal handling for result codes
190 SIG_BASE=36 # Use realtime signals
193 SIG_FAIL=$((SIG_BASE + FAIL))
194 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
196 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
198 kill -s $SIG_UNRESOLVED $SIG_PID
201 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
203 SIG_UNTESTED=$((SIG_BASE + UNTESTED))
205 kill -s $SIG_UNTESTED $SIG_PID
208 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
210 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
211 exit_unsupported () {
212 kill -s $SIG_UNSUPPORTED $SIG_PID
215 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
217 SIG_XFAIL=$((SIG_BASE + XFAIL))
219 kill -s $SIG_XFAIL $SIG_PID
222 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
224 __run_test() { # testfile
225 # setup PID and PPID, $$ is not updated.
226 (cd $TRACING_DIR; read PID _ < /proc/self/stat ; set -e; set -x; . $1)
227 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
231 run_test() { # testfile
232 local testname=`basename $1`
233 local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
235 echo "execute: "$1 > $testlog
237 if [ $VERBOSE -ne 0 ]; then
238 __run_test $1 2>> $testlog | tee -a $testlog
240 __run_test $1 >> $testlog 2>&1
242 eval_result $SIG_RESULT
243 if [ $? -eq 0 ]; then
244 # Remove test log if the test was done as it was expected.
245 [ $KEEP_LOG -eq 0 ] && rm $testlog
252 # load in the helper functions
253 . $TEST_DIR/functions
256 for t in $TEST_CASES; do
261 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
262 prlog "# of failed: " `echo $FAILED_CASES | wc -w`
263 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
264 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
265 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
266 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
267 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
269 # if no error, return 0