tests: Add "ctdb listnodes" and "ctdb xpnn" stub tests
authorMartin Schwenke <martin@meltin.net>
Mon, 17 Mar 2014 02:42:35 +0000 (13:42 +1100)
committerAmitay Isaacs <amitay@gmail.com>
Wed, 26 Mar 2014 02:58:28 +0000 (13:58 +1100)
Tests for xpnn need to implement a stub for ctdb_sys_have_ip().  The
cheapest way of doing this is to read a fake nodemap using the
existing code and check if the IP of the "current" node is the one
being asked about.  However, the fake state initialisation isn't
currently available to without_daemon commands because it is meant to
represent daemon state.  However, it can be made available by moving
the relevant code into a new stub for tevent_context_init().  The stub
still needs to initialise a tevent context - this can be done by
calling a lower level function.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
(Imported from commit 79d28000043bd463beecaeac47855d3a4970eaf2)

tests/src/ctdb_test.c
tests/src/ctdb_test_stubs.c
tests/tool/scripts/local.sh
tests/tool/stubby.listnodes.001.sh [new file with mode: 0755]
tests/tool/stubby.listnodes.002.sh [new file with mode: 0755]
tests/tool/stubby.xpnn.001.sh [new file with mode: 0755]
tests/tool/stubby.xpnn.002.sh [new file with mode: 0755]
tests/tool/stubby.xpnn.003.sh [new file with mode: 0755]

index bbb51bd877c8425bf8c348fbabefd6d1763425db..68a82823b2cdc39037c4a42b9777462862aa7a97 100644 (file)
@@ -30,6 +30,8 @@
 
 #define ctdb_cmdline_client(x, y) \
        ctdb_cmdline_client_stub(x, y)
+#define tevent_context_init(x) \
+       tevent_context_init_stub(x)
 #define ctdb_ctrl_getnodemap(ctdb, timelimit, pnn, tmp_ctx, nodemap) \
        ctdb_ctrl_getnodemap_stub(ctdb, timelimit, pnn, tmp_ctx, nodemap)
 #define ctdb_ctrl_get_ifaces(ctdb, timelimit, pnn, tmp_ctx, ifaces) \
@@ -48,6 +50,8 @@
        ctdb_client_check_message_handlers_stub(ctdb, ids, argc, result)
 #define ctdb_ctrl_getcapabilities(ctdb, timeout, destnode, capabilities) \
        ctdb_ctrl_getcapabilities_stub(ctdb, timeout, destnode, capabilities)
+#define ctdb_sys_have_ip(addr) \
+       ctdb_sys_have_ip_stub(addr)
 
 #include "tools/ctdb.c"
 
@@ -57,6 +61,9 @@
 #endif /* CTDB_TEST_USE_MAIN */
 
 #undef ctdb_cmdline_client
+#undef tevent_context_init
+/* This is called in client/ctdb_client.c so needs a declaration... */
+struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx);
 
 #include "common/cmdline.c"
 
@@ -69,6 +76,7 @@
 #undef ctdb_ctrl_getdebseqnum
 #undef ctdb_client_check_message_handlers
 #undef ctdb_ctrl_getcapabilities
+#undef ctdb_sys_have_ip
 
 #undef TIMELIMIT
 #include "tools/ctdb_vacuum.c"
index 3ca7b9112ea577a91c11f39ac1281db0016b4bdb..1edbe6aee7738261ed67564af2a54f10b03ce7eb 100644 (file)
@@ -17,6 +17,9 @@
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
+/* Useful for functions that don't get struct ctdb_context passed */
+static struct ctdb_context *ctdb_global;
+
 /* Read a nodemap from stdin.  Each line looks like:
  *  <PNN> <FLAGS> [RECMASTER] [CURRENT] [CAPABILITIES]
  * EOF or a blank line terminates input.
@@ -335,6 +338,11 @@ static bool current_node_is_connected (struct ctdb_context *ctdb)
 
 struct ctdb_context *ctdb_cmdline_client_stub(struct tevent_context *ev,
                                              struct timeval req_timeout)
+{
+       return ctdb_global;
+}
+
+struct tevent_context *tevent_context_init_stub(TALLOC_CTX *mem_ctx)
 {
        struct ctdb_context *ctdb;
 
@@ -344,7 +352,9 @@ struct ctdb_context *ctdb_cmdline_client_stub(struct tevent_context *ev,
 
        ctdb_test_stubs_fake_setup(ctdb);
 
-       return ctdb;
+       ctdb_global = ctdb;
+
+       return tevent_context_init_byname(mem_ctx, NULL);
 }
 
 /* Copied from ctdb_recover.c */
