1 # Hey Emacs, this is a -*- shell-script -*- !!! :-)
9 ######################################################################
15 teststarttime=$(date '+%s')
18 echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
19 echo "Running test $name ($(date '+%T'))"
20 echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
25 local name="$1" ; shift
26 local status="$1" ; shift
27 # "$@" is command-line
29 local interp="SKIPPED"
30 local statstr=" (reason $*)"
31 if [ -n "$status" ] ; then
32 if [ $status -eq 0 ] ; then
38 statstr=" (status $status)"
39 testfailures=$(($testfailures+1))
43 testduration=$(($(date +%s)-$teststarttime))
45 echo "=========================================================================="
46 echo "TEST ${interp}: ${name}${statstr} (duration: ${testduration}s)"
47 echo "=========================================================================="
53 exit $(($testfailures+0))
62 [ $(($testfailures+0)) -eq 0 -a $status -ne 0 ] && testfailures=$status
63 status=$(($testfailures+0))
65 # Avoid making a test fail from this point onwards. The test is
69 echo "*** TEST COMPLETE (RC=$status), CLEANING UP..."
71 eval "$ctdb_test_exit_hook" || true
72 unset ctdb_test_exit_hook
74 if $ctdb_test_restart_scheduled || ! cluster_is_healthy ; then
78 # This could be made unconditional but then we might get
79 # duplication from the recovery in restart_ctdb. We want to
80 # leave the recovery in restart_ctdb so that future tests that
81 # might do a manual restart mid-test will benefit.
82 echo "Forcing a recovery..."
89 ctdb_test_exit_hook_add ()
91 ctdb_test_exit_hook="${ctdb_test_exit_hook}${ctdb_test_exit_hook:+ ; }$*"
96 local name="$1" ; shift
98 [ -n "$1" ] || set -- "$name"
100 ctdb_test_begin "$name"
105 ctdb_test_end "$name" "$status" "$*"
118 -h, --help show this screen.
119 -v, --version show test case version.
120 --category show the test category (ACL, CTDB, Samba ...).
121 -d, --description show test case description.
122 --summary show short test case summary.
123 -x trace test using set -x
131 [ -n "$CTDB_DIR" ] || fail "Can not determine version."
133 (cd "$CTDB_DIR" && git describe)
136 ctdb_test_cmd_options()
138 [ -n "$1" ] || return 0
141 -h|--help) ctdb_test_usage 0 ;;
142 -v|--version) ctdb_test_version ;;
143 --category) echo "CTDB" ;;
144 -d|--description) test_info ;;
145 -x) set -x ; return 0 ;;
147 echo "Error: Unknown parameter = $1"
158 scriptname=$(basename "$0")
160 ctdb_test_restart_scheduled=false
162 ctdb_test_cmd_options $@
164 trap "ctdb_test_exit" 0
167 ctdb_test_check_real_cluster ()
169 [ -n "$CTDB_TEST_REAL_CLUSTER" ] && return 0
171 echo "ERROR: This test must be run on a real/virtual cluster, not local daemons."
175 ########################################
178 try_command_on_node ()
180 local nodespec="$1" ; shift
185 while [ "${nodespec#-}" != "$nodespec" ] ; do
186 if [ "$nodespec" = "-v" ] ; then
189 onnode_opts="$nodespec"
191 nodespec="$1" ; shift
196 out=$(onnode -q $onnode_opts "$nodespec" "$cmd" 2>&1) || {
198 echo "Failed to execute \"$cmd\" on node(s) \"$nodespec\""
204 echo "Output of \"$cmd\":"
209 sanity_check_output ()
212 local regexp="$2" # Should be anchored as necessary.
217 local num_lines=$(echo "$output" | wc -l)
218 echo "There are $num_lines lines of output"
219 if [ $num_lines -lt $min_lines ] ; then
220 echo "BAD: that's less than the required number (${min_lines})"
225 local unexpected # local doesn't pass through status of command on RHS.
226 unexpected=$(echo "$output" | egrep -v "$regexp") || status=$?
228 # Note that this is reversed.
229 if [ $status -eq 0 ] ; then
230 echo "BAD: unexpected lines in output:"
231 echo "$unexpected" | cat -A
234 echo "Output lines look OK"
242 local ips="$1" # list of "ip node" lines
244 echo "Sanity checking IPs..."
248 while read x ipp ; do
249 [ "$ipp" = "-1" ] && break
250 if [ -n "$prev" -a "$ipp" != "$prev" ] ; then
257 echo "BAD: a node was -1 or IPs are only assigned to one node"
258 echo "Are you running an old version of CTDB?"
262 # This returns a list of "ip node" lines in $out
266 try_command_on_node $node "$CTDB ip -Y -n all | cut -d ':' -f1-3 | sed -e '1d' -e 's@^:@@' -e 's@:@ @g'"
269 select_test_node_and_ips ()
273 # When selecting test_node we just want a node that has public
274 # IPs. This will work and is economically semi-random. :-)
276 read x test_node <<<"$out"
280 while read ip pnn ; do
281 if [ "$pnn" = "$test_node" ] ; then
282 test_node_ips="${test_node_ips}${test_node_ips:+ }${ip}"
284 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
286 echo "Selected node ${test_node} with IPs: ${test_node_ips}."
287 test_ip="${test_node_ips%% *}"
290 #######################################
292 # Wait until either timeout expires or command succeeds. The command
293 # will be tried once per second.
296 local timeout="$1" ; shift # "$@" is the command...
299 if [ "$1" = "!" ] ; then
304 echo -n "<${timeout}|"
306 while [ $t -gt 0 ] ; do
309 if { ! $negate && [ $rc -eq 0 ] ; } || \
310 { $negate && [ $rc -ne 0 ] ; } ; then
311 echo "|$(($timeout - $t))|"
328 for i in $(seq 1 $1) ; do
335 _cluster_is_healthy ()
337 local out x count line
339 out=$(ctdb -Y status 2>&1) || return 1
345 count=$(($count + 1))
346 [ "${line##:*:*:*1:}" != "$line" ] && return 1
348 [ $count -gt 0 ] && return $?
349 } <<<"$out" # Yay bash!
352 cluster_is_healthy ()
354 if onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
355 echo "Cluster is HEALTHY"
358 echo "Cluster is UNHEALTHY"
359 if ! ${ctdb_test_restart_scheduled:-false} ; then
362 for i in "onnode -q 0 ctdb status" "onnode -q 0 onnode all ctdb scriptstatus" ; do
371 wait_until_healthy ()
373 local timeout="${1:-120}"
375 echo "Waiting for cluster to become healthy..."
377 wait_until 120 _cluster_is_healthy
380 # This function is becoming nicely overloaded. Soon it will collapse! :-)
388 (unhealthy) bits="?:?:?:1:*" ;;
389 (healthy) bits="?:?:?:0:*" ;;
390 (disconnected) bits="1:*" ;;
391 (connected) bits="0:*" ;;
392 (banned) bits="?:1:*" ;;
393 (unbanned) bits="?:0:*" ;;
394 (disabled) bits="?:?:1:*" ;;
395 (enabled) bits="?:?:0:*" ;;
396 (stopped) bits="?:?:?:?:1:*" ;;
397 (notstopped) bits="?:?:?:?:0:*" ;;
398 (frozen) fpat='^[[:space:]]+frozen[[:space:]]+1$' ;;
399 (unfrozen) fpat='^[[:space:]]+frozen[[:space:]]+0$' ;;
400 (monon) mpat='^Monitoring mode:ACTIVE \(0\)$' ;;
401 (monoff) mpat='^Monitoring mode:DISABLED \(1\)$' ;;
403 echo "node_has_status: unknown status \"$status\""
407 if [ -n "$bits" ] ; then
410 out=$(ctdb -Y status 2>&1) || return 1
415 # This needs to be done in 2 steps to avoid false matches.
416 local line_bits="${line#:${pnn}:*:}"
417 [ "$line_bits" = "$line" ] && continue
418 [ "${line_bits#${bits}}" != "$line_bits" ] && return 0
421 } <<<"$out" # Yay bash!
422 elif [ -n "$fpat" ] ; then
423 ctdb statistics -n "$pnn" | egrep -q "$fpat"
424 elif [ -n "$mpat" ] ; then
425 ctdb getmonmode -n "$pnn" | egrep -q "$mpat"
427 echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
432 wait_until_node_has_status ()
436 local timeout="${3:-30}"
438 echo "Waiting until node $pnn has status \"$status\"..."
440 if ! onnode any $CTDB_TEST_WRAPPER wait_until $timeout node_has_status "$pnn" "$status" ; then
441 for i in "onnode -q any ctdb status" "onnode -q any onnode all ctdb scriptstatus" ; do
451 # Useful for superficially testing IP failover.
452 # IPs must be on nodes matching nodeglob.
453 ips_are_on_nodeglob ()
455 local nodeglob="$1" ; shift
462 while read ip pnn ; do
463 for check in $ips ; do
464 if [ "$check" = "$ip" ] ; then
469 ips="${ips/${ip}}" # Remove from list
472 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
474 ips="${ips// }" # Remove any spaces.
478 wait_until_ips_are_on_nodeglob ()
480 echo "Waiting for IPs to fail over..."
482 wait_until 60 ips_are_on_nodeglob "$@"
488 local dst_socket="$2"
492 local pat="^${proto}[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[^[:space:]]+[[:space:]]+${dst_socket//./\\.}[[:space:]]+ESTABLISHED[[:space:]]+${pid}/${prog}[[:space:]]*\$"
493 out=$(netstat -tanp |
500 wait_until_get_src_socket ()
503 local dst_socket="$2"
507 echo "Waiting for ${prog} to establish connection to ${dst_socket}..."
509 wait_until 5 get_src_socket "$@"
512 #######################################
514 # filename will be in $tcpdump_filename, pid in $tcpdump_pid
517 tcpdump_filter="$1" # global
519 echo "Running tcpdump..."
520 tcpdump_filename=$(mktemp)
521 ctdb_test_exit_hook_add "rm -f $tcpdump_filename"
523 # The only way of being sure that tcpdump is listening is to send
524 # some packets that it will see. So we use dummy pings - the -U
525 # option to tcpdump ensures that packets are flushed to the file
526 # as they are captured.
527 local dummy_addr="127.3.2.1"
528 local dummy="icmp and dst host ${dummy_addr} and icmp[icmptype] == icmp-echo"
529 tcpdump -n -p -s 0 -e -U -w $tcpdump_filename -i any "($tcpdump_filter) or ($dummy)" &
530 ctdb_test_exit_hook_add "kill $! >/dev/null 2>&1"
532 echo "Waiting for tcpdump output file to be ready..."
533 ping -q "$dummy_addr" >/dev/null 2>&1 &
534 ctdb_test_exit_hook_add "kill $! >/dev/null 2>&1"
536 tcpdump_listen_for_dummy ()
538 tcpdump -n -r $tcpdump_filename -c 1 "$dummy" >/dev/null 2>&1
541 wait_until 10 tcpdump_listen_for_dummy
544 # By default, wait for 1 matching packet.
547 local count="${1:-1}"
548 local filter="${2:-${tcpdump_filter}}"
552 local found=$(tcpdump -n -r $tcpdump_filename "$filter" 2>/dev/null | wc -l)
553 [ $found -ge $count ]
556 echo "Waiting for tcpdump to capture some packets..."
557 if ! wait_until 30 tcpdump_check ; then
560 for i in "onnode -q 0 ctdb status" "netstat -tanp" "tcpdump -n -e -r $tcpdump_filename" ; do
570 local filter="${1:-${tcpdump_filter}}"
572 tcpdump -n -r $tcpdump_filename "$filter" 2>/dev/null
575 tcptickle_sniff_start ()
580 local in="src host ${dst%:*} and tcp src port ${dst##*:} and dst host ${src%:*} and tcp dst port ${src##*:}"
581 local out="src host ${src%:*} and tcp src port ${src##*:} and dst host ${dst%:*} and tcp dst port ${dst##*:}"
582 local tickle_ack="${in} and (tcp[tcpflags] & tcp-ack != 0) and (tcp[14] == 4) and (tcp[15] == 210)" # win == 1234
583 local ack_ack="${out} and (tcp[tcpflags] & tcp-ack != 0)"
584 tcptickle_reset="${in} and tcp[tcpflags] & tcp-rst != 0"
585 local filter="(${tickle_ack}) or (${ack_ack}) or (${tcptickle_reset})"
587 tcpdump_start "$filter"
590 tcptickle_sniff_wait_show ()
592 tcpdump_wait 1 "$tcptickle_reset"
594 echo "GOOD: here are some TCP tickle packets:"
598 gratarp_sniff_start ()
600 tcpdump_start "arp host ${test_ip}"
603 gratarp_sniff_wait_show ()
607 echo "GOOD: this should be the some gratuitous ARPs:"
612 #######################################
616 echo "Attempting to politely shutdown daemons..."
617 onnode 1 ctdb shutdown -n all || true
619 echo "Sleeping for a while..."
622 if pgrep -f $CTDB_DIR/bin/ctdbd >/dev/null ; then
623 echo "Killing remaining daemons..."
624 pkill -f $CTDB_DIR/bin/ctdbd
626 if pgrep -f $CTDB_DIR/bin/ctdbd >/dev/null ; then
627 echo "Once more with feeling.."
628 pkill -9 $CTDB_DIR/bin/ctdbd
632 local var_dir=$CTDB_DIR/tests/var
633 rm -rf $var_dir/test.db
638 local num_nodes="${CTDB_TEST_NUM_DAEMONS:-2}" # default is 2 nodes
640 local var_dir=$CTDB_DIR/tests/var
642 mkdir -p $var_dir/test.db/persistent
644 local nodes=$var_dir/nodes.txt
645 local public_addresses=$var_dir/public_addresses.txt
646 local no_public_addresses=$var_dir/no_public_addresses.txt
647 rm -f $nodes $public_addresses $no_public_addresses
649 # If there are (strictly) greater than 2 nodes then we'll randomly
650 # choose a node to have no public addresses.
651 local no_public_ips=-1
652 [ $num_nodes -gt 2 ] && no_public_ips=$(($RANDOM % $num_nodes))
653 echo "$no_public_ips" >$no_public_addresses
656 for i in $(seq 1 $num_nodes) ; do
657 if [ "${CTDB_USE_IPV6}x" != "x" ]; then
659 ip addr add ::$i/128 dev lo
661 echo 127.0.0.$i >> $nodes
662 # 2 public addresses on most nodes, just to make things interesting.
663 if [ $(($i - 1)) -ne $no_public_ips ] ; then
664 echo "192.0.2.$i/24 lo" >> $public_addresses
665 echo "192.0.2.$(($i + $num_nodes))/24 lo" >> $public_addresses
674 shift # "$@" gets passed to ctdbd
676 local var_dir=$CTDB_DIR/tests/var
678 local nodes=$var_dir/nodes.txt
679 local public_addresses=$var_dir/public_addresses.txt
680 local no_public_addresses=$var_dir/no_public_addresses.txt
682 local no_public_ips=-1
683 [ -r $no_public_addresses ] && read no_public_ips <$no_public_addresses
685 if [ "$no_public_ips" = $pnn ] ; then
686 echo "Node $no_public_ips will have no public IPs."
689 local ctdb_options="--reclock=$var_dir/rec.lock --nlist $nodes --nopublicipcheck --event-script-dir=$CTDB_DIR/tests/events.d --logfile=$var_dir/daemons.log -d 0 --dbdir=$var_dir/test.db --dbdir-persistent=$var_dir/test.db/persistent --dbdir-state=$var_dir/test.db/state"
691 if [ $(id -u) -eq 0 ]; then
692 ctdb_options="$ctdb_options --public-interface=lo"
695 if [ $pnn -eq $no_public_ips ] ; then
696 ctdb_options="$ctdb_options --public-addresses=/dev/null"
698 ctdb_options="$ctdb_options --public-addresses=$public_addresses"
701 # Need full path so we can use "pkill -f" to kill the daemons.
702 $VALGRIND $CTDB_DIR/bin/ctdbd --socket=$var_dir/sock.$pnn $ctdb_options "$@" ||return 1
707 # "$@" gets passed to ctdbd
709 local num_nodes="${CTDB_TEST_NUM_DAEMONS:-2}" # default is 2 nodes
711 echo "Starting $num_nodes ctdb daemons..."
713 for i in $(seq 0 $(($num_nodes - 1))) ; do
714 daemons_start_1 $i "$@"
717 local var_dir=$CTDB_DIR/tests/var
719 if [ -L /tmp/ctdb.socket -o ! -S /tmp/ctdb.socket ] ; then
720 ln -sf $var_dir/sock.0 /tmp/ctdb.socket || return 1
724 #######################################
726 _ctdb_hack_options ()
728 local ctdb_options="$*"
730 # We really just want to pass CTDB_OPTIONS but on RH
731 # /etc/sysconfig/ctdb can, and frequently does, set that variable.
732 # So instead, we hack badly. We'll add these as we use them.
733 # Note that these may still be overridden by the above file... but
734 # we tend to use the exotic options here... so that is unlikely.
736 case "$ctdb_options" in
737 *--start-as-stopped*)
738 export CTDB_START_AS_STOPPED="yes"
744 _ctdb_hack_options "$@"
746 if [ -e /etc/redhat-release ] ; then
749 /etc/init.d/ctdb restart
755 _ctdb_hack_options "$@"
757 /etc/init.d/ctdb start
762 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
767 # Common things to do after starting one or more nodes.
770 onnode -q 1 $CTDB_TEST_WRAPPER wait_until_healthy || return 1
772 echo "Setting RerecoveryTimeout to 1"
773 onnode -pq all "ctdb setvar RerecoveryTimeout 1"
775 # In recent versions of CTDB, forcing a recovery like this blocks
776 # until the recovery is complete. Hopefully this will help the
777 # cluster to stabilise before a subsequent test.
778 echo "Forcing a recovery..."
779 onnode -q 0 ctdb recover
781 echo "Forcing a recovery..."
782 onnode -q 0 ctdb recover
787 # This assumes that ctdbd is not running on the given node.
791 shift # "$@" is passed to ctdbd start.
793 echo -n "Starting CTDB on node ${pnn}..."
795 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
796 daemons_start_1 $pnn "$@"
798 onnode $pnn $CTDB_TEST_WRAPPER _ctdb_start "$@"
801 # If we're starting only 1 node then we're doing something weird.
802 ctdb_restart_when_done
807 # "$@" is passed to ctdbd start.
809 echo -n "Restarting CTDB"
810 if $ctdb_test_restart_scheduled ; then
811 echo -n " (scheduled)"
817 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
821 onnode -p all $CTDB_TEST_WRAPPER _restart_ctdb "$@"
825 [ $i -lt 5 ] || break
827 echo "That didn't seem to work - sleeping for a while..."
831 onnode -q 1 $CTDB_TEST_WRAPPER wait_until_healthy || return 1
833 echo "Setting RerecoveryTimeout to 1"
834 onnode -pq all "ctdb setvar RerecoveryTimeout 1"
836 # In recent versions of CTDB, forcing a recovery like this blocks
837 # until the recovery is complete. Hopefully this will help the
838 # cluster to stabilise before a subsequent test.
839 echo "Forcing a recovery..."
840 onnode -q 0 ctdb recover
842 echo "Forcing a recovery..."
843 onnode -q 0 ctdb recover
848 ctdb_restart_when_done ()
850 ctdb_test_restart_scheduled=true
853 #######################################
855 install_eventscript ()
857 local script_name="$1"
858 local script_contents="$2"
860 if [ -n "$CTDB_TEST_REAL_CLUSTER" ] ; then
861 # The quoting here is *very* fragile. However, we do
862 # experience the joy of installing a short script using
863 # onnode, and without needing to know the IP addresses of the
865 onnode all "f=\"\${CTDB_BASE:-/etc/ctdb}/events.d/${script_name}\" ; echo \"Installing \$f\" ; echo '${script_contents}' > \"\$f\" ; chmod 755 \"\$f\""
867 f="${CTDB_DIR}/tests/events.d/${script_name}"
868 echo "$script_contents" >"$f"
873 uninstall_eventscript ()
875 local script_name="$1"
877 if [ -n "$CTDB_TEST_REAL_CLUSTER" ] ; then
878 onnode all "rm -vf \"\${CTDB_BASE:-/etc/ctdb}/events.d/${script_name}\""
880 rm -vf "${CTDB_DIR}/tests/events.d/${script_name}"
884 #######################################
886 # This section deals with the 99.ctdb_test eventscript.
888 # Metafunctions: Handle a ctdb-test file on a node.
890 ctdb_test_eventscript_file_create ()
895 try_command_on_node $pnn touch "/tmp/ctdb-test-${type}.${pnn}"
898 ctdb_test_eventscript_file_remove ()
903 try_command_on_node $pnn rm -f "/tmp/ctdb-test-${type}.${pnn}"
906 ctdb_test_eventscript_file_exists ()
911 try_command_on_node $pnn test -f "/tmp/ctdb-test-${type}.${pnn}" >/dev/null 2>&1
915 # Handle a flag file on a node that is removed by 99.ctdb_test on the
917 ctdb_test_eventscript_flag ()
923 ctdb_test_eventscript_file_${cmd} "$pnn" "flag-${event}"
927 # Handle a trigger that causes 99.ctdb_test to fail it's monitor
929 ctdb_test_eventscript_unhealthy_trigger ()
934 ctdb_test_eventscript_file_${cmd} "$pnn" "unhealthy-trigger"
937 # Handle the file that 99.ctdb_test created to show that it has marked
938 # a node unhealthy because it detected the above trigger.
939 ctdb_test_eventscript_unhealthy_detected ()
944 ctdb_test_eventscript_file_${cmd} "$pnn" "unhealthy-detected"
947 # Handle a trigger that causes 99.ctdb_test to timeout it's monitor
948 # event. This should cause the node to be banned.
949 ctdb_test_eventscript_timeout_trigger ()
955 ctdb_test_eventscript_file_${cmd} "$pnn" "${event}-timeout"
958 # Note that the eventscript can't use the above functions!
959 ctdb_test_eventscript_install ()
962 local script='#!/bin/sh
966 rm -vf "/tmp/ctdb-test-flag-${1}.${pnn}"
968 trigger="/tmp/ctdb-test-unhealthy-trigger.${pnn}"
969 detected="/tmp/ctdb-test-unhealthy-detected.${pnn}"
970 timeout_trigger="/tmp/ctdb-test-${1}-timeout.${pnn}"
973 if [ -e "$trigger" ] ; then
974 echo "${0}: Unhealthy because \"$trigger\" detected"
977 elif [ -e "$detected" -a ! -e "$trigger" ] ; then
978 echo "${0}: Healthy again, \"$trigger\" no longer detected"
984 if [ -e "$timeout_trigger" ] ; then
985 echo "${0}: Sleeping for a long time because \"$timeout_trigger\" detected"
995 install_eventscript "99.ctdb_test" "$script"
998 ctdb_test_eventscript_uninstall ()
1000 uninstall_eventscript "99.ctdb_test"
1003 # Note that this only works if you know all other monitor events will
1004 # succeed. You also need to install the eventscript before using it.
1005 wait_for_monitor_event ()
1009 echo "Waiting for a monitor event on node ${pnn}..."
1010 ctdb_test_eventscript_flag create $pnn "monitor"
1012 wait_until 120 ! ctdb_test_eventscript_flag exists $pnn "monitor"