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 " -q|--quiet Do not show error log on stdout"
18 echo " -d|--debug Debug mode (trace all shell commands)"
27 # Ensuring user privilege
28 if [ `id -u` -ne 0 ]; then
29 errexit "this must be run by root user"
33 absdir() { # file_path
34 (cd `dirname $1`; pwd)
38 echo `absdir $1`/`basename $1`
41 find_testcases() { #directory
42 echo `find $1 -name \*.tc | sort`
72 OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"
75 usage 1 "$1 is not a testcase"
80 OPT_TEST_DIR=`abspath $1`
81 OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
84 usage 1 "Invalid option ($1)"
89 if [ "$OPT_TEST_CASES" ]; then
90 TEST_CASES=$OPT_TEST_CASES
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`
99 TRACING_DIR=$DEBUGFS_DIR/tracing
103 TEST_DIR=$TOP_DIR/test.d
104 TEST_CASES=`find_testcases $TEST_DIR`
105 LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
110 # Parse command-line options
113 [ $DEBUG -ne 0 ] && set -x
116 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
117 errexit "No ftrace directory found"
121 LOG_FILE=$LOG_DIR/ftracetest.log
122 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
125 echo "$@" | tee -a $LOG_FILE
128 cat $1 | tee -a $LOG_FILE
130 prlog "=== Ftrace unit tests ==="
133 # Testcase management
134 # Test result codes - Dejagnu extended code
135 PASS=0 # The test succeeded.
136 FAIL=1 # The test failed, but was expected to succeed.
137 UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted)
138 UNTESTED=3 # The test was not run, currently just a placeholder.
139 UNSUPPORTED=4 # The test failed because of lack of feature.
140 XFAIL=5 # The test failed, and was expected to fail.
153 testcase() { # testfile
155 desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
156 prlog -n "[$CASENO]$desc"
159 eval_result() { # sigval
163 PASSED_CASES="$PASSED_CASES $CASENO"
168 FAILED_CASES="$FAILED_CASES $CASENO"
169 return 1 # this is a bug.
172 prlog " [UNRESOLVED]"
173 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
174 return 1 # this is a kind of bug.. something happened.
178 UNTESTED_CASES="$UNTESTED_CASES $CASENO"
182 prlog " [UNSUPPORTED]"
183 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
184 return 1 # this is not a bug, but the result should be reported.
188 XFAILED_CASES="$XFAILED_CASES $CASENO"
193 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
194 return 1 # this must be a test bug
199 # Signal handling for result codes
201 SIG_BASE=36 # Use realtime signals
204 SIG_FAIL=$((SIG_BASE + FAIL))
205 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
207 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
209 kill -s $SIG_UNRESOLVED $SIG_PID
212 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
214 SIG_UNTESTED=$((SIG_BASE + UNTESTED))
216 kill -s $SIG_UNTESTED $SIG_PID
219 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
221 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
222 exit_unsupported () {
223 kill -s $SIG_UNSUPPORTED $SIG_PID
226 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
228 SIG_XFAIL=$((SIG_BASE + XFAIL))
230 kill -s $SIG_XFAIL $SIG_PID
233 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
235 __run_test() { # testfile
236 # setup PID and PPID, $$ is not updated.
237 (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1)
238 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
242 run_test() { # testfile
243 local testname=`basename $1`
244 local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
245 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
247 echo "execute: "$1 > $testlog
249 if [ $VERBOSE -ne 0 ]; then
250 __run_test $1 2>> $testlog | tee -a $testlog
252 __run_test $1 >> $testlog 2>&1
254 eval_result $SIG_RESULT
255 if [ $? -eq 0 ]; then
256 # Remove test log if the test was done as it was expected.
257 [ $KEEP_LOG -eq 0 ] && rm $testlog
259 [ $BE_QUIET -eq 0 ] && catlog $testlog
265 # load in the helper functions
266 . $TEST_DIR/functions
269 for t in $TEST_CASES; do
274 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
275 prlog "# of failed: " `echo $FAILED_CASES | wc -w`
276 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
277 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
278 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
279 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
280 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
282 # if no error, return 0