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 ########################################
75 # Sets: $out, $outfile
76 # * The first 1KB of output is put into $out
77 # * Tests should use $outfile for handling large output
78 # * $outfile is removed after each test
80 outfile="${TEST_VAR_DIR}/try_command_on_node.out"
87 ctdb_test_exit_hook_add outfile_cleanup
89 try_command_on_node ()
91 local nodespec="$1" ; shift
96 while [ "${nodespec#-}" != "$nodespec" ] ; do
97 if [ "$nodespec" = "-v" ] ; then
100 onnode_opts="${onnode_opts}${onnode_opts:+ }${nodespec}"
102 nodespec="$1" ; shift
107 if ! onnode -q $onnode_opts "$nodespec" "$cmd" >"$outfile" 2>&1 ; then
108 echo "Failed to execute \"$cmd\" on node(s) \"$nodespec\""
114 echo "Output of \"$cmd\":"
118 out=$(dd if="$outfile" bs=1k count=1 2>/dev/null)
121 sanity_check_output ()
124 local regexp="$2" # Should be anchored as necessary.
128 local num_lines=$(wc -l <"$outfile")
129 echo "There are $num_lines lines of output"
130 if [ $num_lines -lt $min_lines ] ; then
131 echo "BAD: that's less than the required number (${min_lines})"
136 local unexpected # local doesn't pass through status of command on RHS.
137 unexpected=$(grep -Ev "$regexp" "$outfile") || status=$?
139 # Note that this is reversed.
140 if [ $status -eq 0 ] ; then
141 echo "BAD: unexpected lines in output:"
142 echo "$unexpected" | cat -A
145 echo "Output lines look OK"
151 # This returns a list of "ip node" lines in $outfile
155 try_command_on_node $node \
156 "$CTDB ip -X | awk -F'|' 'NR > 1 { print \$2, \$3 }'"
159 _select_test_node_and_ips ()
161 try_command_on_node any \
162 "$CTDB ip -X all | awk -F'|' 'NR > 1 { print \$2, \$3 }'"
164 test_node="" # this matches no PNN
167 while read ip pnn ; do
168 if [ -z "$test_node" -a "$pnn" != "-1" ] ; then
171 if [ "$pnn" = "$test_node" ] ; then
172 test_node_ips="${test_node_ips}${test_node_ips:+ }${ip}"
176 echo "Selected node ${test_node} with IPs: ${test_node_ips}."
177 test_ip="${test_node_ips%% *}"
180 *:*) test_prefix="${test_ip}/128" ;;
181 *) test_prefix="${test_ip}/32" ;;
184 [ -n "$test_node" ] || return 1
187 select_test_node_and_ips ()
190 while ! _select_test_node_and_ips ; do
191 echo "Unable to find a test node with IPs assigned"
192 if [ $timeout -le 0 ] ; then
193 echo "BAD: Too many attempts"
197 timeout=$(($timeout - 1))
204 get_test_ip_mask_and_iface ()
207 try_command_on_node $test_node "$CTDB ip -v -X | awk -F'|' -v ip=$test_ip '\$2 == ip { print \$4 }'"
210 if [ -z "$TEST_LOCAL_DAEMONS" ] ; then
212 try_command_on_node $test_node ip addr show to $test_ip
219 echo "$test_ip/$mask is on $iface"
224 try_command_on_node -q all "$CTDB pnn"
228 # The subtlety is that "ctdb delip" will fail if the IP address isn't
229 # configured on a node...
230 delete_ip_from_all_nodes ()
238 for _pnn in $all_pnns ; do
239 all_ips_on_node $_pnn
240 while read _i _n ; do
241 if [ "$_ip" = "$_i" ] ; then
242 _nodes="${_nodes}${_nodes:+,}${_pnn}"
247 try_command_on_node -pq "$_nodes" "$CTDB delip $_ip"
250 #######################################
255 for i in $(seq 1 $1) ; do
262 _cluster_is_healthy ()
264 $CTDB nodestatus all >/dev/null
267 _cluster_is_recovered ()
269 node_has_status 0 recovered
274 _cluster_is_healthy && _cluster_is_recovered
277 cluster_is_healthy ()
279 if onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
280 echo "Cluster is HEALTHY"
281 if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_recovered ; then
282 echo "WARNING: cluster in recovery mode!"
287 echo "Cluster is UNHEALTHY"
289 echo "DEBUG AT $(date '+%F %T'):"
291 for i in "onnode -q 0 $CTDB status" \
292 "onnode -q 0 onnode all $CTDB scriptstatus" ; do
302 local timeout="${1:-120}"
304 echo "Waiting for cluster to become ready..."
306 wait_until $timeout onnode -q any $CTDB_TEST_WRAPPER _cluster_is_ready
309 # This function is becoming nicely overloaded. Soon it will collapse! :-)
315 local bits fpat mpat rpat
317 (unhealthy) bits="?|?|?|1|*" ;;
318 (healthy) bits="?|?|?|0|*" ;;
319 (disconnected) bits="1|*" ;;
320 (connected) bits="0|*" ;;
321 (banned) bits="?|1|*" ;;
322 (unbanned) bits="?|0|*" ;;
323 (disabled) bits="?|?|1|*" ;;
324 (enabled) bits="?|?|0|*" ;;
325 (stopped) bits="?|?|?|?|1|*" ;;
326 (notstopped) bits="?|?|?|?|0|*" ;;
327 (frozen) fpat='^[[:space:]]+frozen[[:space:]]+1$' ;;
328 (unfrozen) fpat='^[[:space:]]+frozen[[:space:]]+0$' ;;
329 (recovered) rpat='^Recovery mode:RECOVERY \(1\)$' ;;
330 (notlmaster) rpat="^hash:.* lmaster:${pnn}\$" ;;
332 echo "node_has_status: unknown status \"$status\""
336 if [ -n "$bits" ] ; then
339 out=$($CTDB -X status 2>&1) || return 1
344 # This needs to be done in 2 steps to avoid false matches.
345 local line_bits="${line#|${pnn}|*|}"
346 [ "$line_bits" = "$line" ] && continue
347 [ "${line_bits#${bits}}" != "$line_bits" ] && return 0
350 } <<<"$out" # Yay bash!
351 elif [ -n "$fpat" ] ; then
352 $CTDB statistics -n "$pnn" | egrep -q "$fpat"
353 elif [ -n "$rpat" ] ; then
354 ! $CTDB status -n "$pnn" | egrep -q "$rpat"
356 echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
361 wait_until_node_has_status ()
365 local timeout="${3:-30}"
366 local proxy_pnn="${4:-any}"
368 echo "Waiting until node $pnn has status \"$status\"..."
370 if ! wait_until $timeout onnode $proxy_pnn $CTDB_TEST_WRAPPER node_has_status "$pnn" "$status" ; then
371 for i in "onnode -q any $CTDB status" "onnode -q any onnode all $CTDB scriptstatus" ; do
381 # Useful for superficially testing IP failover.
382 # IPs must be on the given node.
383 # If the first argument is '!' then the IPs must not be on the given node.
387 if [ "$1" = "!" ] ; then
388 negating=true ; shift
390 local node="$1" ; shift
395 all_ips_on_node $node
398 for check in $ips ; do
400 while read ip pnn ; do
401 if [ "$check" = "$ip" ] ; then
402 if [ "$pnn" = "$node" ] ; then
403 if $negating ; then return 1 ; fi
405 if ! $negating ; then return 1 ; fi
407 ips="${ips/${ip}}" # Remove from list
410 # If we're negating and we didn't see the address then it
411 # isn't hosted by anyone!
413 ips="${ips/${check}}"
418 ips="${ips// }" # Remove any spaces.
422 wait_until_ips_are_on_node ()
424 # Go to some trouble to print a use description of what is happening
426 if [ "$1" == "!" ] ; then
433 [ "$i" != "!" ] || continue
434 if [ -z "$node" ] ; then
438 ips="${ips}${ips:+, }${i}"
440 echo "Waiting for ${ips} to ${not}be assigned to node ${node}"
442 wait_until 60 ips_are_on_node "$@"
451 all_ips_on_node $node
453 while read ip pnn ; do
454 if [ "$node" = "$pnn" ] ; then
462 wait_until_node_has_some_ips ()
464 echo "Waiting for some IPs to be assigned to node ${test_node}"
466 wait_until 60 node_has_some_ips "$@"
469 wait_until_node_has_no_ips ()
471 echo "Waiting until no IPs are assigned to node ${test_node}"
473 wait_until 60 ! node_has_some_ips "$@"
476 #######################################
482 if [ -e /etc/redhat-release ] ; then
485 /etc/init.d/ctdb "$cmd"
489 # Stop/start CTDB on all nodes. Override for local daemons.
492 onnode -p all $CTDB_TEST_WRAPPER _service_ctdb stop
496 onnode -p all $CTDB_TEST_WRAPPER _service_ctdb start
501 ctdb_enable_cluster_test_event_scripts
506 onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb start
511 onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb stop
516 onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb restart
522 for i in $(seq 1 5) ; do
524 echo "Start failed. Trying again in a few seconds..."
529 wait_until_ready || {
530 echo "Cluster didn't become ready. Restarting..."
534 echo "Setting RerecoveryTimeout to 1"
535 onnode -pq all "$CTDB setvar RerecoveryTimeout 1"
537 # In recent versions of CTDB, forcing a recovery like this
538 # blocks until the recovery is complete. Hopefully this will
539 # help the cluster to stabilise before a subsequent test.
540 echo "Forcing a recovery..."
541 onnode -q 0 $CTDB recover
544 if ! onnode -q any $CTDB_TEST_WRAPPER _cluster_is_recovered ; then
545 echo "Cluster has gone into recovery again, waiting..."
546 wait_until 30/2 onnode -q any $CTDB_TEST_WRAPPER _cluster_is_recovered
550 # Cluster is still healthy. Good, we're done!
551 if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
552 echo "Cluster became UNHEALTHY again [$(date)]"
553 onnode -p all ctdb status -X 2>&1
554 onnode -p all ctdb scriptstatus 2>&1
559 echo "Doing a sync..."
560 onnode -q 0 $CTDB sync
566 echo "Cluster UNHEALTHY... too many attempts..."
567 onnode -p all ctdb status -X 2>&1
568 onnode -p all ctdb scriptstatus 2>&1
570 # Try to make the calling test fail
577 echo "${CTDB_BASE:-${CTDB_SCRIPTS_BASE}}"
580 #######################################
582 wait_for_monitor_event ()
587 echo "Waiting for a monitor event on node ${pnn}..."
589 try_command_on_node "$pnn" $CTDB scriptstatus || {
590 echo "Unable to get scriptstatus from node $pnn"
594 mv "$outfile" "${outfile}.orig"
596 wait_until 120 _ctdb_scriptstatus_changed
599 _ctdb_scriptstatus_changed ()
601 try_command_on_node "$pnn" $CTDB scriptstatus || {
602 echo "Unable to get scriptstatus from node $pnn"
606 ! diff "$outfile" "${outfile}.orig" >/dev/null
609 #######################################
613 select_test_node_and_ips
615 nfs_first_export=$(showmount -e $test_ip | sed -n -e '2s/ .*//p')
617 echo "Creating test subdirectory..."
618 try_command_on_node $test_node "TMPDIR=$nfs_first_export mktemp -d"
620 try_command_on_node $test_node "chmod 777 $nfs_test_dir"
622 nfs_mnt_d=$(mktemp -d)
623 nfs_local_file="${nfs_mnt_d}/${nfs_test_dir##*/}/TEST_FILE"
624 nfs_remote_file="${nfs_test_dir}/TEST_FILE"
626 ctdb_test_exit_hook_add nfs_test_cleanup
628 echo "Mounting ${test_ip}:${nfs_first_export} on ${nfs_mnt_d} ..."
629 mount -o timeo=1,hard,intr,vers=3 \
630 "[${test_ip}]:${nfs_first_export}" ${nfs_mnt_d}
635 rm -f "$nfs_local_file"
636 umount -f "$nfs_mnt_d"
638 onnode -q $test_node rmdir "$nfs_test_dir"
641 #######################################
643 # If the given IP is hosted then print 2 items: maskbits and iface
649 *:*) _family="inet6" ; _bits=128 ;;
650 *) _family="inet" ; _bits=32 ;;
653 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
654 awk -v family="${_family}" \
655 'NR == 1 { iface = $2; sub(":$", "", iface) } \
656 $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
657 print mask, iface, family }'
662 _addr="${1%/*}" # Remove optional maskbits
664 set -- $(ip_maskbits_iface $_addr)
665 if [ -n "$1" ] ; then
668 echo "Removing public address $_addr/$_maskbits from device $_iface"
669 ip addr del "$_ip/$_maskbits" dev "$_iface" >/dev/null 2>&1 || true
680 #######################################
682 # $1: pnn, $2: DB name
685 try_command_on_node -v $1 $CTDB getdbstatus "$2" |
686 sed -n -e "s@^path: @@p"
689 # $1: pnn, $2: DB name
690 db_ctdb_cattdb_count_records ()
692 # Count the number of keys, excluding any that begin with '_'.
693 # This excludes at least the sequence number record in
694 # persistent/replicated databases. The trailing "|| :" forces
695 # the command to succeed when no records are matched.
696 try_command_on_node $1 \
697 "$CTDB cattdb $2 | grep -c '^key([0-9][0-9]*) = \"[^_]' || :"
701 # $1: pnn, $2: DB name, $3: key string, $4: value string, $5: RSN (default 7)
704 _tdb=$(db_get_path $1 "$2")
706 try_command_on_node $1 $CTDB tstore "$_tdb" "$3" "$4" "$_rsn"
709 # $1: pnn, $2: DB name, $3: dbseqnum (must be < 255!!!!!)
710 db_ctdb_tstore_dbseqnum ()
712 # "__db_sequence_number__" + trailing 0x00
713 _key='0x5f5f64625f73657175656e63655f6e756d6265725f5f00'
715 # Construct 8 byte (unit64_t) database sequence number. This
716 # probably breaks if $3 > 255
717 _value=$(printf "0x%02x%014x" $3 0)
719 db_ctdb_tstore $1 "$2" "$_key" "$_value"
722 #######################################
724 # Enables all of the event scripts used in cluster tests, except for
725 # the mandatory scripts
726 ctdb_enable_cluster_test_event_scripts ()
737 for s in $scripts ; do
738 try_command_on_node all ctdb event script enable legacy "$s"
742 ########################################
744 # Make sure that $CTDB is set.
747 local="${TEST_SUBDIR}/scripts/local.bash"
748 if [ -r "$local" ] ; then