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 --fail-unsupported) 73 UNSUPPORTED_RESULT=1 74 shift 1 75 ;; 76 --logdir|-l) 77 LOG_DIR=$2 78 shift 2 79 ;; 80 *.tc) 81 if [ -f "$1" ]; then 82 OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`" 83 shift 1 84 else 85 usage 1 "$1 is not a testcase" 86 fi 87 ;; 88 *) 89 if [ -d "$1" ]; then 90 OPT_TEST_DIR=`abspath $1` 91 OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`" 92 shift 1 93 else 94 usage 1 "Invalid option ($1)" 95 fi 96 ;; 97 esac 98 done 99 if [ ! -z "$OPT_TEST_CASES" ]; then 100 TEST_CASES=$OPT_TEST_CASES 101 fi 102} 103 104# Parameters 105DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1` 106if [ -z "$DEBUGFS_DIR" ]; then 107 TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1` 108else 109 TRACING_DIR=$DEBUGFS_DIR/tracing 110fi 111 112TOP_DIR=`absdir $0` 113TEST_DIR=$TOP_DIR/test.d 114TEST_CASES=`find_testcases $TEST_DIR` 115LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/ 116KEEP_LOG=0 117DEBUG=0 118VERBOSE=0 119UNSUPPORTED_RESULT=0 120# Parse command-line options 121parse_opts $* 122 123[ $DEBUG -ne 0 ] && set -x 124 125# Verify parameters 126if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then 127 errexit "No ftrace directory found" 128fi 129 130# Preparing logs 131if [ "x$LOG_DIR" = "x-" ]; then 132 LOG_FILE= 133 date 134else 135 LOG_FILE=$LOG_DIR/ftracetest.log 136 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR" 137 date > $LOG_FILE 138fi 139 140prlog() { # messages 141 [ -z "$LOG_FILE" ] && echo "$@" || echo "$@" | tee -a $LOG_FILE 142} 143catlog() { #file 144 [ -z "$LOG_FILE" ] && cat $1 || cat $1 | tee -a $LOG_FILE 145} 146prlog "=== Ftrace unit tests ===" 147 148 149# Testcase management 150# Test result codes - Dejagnu extended code 151PASS=0 # The test succeeded. 152FAIL=1 # The test failed, but was expected to succeed. 153UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted) 154UNTESTED=3 # The test was not run, currently just a placeholder. 155UNSUPPORTED=4 # The test failed because of lack of feature. 156XFAIL=5 # The test failed, and was expected to fail. 157 158# Accumulations 159PASSED_CASES= 160FAILED_CASES= 161UNRESOLVED_CASES= 162UNTESTED_CASES= 163UNSUPPORTED_CASES= 164XFAILED_CASES= 165UNDEFINED_CASES= 166TOTAL_RESULT=0 167 168INSTANCE= 169CASENO=0 170testcase() { # testfile 171 CASENO=$((CASENO+1)) 172 desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:` 173 prlog -n "[$CASENO]$INSTANCE$desc" 174} 175 176test_on_instance() { # testfile 177 grep -q "^#[ \t]*flags:.*instance" $1 178} 179 180eval_result() { # sigval 181 case $1 in 182 $PASS) 183 prlog " [PASS]" 184 PASSED_CASES="$PASSED_CASES $CASENO" 185 return 0 186 ;; 187 $FAIL) 188 prlog " [FAIL]" 189 FAILED_CASES="$FAILED_CASES $CASENO" 190 return 1 # this is a bug. 191 ;; 192 $UNRESOLVED) 193 prlog " [UNRESOLVED]" 194 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO" 195 return 1 # this is a kind of bug.. something happened. 196 ;; 197 $UNTESTED) 198 prlog " [UNTESTED]" 199 UNTESTED_CASES="$UNTESTED_CASES $CASENO" 200 return 0 201 ;; 202 $UNSUPPORTED) 203 prlog " [UNSUPPORTED]" 204 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO" 205 return $UNSUPPORTED_RESULT # depends on use case 206 ;; 207 $XFAIL) 208 prlog " [XFAIL]" 209 XFAILED_CASES="$XFAILED_CASES $CASENO" 210 return 0 211 ;; 212 *) 213 prlog " [UNDEFINED]" 214 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO" 215 return 1 # this must be a test bug 216 ;; 217 esac 218} 219 220# Signal handling for result codes 221SIG_RESULT= 222SIG_BASE=36 # Use realtime signals 223SIG_PID=$$ 224 225exit_pass () { 226 exit 0 227} 228 229SIG_FAIL=$((SIG_BASE + FAIL)) 230exit_fail () { 231 exit 1 232} 233trap 'SIG_RESULT=$FAIL' $SIG_FAIL 234 235SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED)) 236exit_unresolved () { 237 kill -s $SIG_UNRESOLVED $SIG_PID 238 exit 0 239} 240trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED 241 242SIG_UNTESTED=$((SIG_BASE + UNTESTED)) 243exit_untested () { 244 kill -s $SIG_UNTESTED $SIG_PID 245 exit 0 246} 247trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED 248 249SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED)) 250exit_unsupported () { 251 kill -s $SIG_UNSUPPORTED $SIG_PID 252 exit 0 253} 254trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED 255 256SIG_XFAIL=$((SIG_BASE + XFAIL)) 257exit_xfail () { 258 kill -s $SIG_XFAIL $SIG_PID 259 exit 0 260} 261trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL 262 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 267} 268 269# Run one test case 270run_test() { # testfile 271 local testname=`basename $1` 272 if [ ! -z "$LOG_FILE" ] ; then 273 local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX` 274 else 275 local testlog=/proc/self/fd/1 276 fi 277 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX` 278 testcase $1 279 echo "execute$INSTANCE: "$1 > $testlog 280 SIG_RESULT=0 281 if [ -z "$LOG_FILE" ]; then 282 __run_test $1 2>&1 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 287 else 288 __run_test $1 >> $testlog 2>&1 289 fi 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 294 else 295 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog 296 TOTAL_RESULT=1 297 fi 298 rm -rf $TMPDIR 299} 300 301# load in the helper functions 302. $TEST_DIR/functions 303 304# Main loop 305for t in $TEST_CASES; do 306 run_test $t 307done 308 309# Test on instance loop 310INSTANCE=" (instance) " 311for 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` 315 run_test $t 316 rmdir $TRACING_DIR 317 TRACING_DIR=$SAVED_TRACING_DIR 318done 319 320prlog "" 321prlog "# of passed: " `echo $PASSED_CASES | wc -w` 322prlog "# of failed: " `echo $FAILED_CASES | wc -w` 323prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w` 324prlog "# of untested: " `echo $UNTESTED_CASES | wc -w` 325prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w` 326prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w` 327prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w` 328 329# if no error, return 0 330exit $TOTAL_RESULT 331