Merge branch 'martins'
[martins/samba.git] / ctdb / tests / simple / 16_ctdb_config_add_ip.sh
1 #!/bin/bash
2
3 test_info()
4 {
5     cat <<EOF
6 Verify that an IP address can be added to a node using 'ctdb addip'.
7
8 This test goes to some trouble to figure out which IP address to add
9 but assumes a 24-bit subnet mask.  It does not handle IPv6.  It does
10 not do any network level checks that the new IP address is reachable
11 but simply trusts 'ctdb ip' that the address has been added.  There is
12 also an extra prerequisite that the node being added to already has
13 public addresses - this is difficult to avoid if the extra address is
14 to be sensibly chosen.
15
16 Prerequisites:
17
18 * An active CTDB cluster with at least 2 active nodes.
19
20 Steps:
21
22 1. Verify that the status on all of the ctdb nodes is 'OK'.
23 2. Use 'ctdb ip' on one of the nodes to list the IP addresses being
24    served.
25 3. Add an additional public address to be served by the node, using
26    'ctdb addip'.
27 4. Verify that this IP address has been added to the list of IP
28    addresses being served by the node, using the 'ctdb ip' command.
29
30 Expected results:
31
32 * 'ctdb ip' adds an IP address to the list of public IP addresses
33   being served by a node.
34 EOF
35 }
36
37 . ctdb_test_functions.bash
38
39 ctdb_test_init "$@"
40
41 set -e
42
43 onnode 0 $CTDB_TEST_WRAPPER cluster_is_healthy
44
45 echo "Getting list of public IPs..."
46 try_command_on_node 0 "$CTDB ip -n all | sed -e '1d'"
47
48 # When selecting test_node we just want a node that has public IPs.
49 # This will work and is economically semi-randomly.  :-)
50 read x test_node <<<"$out"
51
52 test_node_ips=""
53 all_ips=""
54 while read ip pnn ; do
55     all_ips="${all_ips}${all_ips:+ }${ip}"
56     [ "$pnn" = "$test_node" ] && \
57         test_node_ips="${test_node_ips}${test_node_ips:+ }${ip}"
58 done <<<"$out"
59
60 echo "Selected node ${test_node} with IPs: $test_node_ips"
61
62 # Try to find a free IP adddress.  This is inefficient but should
63 # succeed quickly.
64 try_command_on_node $test_node "ip addr show"
65 all_test_node_ips=$(echo "$out" | sed -rn -e 's@^[[:space:]]+inet[[:space:]]+([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+/[[:digit:]]+).*[[:space:]]([^[:space:]]+)+$@\1:\2@p')
66
67 add_ip=""
68
69 # Use an IP already on one of the nodes, remove the last octet and
70 # loop through the possible IP addreses.
71 for i in $test_node_ips ; do
72     prefix="${i%.*}"
73     for j in $(seq 1 254) ; do
74         try="${prefix}.${j}"
75         # Try to make sure it isn't used anywhere!
76
77         # First, make sure it isn't an existing public address on the
78         # cluster.
79         for k in $all_ips ; do
80             [ "$try" = "$k" ] && continue 2
81         done
82
83         # Also make sure it isn't some other address in use on the
84         # node.
85         for k in $all_test_node_ips ; do
86             [ "$try" = "${k%/*}" ] && continue 2
87         done
88
89         # Get the interface details for $i, which our address is a
90         # close relative of.  This should never fail but it can't hurt
91         # to be careful...
92         for k in $all_test_node_ips ; do
93             if [ "$i" = "${k%/*}" ] ; then
94                         # Found one!
95                 add_ip="${try}/${k#*/}"
96                 break 3
97             fi
98         done
99     done
100 done
101
102 if [ -n "$add_ip" ] ; then
103     echo "Adding IP: ${add_ip/:/ on interface }"
104     try_command_on_node $test_node $CTDB addip ${add_ip/:/ }
105
106     echo "Waiting for IP to be added..."
107     wait_until 60 ips_are_on_nodeglob $test_node $test_node_ips ${add_ip%/*}
108
109     echo "That worked!"
110 else
111     echo "BAD: Unable to find IP address to add."
112     testfailures=1
113 fi
114
115 echo "Restarting cluster to restore configuration..."
116 restart_ctdb
117
118 ctdb_test_exit