ctdb-tests: Fix flakey test complex/18_ctdb_reloadips.sh
[vlendec/samba-autobuild/.git] / ctdb / tests / complex / 18_ctdb_reloadips.sh
1 #!/bin/bash
2
3 test_info()
4 {
5     cat <<EOF
6 Verify that adding/deleting IPs using 'ctdb reloadips' works
7
8 Checks that when IPs are added to and deleted from a single node then
9 those IPs are actually assigned and unassigned from the specified
10 interface.
11
12 Prerequisites:
13
14 * An active CTDB cluster with public IP addresses configured
15
16 Expected results:
17
18 * When IPs are added to a single node then they are assigned to an
19   interface.
20
21 * When IPs are deleted from a single node then they disappear from an
22   interface.
23 EOF
24 }
25
26 . "${TEST_SCRIPTS_DIR}/integration.bash"
27
28 set -e
29
30 ctdb_test_init "$@"
31
32 ctdb_test_check_real_cluster
33
34 cluster_is_healthy
35
36 # Reset configuration
37 ctdb_restart_when_done
38
39 select_test_node_and_ips
40
41 ####################
42
43 # Search for an unused 10.B.1.0/24 network on which to add public IP
44 # addresses.
45
46 # The initial search is for a 10.B.0.0/16 network since some
47 # configurations may use a whole class B for the private network.
48 # Check that there are no public IP addresses (as reported by "ctdb ip
49 # all") or other IP addresses (as reported by "ip addr show") with
50 # the provided prefix.  Note that this is an IPv4-specific test.
51
52 echo "Getting public IP information from CTDB..."
53 try_command_on_node any "$CTDB ip -X -v all"
54 ctdb_ip_info=$(echo "$out" | awk -F'|' 'NR > 1 { print $2, $3, $5 }')
55
56 echo "Getting IP information from interfaces..."
57 try_command_on_node all "ip addr show"
58 ip_addr_info=$(echo "$out" | \
59     awk '$1 == "inet" { ip = $2; sub(/\/.*/, "", ip); print ip }')
60
61 prefix=""
62 for b in $(seq 0 255) ; do
63     prefix="10.${b}"
64
65     # Does the prefix match any IP address returned by "ip addr info"?
66     while read ip ; do
67         if [ "${ip#${prefix}.}" != "$ip" ] ; then
68             prefix=""
69             continue 2
70         fi
71     done <<<"$ip_addr_info"
72
73     # Does the prefix match any public IP address "ctdb ip all"?
74     while read ip pnn iface ; do
75         if [ "${ip#${prefix}.}" != "$ip" ] ; then
76             prefix=""
77             continue 2
78         fi
79     done <<<"$ctdb_ip_info"
80
81     # Got through the IPs without matching prefix - done!
82     break
83 done
84
85 [ -n "$prefix" ] || die "Unable to find a usable IP address prefix"
86
87 # We really want a class C: 10.B.1.0/24
88 prefix="${prefix}.1"
89
90 ####################
91
92 iface=$(echo "$ctdb_ip_info" | awk -v pnn=$test_node '$2 == pnn { print $3 ; exit }')
93
94 ####################
95
96 # This needs to be set only on the recmaster. All nodes should do the trick.
97 new_takeover_timeout=90
98 echo "Setting TakeoverTimeout=${new_takeover_timeout} to avoid potential bans"
99 try_command_on_node all "$CTDB setvar TakeoverTimeout ${new_takeover_timeout}"
100
101 ####################
102
103 addresses=$(get_ctdbd_command_line_option $test_node "public-addresses")
104 echo "Public addresses file on node $test_node is \"$addresses\""
105 backup="${addresses}.$$"
106
107 backup_public_addresses ()
108 {
109     try_command_on_node $test_node "cp -a $addresses $backup"
110 }
111
112 restore_public_addresses ()
113 {
114     try_command_on_node $test_node "mv $backup $addresses >/dev/null 2>&1 || true"
115 }
116 ctdb_test_exit_hook_add restore_public_addresses
117
118 # Now create that backup
119 backup_public_addresses
120
121 ####################
122
123 add_ips_to_original_config ()
124 {
125     local test_node="$1"
126     local addresses="$2"
127     local iface="$3"
128     local prefix="$4"
129     local first="$5"
130     local last="$6"
131
132     echo "Adding new public IPs to original config on node ${test_node}..."
133     echo "IPs will be ${prefix}.${first}/24..${prefix}.${last}/24"
134
135     # Implement this by completely rebuilding the public_addresses
136     # file.  This is easier than deleting entries on a remote node.
137     restore_public_addresses
138     backup_public_addresses
139
140     # Note that tee is a safe way of creating a file on a remote node.
141     # This avoids potential fragility with quoting or redirection.
142     for i in $(seq $first $last) ; do
143         echo "${prefix}.${i}/24 ${iface}"
144     done |
145     try_command_on_node -i $test_node "tee -a $addresses"
146 }
147
148 check_ips ()
149 {
150     local test_node="$1"
151     local iface="$2"
152     local prefix="$3"
153     local first="$4"
154     local last="$5"
155
156     # If just 0 specified then this is an empty range
157     local public_ips_file=$(mktemp)
158     if [ "$first" = 0 -a -z "$last" ] ; then
159         echo "Checking that there are no IPs in ${prefix}.0/24"
160     else
161         local prefix_regexp="inet *${prefix//./\.}"
162
163         echo "Checking IPs in range ${prefix}.${first}/24..${prefix}.${last}/24"
164
165         local i
166         for i in $(seq $first $last) ; do
167             echo "${prefix}.${i}"
168         done | sort >"$public_ips_file"
169     fi
170
171     try_command_on_node $test_node "ip addr show dev ${iface}"
172     local ip_addrs_file=$(mktemp)
173     echo "$out" | \
174         sed -n -e "s@.*inet * \(${prefix//./\.}\.[0-9]*\)/.*@\1@p" | \
175         sort >"$ip_addrs_file"
176
177     local diffs=$(diff "$public_ips_file" "$ip_addrs_file") || true
178     rm -f "$ip_addrs_file" "$public_ips_file"
179
180     if [ -z "$diffs" ] ; then
181         echo "GOOD: IP addresses are as expected"
182     else
183         echo "BAD: IP addresses are incorrect:"
184         echo "$diffs"
185         exit 1
186     fi
187 }
188
189 ####################
190
191 new_ip_max=100
192
193 ####################
194
195 add_ips_to_original_config \
196     $test_node "$addresses" "$iface" "$prefix" 1 $new_ip_max
197
198 try_command_on_node $test_node "$CTDB reloadips"
199
200 check_ips $test_node "$iface" "$prefix" 1 $new_ip_max
201
202 ####################
203
204 # This should be the primary.  Ensure that no other IPs are lost
205 echo "Using 'ctdb reloadips' to remove the 1st address just added..."
206
207 add_ips_to_original_config \
208     $test_node "$addresses" "$iface" "$prefix" 2 $new_ip_max
209
210 try_command_on_node $test_node "$CTDB reloadips"
211
212 check_ips $test_node "$iface" "$prefix" 2 $new_ip_max
213
214 ####################
215
216 # Get rid of about 1/2 the IPs
217 start=$(($new_ip_max / 2 + 1))
218 echo "Updating to include only about 1/2 of the new IPs..."
219
220 add_ips_to_original_config \
221     $test_node "$addresses" "$iface" "$prefix" $start $new_ip_max
222
223 try_command_on_node $test_node "$CTDB reloadips"
224
225 check_ips $test_node "$iface" "$prefix" $start $new_ip_max
226
227 ####################
228
229 # Delete the rest
230 echo "Restoring original IP configuration..."
231 restore_public_addresses
232
233 try_command_on_node $test_node "$CTDB reloadips"
234
235 check_ips $test_node "$iface" "$prefix" 0