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 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>"
30 # Ensuring user privilege
31 if [ `id -u` -ne 0 ]; then
32 errexit "this must be run by root user"
36 absdir() { # file_path
37 (cd `dirname $1`; pwd)
41 echo `absdir $1`/`basename $1`
44 find_testcases() { #directory
45 echo `find $1 -name \*.tc | sort`
61 --verbose|-v|-vv|-vvv)
62 VERBOSE=$((VERBOSE + 1))
63 [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1))
64 [ $1 = '-vvv' ] && VERBOSE=$((VERBOSE + 2))
81 OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"
84 usage 1 "$1 is not a testcase"
89 OPT_TEST_DIR=`abspath $1`
90 OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
93 usage 1 "Invalid option ($1)"
98 if [ "$OPT_TEST_CASES" ]; then
99 TEST_CASES=$OPT_TEST_CASES
104 DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
105 if [ -z "$DEBUGFS_DIR" ]; then
106 TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
108 TRACING_DIR=$DEBUGFS_DIR/tracing
112 TEST_DIR=$TOP_DIR/test.d
113 TEST_CASES=`find_testcases $TEST_DIR`
114 LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
119 # Parse command-line options
122 [ $DEBUG -ne 0 ] && set -x
125 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
126 errexit "No ftrace directory found"
130 LOG_FILE=$LOG_DIR/ftracetest.log
131 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
134 echo "$@" | tee -a $LOG_FILE
137 cat $1 | tee -a $LOG_FILE
139 prlog "=== Ftrace unit tests ==="
142 # Testcase management
143 # Test result codes - Dejagnu extended code
144 PASS=0 # The test succeeded.
145 FAIL=1 # The test failed, but was expected to succeed.
146 UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted)
147 UNTESTED=3 # The test was not run, currently just a placeholder.
148 UNSUPPORTED=4 # The test failed because of lack of feature.
149 XFAIL=5 # The test failed, and was expected to fail.
163 testcase() { # testfile
165 desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
166 prlog -n "[$CASENO]$INSTANCE$desc"
169 test_on_instance() { # testfile
170 grep -q "^#[ \t]*flags:.*instance" $1
173 eval_result() { # sigval
177 PASSED_CASES="$PASSED_CASES $CASENO"
182 FAILED_CASES="$FAILED_CASES $CASENO"
183 return 1 # this is a bug.
186 prlog " [UNRESOLVED]"
187 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
188 return 1 # this is a kind of bug.. something happened.
192 UNTESTED_CASES="$UNTESTED_CASES $CASENO"
196 prlog " [UNSUPPORTED]"
197 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
198 return $UNSUPPORTED_RESULT # depends on use case
202 XFAILED_CASES="$XFAILED_CASES $CASENO"
207 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
208 return 1 # this must be a test bug
213 # Signal handling for result codes
215 SIG_BASE=36 # Use realtime signals
218 SIG_FAIL=$((SIG_BASE + FAIL))
219 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
221 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
223 kill -s $SIG_UNRESOLVED $SIG_PID
226 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
228 SIG_UNTESTED=$((SIG_BASE + UNTESTED))
230 kill -s $SIG_UNTESTED $SIG_PID
233 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
235 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
236 exit_unsupported () {
237 kill -s $SIG_UNSUPPORTED $SIG_PID
240 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
242 SIG_XFAIL=$((SIG_BASE + XFAIL))
244 kill -s $SIG_XFAIL $SIG_PID
247 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
249 __run_test() { # testfile
250 # setup PID and PPID, $$ is not updated.
251 (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1)
252 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
256 run_test() { # testfile
257 local testname=`basename $1`
258 local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
259 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
261 echo "execute$INSTANCE: "$1 > $testlog
263 if [ $VERBOSE -ge 3 ]; then
264 __run_test $1 | tee -a $testlog 2>&1
265 elif [ $VERBOSE -eq 2 ]; then
266 __run_test $1 2>> $testlog | tee -a $testlog
268 __run_test $1 >> $testlog 2>&1
270 eval_result $SIG_RESULT
271 if [ $? -eq 0 ]; then
272 # Remove test log if the test was done as it was expected.
273 [ $KEEP_LOG -eq 0 ] && rm $testlog
275 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog
281 # load in the helper functions
282 . $TEST_DIR/functions
285 for t in $TEST_CASES; do
289 # Test on instance loop
290 INSTANCE=" (instance) "
291 for t in $TEST_CASES; do
292 test_on_instance $t || continue
293 SAVED_TRACING_DIR=$TRACING_DIR
294 export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`
297 TRACING_DIR=$SAVED_TRACING_DIR
301 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
302 prlog "# of failed: " `echo $FAILED_CASES | wc -w`
303 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
304 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
305 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
306 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
307 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
309 # if no error, return 0