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 if [ $VERBOSE -eq -1 ]; then 64 usage "--console can not use with --verbose" 65 fi 66 VERBOSE=$((VERBOSE + 1)) 67 [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1)) 68 [ $1 = '-vvv' ] && VERBOSE=$((VERBOSE + 2)) 69 shift 1 70 ;; 71 --console) 72 if [ $VERBOSE -ne 0 ]; then 73 usage "--console can not use with --verbose" 74 fi 75 VERBOSE=-1 76 shift 1 77 ;; 78 --debug|-d) 79 DEBUG=1 80 shift 1 81 ;; 82 --stop-fail) 83 STOP_FAILURE=1 84 shift 1 85 ;; 86 --fail-unsupported) 87 UNSUPPORTED_RESULT=1 88 shift 1 89 ;; 90 --logdir|-l) 91 LOG_DIR=$2 92 shift 2 93 ;; 94 *.tc) 95 if [ -f "$1" ]; then 96 OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`" 97 shift 1 98 else 99 usage 1 "$1 is not a testcase" 100 fi 101 ;; 102 *) 103 if [ -d "$1" ]; then 104 OPT_TEST_DIR=`abspath $1` 105 OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`" 106 shift 1 107 else 108 usage 1 "Invalid option ($1)" 109 fi 110 ;; 111 esac 112 done 113 if [ ! -z "$OPT_TEST_CASES" ]; then 114 TEST_CASES=$OPT_TEST_CASES 115 fi 116} 117 118# Parameters 119DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1` 120if [ -z "$DEBUGFS_DIR" ]; then 121 TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1` 122else 123 TRACING_DIR=$DEBUGFS_DIR/tracing 124fi 125 126TOP_DIR=`absdir $0` 127TEST_DIR=$TOP_DIR/test.d 128TEST_CASES=`find_testcases $TEST_DIR` 129LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/ 130KEEP_LOG=0 131DEBUG=0 132VERBOSE=0 133UNSUPPORTED_RESULT=0 134STOP_FAILURE=0 135# Parse command-line options 136parse_opts $* 137 138[ $DEBUG -ne 0 ] && set -x 139 140# Verify parameters 141if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then 142 errexit "No ftrace directory found" 143fi 144 145# Preparing logs 146if [ "x$LOG_DIR" = "x-" ]; then 147 LOG_FILE= 148 date 149else 150 LOG_FILE=$LOG_DIR/ftracetest.log 151 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR" 152 date > $LOG_FILE 153fi 154 155# Define text colors 156# Check available colors on the terminal, if any 157ncolors=`tput colors 2>/dev/null` 158color_reset= 159color_red= 160color_green= 161color_blue= 162# If stdout exists and number of colors is eight or more, use them 163if [ -t 1 -a "$ncolors" -a "$ncolors" -ge 8 ]; then 164 color_reset="\e[0m" 165 color_red="\e[31m" 166 color_green="\e[32m" 167 color_blue="\e[34m" 168fi 169 170strip_esc() { 171 # busybox sed implementation doesn't accept "\x1B", so use [:cntrl:] instead. 172 sed -E "s/[[:cntrl:]]\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" 173} 174 175prlog() { # messages 176 newline="\n" 177 if [ "$1" = "-n" ] ; then 178 newline= 179 shift 180 fi 181 printf "$*$newline" 182 [ "$LOG_FILE" ] && printf "$*$newline" | strip_esc >> $LOG_FILE 183} 184catlog() { #file 185 cat $1 186 [ "$LOG_FILE" ] && cat $1 | strip_esc >> $LOG_FILE 187} 188prlog "=== Ftrace unit tests ===" 189 190 191# Testcase management 192# Test result codes - Dejagnu extended code 193PASS=0 # The test succeeded. 194FAIL=1 # The test failed, but was expected to succeed. 195UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted) 196UNTESTED=3 # The test was not run, currently just a placeholder. 197UNSUPPORTED=4 # The test failed because of lack of feature. 198XFAIL=5 # The test failed, and was expected to fail. 199 200# Accumulations 201PASSED_CASES= 202FAILED_CASES= 203UNRESOLVED_CASES= 204UNTESTED_CASES= 205UNSUPPORTED_CASES= 206XFAILED_CASES= 207UNDEFINED_CASES= 208TOTAL_RESULT=0 209 210INSTANCE= 211CASENO=0 212testcase() { # testfile 213 CASENO=$((CASENO+1)) 214 desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:` 215 prlog -n "[$CASENO]$INSTANCE$desc" 216} 217 218test_on_instance() { # testfile 219 grep -q "^#[ \t]*flags:.*instance" $1 220} 221 222eval_result() { # sigval 223 case $1 in 224 $PASS) 225 prlog " [${color_green}PASS${color_reset}]" 226 PASSED_CASES="$PASSED_CASES $CASENO" 227 return 0 228 ;; 229 $FAIL) 230 prlog " [${color_red}FAIL${color_reset}]" 231 FAILED_CASES="$FAILED_CASES $CASENO" 232 return 1 # this is a bug. 233 ;; 234 $UNRESOLVED) 235 prlog " [${color_blue}UNRESOLVED${color_reset}]" 236 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO" 237 return 1 # this is a kind of bug.. something happened. 238 ;; 239 $UNTESTED) 240 prlog " [${color_blue}UNTESTED${color_reset}]" 241 UNTESTED_CASES="$UNTESTED_CASES $CASENO" 242 return 0 243 ;; 244 $UNSUPPORTED) 245 prlog " [${color_blue}UNSUPPORTED${color_reset}]" 246 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO" 247 return $UNSUPPORTED_RESULT # depends on use case 248 ;; 249 $XFAIL) 250 prlog " [${color_red}XFAIL${color_reset}]" 251 XFAILED_CASES="$XFAILED_CASES $CASENO" 252 return 0 253 ;; 254 *) 255 prlog " [${color_blue}UNDEFINED${color_reset}]" 256 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO" 257 return 1 # this must be a test bug 258 ;; 259 esac 260} 261 262# Signal handling for result codes 263SIG_RESULT= 264SIG_BASE=36 # Use realtime signals 265SIG_PID=$$ 266 267exit_pass () { 268 exit 0 269} 270 271SIG_FAIL=$((SIG_BASE + FAIL)) 272exit_fail () { 273 exit 1 274} 275trap 'SIG_RESULT=$FAIL' $SIG_FAIL 276 277SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED)) 278exit_unresolved () { 279 kill -s $SIG_UNRESOLVED $SIG_PID 280 exit 0 281} 282trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED 283 284SIG_UNTESTED=$((SIG_BASE + UNTESTED)) 285exit_untested () { 286 kill -s $SIG_UNTESTED $SIG_PID 287 exit 0 288} 289trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED 290 291SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED)) 292exit_unsupported () { 293 kill -s $SIG_UNSUPPORTED $SIG_PID 294 exit 0 295} 296trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED 297 298SIG_XFAIL=$((SIG_BASE + XFAIL)) 299exit_xfail () { 300 kill -s $SIG_XFAIL $SIG_PID 301 exit 0 302} 303trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL 304 305__run_test() { # testfile 306 # setup PID and PPID, $$ is not updated. 307 (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1) 308 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID 309} 310 311# Run one test case 312run_test() { # testfile 313 local testname=`basename $1` 314 testcase $1 315 if [ ! -z "$LOG_FILE" ] ; then 316 local testlog=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX` 317 else 318 local testlog=/proc/self/fd/1 319 fi 320 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX` 321 echo "execute$INSTANCE: "$1 > $testlog 322 SIG_RESULT=0 323 if [ $VERBOSE -eq -1 ]; then 324 __run_test $1 325 elif [ -z "$LOG_FILE" ]; then 326 __run_test $1 2>&1 327 elif [ $VERBOSE -ge 3 ]; then 328 __run_test $1 | tee -a $testlog 2>&1 329 elif [ $VERBOSE -eq 2 ]; then 330 __run_test $1 2>> $testlog | tee -a $testlog 331 else 332 __run_test $1 >> $testlog 2>&1 333 fi 334 eval_result $SIG_RESULT 335 if [ $? -eq 0 ]; then 336 # Remove test log if the test was done as it was expected. 337 [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog 338 else 339 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog 340 TOTAL_RESULT=1 341 fi 342 rm -rf $TMPDIR 343} 344 345# load in the helper functions 346. $TEST_DIR/functions 347 348# Main loop 349for t in $TEST_CASES; do 350 run_test $t 351 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then 352 echo "A failure detected. Stop test." 353 exit 1 354 fi 355done 356 357# Test on instance loop 358INSTANCE=" (instance) " 359for t in $TEST_CASES; do 360 test_on_instance $t || continue 361 SAVED_TRACING_DIR=$TRACING_DIR 362 export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX` 363 run_test $t 364 rmdir $TRACING_DIR 365 TRACING_DIR=$SAVED_TRACING_DIR 366 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then 367 echo "A failure detected. Stop test." 368 exit 1 369 fi 370done 371(cd $TRACING_DIR; initialize_ftrace) # for cleanup 372 373prlog "" 374prlog "# of passed: " `echo $PASSED_CASES | wc -w` 375prlog "# of failed: " `echo $FAILED_CASES | wc -w` 376prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w` 377prlog "# of untested: " `echo $UNTESTED_CASES | wc -w` 378prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w` 379prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w` 380prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w` 381 382# if no error, return 0 383exit $TOTAL_RESULT 384