3 # ftracetest - Ftrace test shell scripts
5 # Copyright (C) Hitachi Ltd., 2014
8 # Released under the terms of the GPL v2.
10 usage() { # errno [message]
11 [ ! -z "$2" ] && echo $2
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 Increase verbosity of test messages"
17 echo " -vv Alias of -v -v (Show all results in stdout)"
18 echo " -vvv Alias of -v -v -v (Show all commands immediately)"
19 echo " --fail-unsupported Treat UNSUPPORTED as a failure"
20 echo " -d|--debug Debug mode (trace all shell commands)"
21 echo " -l|--logdir <dir> Save logs on the <dir>"
22 echo " If <dir> is -, all logs output in console only"
31 # Ensuring user privilege
32 if [ `id -u` -ne 0 ]; then
33 errexit "this must be run by root user"
37 absdir() { # file_path
38 (cd `dirname $1`; pwd)
42 echo `absdir $1`/`basename $1`
45 find_testcases() { #directory
46 echo `find $1 -name \*.tc | sort`
53 while [ ! -z "$1" ]; do
62 --verbose|-v|-vv|-vvv)
63 VERBOSE=$((VERBOSE + 1))
64 [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1))
65 [ $1 = '-vvv' ] && VERBOSE=$((VERBOSE + 2))
82 OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"
85 usage 1 "$1 is not a testcase"
90 OPT_TEST_DIR=`abspath $1`
91 OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
94 usage 1 "Invalid option ($1)"
99 if [ ! -z "$OPT_TEST_CASES" ]; then
100 TEST_CASES=$OPT_TEST_CASES
105 DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
106 if [ -z "$DEBUGFS_DIR" ]; then
107 TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
109 TRACING_DIR=$DEBUGFS_DIR/tracing
113 TEST_DIR=$TOP_DIR/test.d
114 TEST_CASES=`find_testcases $TEST_DIR`
115 LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
120 # Parse command-line options
123 [ $DEBUG -ne 0 ] && set -x
126 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
127 errexit "No ftrace directory found"
131 if [ "x$LOG_DIR" = "x-" ]; then
135 LOG_FILE=$LOG_DIR/ftracetest.log
136 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
141 [ -z "$LOG_FILE" ] && echo "$@" || echo "$@" | tee -a $LOG_FILE
144 [ -z "$LOG_FILE" ] && cat $1 || cat $1 | tee -a $LOG_FILE
146 prlog "=== Ftrace unit tests ==="
149 # Testcase management
150 # Test result codes - Dejagnu extended code
151 PASS=0 # The test succeeded.
152 FAIL=1 # The test failed, but was expected to succeed.
153 UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted)
154 UNTESTED=3 # The test was not run, currently just a placeholder.
155 UNSUPPORTED=4 # The test failed because of lack of feature.
156 XFAIL=5 # The test failed, and was expected to fail.
170 testcase() { # testfile
172 desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
173 prlog -n "[$CASENO]$INSTANCE$desc"
176 test_on_instance() { # testfile
177 grep -q "^#[ \t]*flags:.*instance" $1
180 eval_result() { # sigval
184 PASSED_CASES="$PASSED_CASES $CASENO"
189 FAILED_CASES="$FAILED_CASES $CASENO"
190 return 1 # this is a bug.
193 prlog " [UNRESOLVED]"
194 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
195 return 1 # this is a kind of bug.. something happened.
199 UNTESTED_CASES="$UNTESTED_CASES $CASENO"
203 prlog " [UNSUPPORTED]"
204 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
205 return $UNSUPPORTED_RESULT # depends on use case
209 XFAILED_CASES="$XFAILED_CASES $CASENO"
214 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
215 return 1 # this must be a test bug
220 # Signal handling for result codes
222 SIG_BASE=36 # Use realtime signals
229 SIG_FAIL=$((SIG_BASE + FAIL))
233 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
235 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
237 kill -s $SIG_UNRESOLVED $SIG_PID
240 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
242 SIG_UNTESTED=$((SIG_BASE + UNTESTED))
244 kill -s $SIG_UNTESTED $SIG_PID
247 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
249 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
250 exit_unsupported () {
251 kill -s $SIG_UNSUPPORTED $SIG_PID
254 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
256 SIG_XFAIL=$((SIG_BASE + XFAIL))
258 kill -s $SIG_XFAIL $SIG_PID
261 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
263 __run_test() { # testfile
264 # setup PID and PPID, $$ is not updated.
265 (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1)
266 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
270 run_test() { # testfile
271 local testname=`basename $1`
272 if [ ! -z "$LOG_FILE" ] ; then
273 local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
275 local testlog=/proc/self/fd/1
277 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
279 echo "execute$INSTANCE: "$1 > $testlog
281 if [ -z "$LOG_FILE" ]; then
283 elif [ $VERBOSE -ge 3 ]; then
284 __run_test $1 | tee -a $testlog 2>&1
285 elif [ $VERBOSE -eq 2 ]; then
286 __run_test $1 2>> $testlog | tee -a $testlog
288 __run_test $1 >> $testlog 2>&1
290 eval_result $SIG_RESULT
291 if [ $? -eq 0 ]; then
292 # Remove test log if the test was done as it was expected.
293 [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog
295 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog
301 # load in the helper functions
302 . $TEST_DIR/functions
305 for t in $TEST_CASES; do
309 # Test on instance loop
310 INSTANCE=" (instance) "
311 for t in $TEST_CASES; do
312 test_on_instance $t || continue
313 SAVED_TRACING_DIR=$TRACING_DIR
314 export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`
317 TRACING_DIR=$SAVED_TRACING_DIR
321 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
322 prlog "# of failed: " `echo $FAILED_CASES | wc -w`
323 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
324 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
325 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
326 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
327 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
329 # if no error, return 0