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 if [ -z "$DEBUGFS_DIR" ]; then
92 TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
94 TRACING_DIR=$DEBUGFS_DIR/tracing
98 TEST_DIR=$TOP_DIR/test.d
99 TEST_CASES=`find_testcases $TEST_DIR`
100 LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
104 # Parse command-line options
107 [ $DEBUG -ne 0 ] && set -x
110 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
111 errexit "No ftrace directory found"
115 LOG_FILE=$LOG_DIR/ftracetest.log
116 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
119 echo "$@" | tee -a $LOG_FILE
122 cat $1 | tee -a $LOG_FILE
124 prlog "=== Ftrace unit tests ==="
127 # Testcase management
128 # Test result codes - Dejagnu extended code
129 PASS=0 # The test succeeded.
130 FAIL=1 # The test failed, but was expected to succeed.
131 UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted)
132 UNTESTED=3 # The test was not run, currently just a placeholder.
133 UNSUPPORTED=4 # The test failed because of lack of feature.
134 XFAIL=5 # The test failed, and was expected to fail.
147 testcase() { # testfile
149 desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
150 prlog -n "[$CASENO]$desc"
153 eval_result() { # sigval
157 PASSED_CASES="$PASSED_CASES $CASENO"
162 FAILED_CASES="$FAILED_CASES $CASENO"
163 return 1 # this is a bug.
166 prlog " [UNRESOLVED]"
167 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
168 return 1 # this is a kind of bug.. something happened.
172 UNTESTED_CASES="$UNTESTED_CASES $CASENO"
176 prlog " [UNSUPPORTED]"
177 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
178 return 1 # this is not a bug, but the result should be reported.
182 XFAILED_CASES="$XFAILED_CASES $CASENO"
187 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
188 return 1 # this must be a test bug
193 # Signal handling for result codes
195 SIG_BASE=36 # Use realtime signals
198 SIG_FAIL=$((SIG_BASE + FAIL))
199 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
201 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
203 kill -s $SIG_UNRESOLVED $SIG_PID
206 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
208 SIG_UNTESTED=$((SIG_BASE + UNTESTED))
210 kill -s $SIG_UNTESTED $SIG_PID
213 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
215 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
216 exit_unsupported () {
217 kill -s $SIG_UNSUPPORTED $SIG_PID
220 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
222 SIG_XFAIL=$((SIG_BASE + XFAIL))
224 kill -s $SIG_XFAIL $SIG_PID
227 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
229 __run_test() { # testfile
230 # setup PID and PPID, $$ is not updated.
231 (cd $TRACING_DIR; read PID _ < /proc/self/stat ; set -e; set -x; . $1)
232 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
236 run_test() { # testfile
237 local testname=`basename $1`
238 local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
240 echo "execute: "$1 > $testlog
242 if [ $VERBOSE -ne 0 ]; then
243 __run_test $1 2>> $testlog | tee -a $testlog
245 __run_test $1 >> $testlog 2>&1
247 eval_result $SIG_RESULT
248 if [ $? -eq 0 ]; then
249 # Remove test log if the test was done as it was expected.
250 [ $KEEP_LOG -eq 0 ] && rm $testlog
257 # load in the helper functions
258 . $TEST_DIR/functions
261 for t in $TEST_CASES; do
266 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
267 prlog "# of failed: " `echo $FAILED_CASES | wc -w`
268 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
269 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
270 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
271 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
272 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
274 # if no error, return 0