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 (at] hitachi.com> 7 # 8 # Released under the terms of the GPL v2. 9 10 usage() { # errno [message] 11 [ ! -z "$2" ] && echo $2 12 echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]" 13 echo " Options:" 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>" 22 echo " If <dir> is -, all logs output in console only" 23 exit $1 24 } 25 26 errexit() { # message 27 echo "Error: $1" 1>&2 28 exit 1 29 } 30 31 # Ensuring user privilege 32 if [ `id -u` -ne 0 ]; then 33 errexit "this must be run by root user" 34 fi 35 36 # Utilities 37 absdir() { # file_path 38 (cd `dirname $1`; pwd) 39 } 40 41 abspath() { 42 echo `absdir $1`/`basename $1` 43 } 44 45 find_testcases() { #directory 46 echo `find $1 -name \*.tc | sort` 47 } 48 49 parse_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 119 DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1` 120 if [ -z "$DEBUGFS_DIR" ]; then 121 TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1` 122 else 123 TRACING_DIR=$DEBUGFS_DIR/tracing 124 fi 125 126 TOP_DIR=`absdir $0` 127 TEST_DIR=$TOP_DIR/test.d 128 TEST_CASES=`find_testcases $TEST_DIR` 129 LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/ 130 KEEP_LOG=0 131 DEBUG=0 132 VERBOSE=0 133 UNSUPPORTED_RESULT=0 134 STOP_FAILURE=0 135 # Parse command-line options 136 parse_opts $* 137 138 [ $DEBUG -ne 0 ] && set -x 139 140 # Verify parameters 141 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then 142 errexit "No ftrace directory found" 143 fi 144 145 # Preparing logs 146 if [ "x$LOG_DIR" = "x-" ]; then 147 LOG_FILE= 148 date 149 else 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 153 fi 154 155 # Define text colors 156 # Check available colors on the terminal, if any 157 ncolors=`tput colors 2>/dev/null` 158 color_reset= 159 color_red= 160 color_green= 161 color_blue= 162 # If stdout exists and number of colors is eight or more, use them 163 if [ -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" 168 fi 169 170 strip_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 175 prlog() { # messages 176 echo -e "$@" 177 [ "$LOG_FILE" ] && echo -e "$@" | strip_esc >> $LOG_FILE 178 } 179 catlog() { #file 180 cat $1 181 [ "$LOG_FILE" ] && cat $1 | strip_esc >> $LOG_FILE 182 } 183 prlog "=== Ftrace unit tests ===" 184 185 186 # Testcase management 187 # Test result codes - Dejagnu extended code 188 PASS=0 # The test succeeded. 189 FAIL=1 # The test failed, but was expected to succeed. 190 UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted) 191 UNTESTED=3 # The test was not run, currently just a placeholder. 192 UNSUPPORTED=4 # The test failed because of lack of feature. 193 XFAIL=5 # The test failed, and was expected to fail. 194 195 # Accumulations 196 PASSED_CASES= 197 FAILED_CASES= 198 UNRESOLVED_CASES= 199 UNTESTED_CASES= 200 UNSUPPORTED_CASES= 201 XFAILED_CASES= 202 UNDEFINED_CASES= 203 TOTAL_RESULT=0 204 205 INSTANCE= 206 CASENO=0 207 testcase() { # testfile 208 CASENO=$((CASENO+1)) 209 desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:` 210 prlog -n "[$CASENO]$INSTANCE$desc" 211 } 212 213 test_on_instance() { # testfile 214 grep -q "^#[ \t]*flags:.*instance" $1 215 } 216 217 eval_result() { # sigval 218 case $1 in 219 $PASS) 220 prlog " [${color_green}PASS${color_reset}]" 221 PASSED_CASES="$PASSED_CASES $CASENO" 222 return 0 223 ;; 224 $FAIL) 225 prlog " [${color_red}FAIL${color_reset}]" 226 FAILED_CASES="$FAILED_CASES $CASENO" 227 return 1 # this is a bug. 228 ;; 229 $UNRESOLVED) 230 prlog " [${color_blue}UNRESOLVED${color_reset}]" 231 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO" 232 return 1 # this is a kind of bug.. something happened. 233 ;; 234 $UNTESTED) 235 prlog " [${color_blue}UNTESTED${color_reset}]" 236 UNTESTED_CASES="$UNTESTED_CASES $CASENO" 237 return 0 238 ;; 239 $UNSUPPORTED) 240 prlog " [${color_blue}UNSUPPORTED${color_reset}]" 241 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO" 242 return $UNSUPPORTED_RESULT # depends on use case 243 ;; 244 $XFAIL) 245 prlog " [${color_red}XFAIL${color_reset}]" 246 XFAILED_CASES="$XFAILED_CASES $CASENO" 247 return 0 248 ;; 249 *) 250 prlog " [${color_blue}UNDEFINED${color_reset}]" 251 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO" 252 return 1 # this must be a test bug 253 ;; 254 esac 255 } 256 257 # Signal handling for result codes 258 SIG_RESULT= 259 SIG_BASE=36 # Use realtime signals 260 SIG_PID=$$ 261 262 exit_pass () { 263 exit 0 264 } 265 266 SIG_FAIL=$((SIG_BASE + FAIL)) 267 exit_fail () { 268 exit 1 269 } 270 trap 'SIG_RESULT=$FAIL' $SIG_FAIL 271 272 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED)) 273 exit_unresolved () { 274 kill -s $SIG_UNRESOLVED $SIG_PID 275 exit 0 276 } 277 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED 278 279 SIG_UNTESTED=$((SIG_BASE + UNTESTED)) 280 exit_untested () { 281 kill -s $SIG_UNTESTED $SIG_PID 282 exit 0 283 } 284 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED 285 286 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED)) 287 exit_unsupported () { 288 kill -s $SIG_UNSUPPORTED $SIG_PID 289 exit 0 290 } 291 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED 292 293 SIG_XFAIL=$((SIG_BASE + XFAIL)) 294 exit_xfail () { 295 kill -s $SIG_XFAIL $SIG_PID 296 exit 0 297 } 298 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL 299 300 __run_test() { # testfile 301 # setup PID and PPID, $$ is not updated. 302 (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1) 303 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID 304 } 305 306 # Run one test case 307 run_test() { # testfile 308 local testname=`basename $1` 309 testcase $1 310 if [ ! -z "$LOG_FILE" ] ; then 311 local testlog=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX` 312 else 313 local testlog=/proc/self/fd/1 314 fi 315 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX` 316 echo "execute$INSTANCE: "$1 > $testlog 317 SIG_RESULT=0 318 if [ $VERBOSE -eq -1 ]; then 319 __run_test $1 320 elif [ -z "$LOG_FILE" ]; then 321 __run_test $1 2>&1 322 elif [ $VERBOSE -ge 3 ]; then 323 __run_test $1 | tee -a $testlog 2>&1 324 elif [ $VERBOSE -eq 2 ]; then 325 __run_test $1 2>> $testlog | tee -a $testlog 326 else 327 __run_test $1 >> $testlog 2>&1 328 fi 329 eval_result $SIG_RESULT 330 if [ $? -eq 0 ]; then 331 # Remove test log if the test was done as it was expected. 332 [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog 333 else 334 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog 335 TOTAL_RESULT=1 336 fi 337 rm -rf $TMPDIR 338 } 339 340 # load in the helper functions 341 . $TEST_DIR/functions 342 343 # Main loop 344 for t in $TEST_CASES; do 345 run_test $t 346 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then 347 echo "A failure detected. Stop test." 348 exit 1 349 fi 350 done 351 352 # Test on instance loop 353 INSTANCE=" (instance) " 354 for t in $TEST_CASES; do 355 test_on_instance $t || continue 356 SAVED_TRACING_DIR=$TRACING_DIR 357 export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX` 358 run_test $t 359 rmdir $TRACING_DIR 360 TRACING_DIR=$SAVED_TRACING_DIR 361 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then 362 echo "A failure detected. Stop test." 363 exit 1 364 fi 365 done 366 (cd $TRACING_DIR; initialize_ftrace) # for cleanup 367 368 prlog "" 369 prlog "# of passed: " `echo $PASSED_CASES | wc -w` 370 prlog "# of failed: " `echo $FAILED_CASES | wc -w` 371 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w` 372 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w` 373 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w` 374 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w` 375 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w` 376 377 # if no error, return 0 378 exit $TOTAL_RESULT 379