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