Home | History | Annotate | Download | only in ftrace
      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 [ "$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 Show all stdout messages in testcases"
     17 echo "		-d|--debug Debug mode (trace all shell commands)"
     18 exit $1
     19 }
     20 
     21 errexit() { # message
     22   echo "Error: $1" 1>&2
     23   exit 1
     24 }
     25 
     26 # Ensuring user privilege
     27 if [ `id -u` -ne 0 ]; then
     28   errexit "this must be run by root user"
     29 fi
     30 
     31 # Utilities
     32 absdir() { # file_path
     33   (cd `dirname $1`; pwd)
     34 }
     35 
     36 abspath() {
     37   echo `absdir $1`/`basename $1`
     38 }
     39 
     40 find_testcases() { #directory
     41   echo `find $1 -name \*.tc | sort`
     42 }
     43 
     44 parse_opts() { # opts
     45   local OPT_TEST_CASES=
     46   local OPT_TEST_DIR=
     47 
     48   while [ "$1" ]; do
     49     case "$1" in
     50     --help|-h)
     51       usage 0
     52     ;;
     53     --keep|-k)
     54       KEEP_LOG=1
     55       shift 1
     56     ;;
     57     --verbose|-v)
     58       VERBOSE=1
     59       shift 1
     60     ;;
     61     --debug|-d)
     62       DEBUG=1
     63       shift 1
     64     ;;
     65     *.tc)
     66       if [ -f "$1" ]; then
     67         OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"
     68         shift 1
     69       else
     70         usage 1 "$1 is not a testcase"
     71       fi
     72       ;;
     73     *)
     74       if [ -d "$1" ]; then
     75         OPT_TEST_DIR=`abspath $1`
     76         OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
     77         shift 1
     78       else
     79         usage 1 "Invalid option ($1)"
     80       fi
     81     ;;
     82     esac
     83   done
     84   if [ "$OPT_TEST_CASES" ]; then
     85     TEST_CASES=$OPT_TEST_CASES
     86   fi
     87 }
     88 
     89 # Parameters
     90 DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
     91 if [ -z "$DEBUGFS_DIR" ]; then
     92     TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
     93 else
     94     TRACING_DIR=$DEBUGFS_DIR/tracing
     95 fi
     96 
     97 TOP_DIR=`absdir $0`
     98 TEST_DIR=$TOP_DIR/test.d
     99 TEST_CASES=`find_testcases $TEST_DIR`
    100 LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
    101 KEEP_LOG=0
    102 DEBUG=0
    103 VERBOSE=0
    104 # Parse command-line options
    105 parse_opts $*
    106 
    107 [ $DEBUG -ne 0 ] && set -x
    108 
    109 # Verify parameters
    110 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
    111   errexit "No ftrace directory found"
    112 fi
    113 
    114 # Preparing logs
    115 LOG_FILE=$LOG_DIR/ftracetest.log
    116 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
    117 date > $LOG_FILE
    118 prlog() { # messages
    119   echo "$@" | tee -a $LOG_FILE
    120 }
    121 catlog() { #file
    122   cat $1 | tee -a $LOG_FILE
    123 }
    124 prlog "=== Ftrace unit tests ==="
    125 
    126 
    127 # Testcase management
    128 # Test result codes - Dejagnu extended code
    129 PASS=0	# The test succeeded.
    130 FAIL=1	# The test failed, but was expected to succeed.
    131 UNRESOLVED=2  # The test produced indeterminate results. (e.g. interrupted)
    132 UNTESTED=3    # The test was not run, currently just a placeholder.
    133 UNSUPPORTED=4 # The test failed because of lack of feature.
    134 XFAIL=5	# The test failed, and was expected to fail.
    135 
    136 # Accumulations
    137 PASSED_CASES=
    138 FAILED_CASES=
    139 UNRESOLVED_CASES=
    140 UNTESTED_CASES=
    141 UNSUPPORTED_CASES=
    142 XFAILED_CASES=
    143 UNDEFINED_CASES=
    144 TOTAL_RESULT=0
    145 
    146 CASENO=0
    147 testcase() { # testfile
    148   CASENO=$((CASENO+1))
    149   desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
    150   prlog -n "[$CASENO]$desc"
    151 }
    152 
    153 eval_result() { # sigval
    154   case $1 in
    155     $PASS)
    156       prlog "	[PASS]"
    157       PASSED_CASES="$PASSED_CASES $CASENO"
    158       return 0
    159     ;;
    160     $FAIL)
    161       prlog "	[FAIL]"
    162       FAILED_CASES="$FAILED_CASES $CASENO"
    163       return 1 # this is a bug.
    164     ;;
    165     $UNRESOLVED)
    166       prlog "	[UNRESOLVED]"
    167       UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
    168       return 1 # this is a kind of bug.. something happened.
    169     ;;
    170     $UNTESTED)
    171       prlog "	[UNTESTED]"
    172       UNTESTED_CASES="$UNTESTED_CASES $CASENO"
    173       return 0
    174     ;;
    175     $UNSUPPORTED)
    176       prlog "	[UNSUPPORTED]"
    177       UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
    178       return 1 # this is not a bug, but the result should be reported.
    179     ;;
    180     $XFAIL)
    181       prlog "	[XFAIL]"
    182       XFAILED_CASES="$XFAILED_CASES $CASENO"
    183       return 0
    184     ;;
    185     *)
    186       prlog "	[UNDEFINED]"
    187       UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
    188       return 1 # this must be a test bug
    189     ;;
    190   esac
    191 }
    192 
    193 # Signal handling for result codes
    194 SIG_RESULT=
    195 SIG_BASE=36	# Use realtime signals
    196 SIG_PID=$$
    197 
    198 SIG_FAIL=$((SIG_BASE + FAIL))
    199 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
    200 
    201 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
    202 exit_unresolved () {
    203   kill -s $SIG_UNRESOLVED $SIG_PID
    204   exit 0
    205 }
    206 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
    207 
    208 SIG_UNTESTED=$((SIG_BASE + UNTESTED))
    209 exit_untested () {
    210   kill -s $SIG_UNTESTED $SIG_PID
    211   exit 0
    212 }
    213 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
    214 
    215 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
    216 exit_unsupported () {
    217   kill -s $SIG_UNSUPPORTED $SIG_PID
    218   exit 0
    219 }
    220 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
    221 
    222 SIG_XFAIL=$((SIG_BASE + XFAIL))
    223 exit_xfail () {
    224   kill -s $SIG_XFAIL $SIG_PID
    225   exit 0
    226 }
    227 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
    228 
    229 __run_test() { # testfile
    230   # setup PID and PPID, $$ is not updated.
    231   (cd $TRACING_DIR; read PID _ < /proc/self/stat ; set -e; set -x; . $1)
    232   [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
    233 }
    234 
    235 # Run one test case
    236 run_test() { # testfile
    237   local testname=`basename $1`
    238   local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
    239   testcase $1
    240   echo "execute: "$1 > $testlog
    241   SIG_RESULT=0
    242   if [ $VERBOSE -ne 0 ]; then
    243     __run_test $1 2>> $testlog | tee -a $testlog
    244   else
    245     __run_test $1 >> $testlog 2>&1
    246   fi
    247   eval_result $SIG_RESULT
    248   if [ $? -eq 0 ]; then
    249     # Remove test log if the test was done as it was expected.
    250     [ $KEEP_LOG -eq 0 ] && rm $testlog
    251   else
    252     catlog $testlog
    253     TOTAL_RESULT=1
    254   fi
    255 }
    256 
    257 # load in the helper functions
    258 . $TEST_DIR/functions
    259 
    260 # Main loop
    261 for t in $TEST_CASES; do
    262   run_test $t
    263 done
    264 
    265 prlog ""
    266 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
    267 prlog "# of failed: " `echo $FAILED_CASES | wc -w`
    268 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
    269 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
    270 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
    271 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
    272 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
    273 
    274 # if no error, return 0
    275 exit $TOTAL_RESULT
    276