+++ /dev/null
-../../../config/interface_modify.sh
\ No newline at end of file
+++ /dev/null
-#!/bin/sh
-
-# Eventscript unit test harness.
-
-cd $(dirname "$0")
-export EVENTSCRIPTS_TESTS_DIR=$(pwd)
-
-test_dir=$(dirname "$EVENTSCRIPTS_TESTS_DIR")
-
-export EVENTSCRIPT_TESTS_CAT_RESULTS_OPTS=""
-export EVENTSCRIPT_TESTS_DIFF_RESULTS=false
-
-opts="-d"
-
-for i ; do
- case "$i" in
- -v)
- export EVENTSCRIPT_TESTS_VERBOSE="yes"
- shift
- ;;
- -T)
- # This will cause tests to fail but is good for debugging
- # individual tests when they fail.
- export EVENTSCRIPTS_TESTS_TRACE="sh -x"
- shift
- ;;
- -A)
- # Useful for detecting whitespace differences in results
- export EVENTSCRIPT_TESTS_CAT_RESULTS_OPTS="-A"
- shift
- ;;
- -D)
- # Useful for detecting whitespace differences in results
- export EVENTSCRIPT_TESTS_DIFF_RESULTS=true
- shift
- ;;
- -*)
- opts="$opts $i"
- shift
- ;;
- *)
- break
- esac
-done
-
-tests=""
-if [ -z "$*" ] ; then
- tests=$(ls simple/[0-9][0-9].*.*.[0-9][0-9][0-9].sh simple/[0-9][0-9].*.*.[0-9][0-9][0-9]/run_test.sh 2>/dev/null)
-fi
-
-"$test_dir/scripts/run_tests" $opts "$@" $tests || exit 1
-
-exit 0
######################################################################
-if [ "$TEST_VERBOSE" = "yes" ] ; then
+if "$TEST_VERBOSE" ; then
debug () { echo "$@" ; }
else
debug () { : ; }
printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
}
-# Set the required result for a test.
-# - Argument 1 is exit code.
-# - Argument 2, if present is the required test output but "--"
-# indicates empty output.
-# If argument 2 is not present or null then read required test output
-# from stdin.
-required_result ()
+_extra_header ()
{
- required_rc="${1:-0}"
- if [ -n "$2" ] ; then
- if [ "$2" = "--" ] ; then
- required_output=""
- else
- required_output="$2"
- fi
- else
- if ! tty -s ; then
- required_output=$(cat)
- else
- required_output=""
- fi
- fi
-}
-
-ok ()
-{
- required_result 0 "$@"
-}
-
-ok_null ()
-{
- ok --
-}
-
-result_print ()
-{
- _passed="$1"
- _out="$2"
- _rc="$3"
- _iteration="$4"
-
- if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
- if [ -n "$_iteration" ] ; then
- cat <<EOF
-
-==================================================
-Iteration $_iteration
-EOF
- fi
-
-cat <<EOF
---------------------------------------------------
-Output (Exit status: ${_rc}):
---------------------------------------------------
-EOF
- echo "$_out" | cat $EVENTSCRIPT_TESTS_CAT_RESULTS_OPTS
- fi
-
- if ! $_passed ; then
- cat <<EOF
---------------------------------------------------
-Required output (Exit status: ${required_rc}):
---------------------------------------------------
-EOF
- echo "$required_output" | cat $EVENTSCRIPT_TESTS_CAT_RESULTS_OPTS
-
- if $EVENTSCRIPT_TESTS_DIFF_RESULTS ; then
- _outr=$(mktemp)
- echo "$required_output" >"$_outr"
-
- _outf=$(mktemp)
- echo "$_out" >"$_outf"
-
- cat <<EOF
---------------------------------------------------
-Diff:
---------------------------------------------------
-EOF
- diff -u "$_outr" "$_outf" | cat -A
- rm "$_outr" "$_outf"
- fi
- fi
-}
-
-result_footer ()
-{
- _passed="$1"
-
- if [ "$TEST_VERBOSE" = "yes" ] || ! $_passed ; then
-
- cat <<EOF
---------------------------------------------------
+ cat <<EOF
CTDB_BASE="$CTDB_BASE"
CTDB_ETCDIR="$CTDB_ETCDIR"
ctdb client is "$(which ctdb)"
---------------------------------------------------
EOF
- fi
-
- if $_passed ; then
- echo "PASSED"
- return 0
- else
- echo
- echo "FAILED"
- return 1
- fi
-}
-
-result_check ()
-{
- _rc=$?
-
- if [ -n "$OUT_FILTER" ] ; then
- _fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
- else
- _fout="$_out"
- fi
-
- if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
- _passed=true
- else
- _passed=false
- fi
-
- result_print "$_passed" "$_out" "$_rc"
- result_footer "$_passed"
}
# Run an eventscript once. The test passes if the return code and
{
[ -n "$event" ] || die 'simple_test: $event not set'
+ _extra_header=$(_extra_header)
+
echo "Running eventscript \"$script $event${1:+ }$*\""
- _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
+ _out=$($TEST_COMMAND_TRACE "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
- result_check
+ result_check "$_extra_header"
}
simple_test_event ()
_result=false
fi
- result_print "$_passed" "$_out" "$_rc" "$iteration"
+ result_print "$_passed" "$_out" "$_rc" "Iteration $iteration"
done
- result_footer "$_result"
+ result_footer "$_result" "$(_extra_header)"
}
trap "rm -f $_out $_rc" 0
}
-required_result ()
+_extra_header ()
{
- required_rc="${1:-0}"
- required_output=$(cat)
+ cat <<EOF
+CTDB_NODES_FILE="${CTDB_NODES_FILE}"
+CTDB_BASE="$CTDB_BASE"
+$(which ctdb)
+
+EOF
}
simple_test ()
shift
_sort="sort"
fi
+
_out=$("$@" 2>&1)
_rc=$?
_out=$(echo "$_out" | $_sort )
- if [ "$_out" = "$required_output" -a $_rc = $required_rc ] ; then
- echo "PASSED"
- else
- cat <<EOF
-CTDB_NODES_FILE="${CTDB_NODES_FILE}"
-CTDB_BASE="$CTDB_BASE"
-$(which ctdb)
+ # Can't do this inline or it affects return code
+ _extra_header="$(_extra_header)"
-##################################################
-Required output (Exit status: ${required_rc}):
-##################################################
-$required_output
-##################################################
-Actual output (Exit status: ${_rc}):
-##################################################
-$_out
-EOF
- return 1
- fi
+ # Get the return code back into $?
+ (exit $_rc)
+
+ result_check "$_extra_header"
}
cat <<EOF
Usage: run_tests [OPTIONS] [TESTS]
+Options:
+ -s Print a summary of tests results after running all tests
+ -v Verbose - print test output for non-failures (only some tests)
+ -A Use "cat -A" to print test output (only some tests)
+ -D Show diff between failed/expected test output (some tests only)
+ -X Trace certain scripts run by tests using -x (only some tests)
+ -d Print descriptions of tests instead of filenames (dodgy!)
+ -q Quiet - don't show tests being run (hint: use with -s)
+ -x Trace this script with the -x option
EOF
exit 1
}
quiet=false
export TEST_VERBOSE=false
+export TEST_COMMAND_TRACE=""
+export TEST_CAT_RESULTS_OPTS=""
+export TEST_DIFF_RESULTS=false
-temp=$(getopt -n "$prog" -o "xdhqsv" -l help -- "$@")
+temp=$(getopt -n "$prog" -o "xdhqsvXAD" -l help -- "$@")
[ $? != 0 ] && usage
-q) quiet=true ; shift ;;
-s) with_summary=true ; shift ;;
-v) TEST_VERBOSE=true ; shift ;;
+ -X) TEST_COMMAND_TRACE="sh -x" ; shift ;;
+ -A) TEST_CAT_RESULTS_OPTS="-A" ; shift ;;
+ -D) TEST_DIFF_RESULTS=true ; shift ;;
--) shift ; break ;;
*) usage ;;
esac
# Common variables and functions for CTDB unit tests.
+# Set the required result for a test.
+# - Argument 1 is exit code.
+# - Argument 2, if present is the required test output but "--"
+# indicates empty output.
+# If argument 2 is not present or null then read required test output
+# from stdin.
required_result ()
{
required_rc="${1:-0}"
- required_output=$(cat)
+ if [ -n "$2" ] ; then
+ if [ "$2" = "--" ] ; then
+ required_output=""
+ else
+ required_output="$2"
+ fi
+ else
+ if ! tty -s ; then
+ required_output=$(cat)
+ else
+ required_output=""
+ fi
+ fi
+}
+
+ok ()
+{
+ required_result 0 "$@"
+}
+
+ok_null ()
+{
+ ok --
+}
+
+result_print ()
+{
+ _passed="$1"
+ _out="$2"
+ _rc="$3"
+ _extra_header="$4"
+
+ if "$TEST_VERBOSE" || ! $_passed ; then
+ if [ -n "$_extra_header" ] ; then
+ cat <<EOF
+
+==================================================
+$_extra_header
+EOF
+ fi
+
+cat <<EOF
+--------------------------------------------------
+Output (Exit status: ${_rc}):
+--------------------------------------------------
+EOF
+ echo "$_out" | cat $TEST_CAT_RESULTS_OPTS
+ fi
+
+ if ! $_passed ; then
+ cat <<EOF
+--------------------------------------------------
+Required output (Exit status: ${required_rc}):
+--------------------------------------------------
+EOF
+ echo "$required_output" | cat $TEST_CAT_RESULTS_OPTS
+
+ if $TEST_DIFF_RESULTS ; then
+ _outr=$(mktemp)
+ echo "$required_output" >"$_outr"
+
+ _outf=$(mktemp)
+ echo "$_out" >"$_outf"
+
+ cat <<EOF
+--------------------------------------------------
+Diff:
+--------------------------------------------------
+EOF
+ diff -u "$_outr" "$_outf" | cat -A
+ rm "$_outr" "$_outf"
+ fi
+ fi
+}
+
+result_footer ()
+{
+ _passed="$1"
+ _extra_footer="$2"
+
+ if "$TEST_VERBOSE" || ! $_passed ; then
+ if [ -n "$_extra_footer" ] ; then
+ cat <<EOF
+--------------------------------------------------
+$_extra_footer
+--------------------------------------------------
+EOF
+ fi
+ fi
+
+ if $_passed ; then
+ echo "PASSED"
+ return 0
+ else
+ echo
+ echo "FAILED"
+ return 1
+ fi
+}
+
+result_check ()
+{
+ _rc=$?
+
+ _extra_header="$1"
+
+ if [ -n "$OUT_FILTER" ] ; then
+ _fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
+ else
+ _fout="$_out"
+ fi
+
+ if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
+ _passed=true
+ else
+ _passed=false
+ fi
+
+ result_print "$_passed" "$_out" "$_rc" "$_extra_header"
+ result_footer "$_passed"
}
local="${TEST_SUBDIR}/scripts/local.sh"
simple_test ()
{
- _states="$1"
- _out=$($test_prog $_states 2>&1)
- _rc=$?
-
+ # Do some filtering of the output to replace date/time.
if [ "$algorithm" = "lcp2" -a -n "$CTDB_TEST_LOGLEVEL" ] ; then
- OUT_FILTER='s@^.*:@DATE TIME \[PID\]:@'
- fi
-
- if [ -n "$OUT_FILTER" ] ; then
- _fout=$(echo "$_out" | sed -r "$OUT_FILTER")
- else
- _fout="$_out"
+ OUT_FILTER='s@^.*:@DATE\ TIME\ \[PID\]:@'
fi
- if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
- echo "PASSED"
- else
- cat <<EOF
-Algorithm: $algorithm
+ _states="$1"
+ _out=$($test_prog $_states 2>&1)
-##################################################
-Required output (Exit status: ${required_rc}):
-##################################################
-$required_output
-##################################################
-Actual output (Exit status: ${_rc}):
-##################################################
-$_out
-EOF
- return 1
- fi
+ result_check "Algorithm: $algorithm"
}
# Hey Emacs, this is a -*- shell-script -*- !!! :-)
-# Print a message and exit.
-die () { echo "$@" >&2 ; exit 1 ; }
-
test_bin="$(dirname ${TEST_SUBDIR})/bin"
define_test ()
simple_test ()
{
- _out=$($test_prog "$@" 2>&1)
- _rc=$?
-
# Most of the tests when the tool fails will have a date/time/pid
# prefix. Strip that because it isn't possible to match it.
if [ $required_rc -ne 0 ] ; then
- OUT_FILTER='s@^[0-9/]+ [0-9:\.]+ \[[ 0-9]+\]:@DATE TIME \[PID\]:@'
+ OUT_FILTER='s@^[0-9/]+\ [0-9:\.]+\ \[[\ 0-9]+\]:@DATE\ TIME\ \[PID\]:@'
fi
- if [ -n "$OUT_FILTER" ] ; then
- _fout=$(echo "$_out" | sed -r "$OUT_FILTER")
- else
- _fout="$_out"
- fi
+ _out=$($test_prog "$@" 2>&1)
- if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
- if [ "$TEST_VERBOSE" = "yes" ] ; then
- cat <<EOF
-##################################################
-Output (Exit status: ${_rc}):
-##################################################
-$_fout
-EOF
- fi
- echo "PASSED"
- else
- cat -A <<EOF
-##################################################
-Required output (Exit status: ${required_rc}):
-##################################################
-$required_output
-##################################################
-Actual output (Exit status: ${_rc}):
-##################################################
-$_fout
-EOF
- return 1
- fi
+ result_check
}