@@ -543,3 +553,27 @@ int ctdb_ctrl_getcapabilities_stub(struct ctdb_context *ctdb,
        *capabilities = ctdb->nodes[destnode]->capabilities;
        return 0;
 }
+
+/* This is to support testing ctdb xpnn */
+
+bool ctdb_sys_have_ip_stub(ctdb_sock_addr *addr)
+{
+       int i;
+       struct ctdb_context *ctdb = ctdb_global;
+
+       for (i = 0; i < ctdb->num_nodes; i++) {
+               ctdb_sock_addr node_addr;
+
+               if (ctdb->pnn == ctdb->nodes[i]->pnn) {
+                       if (!parse_ip(ctdb->nodes[i]->address.address, NULL, 0,
+                                     &node_addr)) {
+                               continue;
+                       }
+                       if (ctdb_same_ip(addr, &node_addr)) {
+                               return true;
+                       }
+               }
+       }
+
+       return false;
+}
index 385e2ad64b4d997f11e3c5a7eff760593de11f98..63f8bf60d8d9893f21ceadc2eab6a165e8c5e809 100644 (file)
@@ -42,6 +42,17 @@ setup_natgw ()
     cat >"$CTDB_NATGW_NODES"
 }
 
+setup_nodes ()
+{
+    debug "Setting up CTDB_NODES"
+
+    # These will accumulate, 1 per test... but will be cleaned up at
+    # the end.
+    export CTDB_NODES=$(mktemp --tmpdir="$TEST_VAR_DIR")
+
+    cat >"$CTDB_NODES"
+}
+
 simple_test ()
 {
     # Most of the tests when the tool fails will have a date/time/pid
diff --git a/tests/tool/stubby.listnodes.001.sh b/tests/tool/stubby.listnodes.001.sh
new file mode 100755 (executable)
index 0000000..35a0c14
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+define_test "missing nodes file"
+
+setup_nodes <<EOF
+192.168.20.41
+192.168.20.42
+192.168.20.43
+EOF
+
+rm -f "$CTDB_NODES"
+
+required_result 255 <<EOF
+DATE TIME [PID]: Failed to read nodes file
+EOF
+
+simple_test <<EOF
+EOF
diff --git a/tests/tool/stubby.listnodes.002.sh b/tests/tool/stubby.listnodes.002.sh
new file mode 100755 (executable)
index 0000000..d807e1e
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+define_test "missing nodes file"
+
+setup_nodes <<EOF
+192.168.20.41
+192.168.20.42
+192.168.20.43
+EOF
+
+required_result 0 <<EOF
+192.168.20.41
+192.168.20.42
+192.168.20.43
+EOF
+
+simple_test <<EOF
+EOF
diff --git a/tests/tool/stubby.xpnn.001.sh b/tests/tool/stubby.xpnn.001.sh
new file mode 100755 (executable)
index 0000000..e59e40c
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+define_test "3 nodes, current is 0"
+
+setup_nodes <<EOF
+192.168.20.41
+192.168.20.42
+192.168.20.43
+EOF
+
+required_result 0 "PNN:0"
+
+simple_test <<EOF
+NODEMAP
+0       192.168.20.41   0x0     CURRENT
+1       192.168.20.42   0x0
+2       192.168.20.43   0x0
+EOF
diff --git a/tests/tool/stubby.xpnn.002.sh b/tests/tool/stubby.xpnn.002.sh
new file mode 100755 (executable)
index 0000000..cc4f38c
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+define_test "3 nodes, current is 2"
+
+setup_nodes <<EOF
+192.168.20.41
+192.168.20.42
+192.168.20.43
+EOF
+
+required_result 0 "PNN:2"
+
+simple_test <<EOF
+NODEMAP
+0       192.168.20.41   0x0
+1       192.168.20.42   0x0
+2       192.168.20.43   0x0     CURRENT
+EOF
diff --git a/tests/tool/stubby.xpnn.003.sh b/tests/tool/stubby.xpnn.003.sh
new file mode 100755 (executable)
index 0000000..152d230
--- /dev/null
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+define_test "missing nodes file"
+
+setup_nodes <<EOF
+192.168.20.41
+192.168.20.42
+192.168.20.43
+EOF
+
+rm -f "$CTDB_NODES"
+
+required_result 255 <<EOF
+DATE TIME [PID]: Failed to read nodes file
+EOF
+
+simple_test <<EOF
+NODEMAP
+0       192.168.20.41   0x0     CURRENT
+1       192.168.20.42   0x0
+2       192.168.20.43   0x0
+EOF