1 # Hey Emacs, this is a -*- shell-script -*- !!! :-)
3 . "${TEST_SCRIPTS_DIR}/common.sh"
5 # If we're not running on a real cluster then we need a local copy of
6 # ctdb (and other stuff) in $PATH and we will use local daemons.
7 if [ -n "$TEST_LOCAL_DAEMONS" ] ; then
8 export CTDB_NODES_SOCKETS=""
9 for i in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
10 CTDB_NODES_SOCKETS="${CTDB_NODES_SOCKETS}${CTDB_NODES_SOCKETS:+ }${TEST_VAR_DIR}/sock.${i}"
13 # Use in-tree binaries if running against local daemons.
14 # Otherwise CTDB need to be installed on all nodes.
15 if [ -n "$ctdb_dir" -a -d "${ctdb_dir}/bin" ] ; then
16 PATH="${ctdb_dir}/bin:${PATH}"
19 export CTDB_NODES="${TEST_VAR_DIR}/nodes.txt"
22 ######################################################################
24 export CTDB_TIMEOUT=60
26 if [ -n "$CTDB_TEST_REMOTE_DIR" ] ; then
27 CTDB_TEST_WRAPPER="${CTDB_TEST_REMOTE_DIR}/test_wrap"
29 _d=$(cd ${TEST_SCRIPTS_DIR}; echo $PWD)
30 CTDB_TEST_WRAPPER="$_d/test_wrap"
32 export CTDB_TEST_WRAPPER
34 # If $VALGRIND is set then use it whenever ctdb is called, but only if
35 # $CTDB is not already set.
36 [ -n "$CTDB" ] || export CTDB="${VALGRIND}${VALGRIND:+ }ctdb"
39 PATH="${TEST_SCRIPTS_DIR}:${PATH}"
41 ######################################################################
43 ctdb_check_time_logs ()
52 out=$(onnode all tail -n 20 "${TEST_VAR_DIR}/ctdb.test.time.log" 2>&1)
54 if [ $? -eq 0 ] ; then
59 node="${line#>> NODE: }"
66 if [ -n "$ds_prev" ] && \
67 [ $(($ds_curr - $ds_prev)) -ge $threshold ] ; then
68 echo "Node $node had time jump of $(($ds_curr - $ds_prev))ds between $(date +'%T' -d @${ds_prev%?}) and $(date +'%T' -d @${ds_curr%?})"
77 echo Error getting time logs
80 echo "Check time sync (test client first):"
83 echo "Information from test client:"
86 echo "Information from cluster nodes:"
87 onnode all "top -b -n 1 ; echo '/proc/slabinfo' ; cat /proc/slabinfo"
97 [ $(($testfailures+0)) -eq 0 -a $status -ne 0 ] && testfailures=$status
98 status=$(($testfailures+0))
100 # Avoid making a test fail from this point onwards. The test is
104 echo "*** TEST COMPLETED (RC=$status) AT $(date '+%F %T'), CLEANING UP..."
106 if [ -z "$TEST_LOCAL_DAEMONS" -a -n "$CTDB_TEST_TIME_LOGGING" -a \
107 $status -ne 0 ] ; then
111 eval "$ctdb_test_exit_hook" || true
112 unset ctdb_test_exit_hook
114 if $ctdb_test_restart_scheduled || ! cluster_is_healthy ; then
118 # This could be made unconditional but then we might get
119 # duplication from the recovery in restart_ctdb. We want to
120 # leave the recovery in restart_ctdb so that future tests that
121 # might do a manual restart mid-test will benefit.
122 echo "Forcing a recovery..."
123 onnode 0 $CTDB recover
129 ctdb_test_exit_hook_add ()
131 ctdb_test_exit_hook="${ctdb_test_exit_hook}${ctdb_test_exit_hook:+ ; }$*"
136 scriptname=$(basename "$0")
138 ctdb_test_restart_scheduled=false
140 trap "ctdb_test_exit" 0
143 ########################################
146 try_command_on_node ()
148 local nodespec="$1" ; shift
153 while [ "${nodespec#-}" != "$nodespec" ] ; do
154 if [ "$nodespec" = "-v" ] ; then
157 onnode_opts="$nodespec"
159 nodespec="$1" ; shift
164 out=$(onnode -q $onnode_opts "$nodespec" "$cmd" 2>&1) || {
166 echo "Failed to execute \"$cmd\" on node(s) \"$nodespec\""
172 echo "Output of \"$cmd\":"
177 sanity_check_output ()
180 local regexp="$2" # Should be anchored as necessary.
185 local num_lines=$(echo "$output" | wc -l)
186 echo "There are $num_lines lines of output"
187 if [ $num_lines -lt $min_lines ] ; then
188 echo "BAD: that's less than the required number (${min_lines})"
193 local unexpected # local doesn't pass through status of command on RHS.
194 unexpected=$(echo "$output" | egrep -v "$regexp") || status=$?
196 # Note that this is reversed.
197 if [ $status -eq 0 ] ; then
198 echo "BAD: unexpected lines in output:"
199 echo "$unexpected" | cat -A
202 echo "Output lines look OK"
210 local ips="$1" # list of "ip node" lines
212 echo "Sanity checking IPs..."
216 while read x ipp ; do
217 [ "$ipp" = "-1" ] && break
218 if [ -n "$prev" -a "$ipp" != "$prev" ] ; then
225 echo "BAD: a node was -1 or IPs are only assigned to one node"
226 echo "Are you running an old version of CTDB?"
230 # This returns a list of "ip node" lines in $out
234 try_command_on_node $node "$CTDB ip -Y -n all | cut -d ':' -f1-3 | sed -e '1d' -e 's@^:@@' -e 's@:@ @g'"
237 select_test_node_and_ips ()
241 # When selecting test_node we just want a node that has public
242 # IPs. This will work and is economically semi-random. :-)
244 read x test_node <<<"$out"
248 while read ip pnn ; do
249 if [ "$pnn" = "$test_node" ] ; then
250 test_node_ips="${test_node_ips}${test_node_ips:+ }${ip}"
252 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
254 echo "Selected node ${test_node} with IPs: ${test_node_ips}."
255 test_ip="${test_node_ips%% *}"
258 #######################################
260 # Wait until either timeout expires or command succeeds. The command
261 # will be tried once per second.
264 local timeout="$1" ; shift # "$@" is the command...
267 if [ "$1" = "!" ] ; then
272 echo -n "<${timeout}|"
274 while [ $t -gt 0 ] ; do
277 if { ! $negate && [ $rc -eq 0 ] ; } || \
278 { $negate && [ $rc -ne 0 ] ; } ; then
279 echo "|$(($timeout - $t))|"
296 for i in $(seq 1 $1) ; do
303 _cluster_is_healthy ()
305 local out x count line
307 out=$($CTDB -Y status 2>/dev/null) || return 1
313 # We need to see valid lines if we're going to be healthy.
314 [ "${line#:[0-9]}" != "$line" ] && count=$(($count + 1))
315 # A line indicating a node is unhealthy causes failure.
316 [ "${line##:*:*:*1:}" != "$line" ] && return 1
318 [ $count -gt 0 ] && return $?
319 } <<<"$out" # Yay bash!
322 cluster_is_healthy ()
324 if onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
325 echo "Cluster is HEALTHY"
328 echo "Cluster is UNHEALTHY"
329 if ! ${ctdb_test_restart_scheduled:-false} ; then
330 echo "DEBUG AT $(date '+%F %T'):"
332 for i in "onnode -q 0 $CTDB status" "onnode -q 0 onnode all $CTDB scriptstatus" ; do
341 wait_until_healthy ()
343 local timeout="${1:-120}"
345 echo "Waiting for cluster to become healthy..."
347 wait_until 120 _cluster_is_healthy
350 # This function is becoming nicely overloaded. Soon it will collapse! :-)
358 (unhealthy) bits="?:?:?:1:*" ;;
359 (healthy) bits="?:?:?:0:*" ;;
360 (disconnected) bits="1:*" ;;
361 (connected) bits="0:*" ;;
362 (banned) bits="?:1:*" ;;
363 (unbanned) bits="?:0:*" ;;
364 (disabled) bits="?:?:1:*" ;;
365 (enabled) bits="?:?:0:*" ;;
366 (stopped) bits="?:?:?:?:1:*" ;;
367 (notstopped) bits="?:?:?:?:0:*" ;;
368 (frozen) fpat='^[[:space:]]+frozen[[:space:]]+1$' ;;
369 (unfrozen) fpat='^[[:space:]]+frozen[[:space:]]+0$' ;;
370 (monon) mpat='^Monitoring mode:ACTIVE \(0\)$' ;;
371 (monoff) mpat='^Monitoring mode:DISABLED \(1\)$' ;;
373 echo "node_has_status: unknown status \"$status\""
377 if [ -n "$bits" ] ; then
380 out=$($CTDB -Y status 2>&1) || return 1
385 # This needs to be done in 2 steps to avoid false matches.
386 local line_bits="${line#:${pnn}:*:}"
387 [ "$line_bits" = "$line" ] && continue
388 [ "${line_bits#${bits}}" != "$line_bits" ] && return 0
391 } <<<"$out" # Yay bash!
392 elif [ -n "$fpat" ] ; then
393 $CTDB statistics -n "$pnn" | egrep -q "$fpat"
394 elif [ -n "$mpat" ] ; then
395 $CTDB getmonmode -n "$pnn" | egrep -q "$mpat"
397 echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
402 wait_until_node_has_status ()
406 local timeout="${3:-30}"
407 local proxy_pnn="${4:-any}"
409 echo "Waiting until node $pnn has status \"$status\"..."
411 if ! wait_until $timeout onnode $proxy_pnn $CTDB_TEST_WRAPPER node_has_status "$pnn" "$status" ; then
412 for i in "onnode -q any $CTDB status" "onnode -q any onnode all $CTDB scriptstatus" ; do
422 # Useful for superficially testing IP failover.
423 # IPs must be on nodes matching nodeglob.
424 ips_are_on_nodeglob ()
426 local nodeglob="$1" ; shift
433 while read ip pnn ; do
434 for check in $ips ; do
435 if [ "$check" = "$ip" ] ; then
440 ips="${ips/${ip}}" # Remove from list
443 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
445 ips="${ips// }" # Remove any spaces.
449 wait_until_ips_are_on_nodeglob ()
451 echo "Waiting for IPs to fail over..."
453 wait_until 60 ips_are_on_nodeglob "$@"
464 while read ip pnn ; do
465 if [ "$node" = "$pnn" ] ; then
468 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
473 wait_until_node_has_some_ips ()
475 echo "Waiting for node to have some IPs..."
477 wait_until 60 node_has_some_ips "$@"
480 #######################################
484 echo "Attempting to politely shutdown daemons..."
485 onnode 1 $CTDB shutdown -n all || true
487 echo "Sleeping for a while..."
490 local pat="ctdbd --socket=.* --nlist .* --nopublicipcheck"
491 if pgrep -f "$pat" >/dev/null ; then
492 echo "Killing remaining daemons..."
495 if pgrep -f "$pat" >/dev/null ; then
496 echo "Once more with feeling.."
501 rm -rf "${TEST_VAR_DIR}/test.db"
506 mkdir -p "${TEST_VAR_DIR}/test.db/persistent"
508 local public_addresses_all="${TEST_VAR_DIR}/public_addresses_all"
509 local no_public_addresses="${TEST_VAR_DIR}/no_public_addresses.txt"
510 rm -f $CTDB_NODES $public_addresses_all $no_public_addresses
512 # If there are (strictly) greater than 2 nodes then we'll randomly
513 # choose a node to have no public addresses.
514 local no_public_ips=-1
515 [ $TEST_LOCAL_DAEMONS -gt 2 ] && no_public_ips=$(($RANDOM % $TEST_LOCAL_DAEMONS))
516 echo "$no_public_ips" >$no_public_addresses
518 # When running certain tests we add and remove eventscripts, so we
519 # need to be able to modify the events.d/ directory. Therefore,
520 # we use a temporary events.d/ directory under $TEST_VAR_DIR. We
521 # copy the actual test eventscript(s) in there from the original
522 # events.d/ directory that sits alongside $TEST_SCRIPT_DIR.
523 local top=$(dirname "$TEST_SCRIPTS_DIR")
524 local events_d="${top}/events.d"
525 mkdir -p "${TEST_VAR_DIR}/events.d"
526 cp -p "${events_d}/"* "${TEST_VAR_DIR}/events.d/"
529 for i in $(seq 1 $TEST_LOCAL_DAEMONS) ; do
530 if [ "${CTDB_USE_IPV6}x" != "x" ]; then
531 echo ::$i >>"$CTDB_NODES"
532 ip addr add ::$i/128 dev lo
534 echo 127.0.0.$i >>"$CTDB_NODES"
535 # 2 public addresses on most nodes, just to make things interesting.
536 if [ $(($i - 1)) -ne $no_public_ips ] ; then
537 echo "192.0.2.$i/24 lo" >>"$public_addresses_all"
538 echo "192.0.2.$(($i + $TEST_LOCAL_DAEMONS))/24 lo" >>"$public_addresses_all"
547 shift # "$@" gets passed to ctdbd
549 local public_addresses_all="${TEST_VAR_DIR}/public_addresses_all"
550 local public_addresses_mine="${TEST_VAR_DIR}/public_addresses.${pnn}"
551 local no_public_addresses="${TEST_VAR_DIR}/no_public_addresses.txt"
553 local no_public_ips=-1
554 [ -r $no_public_addresses ] && read no_public_ips <$no_public_addresses
556 if [ "$no_public_ips" = $pnn ] ; then
557 echo "Node $no_public_ips will have no public IPs."
560 local node_ip=$(sed -n -e "$(($pnn + 1))p" "$CTDB_NODES")
561 local ctdb_options="--reclock=${TEST_VAR_DIR}/rec.lock --nlist $CTDB_NODES --nopublicipcheck --node-ip=${node_ip} --event-script-dir=${TEST_VAR_DIR}/events.d --logfile=${TEST_VAR_DIR}/daemons.log -d 3 --dbdir=${TEST_VAR_DIR}/test.db --dbdir-persistent=${TEST_VAR_DIR}/test.db/persistent --dbdir-state=${TEST_VAR_DIR}/test.db/state"
563 if [ -z "$CTDB_TEST_REAL_CLUSTER" ]; then
564 ctdb_options="$ctdb_options --public-interface=lo"
567 if [ $pnn -eq $no_public_ips ] ; then
568 ctdb_options="$ctdb_options --public-addresses=/dev/null"
570 cp "$public_addresses_all" "$public_addresses_mine"
571 ctdb_options="$ctdb_options --public-addresses=$public_addresses_mine"
574 # We'll use "pkill -f" to kill the daemons with
575 # "--socket=.* --nlist .* --nopublicipcheck" as context.
576 $VALGRIND ctdbd --socket="${TEST_VAR_DIR}/sock.$pnn" $ctdb_options "$@" ||return 1
581 # "$@" gets passed to ctdbd
583 echo "Starting $TEST_LOCAL_DAEMONS ctdb daemons..."
585 for i in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
586 daemons_start_1 $i "$@"
589 if [ -L /tmp/ctdb.socket -o ! -S /tmp/ctdb.socket ] ; then
590 ln -sf "${TEST_VAR_DIR}/sock.0" /tmp/ctdb.socket || return 1
594 #######################################
596 _ctdb_hack_options ()
598 local ctdb_options="$*"
600 # We really just want to pass CTDB_OPTIONS but on RH
601 # /etc/sysconfig/ctdb can, and frequently does, set that variable.
602 # So instead, we hack badly. We'll add these as we use them.
603 # Note that these may still be overridden by the above file... but
604 # we tend to use the exotic options here... so that is unlikely.
606 case "$ctdb_options" in
607 *--start-as-stopped*)
608 export CTDB_START_AS_STOPPED="yes"
614 _ctdb_hack_options "$@"
616 if [ -e /etc/redhat-release ] ; then
619 /etc/init.d/ctdb restart
625 _ctdb_hack_options "$@"
627 /etc/init.d/ctdb start
632 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
637 # Common things to do after starting one or more nodes.
640 onnode -q 1 $CTDB_TEST_WRAPPER wait_until_healthy || return 1
642 echo "Setting RerecoveryTimeout to 1"
643 onnode -pq all "$CTDB setvar RerecoveryTimeout 1"
645 # In recent versions of CTDB, forcing a recovery like this blocks
646 # until the recovery is complete. Hopefully this will help the
647 # cluster to stabilise before a subsequent test.
648 echo "Forcing a recovery..."
649 onnode -q 0 $CTDB recover
651 echo "Forcing a recovery..."
652 onnode -q 0 $CTDB recover
657 # This assumes that ctdbd is not running on the given node.
661 shift # "$@" is passed to ctdbd start.
663 echo -n "Starting CTDB on node ${pnn}..."
665 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
666 daemons_start_1 $pnn "$@"
668 onnode $pnn $CTDB_TEST_WRAPPER _ctdb_start "$@"
671 # If we're starting only 1 node then we're doing something weird.
672 ctdb_restart_when_done
677 # "$@" is passed to ctdbd start.
679 echo -n "Restarting CTDB"
680 if $ctdb_test_restart_scheduled ; then
681 echo -n " (scheduled)"
686 for i in $(seq 1 5) ; do
687 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
691 onnode -p all $CTDB_TEST_WRAPPER _restart_ctdb "$@"
693 echo "Restart failed. Trying again in a few seconds..."
698 onnode -q 1 $CTDB_TEST_WRAPPER wait_until_healthy || {
699 echo "Cluster didn't become healthy. Restarting..."
703 local debug_out=$(onnode -p all ctdb status -Y 2>&1; onnode -p all ctdb scriptstatus 2>&1)
705 echo "Setting RerecoveryTimeout to 1"
706 onnode -pq all "$CTDB setvar RerecoveryTimeout 1"
708 # In recent versions of CTDB, forcing a recovery like this
709 # blocks until the recovery is complete. Hopefully this will
710 # help the cluster to stabilise before a subsequent test.
711 echo "Forcing a recovery..."
712 onnode -q 0 $CTDB recover
714 echo "Forcing a recovery..."
715 onnode -q 0 $CTDB recover
717 # Cluster is still healthy. Good, we're done!
718 if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
719 echo "Cluster become UNHEALTHY again. Restarting..."
723 echo "Doing a sync..."
724 onnode -q 0 $CTDB sync
730 echo "Cluster UNHEALTHY... too many attempts..."
732 # Try to make the calling test fail
737 ctdb_restart_when_done ()
739 ctdb_test_restart_scheduled=true
742 get_ctdbd_command_line_option ()
747 try_command_on_node "$pnn" "$CTDB getpid" || \
748 die "Unable to get PID of ctdbd on node $pnn"
750 local pid="${out#*:}"
751 try_command_on_node "$pnn" "ps -p $pid -o args hww" || \
752 die "Unable to get command-line of PID $pid"
754 # Strip everything up to and including --option
755 local t="${out#*--${option}}"
756 # Strip leading '=' or space if present
759 # Strip any following options and print
763 #######################################
765 install_eventscript ()
767 local script_name="$1"
768 local script_contents="$2"
770 if [ -z "$TEST_LOCAL_DAEMONS" ] ; then
771 # The quoting here is *very* fragile. However, we do
772 # experience the joy of installing a short script using
773 # onnode, and without needing to know the IP addresses of the
775 onnode all "f=\"\${CTDB_BASE:-/etc/ctdb}/events.d/${script_name}\" ; echo \"Installing \$f\" ; echo '${script_contents}' > \"\$f\" ; chmod 755 \"\$f\""
777 f="${TEST_VAR_DIR}/events.d/${script_name}"
778 echo "$script_contents" >"$f"
783 uninstall_eventscript ()
785 local script_name="$1"
787 if [ -z "$TEST_LOCAL_DAEMONS" ] ; then
788 onnode all "rm -vf \"\${CTDB_BASE:-/etc/ctdb}/events.d/${script_name}\""
790 rm -vf "${TEST_VAR_DIR}/events.d/${script_name}"
794 #######################################
796 # This section deals with the 99.ctdb_test eventscript.
798 # Metafunctions: Handle a ctdb-test file on a node.
800 ctdb_test_eventscript_file_create ()
805 try_command_on_node $pnn touch "/tmp/ctdb-test-${type}.${pnn}"
808 ctdb_test_eventscript_file_remove ()
813 try_command_on_node $pnn rm -f "/tmp/ctdb-test-${type}.${pnn}"
816 ctdb_test_eventscript_file_exists ()
821 try_command_on_node $pnn test -f "/tmp/ctdb-test-${type}.${pnn}" >/dev/null 2>&1
825 # Handle a flag file on a node that is removed by 99.ctdb_test on the
827 ctdb_test_eventscript_flag ()
833 ctdb_test_eventscript_file_${cmd} "$pnn" "flag-${event}"
837 # Handle a trigger that causes 99.ctdb_test to fail it's monitor
839 ctdb_test_eventscript_unhealthy_trigger ()
844 ctdb_test_eventscript_file_${cmd} "$pnn" "unhealthy-trigger"
847 # Handle the file that 99.ctdb_test created to show that it has marked
848 # a node unhealthy because it detected the above trigger.
849 ctdb_test_eventscript_unhealthy_detected ()
854 ctdb_test_eventscript_file_${cmd} "$pnn" "unhealthy-detected"
857 # Handle a trigger that causes 99.ctdb_test to timeout it's monitor
858 # event. This should cause the node to be banned.
859 ctdb_test_eventscript_timeout_trigger ()
865 ctdb_test_eventscript_file_${cmd} "$pnn" "${event}-timeout"
868 # Note that the eventscript can't use the above functions!
869 ctdb_test_eventscript_install ()
872 local script='#!/bin/sh
876 rm -vf "/tmp/ctdb-test-flag-${1}.${pnn}"
878 trigger="/tmp/ctdb-test-unhealthy-trigger.${pnn}"
879 detected="/tmp/ctdb-test-unhealthy-detected.${pnn}"
880 timeout_trigger="/tmp/ctdb-test-${1}-timeout.${pnn}"
883 if [ -e "$trigger" ] ; then
884 echo "${0}: Unhealthy because \"$trigger\" detected"
887 elif [ -e "$detected" -a ! -e "$trigger" ] ; then
888 echo "${0}: Healthy again, \"$trigger\" no longer detected"
894 if [ -e "$timeout_trigger" ] ; then
895 echo "${0}: Sleeping for a long time because \"$timeout_trigger\" detected"
905 install_eventscript "99.ctdb_test" "$script"
908 ctdb_test_eventscript_uninstall ()
910 uninstall_eventscript "99.ctdb_test"
913 # Note that this only works if you know all other monitor events will
914 # succeed. You also need to install the eventscript before using it.
915 wait_for_monitor_event ()
919 echo "Waiting for a monitor event on node ${pnn}..."
920 ctdb_test_eventscript_flag create $pnn "monitor"
922 wait_until 120 ! ctdb_test_eventscript_flag exists $pnn "monitor"
926 # Make sure that $CTDB is set.
929 local="${TEST_SUBDIR}/scripts/local.bash"
930 if [ -r "$local" ] ; then