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[ "$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 [ "$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 [ "$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 225SIG_FAIL=$((SIG_BASE + FAIL)) 226trap 'SIG_RESULT=$FAIL' $SIG_FAIL 227 228SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED)) 229exit_unresolved () { 230 kill -s $SIG_UNRESOLVED $SIG_PID 231 exit 0 232} 233trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED 234 235SIG_UNTESTED=$((SIG_BASE + UNTESTED)) 236exit_untested () { 237 kill -s $SIG_UNTESTED $SIG_PID 238 exit 0 239} 240trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED 241 242SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED)) 243exit_unsupported () { 244 kill -s $SIG_UNSUPPORTED $SIG_PID 245 exit 0 246} 247trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED 248 249SIG_XFAIL=$((SIG_BASE + XFAIL)) 250exit_xfail () { 251 kill -s $SIG_XFAIL $SIG_PID 252 exit 0 253} 254trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL 255 256__run_test() { # testfile 257 # setup PID and PPID, $$ is not updated. 258 (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1) 259 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID 260} 261 262# Run one test case 263run_test() { # testfile 264 local testname=`basename $1` 265 if [ ! -z "$LOG_FILE" ] ; then 266 local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX` 267 else 268 local testlog=/proc/self/fd/1 269 fi 270 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX` 271 testcase $1 272 echo "execute$INSTANCE: "$1 > $testlog 273 SIG_RESULT=0 274 if [ -z "$LOG_FILE" ]; then 275 __run_test $1 2>&1 276 elif [ $VERBOSE -ge 3 ]; then 277 __run_test $1 | tee -a $testlog 2>&1 278 elif [ $VERBOSE -eq 2 ]; then 279 __run_test $1 2>> $testlog | tee -a $testlog 280 else 281 __run_test $1 >> $testlog 2>&1 282 fi 283 eval_result $SIG_RESULT 284 if [ $? -eq 0 ]; then 285 # Remove test log if the test was done as it was expected. 286 [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog 287 else 288 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog 289 TOTAL_RESULT=1 290 fi 291 rm -rf $TMPDIR 292} 293 294# load in the helper functions 295. $TEST_DIR/functions 296 297# Main loop 298for t in $TEST_CASES; do 299 run_test $t 300done 301 302# Test on instance loop 303INSTANCE=" (instance) " 304for t in $TEST_CASES; do 305 test_on_instance $t || continue 306 SAVED_TRACING_DIR=$TRACING_DIR 307 export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX` 308 run_test $t 309 rmdir $TRACING_DIR 310 TRACING_DIR=$SAVED_TRACING_DIR 311done 312 313prlog "" 314prlog "# of passed: " `echo $PASSED_CASES | wc -w` 315prlog "# of failed: " `echo $FAILED_CASES | wc -w` 316prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w` 317prlog "# of untested: " `echo $UNTESTED_CASES | wc -w` 318prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w` 319prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w` 320prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w` 321 322# if no error, return 0 323exit $TOTAL_RESULT 324