ctdb-tests: Test ctdb CLI tool via a stub
authorMartin Schwenke <martin@meltin.net>
Mon, 14 Dec 2015 10:08:03 +0000 (21:08 +1100)
committerAmitay Isaacs <amitay@samba.org>
Mon, 25 Jan 2016 06:18:25 +0000 (07:18 +0100)
Some features, such NAT gateway and LVS support, can be implemented
without daemon and (internal) ctdb CLI tool support.  These are
non-core features that don't need incredible performance and they
don't need to be in the core code.  They can easily be reimplemented
in scripts, along with some configuration changes.

For continuity, the ctdb CLI tool code will call out to helper scripts
so that the current status information can still be provided.  Those
helper scripts may then reinvoke the ctdb CLI tool to gather
information.

So, redo the tool testing using a "ctdb" stub command.  This will
swallow standard input and feed it to the test program each time the
"ctdb" stub is called.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/tests/tool/scripts/local.sh
ctdb/tests/tool/stubs/ctdb [new file with mode: 0755]

index 737cb716278f8985c5186fcd1853f44ab8179626..68a25d2dad7755b7890ff4e3c8a2a5a1268c457f 100644 (file)
@@ -1,5 +1,11 @@
 # Hey Emacs, this is a -*- shell-script -*- !!!  :-)
 
+# Augment PATH with stubs/ directory.
+
+if [ -d "${TEST_SUBDIR}/stubs" ] ; then
+    PATH="${TEST_SUBDIR}/stubs:$PATH"
+fi
+
 if "$TEST_VERBOSE" ; then
     debug () { echo "$@" ; }
 else
@@ -14,12 +20,14 @@ define_test ()
        func.*)
            _func="${_f#func.}"
            _func="${_func%.*}" # Strip test number
-           test_prog="ctdb_functest ${_func}"
+           export CTDB_TEST_PROG="ctdb_functest"
+           test_args="$_func"
            ;;
        stubby.*)
            _cmd="${_f#stubby.}"
            _cmd="${_cmd%.*}" # Strip test number
-           test_prog="ctdb_stubtest ${_cmd}"
+           export CTDB_TEST_PROG="ctdb_stubtest"
+           test_args="$_cmd"
            ;;
        *)
            die "Unknown pattern for testcase \"$_f\""
@@ -71,5 +79,5 @@ simple_test ()
     : ${CTDB_DEBUGLEVEL:=3}
     export CTDB_DEBUGLEVEL
 
-    unit_test $test_prog "$@"
+    unit_test ctdb $test_args "$@"
 }
diff --git a/ctdb/tests/tool/stubs/ctdb b/ctdb/tests/tool/stubs/ctdb
new file mode 100755 (executable)
index 0000000..c6e1e70
--- /dev/null
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+# If "ctdb" is called several time in a test then it must always get
+# the same input.  So, it is read here the first time and then fed to
+# all future instances.
+if [ -z "$_CTDB_TOOL_STUB_INPUT" ] ; then
+    if ! tty -s ; then
+       _CTDB_TOOL_STUB_INPUT=$(cat)
+    else
+       _CTDB_TOOL_STUB_INPUT=""
+    fi
+    # Let's not try being subtle about whether this variable is unset
+    # or empty.  If we've been here then it is set, even if input was
+    # empty.
+    if [ -z "$_CTDB_TOOL_STUB_INPUT" ] ; then
+       _CTDB_TOOL_STUB_INPUT="@@@EMPTY@@@"
+    fi
+    export _CTDB_TOOL_STUB_INPUT
+fi
+
+if [ "$_CTDB_TOOL_STUB_INPUT" != "@@@EMPTY@@@" ] ; then
+    exec "$CTDB_TEST_PROG" "$@" <<EOF
+$_CTDB_TOOL_STUB_INPUT
+EOF
+else
+    exec "$CTDB_TEST_PROG" "$@" </dev/null
+fi