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
170prlog() { # messages
171  [ -z "$LOG_FILE" ] && echo -e "$@" || echo -e "$@" | tee -a $LOG_FILE
172}
173catlog() { #file
174  [ -z "$LOG_FILE" ] && cat $1 || cat $1 | tee -a $LOG_FILE
175}
176prlog "=== Ftrace unit tests ==="
177
178
179# Testcase management
180# Test result codes - Dejagnu extended code
181PASS=0	# The test succeeded.
182FAIL=1	# The test failed, but was expected to succeed.
183UNRESOLVED=2  # The test produced indeterminate results. (e.g. interrupted)
184UNTESTED=3    # The test was not run, currently just a placeholder.
185UNSUPPORTED=4 # The test failed because of lack of feature.
186XFAIL=5	# The test failed, and was expected to fail.
187
188# Accumulations
189PASSED_CASES=
190FAILED_CASES=
191UNRESOLVED_CASES=
192UNTESTED_CASES=
193UNSUPPORTED_CASES=
194XFAILED_CASES=
195UNDEFINED_CASES=
196TOTAL_RESULT=0
197
198INSTANCE=
199CASENO=0
200testcase() { # testfile
201  CASENO=$((CASENO+1))
202  desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
203  prlog -n "[$CASENO]$INSTANCE$desc"
204}
205
206test_on_instance() { # testfile
207  grep -q "^#[ \t]*flags:.*instance" $1
208}
209
210eval_result() { # sigval
211  case $1 in
212    $PASS)
213      prlog "	[${color_green}PASS${color_reset}]"
214      PASSED_CASES="$PASSED_CASES $CASENO"
215      return 0
216    ;;
217    $FAIL)
218      prlog "	[${color_red}FAIL${color_reset}]"
219      FAILED_CASES="$FAILED_CASES $CASENO"
220      return 1 # this is a bug.
221    ;;
222    $UNRESOLVED)
223      prlog "	[${color_blue}UNRESOLVED${color_reset}]"
224      UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
225      return 1 # this is a kind of bug.. something happened.
226    ;;
227    $UNTESTED)
228      prlog "	[${color_blue}UNTESTED${color_reset}]"
229      UNTESTED_CASES="$UNTESTED_CASES $CASENO"
230      return 0
231    ;;
232    $UNSUPPORTED)
233      prlog "	[${color_blue}UNSUPPORTED${color_reset}]"
234      UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
235      return $UNSUPPORTED_RESULT # depends on use case
236    ;;
237    $XFAIL)
238      prlog "	[${color_red}XFAIL${color_reset}]"
239      XFAILED_CASES="$XFAILED_CASES $CASENO"
240      return 0
241    ;;
242    *)
243      prlog "	[${color_blue}UNDEFINED${color_reset}]"
244      UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
245      return 1 # this must be a test bug
246    ;;
247  esac
248}
249
250# Signal handling for result codes
251SIG_RESULT=
252SIG_BASE=36	# Use realtime signals
253SIG_PID=$$
254
255exit_pass () {
256  exit 0
257}
258
259SIG_FAIL=$((SIG_BASE + FAIL))
260exit_fail () {
261  exit 1
262}
263trap 'SIG_RESULT=$FAIL' $SIG_FAIL
264
265SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
266exit_unresolved () {
267  kill -s $SIG_UNRESOLVED $SIG_PID
268  exit 0
269}
270trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
271
272SIG_UNTESTED=$((SIG_BASE + UNTESTED))
273exit_untested () {
274  kill -s $SIG_UNTESTED $SIG_PID
275  exit 0
276}
277trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
278
279SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
280exit_unsupported () {
281  kill -s $SIG_UNSUPPORTED $SIG_PID
282  exit 0
283}
284trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
285
286SIG_XFAIL=$((SIG_BASE + XFAIL))
287exit_xfail () {
288  kill -s $SIG_XFAIL $SIG_PID
289  exit 0
290}
291trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
292
293__run_test() { # testfile
294  # setup PID and PPID, $$ is not updated.
295  (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1)
296  [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
297}
298
299# Run one test case
300run_test() { # testfile
301  local testname=`basename $1`
302  testcase $1
303  if [ ! -z "$LOG_FILE" ] ; then
304    local testlog=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX`
305  else
306    local testlog=/proc/self/fd/1
307  fi
308  export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
309  echo "execute$INSTANCE: "$1 > $testlog
310  SIG_RESULT=0
311  if [ $VERBOSE -eq -1 ]; then
312    __run_test $1
313  elif [ -z "$LOG_FILE" ]; then
314    __run_test $1 2>&1
315  elif [ $VERBOSE -ge 3 ]; then
316    __run_test $1 | tee -a $testlog 2>&1
317  elif [ $VERBOSE -eq 2 ]; then
318    __run_test $1 2>> $testlog | tee -a $testlog
319  else
320    __run_test $1 >> $testlog 2>&1
321  fi
322  eval_result $SIG_RESULT
323  if [ $? -eq 0 ]; then
324    # Remove test log if the test was done as it was expected.
325    [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog
326  else
327    [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog
328    TOTAL_RESULT=1
329  fi
330  rm -rf $TMPDIR
331}
332
333# load in the helper functions
334. $TEST_DIR/functions
335
336# Main loop
337for t in $TEST_CASES; do
338  run_test $t
339  if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
340    echo "A failure detected. Stop test."
341    exit 1
342  fi
343done
344
345# Test on instance loop
346INSTANCE=" (instance) "
347for t in $TEST_CASES; do
348  test_on_instance $t || continue
349  SAVED_TRACING_DIR=$TRACING_DIR
350  export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`
351  run_test $t
352  rmdir $TRACING_DIR
353  TRACING_DIR=$SAVED_TRACING_DIR
354  if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
355    echo "A failure detected. Stop test."
356    exit 1
357  fi
358done
359(cd $TRACING_DIR; initialize_ftrace) # for cleanup
360
361prlog ""
362prlog "# of passed: " `echo $PASSED_CASES | wc -w`
363prlog "# of failed: " `echo $FAILED_CASES | wc -w`
364prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
365prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
366prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
367prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
368prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
369
370# if no error, return 0
371exit $TOTAL_RESULT
372