Skip to content

Commit

Permalink
ctdb-tests: Simplify nodes file handling in tool tests
Browse files Browse the repository at this point in the history
Instead of using an intermediate environment variable for nodes files,
just create "node" or "nodes.<pnn>" in CTDB_BASE.  This makes the
nodes file loading in fake_ctdb slightly repetitive but simplifies the
test scripts a lot.  It also remove several instance of the CTDB_NODES
variable from the code base, so it is no longer found by "git grep".

Use an empty nodes file to indicate that fake_ctdbd should fail to
read it.

Signed-off-by: Martin Schwenke <[email protected]>
Reviewed-by: Amitay Isaacs <[email protected]>
  • Loading branch information
martin-schwenke authored and Amitay Isaacs committed Mar 19, 2018
1 parent 3a7c49d commit 480c586
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
51 changes: 36 additions & 15 deletions ctdb/tests/src/fake_ctdbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,28 +380,49 @@ static struct ctdb_node_map *read_nodes_file(TALLOC_CTX *mem_ctx,
uint32_t pnn)
{
struct ctdb_node_map *nodemap;
char nodepath[PATH_MAX];
const char *nodes_list;

/* read the nodes file */
sprintf(nodepath, "CTDB_NODES_%u", pnn);
nodes_list = getenv(nodepath);
if (nodes_list == NULL) {
nodes_list = getenv("CTDB_NODES");
if (nodes_list == NULL) {
DEBUG(DEBUG_INFO, ("Nodes file not defined\n"));
char nodes_list[PATH_MAX];
const char *ctdb_base;
int num;

ctdb_base = getenv("CTDB_BASE");
if (ctdb_base == NULL) {
D_ERR("CTDB_BASE is not set\n");
return NULL;
}

/* read optional node-specific nodes file */
num = snprintf(nodes_list, sizeof(nodes_list),
"%s/nodes.%d", ctdb_base, pnn);
if (num == sizeof(nodes_list)) {
D_ERR("nodes file path too long\n");
return NULL;
}
nodemap = ctdb_read_nodes_file(mem_ctx, nodes_list);
if (nodemap != NULL) {
/* Fake a load failure for an empty nodemap */
if (nodemap->num == 0) {
talloc_free(nodemap);

D_ERR("Failed to read nodes file \"%s\"\n", nodes_list);
return NULL;
}

return nodemap;
}

nodemap = ctdb_read_nodes_file(mem_ctx, nodes_list);
if (nodemap == NULL) {
DEBUG(DEBUG_INFO, ("Failed to read nodes file \"%s\"\n",
nodes_list));
/* read normal nodes file */
num = snprintf(nodes_list, sizeof(nodes_list), "%s/nodes", ctdb_base);
if (num == sizeof(nodes_list)) {
D_ERR("nodes file path too long\n");
return NULL;
}
nodemap = ctdb_read_nodes_file(mem_ctx, nodes_list);
if (nodemap != NULL) {
return nodemap;
}

return nodemap;
DBG_ERR("Failed to read nodes file \"%s\"\n", nodes_list);
return NULL;
}

static struct interface_map *interfaces_init(TALLOC_CTX *mem_ctx)
Expand Down
5 changes: 3 additions & 2 deletions ctdb/tests/tool/ctdb.listnodes.001.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ setup_nodes <<EOF
192.168.20.43
EOF

rm -f "$CTDB_NODES"
f="${CTDB_BASE}/nodes"
rm -f "$f"

required_result 1 <<EOF
${TEST_DATE_STAMP}Failed to read nodes file "${CTDB_NODES}"
${TEST_DATE_STAMP}Failed to read nodes file "${f}"
EOF

simple_test
6 changes: 1 addition & 5 deletions ctdb/tests/tool/ctdb.reloadnodes.003.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ setup_nodes <<EOF
192.168.20.43
EOF

# fake_ctdbd returns error for empty file
setup_nodes 1 <<EOF
192.168.20.41
#192.168.20.42
192.168.20.43
EOF

rm "$CTDB_NODES_1"

setup_ctdbd <<EOF
NODEMAP
0 192.168.20.41 0x0 CURRENT RECMASTER
Expand Down
16 changes: 1 addition & 15 deletions ctdb/tests/tool/scripts/local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,9 @@ setup_nodes ()
{
_pnn="$1"

_v="CTDB_NODES${_pnn:+_}${_pnn}"
debug "Setting up ${_v}"
_f="${CTDB_BASE}/nodes${_pnn:+.}${_pnn}"

eval export "${_v}"=$(mktemp --tmpdir="$TEST_VAR_DIR")

eval _f="\${${_v}}"
test_cleanup "rm -f ${_f}"
cat >"$_f"

# You can't be too careful about what might be in the
# environment... so clean up when setting the default variable.
if [ -z "$_pnn" ] ; then
_n=$(wc -l "$CTDB_NODES" | awk '{ print $1 }')
for _i in $(seq 0 $_n) ; do
eval unset "CTDB_NODES_${_i}"
done
fi
}

simple_test_other ()
Expand Down

0 comments on commit 480c586

Please sign in to comment.