ctdb-tools: Detect unknown node number
authorMartin Schwenke <martin@meltin.net>
Fri, 12 Oct 2018 03:41:42 +0000 (14:41 +1100)
committerAmitay Isaacs <amitay@samba.org>
Tue, 6 Nov 2018 06:16:14 +0000 (07:16 +0100)
If there aren't enough addresses in the list then the shift will
silently fail and the printed address will be the unshifted value of
$1, which is incorrect/unexpected.  So, sanity check the node number.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/tests/onnode/0010.sh [new file with mode: 0755]
ctdb/tests/onnode/0011.sh [new file with mode: 0755]
ctdb/tools/onnode

diff --git a/ctdb/tests/onnode/0010.sh b/ctdb/tests/onnode/0010.sh
new file mode 100755 (executable)
index 0000000..241cf58
--- /dev/null
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+cmd="$ONNODE 4 hostname"
+
+define_test "$cmd" "invalid pnn 4"
+
+required_result 1 <<EOF
+onnode: "node 4" does not exist
+EOF
+
+simple_test $cmd
diff --git a/ctdb/tests/onnode/0011.sh b/ctdb/tests/onnode/0011.sh
new file mode 100755 (executable)
index 0000000..4604533
--- /dev/null
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+cmd="$ONNODE 99 hostname"
+
+define_test "$cmd" "invalid pnn 99"
+
+required_result 1 <<EOF
+onnode: "node 99" does not exist
+EOF
+
+simple_test $cmd
index eabe034d227a7a177bb04070f916dd92da58c3b3..13b0d19b3eececb2ddcbedb7fcd5c9a73d72ad4d 100755 (executable)
@@ -102,8 +102,12 @@ echo_nth ()
 {
     local n="$1" ; shift
 
-    shift "$n"
-    local node="$1"
+    # Note that this is 0-based
+    local node=""
+    if [ "$n" -le $# ] ; then
+           shift "$n"
+           node="$1"
+    fi
 
     if [ -n "$node" -a "$node" != "#DEAD" ] ; then
        echo "$node"