1 # Hey Emacs, this is a -*- shell-script -*- !!! :-)
3 . "${TEST_SCRIPTS_DIR}/common.sh"
5 ######################################################################
9 if [ -n "$CTDB_TEST_REMOTE_DIR" ] ; then
10 CTDB_TEST_WRAPPER="${CTDB_TEST_REMOTE_DIR}/test_wrap"
12 _d=$(cd ${TEST_SCRIPTS_DIR}; echo $PWD)
13 CTDB_TEST_WRAPPER="$_d/test_wrap"
15 export CTDB_TEST_WRAPPER
17 # If $VALGRIND is set then use it whenever ctdb is called, but only if
18 # $CTDB is not already set.
19 [ -n "$CTDB" ] || export CTDB="${VALGRIND}${VALGRIND:+ }ctdb"
22 PATH="${TEST_SCRIPTS_DIR}:${PATH}"
24 ######################################################################
32 # run_tests.sh pipes stdout into tee. If the tee process is
33 # killed then any attempt to write to stdout (e.g. echo) will
34 # result in SIGPIPE, terminating the caller. Ignore SIGPIPE to
35 # ensure that all clean-up is run.
38 # Avoid making a test fail from this point onwards. The test is
42 echo "*** TEST COMPLETED (RC=$status) AT $(date '+%F %T'), CLEANING UP..."
44 eval "$ctdb_test_exit_hook" || true
45 unset ctdb_test_exit_hook
47 echo "Stopping cluster..."
53 ctdb_test_exit_hook_add ()
55 ctdb_test_exit_hook="${ctdb_test_exit_hook}${ctdb_test_exit_hook:+ ; }$*"
60 ctdb_test_restart_scheduled=false
62 trap "ctdb_test_exit" 0
64 ctdb_stop_all >/dev/null 2>&1 || true
66 echo "Configuring cluster..."
69 echo "Starting cluster..."
72 echo "*** SETUP COMPLETE AT $(date '+%F %T'), RUNNING TEST..."
75 ########################################
78 try_command_on_node ()
80 local nodespec="$1" ; shift
85 while [ "${nodespec#-}" != "$nodespec" ] ; do
86 if [ "$nodespec" = "-v" ] ; then
89 onnode_opts="${onnode_opts}${onnode_opts:+ }${nodespec}"
96 out=$(onnode -q $onnode_opts "$nodespec" "$cmd" 2>&1) || {
98 echo "Failed to execute \"$cmd\" on node(s) \"$nodespec\""
104 echo "Output of \"$cmd\":"
109 sanity_check_output ()
112 local regexp="$2" # Should be anchored as necessary.
117 local num_lines=$(echo "$output" | wc -l)
118 echo "There are $num_lines lines of output"
119 if [ $num_lines -lt $min_lines ] ; then
120 echo "BAD: that's less than the required number (${min_lines})"
125 local unexpected # local doesn't pass through status of command on RHS.
126 unexpected=$(echo "$output" | egrep -v "$regexp") || status=$?
128 # Note that this is reversed.
129 if [ $status -eq 0 ] ; then
130 echo "BAD: unexpected lines in output:"
131 echo "$unexpected" | cat -A
134 echo "Output lines look OK"
142 local ips="$1" # list of "ip node" lines
144 echo "Sanity checking IPs..."
148 while read x ipp ; do
149 [ "$ipp" = "-1" ] && break
150 if [ -n "$prev" -a "$ipp" != "$prev" ] ; then
157 echo "BAD: a node was -1 or IPs are only assigned to one node:"
159 echo "Are you running an old version of CTDB?"
163 # This returns a list of "ip node" lines in $out
167 try_command_on_node $node \
168 "$CTDB ip -X | awk -F'|' 'NR > 1 { print \$2, \$3 }'"
171 _select_test_node_and_ips ()
173 try_command_on_node any \
174 "$CTDB ip -X all | awk -F'|' 'NR > 1 { print \$2, \$3 }'"
176 test_node="" # this matches no PNN
179 while read ip pnn ; do
180 if [ -z "$test_node" -a "$pnn" != "-1" ] ; then
183 if [ "$pnn" = "$test_node" ] ; then
184 test_node_ips="${test_node_ips}${test_node_ips:+ }${ip}"
186 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
188 echo "Selected node ${test_node} with IPs: ${test_node_ips}."
189 test_ip="${test_node_ips%% *}"
192 *:*) test_prefix="${test_ip}/128" ;;
193 *) test_prefix="${test_ip}/32" ;;
196 [ -n "$test_node" ] || return 1
199 select_test_node_and_ips ()
202 while ! _select_test_node_and_ips ; do
203 echo "Unable to find a test node with IPs assigned"
204 if [ $timeout -le 0 ] ; then
205 echo "BAD: Too many attempts"
209 timeout=$(($timeout - 1))
216 get_test_ip_mask_and_iface ()
219 try_command_on_node $test_node "$CTDB ip -v -X | awk -F'|' -v ip=$test_ip '\$2 == ip { print \$4 }'"
222 if [ -z "$TEST_LOCAL_DAEMONS" ] ; then
224 try_command_on_node $test_node ip addr show to $test_ip
231 echo "$test_ip/$mask is on $iface"
236 try_command_on_node -q all "$CTDB pnn"
240 # The subtlety is that "ctdb delip" will fail if the IP address isn't
241 # configured on a node...
242 delete_ip_from_all_nodes ()
250 for _pnn in $all_pnns ; do
251 all_ips_on_node $_pnn
252 while read _i _n ; do
253 if [ "$_ip" = "$_i" ] ; then
254 _nodes="${_nodes}${_nodes:+,}${_pnn}"
256 done <<<"$out" # bashism
259 try_command_on_node -pq "$_nodes" "$CTDB delip $_ip"
262 #######################################
267 for i in $(seq 1 $1) ; do
274 _cluster_is_healthy ()
276 $CTDB nodestatus all >/dev/null
279 _cluster_is_recovered ()
281 node_has_status 0 recovered
286 _cluster_is_healthy && _cluster_is_recovered
289 cluster_is_healthy ()
291 if onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
292 echo "Cluster is HEALTHY"
293 if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_recovered ; then
294 echo "WARNING: cluster in recovery mode!"
299 echo "Cluster is UNHEALTHY"
301 echo "DEBUG AT $(date '+%F %T'):"
303 for i in "onnode -q 0 $CTDB status" \
304 "onnode -q 0 onnode all $CTDB scriptstatus" ; do
314 local timeout="${1:-120}"
316 echo "Waiting for cluster to become ready..."
318 wait_until $timeout onnode -q any $CTDB_TEST_WRAPPER _cluster_is_ready
321 # This function is becoming nicely overloaded. Soon it will collapse! :-)
327 local bits fpat mpat rpat
329 (unhealthy) bits="?|?|?|1|*" ;;
330 (healthy) bits="?|?|?|0|*" ;;
331 (disconnected) bits="1|*" ;;
332 (connected) bits="0|*" ;;
333 (banned) bits="?|1|*" ;;
334 (unbanned) bits="?|0|*" ;;
335 (disabled) bits="?|?|1|*" ;;
336 (enabled) bits="?|?|0|*" ;;
337 (stopped) bits="?|?|?|?|1|*" ;;
338 (notstopped) bits="?|?|?|?|0|*" ;;
339 (frozen) fpat='^[[:space:]]+frozen[[:space:]]+1$' ;;
340 (unfrozen) fpat='^[[:space:]]+frozen[[:space:]]+0$' ;;
341 (recovered) rpat='^Recovery mode:RECOVERY \(1\)$' ;;
342 (notlmaster) rpat="^hash:.* lmaster:${pnn}\$" ;;
344 echo "node_has_status: unknown status \"$status\""
348 if [ -n "$bits" ] ; then
351 out=$($CTDB -X status 2>&1) || return 1
356 # This needs to be done in 2 steps to avoid false matches.
357 local line_bits="${line#|${pnn}|*|}"
358 [ "$line_bits" = "$line" ] && continue
359 [ "${line_bits#${bits}}" != "$line_bits" ] && return 0
362 } <<<"$out" # Yay bash!
363 elif [ -n "$fpat" ] ; then
364 $CTDB statistics -n "$pnn" | egrep -q "$fpat"
365 elif [ -n "$rpat" ] ; then
366 ! $CTDB status -n "$pnn" | egrep -q "$rpat"
368 echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
373 wait_until_node_has_status ()
377 local timeout="${3:-30}"
378 local proxy_pnn="${4:-any}"
380 echo "Waiting until node $pnn has status \"$status\"..."
382 if ! wait_until $timeout onnode $proxy_pnn $CTDB_TEST_WRAPPER node_has_status "$pnn" "$status" ; then
383 for i in "onnode -q any $CTDB status" "onnode -q any onnode all $CTDB scriptstatus" ; do
393 # Useful for superficially testing IP failover.
394 # IPs must be on the given node.
395 # If the first argument is '!' then the IPs must not be on the given node.
399 if [ "$1" = "!" ] ; then
400 negating=true ; shift
402 local node="$1" ; shift
407 all_ips_on_node $node
410 for check in $ips ; do
412 while read ip pnn ; do
413 if [ "$check" = "$ip" ] ; then
414 if [ "$pnn" = "$node" ] ; then
415 if $negating ; then return 1 ; fi
417 if ! $negating ; then return 1 ; fi
419 ips="${ips/${ip}}" # Remove from list
422 # If we're negating and we didn't see the address then it
423 # isn't hosted by anyone!
425 ips="${ips/${check}}"
427 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
430 ips="${ips// }" # Remove any spaces.
434 wait_until_ips_are_on_node ()
436 # Go to some trouble to print a use description of what is happening
438 if [ "$1" == "!" ] ; then
445 [ "$i" != "!" ] || continue
446 if [ -z "$node" ] ; then
450 ips="${ips}${ips:+, }${i}"
452 echo "Waiting for ${ips} to ${not}be assigned to node ${node}"
454 wait_until 60 ips_are_on_node "$@"
463 all_ips_on_node $node
465 while read ip pnn ; do
466 if [ "$node" = "$pnn" ] ; then
469 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
474 wait_until_node_has_some_ips ()
476 echo "Waiting for some IPs to be assigned to node ${test_node}"
478 wait_until 60 node_has_some_ips "$@"
481 wait_until_node_has_no_ips ()
483 echo "Waiting until no IPs are assigned to node ${test_node}"
485 wait_until 60 ! node_has_some_ips "$@"
488 #######################################
494 if [ -e /etc/redhat-release ] ; then
497 /etc/init.d/ctdb "$cmd"
501 # Stop/start CTDB on all nodes. Override for local daemons.
504 onnode -p all $CTDB_TEST_WRAPPER _service_ctdb stop
508 onnode -p all $CTDB_TEST_WRAPPER _service_ctdb start
513 ctdb_enable_cluster_test_event_scripts
518 onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb start
523 onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb stop
528 onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb restart
534 for i in $(seq 1 5) ; do
536 echo "Start failed. Trying again in a few seconds..."
541 wait_until_ready || {
542 echo "Cluster didn't become ready. Restarting..."
546 echo "Setting RerecoveryTimeout to 1"
547 onnode -pq all "$CTDB setvar RerecoveryTimeout 1"
549 # In recent versions of CTDB, forcing a recovery like this
550 # blocks until the recovery is complete. Hopefully this will
551 # help the cluster to stabilise before a subsequent test.
552 echo "Forcing a recovery..."
553 onnode -q 0 $CTDB recover
556 if ! onnode -q any $CTDB_TEST_WRAPPER _cluster_is_recovered ; then
557 echo "Cluster has gone into recovery again, waiting..."
558 wait_until 30/2 onnode -q any $CTDB_TEST_WRAPPER _cluster_is_recovered
562 # Cluster is still healthy. Good, we're done!
563 if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
564 echo "Cluster became UNHEALTHY again [$(date)]"
565 onnode -p all ctdb status -X 2>&1
566 onnode -p all ctdb scriptstatus 2>&1
571 echo "Doing a sync..."
572 onnode -q 0 $CTDB sync
578 echo "Cluster UNHEALTHY... too many attempts..."
579 onnode -p all ctdb status -X 2>&1
580 onnode -p all ctdb scriptstatus 2>&1
582 # Try to make the calling test fail
587 ctdb_restart_when_done ()
589 ctdb_test_restart_scheduled=true
594 echo "${CTDB_BASE:-${CTDB_SCRIPTS_BASE}}"
597 #######################################
599 wait_for_monitor_event ()
604 echo "Waiting for a monitor event on node ${pnn}..."
606 try_command_on_node "$pnn" $CTDB scriptstatus || {
607 echo "Unable to get scriptstatus from node $pnn"
611 local ctdb_scriptstatus_original="$out"
612 wait_until 120 _ctdb_scriptstatus_changed
615 _ctdb_scriptstatus_changed ()
617 try_command_on_node "$pnn" $CTDB scriptstatus || {
618 echo "Unable to get scriptstatus from node $pnn"
622 [ "$out" != "$ctdb_scriptstatus_original" ]
625 #######################################
629 select_test_node_and_ips
631 nfs_first_export=$(showmount -e $test_ip | sed -n -e '2s/ .*//p')
633 echo "Creating test subdirectory..."
634 try_command_on_node $test_node "TMPDIR=$nfs_first_export mktemp -d"
636 try_command_on_node $test_node "chmod 777 $nfs_test_dir"
638 nfs_mnt_d=$(mktemp -d)
639 nfs_local_file="${nfs_mnt_d}/${nfs_test_dir##*/}/TEST_FILE"
640 nfs_remote_file="${nfs_test_dir}/TEST_FILE"
642 ctdb_test_exit_hook_add nfs_test_cleanup
644 echo "Mounting ${test_ip}:${nfs_first_export} on ${nfs_mnt_d} ..."
645 mount -o timeo=1,hard,intr,vers=3 \
646 "[${test_ip}]:${nfs_first_export}" ${nfs_mnt_d}
651 rm -f "$nfs_local_file"
652 umount -f "$nfs_mnt_d"
654 onnode -q $test_node rmdir "$nfs_test_dir"
657 #######################################
659 # If the given IP is hosted then print 2 items: maskbits and iface
665 *:*) _family="inet6" ; _bits=128 ;;
666 *) _family="inet" ; _bits=32 ;;
669 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
670 awk -v family="${_family}" \
671 'NR == 1 { iface = $2; sub(":$", "", iface) } \
672 $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
673 print mask, iface, family }'
678 _addr="${1%/*}" # Remove optional maskbits
680 set -- $(ip_maskbits_iface $_addr)
681 if [ -n "$1" ] ; then
684 echo "Removing public address $_addr/$_maskbits from device $_iface"
685 ip addr del "$_ip/$_maskbits" dev "$_iface" >/dev/null 2>&1 || true
696 #######################################
698 # $1: pnn, $2: DB name
701 try_command_on_node -v $1 $CTDB getdbstatus "$2" |
702 sed -n -e "s@^path: @@p"
705 # $1: pnn, $2: DB name
706 db_ctdb_cattdb_count_records ()
708 # Count the number of keys, excluding any that begin with '_'.
709 # This excludes at least the sequence number record in
710 # persistent/replicated databases. The trailing "|| :" forces
711 # the command to succeed when no records are matched.
712 try_command_on_node $1 \
713 "$CTDB cattdb $2 | grep -c '^key([0-9][0-9]*) = \"[^_]' || :"
717 # $1: pnn, $2: DB name, $3: key string, $4: value string, $5: RSN (default 7)
720 _tdb=$(db_get_path $1 "$2")
722 try_command_on_node $1 $CTDB tstore "$_tdb" "$3" "$4" "$_rsn"
725 # $1: pnn, $2: DB name, $3: dbseqnum (must be < 255!!!!!)
726 db_ctdb_tstore_dbseqnum ()
728 # "__db_sequence_number__" + trailing 0x00
729 _key='0x5f5f64625f73657175656e63655f6e756d6265725f5f00'
731 # Construct 8 byte (unit64_t) database sequence number. This
732 # probably breaks if $3 > 255
733 _value=$(printf "0x%02x%014x" $3 0)
735 db_ctdb_tstore $1 "$2" "$_key" "$_value"
738 #######################################
740 # Enables all of the event scripts used in cluster tests, except for
741 # the mandatory scripts
742 ctdb_enable_cluster_test_event_scripts ()
753 for s in $scripts ; do
754 try_command_on_node all ctdb event script enable legacy "$s"
758 ########################################
760 # Make sure that $CTDB is set.
763 local="${TEST_SUBDIR}/scripts/local.bash"
764 if [ -r "$local" ] ; then