From 79d28000043bd463beecaeac47855d3a4970eaf2 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Mon, 17 Mar 2014 13:42:35 +1100 Subject: [PATCH] ctdb-tests: Add "ctdb listnodes" and "ctdb xpnn" stub tests 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 Reviewed-by: Amitay Isaacs --- ctdb/tests/src/ctdb_test.c | 8 ++++++++ ctdb/tests/src/ctdb_test_stubs.c | 36 ++++++++++++++++++++++++++++++++- ctdb/tests/tool/scripts/local.sh | 11 ++++++++++ ctdb/tests/tool/stubby.listnodes.001.sh | 20 ++++++++++++++++++ ctdb/tests/tool/stubby.listnodes.002.sh | 20 ++++++++++++++++++ ctdb/tests/tool/stubby.xpnn.001.sh | 20 ++++++++++++++++++ ctdb/tests/tool/stubby.xpnn.002.sh | 20 ++++++++++++++++++ ctdb/tests/tool/stubby.xpnn.003.sh | 24 ++++++++++++++++++++++ 8 files changed, 158 insertions(+), 1 deletion(-) create mode 100755 ctdb/tests/tool/stubby.listnodes.001.sh create mode 100755 ctdb/tests/tool/stubby.listnodes.002.sh create mode 100755 ctdb/tests/tool/stubby.xpnn.001.sh create mode 100755 ctdb/tests/tool/stubby.xpnn.002.sh create mode 100755 ctdb/tests/tool/stubby.xpnn.003.sh diff --git a/ctdb/tests/src/ctdb_test.c b/ctdb/tests/src/ctdb_test.c index bbb51bd877c..68a82823b2c 100644 --- a/ctdb/tests/src/ctdb_test.c +++ b/ctdb/tests/src/ctdb_test.c @@ -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" diff --git a/ctdb/tests/src/ctdb_test_stubs.c b/ctdb/tests/src/ctdb_test_stubs.c index 3ca7b9112ea..1edbe6aee77 100644 --- a/ctdb/tests/src/ctdb_test_stubs.c +++ b/ctdb/tests/src/ctdb_test_stubs.c @@ -17,6 +17,9 @@ along with this program; if not, see . */ +/* 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: * [RECMASTER] [CURRENT] [CAPABILITIES] * EOF or a blank line terminates input. @@ -336,6 +339,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; ctdb = talloc_zero(NULL, struct ctdb_context); @@ -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; +} diff --git a/ctdb/tests/tool/scripts/local.sh b/ctdb/tests/tool/scripts/local.sh index 6cad929e85e..8d7d56c0c62 100644 --- a/ctdb/tests/tool/scripts/local.sh +++ b/ctdb/tests/tool/scripts/local.sh @@ -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 () { _out=$($VALGRIND $test_prog "$@" 2>&1) diff --git a/ctdb/tests/tool/stubby.listnodes.001.sh b/ctdb/tests/tool/stubby.listnodes.001.sh new file mode 100755 index 00000000000..35a0c14b8eb --- /dev/null +++ b/ctdb/tests/tool/stubby.listnodes.001.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +. "${TEST_SCRIPTS_DIR}/unit.sh" + +define_test "missing nodes file" + +setup_nodes <