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 trap "ctdb_test_exit" 0
62 ctdb_stop_all >/dev/null 2>&1 || true
64 echo "Configuring cluster..."
65 setup_ctdb "$@" || exit 1
67 echo "Starting cluster..."
70 echo "*** SETUP COMPLETE AT $(date '+%F %T'), RUNNING TEST..."
73 ########################################
76 try_command_on_node ()
78 local nodespec="$1" ; shift
83 while [ "${nodespec#-}" != "$nodespec" ] ; do
84 if [ "$nodespec" = "-v" ] ; then
87 onnode_opts="${onnode_opts}${onnode_opts:+ }${nodespec}"
94 out=$(onnode -q $onnode_opts "$nodespec" "$cmd" 2>&1) || {
96 echo "Failed to execute \"$cmd\" on node(s) \"$nodespec\""
102 echo "Output of \"$cmd\":"
107 sanity_check_output ()
110 local regexp="$2" # Should be anchored as necessary.
114 local num_lines=$(echo "$out" | wc -l)
115 echo "There are $num_lines lines of output"
116 if [ $num_lines -lt $min_lines ] ; then
117 echo "BAD: that's less than the required number (${min_lines})"
122 local unexpected # local doesn't pass through status of command on RHS.
123 unexpected=$(echo "$out" | egrep -v "$regexp") || status=$?
125 # Note that this is reversed.
126 if [ $status -eq 0 ] ; then
127 echo "BAD: unexpected lines in output:"
128 echo "$unexpected" | cat -A
131 echo "Output lines look OK"
139 local ips="$1" # list of "ip node" lines
141 echo "Sanity checking IPs..."
145 while read x ipp ; do
146 [ "$ipp" = "-1" ] && break
147 if [ -n "$prev" -a "$ipp" != "$prev" ] ; then
154 echo "BAD: a node was -1 or IPs are only assigned to one node:"
156 echo "Are you running an old version of CTDB?"
160 # This returns a list of "ip node" lines in $out
164 try_command_on_node $node \
165 "$CTDB ip -X | awk -F'|' 'NR > 1 { print \$2, \$3 }'"
168 _select_test_node_and_ips ()
170 try_command_on_node any \
171 "$CTDB ip -X all | awk -F'|' 'NR > 1 { print \$2, \$3 }'"
173 test_node="" # this matches no PNN
176 while read ip pnn ; do
177 if [ -z "$test_node" -a "$pnn" != "-1" ] ; then
180 if [ "$pnn" = "$test_node" ] ; then
181 test_node_ips="${test_node_ips}${test_node_ips:+ }${ip}"
183 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
185 echo "Selected node ${test_node} with IPs: ${test_node_ips}."
186 test_ip="${test_node_ips%% *}"
189 *:*) test_prefix="${test_ip}/128" ;;
190 *) test_prefix="${test_ip}/32" ;;
193 [ -n "$test_node" ] || return 1
196 select_test_node_and_ips ()
199 while ! _select_test_node_and_ips ; do
200 echo "Unable to find a test node with IPs assigned"
201 if [ $timeout -le 0 ] ; then
202 echo "BAD: Too many attempts"
206 timeout=$(($timeout - 1))
213 get_test_ip_mask_and_iface ()
216 try_command_on_node $test_node "$CTDB ip -v -X | awk -F'|' -v ip=$test_ip '\$2 == ip { print \$4 }'"
219 if [ -z "$TEST_LOCAL_DAEMONS" ] ; then
221 try_command_on_node $test_node ip addr show to $test_ip
228 echo "$test_ip/$mask is on $iface"
233 try_command_on_node -q all "$CTDB pnn"
237 # The subtlety is that "ctdb delip" will fail if the IP address isn't
238 # configured on a node...
239 delete_ip_from_all_nodes ()
247 for _pnn in $all_pnns ; do
248 all_ips_on_node $_pnn
249 while read _i _n ; do
250 if [ "$_ip" = "$_i" ] ; then
251 _nodes="${_nodes}${_nodes:+,}${_pnn}"
253 done <<<"$out" # bashism
256 try_command_on_node -pq "$_nodes" "$CTDB delip $_ip"
259 #######################################
264 for i in $(seq 1 $1) ; do
271 _cluster_is_healthy ()
273 $CTDB nodestatus all >/dev/null
276 _cluster_is_recovered ()
278 node_has_status 0 recovered
283 _cluster_is_healthy && _cluster_is_recovered
286 cluster_is_healthy ()
288 if onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
289 echo "Cluster is HEALTHY"
290 if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_recovered ; then
291 echo "WARNING: cluster in recovery mode!"
296 echo "Cluster is UNHEALTHY"
298 echo "DEBUG AT $(date '+%F %T'):"
300 for i in "onnode -q 0 $CTDB status" \
301 "onnode -q 0 onnode all $CTDB scriptstatus" ; do
311 local timeout="${1:-120}"
313 echo "Waiting for cluster to become ready..."
315 wait_until $timeout onnode -q any $CTDB_TEST_WRAPPER _cluster_is_ready
318 # This function is becoming nicely overloaded. Soon it will collapse! :-)
324 local bits fpat mpat rpat
326 (unhealthy) bits="?|?|?|1|*" ;;
327 (healthy) bits="?|?|?|0|*" ;;
328 (disconnected) bits="1|*" ;;
329 (connected) bits="0|*" ;;
330 (banned) bits="?|1|*" ;;
331 (unbanned) bits="?|0|*" ;;
332 (disabled) bits="?|?|1|*" ;;
333 (enabled) bits="?|?|0|*" ;;
334 (stopped) bits="?|?|?|?|1|*" ;;
335 (notstopped) bits="?|?|?|?|0|*" ;;
336 (frozen) fpat='^[[:space:]]+frozen[[:space:]]+1$' ;;
337 (unfrozen) fpat='^[[:space:]]+frozen[[:space:]]+0$' ;;
338 (recovered) rpat='^Recovery mode:RECOVERY \(1\)$' ;;
339 (notlmaster) rpat="^hash:.* lmaster:${pnn}\$" ;;
341 echo "node_has_status: unknown status \"$status\""
345 if [ -n "$bits" ] ; then
348 out=$($CTDB -X status 2>&1) || return 1
353 # This needs to be done in 2 steps to avoid false matches.
354 local line_bits="${line#|${pnn}|*|}"
355 [ "$line_bits" = "$line" ] && continue
356 [ "${line_bits#${bits}}" != "$line_bits" ] && return 0
359 } <<<"$out" # Yay bash!
360 elif [ -n "$fpat" ] ; then
361 $CTDB statistics -n "$pnn" | egrep -q "$fpat"
362 elif [ -n "$rpat" ] ; then
363 ! $CTDB status -n "$pnn" | egrep -q "$rpat"
365 echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
370 wait_until_node_has_status ()
374 local timeout="${3:-30}"
375 local proxy_pnn="${4:-any}"
377 echo "Waiting until node $pnn has status \"$status\"..."
379 if ! wait_until $timeout onnode $proxy_pnn $CTDB_TEST_WRAPPER node_has_status "$pnn" "$status" ; then
380 for i in "onnode -q any $CTDB status" "onnode -q any onnode all $CTDB scriptstatus" ; do
390 # Useful for superficially testing IP failover.
391 # IPs must be on the given node.
392 # If the first argument is '!' then the IPs must not be on the given node.
396 if [ "$1" = "!" ] ; then
397 negating=true ; shift
399 local node="$1" ; shift
404 all_ips_on_node $node
407 for check in $ips ; do
409 while read ip pnn ; do
410 if [ "$check" = "$ip" ] ; then
411 if [ "$pnn" = "$node" ] ; then
412 if $negating ; then return 1 ; fi
414 if ! $negating ; then return 1 ; fi
416 ips="${ips/${ip}}" # Remove from list
419 # If we're negating and we didn't see the address then it
420 # isn't hosted by anyone!
422 ips="${ips/${check}}"
424 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
427 ips="${ips// }" # Remove any spaces.
431 wait_until_ips_are_on_node ()
433 # Go to some trouble to print a use description of what is happening
435 if [ "$1" == "!" ] ; then
442 [ "$i" != "!" ] || continue
443 if [ -z "$node" ] ; then
447 ips="${ips}${ips:+, }${i}"
449 echo "Waiting for ${ips} to ${not}be assigned to node ${node}"
451 wait_until 60 ips_are_on_node "$@"
460 all_ips_on_node $node
462 while read ip pnn ; do
463 if [ "$node" = "$pnn" ] ; then
466 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
471 wait_until_node_has_some_ips ()
473 echo "Waiting for some IPs to be assigned to node ${test_node}"
475 wait_until 60 node_has_some_ips "$@"
478 wait_until_node_has_no_ips ()
480 echo "Waiting until no IPs are assigned to node ${test_node}"
482 wait_until 60 ! node_has_some_ips "$@"
485 #######################################
491 if [ -e /etc/redhat-release ] ; then
494 /etc/init.d/ctdb "$cmd"
498 # Stop/start CTDB on all nodes. Override for local daemons.
501 onnode -p all $CTDB_TEST_WRAPPER _service_ctdb stop
505 onnode -p all $CTDB_TEST_WRAPPER _service_ctdb start
510 ctdb_enable_cluster_test_event_scripts
515 onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb start
520 onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb stop
525 onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb restart
531 for i in $(seq 1 5) ; do
533 echo "Start failed. Trying again in a few seconds..."
538 wait_until_ready || {
539 echo "Cluster didn't become ready. Restarting..."
543 echo "Setting RerecoveryTimeout to 1"
544 onnode -pq all "$CTDB setvar RerecoveryTimeout 1"
546 # In recent versions of CTDB, forcing a recovery like this
547 # blocks until the recovery is complete. Hopefully this will
548 # help the cluster to stabilise before a subsequent test.
549 echo "Forcing a recovery..."
550 onnode -q 0 $CTDB recover
553 if ! onnode -q any $CTDB_TEST_WRAPPER _cluster_is_recovered ; then
554 echo "Cluster has gone into recovery again, waiting..."
555 wait_until 30/2 onnode -q any $CTDB_TEST_WRAPPER _cluster_is_recovered
559 # Cluster is still healthy. Good, we're done!
560 if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
561 echo "Cluster became UNHEALTHY again [$(date)]"
562 onnode -p all ctdb status -X 2>&1
563 onnode -p all ctdb scriptstatus 2>&1
568 echo "Doing a sync..."
569 onnode -q 0 $CTDB sync
575 echo "Cluster UNHEALTHY... too many attempts..."
576 onnode -p all ctdb status -X 2>&1
577 onnode -p all ctdb scriptstatus 2>&1
579 # Try to make the calling test fail
586 echo "${CTDB_BASE:-${CTDB_SCRIPTS_BASE}}"
589 #######################################
591 wait_for_monitor_event ()
596 echo "Waiting for a monitor event on node ${pnn}..."
598 try_command_on_node "$pnn" $CTDB scriptstatus || {
599 echo "Unable to get scriptstatus from node $pnn"
603 local ctdb_scriptstatus_original="$out"
604 wait_until 120 _ctdb_scriptstatus_changed
607 _ctdb_scriptstatus_changed ()
609 try_command_on_node "$pnn" $CTDB scriptstatus || {
610 echo "Unable to get scriptstatus from node $pnn"
614 [ "$out" != "$ctdb_scriptstatus_original" ]
617 #######################################
621 select_test_node_and_ips
623 nfs_first_export=$(showmount -e $test_ip | sed -n -e '2s/ .*//p')
625 echo "Creating test subdirectory..."
626 try_command_on_node $test_node "TMPDIR=$nfs_first_export mktemp -d"
628 try_command_on_node $test_node "chmod 777 $nfs_test_dir"
630 nfs_mnt_d=$(mktemp -d)
631 nfs_local_file="${nfs_mnt_d}/${nfs_test_dir##*/}/TEST_FILE"
632 nfs_remote_file="${nfs_test_dir}/TEST_FILE"
634 ctdb_test_exit_hook_add nfs_test_cleanup
636 echo "Mounting ${test_ip}:${nfs_first_export} on ${nfs_mnt_d} ..."
637 mount -o timeo=1,hard,intr,vers=3 \
638 "[${test_ip}]:${nfs_first_export}" ${nfs_mnt_d}
643 rm -f "$nfs_local_file"
644 umount -f "$nfs_mnt_d"
646 onnode -q $test_node rmdir "$nfs_test_dir"
649 #######################################
651 # If the given IP is hosted then print 2 items: maskbits and iface
657 *:*) _family="inet6" ; _bits=128 ;;
658 *) _family="inet" ; _bits=32 ;;
661 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
662 awk -v family="${_family}" \
663 'NR == 1 { iface = $2; sub(":$", "", iface) } \
664 $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
665 print mask, iface, family }'
670 _addr="${1%/*}" # Remove optional maskbits
672 set -- $(ip_maskbits_iface $_addr)
673 if [ -n "$1" ] ; then
676 echo "Removing public address $_addr/$_maskbits from device $_iface"
677 ip addr del "$_ip/$_maskbits" dev "$_iface" >/dev/null 2>&1 || true
688 #######################################
690 # $1: pnn, $2: DB name
693 try_command_on_node -v $1 $CTDB getdbstatus "$2" |
694 sed -n -e "s@^path: @@p"
697 # $1: pnn, $2: DB name
698 db_ctdb_cattdb_count_records ()
700 # Count the number of keys, excluding any that begin with '_'.
701 # This excludes at least the sequence number record in
702 # persistent/replicated databases. The trailing "|| :" forces
703 # the command to succeed when no records are matched.
704 try_command_on_node $1 \
705 "$CTDB cattdb $2 | grep -c '^key([0-9][0-9]*) = \"[^_]' || :"
709 # $1: pnn, $2: DB name, $3: key string, $4: value string, $5: RSN (default 7)
712 _tdb=$(db_get_path $1 "$2")
714 try_command_on_node $1 $CTDB tstore "$_tdb" "$3" "$4" "$_rsn"
717 # $1: pnn, $2: DB name, $3: dbseqnum (must be < 255!!!!!)
718 db_ctdb_tstore_dbseqnum ()
720 # "__db_sequence_number__" + trailing 0x00
721 _key='0x5f5f64625f73657175656e63655f6e756d6265725f5f00'
723 # Construct 8 byte (unit64_t) database sequence number. This
724 # probably breaks if $3 > 255
725 _value=$(printf "0x%02x%014x" $3 0)
727 db_ctdb_tstore $1 "$2" "$_key" "$_value"
730 #######################################
732 # Enables all of the event scripts used in cluster tests, except for
733 # the mandatory scripts
734 ctdb_enable_cluster_test_event_scripts ()
745 for s in $scripts ; do
746 try_command_on_node all ctdb event script enable legacy "$s"
750 ########################################
752 # Make sure that $CTDB is set.
755 local="${TEST_SUBDIR}/scripts/local.bash"
756 if [ -r "$local" ] ; then