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  echo -e "$@"
177  [ "$LOG_FILE" ] && echo -e "$@" | strip_esc >> $LOG_FILE
178}
179catlog() { #file
180  cat $1
181  [ "$LOG_FILE" ] && cat $1 | strip_esc >> $LOG_FILE
182}
183prlog "=== Ftrace unit tests ==="
184
185
186# Testcase management
187# Test result codes - Dejagnu extended code
188PASS=0	# The test succeeded.
189FAIL=1	# The test failed, but was expected to succeed.
190UNRESOLVED=2  # The test produced indeterminate results. (e.g. interrupted)
191UNTESTED=3    # The test was not run, currently just a placeholder.
192UNSUPPORTED=4 # The test failed because of lack of feature.
193XFAIL=5	# The test failed, and was expected to fail.
194
195# Accumulations
196PASSED_CASES=
197FAILED_CASES=
198UNRESOLVED_CASES=
199UNTESTED_CASES=
200UNSUPPORTED_CASES=
201XFAILED_CASES=
202UNDEFINED_CASES=
203TOTAL_RESULT=0
204
205INSTANCE=
206CASENO=0
207testcase() { # testfile
208  CASENO=$((CASENO+1))
209  desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
210  prlog -n "[$CASENO]$INSTANCE$desc"
211}
212
213test_on_instance() { # testfile
214  grep -q "^#[ \t]*flags:.*instance" $1
215}
216
217eval_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
258SIG_RESULT=
259SIG_BASE=36	# Use realtime signals
260SIG_PID=$$
261
262exit_pass () {
263  exit 0
264}
265
266SIG_FAIL=$((SIG_BASE + FAIL))
267exit_fail () {
268  exit 1
269}
270trap 'SIG_RESULT=$FAIL' $SIG_FAIL
271
272SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
273exit_unresolved () {
274  kill -s $SIG_UNRESOLVED $SIG_PID
275  exit 0
276}
277trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
278
279SIG_UNTESTED=$((SIG_BASE + UNTESTED))
280exit_untested () {
281  kill -s $SIG_UNTESTED $SIG_PID
282  exit 0
283}
284trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
285
286SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
287exit_unsupported () {
288  kill -s $SIG_UNSUPPORTED $SIG_PID
289  exit 0
290}
291trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
292
293SIG_XFAIL=$((SIG_BASE + XFAIL))
294exit_xfail () {
295  kill -s $SIG_XFAIL $SIG_PID
296  exit 0
297}
298trap '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
307run_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
344for 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
350done
351
352# Test on instance loop
353INSTANCE=" (instance) "
354for 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
365done
366(cd $TRACING_DIR; initialize_ftrace) # for cleanup
367
368prlog ""
369prlog "# of passed: " `echo $PASSED_CASES | wc -w`
370prlog "# of failed: " `echo $FAILED_CASES | wc -w`
371prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
372prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
373prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
374prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
375prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
376
377# if no error, return 0
378exit $TOTAL_RESULT
379