tests: More unit test factoring/rationalisation and bug fixes
authorMartin Schwenke <martin@meltin.net>
Wed, 18 Apr 2012 00:37:45 +0000 (10:37 +1000)
committerMartin Schwenke <martin@meltin.net>
Wed, 18 Apr 2012 00:37:45 +0000 (10:37 +1000)
Move some options from eventscripts/run_tests.sh to scripts/run_tests.
Remove the former.

Move some functions from eventscripts/scripts/local.sh to
scripts/unit.sh.

Both of these are modified during move so they are no longer
eventscript-specific.

Tweak */local.sh so that the new functions in unit.sh are used.

Signed-off-by: Martin Schwenke <martin@meltin.net>
(This used to be ctdb commit 7ff485687891732074c9fc9998502ca197663d02)

ctdb/tests/eventscripts/etc-ctdb/interface_modify.sh [deleted symlink]
ctdb/tests/eventscripts/run_tests.sh [deleted file]
ctdb/tests/eventscripts/scripts/local.sh
ctdb/tests/onnode/scripts/local.sh
ctdb/tests/scripts/run_tests
ctdb/tests/scripts/unit.sh
ctdb/tests/takeover/scripts/local.sh
ctdb/tests/tool/scripts/local.sh

diff --git a/ctdb/tests/eventscripts/etc-ctdb/interface_modify.sh b/ctdb/tests/eventscripts/etc-ctdb/interface_modify.sh
deleted file mode 120000 (symlink)
index 94f555c..0000000
+++ /dev/null
@@ -1 +0,0 @@
-../../../config/interface_modify.sh
\ No newline at end of file
diff --git a/ctdb/tests/eventscripts/run_tests.sh b/ctdb/tests/eventscripts/run_tests.sh
deleted file mode 100755 (executable)
index 406689a..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-#!/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
index 2ee8fd20f0cd30fbc3ef9ec00f287818af7d581d..6d7de894651515e3eb8516af795ecd19d469be14 100644 (file)
@@ -38,7 +38,7 @@ export CTDB_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb"
 
 ######################################################################
 
-if [ "$TEST_VERBOSE" = "yes" ] ; then
+if "$TEST_VERBOSE" ; then
     debug () { echo "$@" ; }
 else
     debug () { : ; }
@@ -672,133 +672,13 @@ define_test ()
     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
@@ -815,10 +695,12 @@ simple_test ()
 {
     [ -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 ()
@@ -912,8 +794,8 @@ iterate_test ()
            _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)"
 }
index a990c695f8d401a3c671880dcb4404ff29fa4791..d6dd41c293a7df3fffc1748ce5829e1fe5be0130 100644 (file)
@@ -49,10 +49,14 @@ ctdb_set_output ()
     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 ()
@@ -62,27 +66,16 @@ 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"
 }
index 5e2de17c8055b7c7f2d935a40fdc28e43cb28d10..3a5ec96b74f739a1242024c6b2ad8e738dfc22ab 100755 (executable)
@@ -11,6 +11,15 @@ usage() {
     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
 }
@@ -22,8 +31,11 @@ with_desc=false
 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
 
@@ -36,6 +48,9 @@ while true ; do
        -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
index 977cc9ae9c4d0269f2b8cf40f341038434a05eec..6f92d6cb163c6a8ccb31be9160bf337fe59463f6 100644 (file)
 
 # 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"
index 0e6230ab6fc550b25377c0cda7b925214d87bdf2..ba46ef26514e83f8086efb320d2bdcef3466fff2 100644 (file)
@@ -24,35 +24,13 @@ define_test ()
 
 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"
 }
index fe8416ee98e278062a6f50bc569b18fc92e2710e..1105628a4b041c441ebc2c1993e0e02b0ca5b1cc 100644 (file)
@@ -1,8 +1,5 @@
 # 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 ()
@@ -29,42 +26,13 @@ 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
 }