1#!/bin/sh 2 3# ftracetest - Ftrace test shell scripts 4# 5# Copyright (C) Hitachi Ltd., 2014 6# Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> 7# 8# Released under the terms of the GPL v2. 9 10usage() { # errno [message] 11[ ! -z "$2" ] && echo $2 12echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]" 13echo " Options:" 14echo " -h|--help Show help message" 15echo " -k|--keep Keep passed test logs" 16echo " -v|--verbose Increase verbosity of test messages" 17echo " -vv Alias of -v -v (Show all results in stdout)" 18echo " -vvv Alias of -v -v -v (Show all commands immediately)" 19echo " --fail-unsupported Treat UNSUPPORTED as a failure" 20echo " -d|--debug Debug mode (trace all shell commands)" 21echo " -l|--logdir <dir> Save logs on the <dir>" 22echo " If <dir> is -, all logs output in console only" 23exit $1 24} 25 26errexit() { # message 27 echo "Error: $1" 1>&2 28 exit 1 29} 30 31# Ensuring user privilege 32if [ `id -u` -ne 0 ]; then 33 errexit "this must be run by root user" 34fi 35 36# Utilities 37absdir() { # file_path 38 (cd `dirname $1`; pwd) 39} 40 41abspath() { 42 echo `absdir $1`/`basename $1` 43} 44 45find_testcases() { #directory 46 echo `find $1 -name \*.tc | sort` 47} 48 49parse_opts() { # opts 50 local OPT_TEST_CASES= 51 local OPT_TEST_DIR= 52 53 while [ ! -z "$1" ]; do 54 case "$1" in 55 --help|-h) 56 usage 0 57 ;; 58 --keep|-k) 59 KEEP_LOG=1 60 shift 1 61 ;; 62 --verbose|-v|-vv|-vvv) 63 VERBOSE=$((VERBOSE + 1)) 64 [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1)) 65 [ $1 = '-vvv' ] && VERBOSE=$((VERBOSE + 2)) 66 shift 1 67 ;; 68 --debug|-d) 69 DEBUG=1 70 shift 1 71 ;; 72 --stop-fail) 73 STOP_FAILURE=1 74 shift 1 75 ;; 76 --fail-unsupported) 77 UNSUPPORTED_RESULT=1 78 shift 1 79 ;; 80 --logdir|-l) 81 LOG_DIR=$2 82 shift 2 83 ;; 84 *.tc) 85 if [ -f "$1" ]; then 86 OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`" 87 shift 1 88 else 89 usage 1 "$1 is not a testcase" 90 fi 91 ;; 92 *) 93 if [ -d "$1" ]; then 94 OPT_TEST_DIR=`abspath $1` 95 OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`" 96 shift 1 97 else 98 usage 1 "Invalid option ($1)" 99 fi 100 ;; 101 esac 102 done 103 if [ ! -z "$OPT_TEST_CASES" ]; then 104 TEST_CASES=$OPT_TEST_CASES 105 fi 106} 107 108# Parameters 109DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1` 110if [ -z "$DEBUGFS_DIR" ]; then 111 TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1` 112else 113 TRACING_DIR=$DEBUGFS_DIR/tracing 114fi 115 116TOP_DIR=`absdir $0` 117TEST_DIR=$TOP_DIR/test.d 118TEST_CASES=`find_testcases $TEST_DIR` 119LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/ 120KEEP_LOG=0 121DEBUG=0 122VERBOSE=0 123UNSUPPORTED_RESULT=0 124STOP_FAILURE=0 125# Parse command-line options 126parse_opts $* 127 128[ $DEBUG -ne 0 ] && set -x 129 130# Verify parameters 131if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then 132 errexit "No ftrace directory found" 133fi 134 135# Preparing logs 136if [ "x$LOG_DIR" = "x-" ]; then 137 LOG_FILE= 138 date 139else 140 LOG_FILE=$LOG_DIR/ftracetest.log 141 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR" 142 date > $LOG_FILE 143fi 144 145prlog() { # messages 146 [ -z "$LOG_FILE" ] && echo "$@" || echo "$@" | tee -a $LOG_FILE 147} 148catlog() { #file 149 [ -z "$LOG_FILE" ] && cat $1 || cat $1 | tee -a $LOG_FILE 150} 151prlog "=== Ftrace unit tests ===" 152 153 154# Testcase management 155# Test result codes - Dejagnu extended code 156PASS=0 # The test succeeded. 157FAIL=1 # The test failed, but was expected to succeed. 158UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted) 159UNTESTED=3 # The test was not run, currently just a placeholder. 160UNSUPPORTED=4 # The test failed because of lack of feature. 161XFAIL=5 # The test failed, and was expected to fail. 162 163# Accumulations 164PASSED_CASES= 165FAILED_CASES= 166UNRESOLVED_CASES= 167UNTESTED_CASES= 168UNSUPPORTED_CASES= 169XFAILED_CASES= 170UNDEFINED_CASES= 171TOTAL_RESULT=0 172 173INSTANCE= 174CASENO=0 175testcase() { # testfile 176 CASENO=$((CASENO+1)) 177 desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:` 178 prlog -n "[$CASENO]$INSTANCE$desc" 179} 180 181test_on_instance() { # testfile 182 grep -q "^#[ \t]*flags:.*instance" $1 183} 184 185eval_result() { # sigval 186 case $1 in 187 $PASS) 188 prlog " [PASS]" 189 PASSED_CASES="$PASSED_CASES $CASENO" 190 return 0 191 ;; 192 $FAIL) 193 prlog " [FAIL]" 194 FAILED_CASES="$FAILED_CASES $CASENO" 195 return 1 # this is a bug. 196 ;; 197 $UNRESOLVED) 198 prlog " [UNRESOLVED]" 199 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO" 200 return 1 # this is a kind of bug.. something happened. 201 ;; 202 $UNTESTED) 203 prlog " [UNTESTED]" 204 UNTESTED_CASES="$UNTESTED_CASES $CASENO" 205 return 0 206 ;; 207 $UNSUPPORTED) 208 prlog " [UNSUPPORTED]" 209 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO" 210 return $UNSUPPORTED_RESULT # depends on use case 211 ;; 212 $XFAIL) 213 prlog " [XFAIL]" 214 XFAILED_CASES="$XFAILED_CASES $CASENO" 215 return 0 216 ;; 217 *) 218 prlog " [UNDEFINED]" 219 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO" 220 return 1 # this must be a test bug 221 ;; 222 esac 223} 224 225# Signal handling for result codes 226SIG_RESULT= 227SIG_BASE=36 # Use realtime signals 228SIG_PID=$$ 229 230exit_pass () { 231 exit 0 232} 233 234SIG_FAIL=$((SIG_BASE + FAIL)) 235exit_fail () { 236 exit 1 237} 238trap 'SIG_RESULT=$FAIL' $SIG_FAIL 239 240SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED)) 241exit_unresolved () { 242 kill -s $SIG_UNRESOLVED $SIG_PID 243 exit 0 244} 245trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED 246 247SIG_UNTESTED=$((SIG_BASE + UNTESTED)) 248exit_untested () { 249 kill -s $SIG_UNTESTED $SIG_PID 250 exit 0 251} 252trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED 253 254SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED)) 255exit_unsupported () { 256 kill -s $SIG_UNSUPPORTED $SIG_PID 257 exit 0 258} 259trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED 260 261SIG_XFAIL=$((SIG_BASE + XFAIL)) 262exit_xfail () { 263 kill -s $SIG_XFAIL $SIG_PID 264 exit 0 265} 266trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL 267 268__run_test() { # testfile 269 # setup PID and PPID, $$ is not updated. 270 (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1) 271 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID 272} 273 274# Run one test case 275run_test() { # testfile 276 local testname=`basename $1` 277 if [ ! -z "$LOG_FILE" ] ; then 278 local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX` 279 else 280 local testlog=/proc/self/fd/1 281 fi 282 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX` 283 testcase $1 284 echo "execute$INSTANCE: "$1 > $testlog 285 SIG_RESULT=0 286 if [ -z "$LOG_FILE" ]; then 287 __run_test $1 2>&1 288 elif [ $VERBOSE -ge 3 ]; then 289 __run_test $1 | tee -a $testlog 2>&1 290 elif [ $VERBOSE -eq 2 ]; then 291 __run_test $1 2>> $testlog | tee -a $testlog 292 else 293 __run_test $1 >> $testlog 2>&1 294 fi 295 eval_result $SIG_RESULT 296 if [ $? -eq 0 ]; then 297 # Remove test log if the test was done as it was expected. 298 [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog 299 else 300 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog 301 TOTAL_RESULT=1 302 fi 303 rm -rf $TMPDIR 304} 305 306# load in the helper functions 307. $TEST_DIR/functions 308 309# Main loop 310for t in $TEST_CASES; do 311 run_test $t 312 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then 313 echo "A failure detected. Stop test." 314 exit 1 315 fi 316done 317 318# Test on instance loop 319INSTANCE=" (instance) " 320for t in $TEST_CASES; do 321 test_on_instance $t || continue 322 SAVED_TRACING_DIR=$TRACING_DIR 323 export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX` 324 run_test $t 325 rmdir $TRACING_DIR 326 TRACING_DIR=$SAVED_TRACING_DIR 327 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then 328 echo "A failure detected. Stop test." 329 exit 1 330 fi 331done 332 333prlog "" 334prlog "# of passed: " `echo $PASSED_CASES | wc -w` 335prlog "# of failed: " `echo $FAILED_CASES | wc -w` 336prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w` 337prlog "# of untested: " `echo $UNTESTED_CASES | wc -w` 338prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w` 339prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w` 340prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w` 341 342# if no error, return 0 343exit $TOTAL_RESULT 344