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.
130 [ -n "$CTDB_DIR" ] || fail "Can not determine version."
132 (cd "$CTDB_DIR" && git describe)
135 ctdb_test_cmd_options()
137 [ -n "$1" ] || return 0
140 -h|--help) ctdb_test_usage 0 ;;
141 -v|--version) ctdb_test_version ;;
142 --category) echo "CTDB" ;;
143 -d|--description) test_info ;;
144 --summary) test_info | head -1 ;;
146 echo "Error: Unknown parameter = $1"
157 scriptname=$(basename "$0")
159 ctdb_test_restart_scheduled=false
161 ctdb_test_cmd_options $@
163 trap "ctdb_test_exit" 0
166 ctdb_test_check_real_cluster ()
168 [ -n "$CTDB_TEST_REAL_CLUSTER" ] && return 0
170 echo "ERROR: This test must be run on a real/virtual cluster, not local daemons."
174 ########################################
177 try_command_on_node ()
179 local nodespec="$1" ; shift
184 while [ "${nodespec#-}" != "$nodespec" ] ; do
185 if [ "$nodespec" = "-v" ] ; then
188 onnode_opts="$nodespec"
190 nodespec="$1" ; shift
195 out=$(onnode -q $onnode_opts "$nodespec" "$cmd" 2>&1) || {
197 echo "Failed to execute \"$cmd\" on node(s) \"$nodespec\""
203 echo "Output of \"$cmd\":"
208 sanity_check_output ()
211 local regexp="$2" # Should be anchored as necessary.
216 local num_lines=$(echo "$output" | wc -l)
217 echo "There are $num_lines lines of output"
218 if [ $num_lines -lt $min_lines ] ; then
219 echo "BAD: that's less than the required number (${min_lines})"
224 local unexpected # local doesn't pass through status of command on RHS.
225 unexpected=$(echo "$output" | egrep -v "$regexp") || status=$?
227 # Note that this is reversed.
228 if [ $status -eq 0 ] ; then
229 echo "BAD: unexpected lines in output:"
230 echo "$unexpected" | cat -A
233 echo "Output lines look OK"
241 local ips="$1" # Output of "ctdb ip -n all"
243 echo "Sanity checking IPs..."
247 while read x ipp ; do
248 [ "$ipp" = "-1" ] && break
249 if [ -n "$prev" -a "$ipp" != "$prev" ] ; then
256 echo "BAD: a node was -1 or IPs are only assigned to one node"
257 echo "Are you running an old version of CTDB?"
261 select_test_node_and_ips ()
263 try_command_on_node 0 "$CTDB ip -n all | sed -e '1d'"
265 # When selecting test_node we just want a node that has public
266 # IPs. This will work and is economically semi-random. :-)
268 read x test_node <<<"$out"
272 while read ip pnn ; do
273 if [ "$pnn" = "$test_node" ] ; then
274 test_node_ips="${test_node_ips}${test_node_ips:+ }${ip}"
276 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
278 echo "Selected node ${test_node} with IPs: ${test_node_ips}."
279 test_ip="${test_node_ips%% *}"
282 #######################################
284 # Wait until either timeout expires or command succeeds. The command
285 # will be tried once per second.
288 local timeout="$1" ; shift # "$@" is the command...
290 echo -n "<${timeout}|"
292 while [ $t -gt 0 ] ; do
294 echo "|$(($timeout - $t))|"
311 for i in $(seq 1 $1) ; do
318 _cluster_is_healthy ()
320 local out x count line
322 out=$(ctdb -Y status 2>&1) || return 1
328 count=$(($count + 1))
329 [ "${line##:*:*:*1:}" != "$line" ] && return 1
331 [ $count -gt 0 ] && return $?
332 } <<<"$out" # Yay bash!
335 cluster_is_healthy ()
337 if onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
338 echo "Cluster is HEALTHY"
341 echo "Cluster is UNHEALTHY"
342 if ! ${ctdb_test_restart_scheduled:-false} ; then
345 for i in "onnode -q 0 ctdb status" "onnode -q 0 onnode all ctdb scriptstatus" ; do
354 wait_until_healthy ()
356 local timeout="${1:-120}"
358 echo "Waiting for cluster to become healthy..."
360 wait_until 120 _cluster_is_healthy
363 # This function is becoming nicely overloaded. Soon it will collapse! :-)
371 (unhealthy) bits="?:?:?:1:*" ;;
372 (healthy) bits="?:?:?:0:*" ;;
373 (disconnected) bits="1:*" ;;
374 (connected) bits="0:*" ;;
375 (banned) bits="?:1:*" ;;
376 (unbanned) bits="?:0:*" ;;
377 (disabled) bits="?:?:1:*" ;;
378 (enabled) bits="?:?:0:*" ;;
379 (stopped) bits="?:?:?:?:1:*" ;;
380 (notstopped) bits="?:?:?:?:0:*" ;;
381 (frozen) fpat='^[[:space:]]+frozen[[:space:]]+1$' ;;
382 (unfrozen) fpat='^[[:space:]]+frozen[[:space:]]+0$' ;;
383 (monon) mpat='^Monitoring mode:ACTIVE \(0\)$' ;;
384 (monoff) mpat='^Monitoring mode:DISABLED \(1\)$' ;;
386 echo "node_has_status: unknown status \"$status\""
390 if [ -n "$bits" ] ; then
393 out=$(ctdb -Y status 2>&1) || return 1
398 [ "${line#:${pnn}:*:${bits}}" != "$line" ] && return 0
401 } <<<"$out" # Yay bash!
402 elif [ -n "$fpat" ] ; then
403 ctdb statistics -n "$pnn" | egrep -q "$fpat"
404 elif [ -n "$mpat" ] ; then
405 ctdb getmonmode -n "$pnn" | egrep -q "$mpat"
407 echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
412 wait_until_node_has_status ()
416 local timeout="${3:-30}"
418 echo "Waiting until node $pnn has status \"$status\"..."
420 onnode any $CTDB_TEST_WRAPPER wait_until $timeout node_has_status "$pnn" "$status"
423 # Useful for superficially testing IP failover.
424 # IPs must be on nodes matching nodeglob.
425 ips_are_on_nodeglob ()
427 local nodeglob="$1" ; shift
432 try_command_on_node 1 ctdb ip -n all
434 while read ip pnn ; do
435 for check in $ips ; do
436 if [ "$check" = "$ip" ] ; then
441 ips="${ips/${ip}}" # Remove from list
444 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
446 ips="${ips// }" # Remove any spaces.
450 wait_until_ips_are_on_nodeglob ()
452 echo "Waiting for IPs to fail over..."
454 wait_until 60 ips_are_on_nodeglob "$@"
460 local dst_socket="$2"
464 local pat="^${proto}[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[^[:space:]]+[[:space:]]+${dst_socket//./\\.}[[:space:]]+ESTABLISHED[[:space:]]+${pid}/${prog}[[:space:]]*\$"
465 out=$(netstat -tanp |
472 wait_until_get_src_socket ()
475 local dst_socket="$2"
479 echo "Waiting for ${prog} to establish connection to ${dst_socket}..."
481 wait_until 5 get_src_socket "$@"
484 #######################################
486 # filename will be in $tcpdump_filename, pid in $tcpdump_pid
489 tcpdump_filter="$1" # global
491 echo "Running tcpdump..."
492 tcpdump_filename=$(mktemp)
493 ctdb_test_exit_hook_add "rm -f $tcpdump_filename"
495 # The only way of being sure that tcpdump is listening is to send
496 # some packets that it will see. So we use dummy pings - the -U
497 # option to tcpdump ensures that packets are flushed to the file
498 # as they are captured.
499 local dummy_addr="127.3.2.1"
500 local dummy="icmp and dst host ${dummy_addr} and icmp[icmptype] == icmp-echo"
501 tcpdump -n -p -s 0 -e -U -w $tcpdump_filename -i any "($tcpdump_filter) or ($dummy)" &
502 ctdb_test_exit_hook_add "kill $! >/dev/null 2>&1"
504 echo "Waiting for tcpdump output file to be ready..."
505 ping -q "$dummy_addr" >/dev/null 2>&1 &
506 ctdb_test_exit_hook_add "kill $! >/dev/null 2>&1"
508 tcpdump_listen_for_dummy ()
510 tcpdump -n -r $tcpdump_filename -c 1 "$dummy" >/dev/null 2>&1
513 wait_until 10 tcpdump_listen_for_dummy
516 # By default, wait for 1 matching packet.
519 local count="${1:-1}"
520 local filter="${2:-${tcpdump_filter}}"
524 local found=$(tcpdump -n -r $tcpdump_filename "$filter" 2>/dev/null | wc -l)
525 [ $found -ge $count ]
528 echo "Waiting for tcpdump to capture some packets..."
529 if ! wait_until 30 tcpdump_check ; then
532 for i in "onnode -q 0 ctdb status" "netstat -tanp" "tcpdump -n -e -r $tcpdump_filename" ; do
542 local filter="${1:-${tcpdump_filter}}"
544 tcpdump -n -r $tcpdump_filename "$filter" 2>/dev/null
547 tcptickle_sniff_start ()
552 local in="src host ${dst%:*} and tcp src port ${dst##*:} and dst host ${src%:*} and tcp dst port ${src##*:}"
553 local out="src host ${src%:*} and tcp src port ${src##*:} and dst host ${dst%:*} and tcp dst port ${dst##*:}"
554 local tickle_ack="${in} and (tcp[tcpflags] & tcp-ack != 0) and (tcp[14] == 4) and (tcp[15] == 210)" # win == 1234
555 local ack_ack="${out} and (tcp[tcpflags] & tcp-ack != 0)"
556 tcptickle_reset="${in} and tcp[tcpflags] & tcp-rst != 0"
557 local filter="(${tickle_ack}) or (${ack_ack}) or (${tcptickle_reset})"
559 tcpdump_start "$filter"
562 tcptickle_sniff_wait_show ()
564 tcpdump_wait 1 "$tcptickle_reset"
566 echo "GOOD: here are some TCP tickle packets:"
570 gratarp_sniff_start ()
572 tcpdump_start "arp host ${test_ip}"
575 gratarp_sniff_wait_show ()
579 echo "GOOD: this should be the some gratuitous ARPs:"
584 #######################################
588 echo "Attempting to politely shutdown daemons..."
589 onnode 1 ctdb shutdown -n all || true
591 echo "Sleeping for a while..."
594 if pgrep -f $CTDB_DIR/bin/ctdbd >/dev/null ; then
595 echo "Killing remaining daemons..."
596 pkill -f $CTDB_DIR/bin/ctdbd
598 if pgrep -f $CTDB_DIR/bin/ctdbd >/dev/null ; then
599 echo "Once more with feeling.."
600 pkill -9 $CTDB_DIR/bin/ctdbd
604 local var_dir=$CTDB_DIR/tests/var
605 rm -rf $var_dir/test.db
610 local num_nodes="${CTDB_TEST_NUM_DAEMONS:-2}" # default is 2 nodes
612 local var_dir=$CTDB_DIR/tests/var
614 mkdir -p $var_dir/test.db/persistent
616 local nodes=$var_dir/nodes.txt
617 local public_addresses=$var_dir/public_addresses.txt
618 local no_public_addresses=$var_dir/no_public_addresses.txt
619 rm -f $nodes $public_addresses $no_public_addresses
621 # If there are (strictly) greater than 2 nodes then we'll randomly
622 # choose a node to have no public addresses.
623 local no_public_ips=-1
624 [ $num_nodes -gt 2 ] && no_public_ips=$(($RANDOM % $num_nodes))
625 echo "$no_public_ips" >$no_public_addresses
628 for i in $(seq 1 $num_nodes) ; do
629 if [ "${CTDB_USE_IPV6}x" != "x" ]; then
631 ip addr add ::$i/128 dev lo
633 echo 127.0.0.$i >> $nodes
634 # 2 public addresses on most nodes, just to make things interesting.
635 if [ $(($i - 1)) -ne $no_public_ips ] ; then
636 echo "192.0.2.$i/24 lo" >> $public_addresses
637 echo "192.0.2.$(($i + $num_nodes))/24 lo" >> $public_addresses
646 shift # "$@" gets passed to ctdbd
648 local var_dir=$CTDB_DIR/tests/var
650 local nodes=$var_dir/nodes.txt
651 local public_addresses=$var_dir/public_addresses.txt
652 local no_public_addresses=$var_dir/no_public_addresses.txt
654 local no_public_ips=-1
655 [ -r $no_public_addresses ] && read no_public_ips <$no_public_addresses
657 if [ "$no_public_ips" = $pnn ] ; then
658 echo "Node $no_public_ips will have no public IPs."
661 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"
663 if [ $(id -u) -eq 0 ]; then
664 ctdb_options="$ctdb_options --public-interface=lo"
667 if [ $pnn -eq $no_public_ips ] ; then
668 ctdb_options="$ctdb_options --public-addresses=/dev/null"
670 ctdb_options="$ctdb_options --public-addresses=$public_addresses"
673 # Need full path so we can use "pkill -f" to kill the daemons.
674 $VALGRIND $CTDB_DIR/bin/ctdbd --socket=$var_dir/sock.$pnn $ctdb_options "$@" ||return 1
679 # "$@" gets passed to ctdbd
681 local num_nodes="${CTDB_TEST_NUM_DAEMONS:-2}" # default is 2 nodes
683 echo "Starting $num_nodes ctdb daemons..."
685 for i in $(seq 0 $(($num_nodes - 1))) ; do
686 daemons_start_1 $i "$@"
689 local var_dir=$CTDB_DIR/tests/var
691 if [ -L /tmp/ctdb.socket -o ! -S /tmp/ctdb.socket ] ; then
692 ln -sf $var_dir/sock.0 /tmp/ctdb.socket || return 1
696 #######################################
698 _ctdb_hack_options ()
700 local ctdb_options="$*"
702 # We really just want to pass CTDB_OPTIONS but on RH
703 # /etc/sysconfig/ctdb can, and frequently does, set that variable.
704 # So instead, we hack badly. We'll add these as we use them.
705 # Note that these may still be overridden by the above file... but
706 # we tend to use the exotic options here... so that is unlikely.
708 case "$ctdb_options" in
709 *--start-as-stopped*)
710 export CTDB_START_AS_STOPPED="yes"
716 _ctdb_hack_options "$@"
718 if [ -e /etc/redhat-release ] ; then
721 /etc/init.d/ctdb restart
727 _ctdb_hack_options "$@"
729 /etc/init.d/ctdb start
734 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
739 # Common things to do after starting one or more nodes.
742 onnode -q 1 $CTDB_TEST_WRAPPER wait_until_healthy || return 1
744 echo "Setting RerecoveryTimeout to 1"
745 onnode -pq all "ctdb setvar RerecoveryTimeout 1"
747 # In recent versions of CTDB, forcing a recovery like this blocks
748 # until the recovery is complete. Hopefully this will help the
749 # cluster to stabilise before a subsequent test.
750 echo "Forcing a recovery..."
751 onnode -q 0 ctdb recover
753 echo "Forcing a recovery..."
754 onnode -q 0 ctdb recover
759 # This assumes that ctdbd is not running on the given node.
763 shift # "$@" is passed to ctdbd start.
765 echo -n "Starting CTDB on node ${pnn}..."
767 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
768 daemons_start_1 $pnn "$@"
770 onnode $pnn $CTDB_TEST_WRAPPER _ctdb_start "$@"
773 # If we're starting only 1 node then we're doing something weird.
774 ctdb_restart_when_done
779 # "$@" is passed to ctdbd start.
781 echo -n "Restarting CTDB"
782 if $ctdb_test_restart_scheduled ; then
783 echo -n " (scheduled)"
789 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
793 onnode -p all $CTDB_TEST_WRAPPER _restart_ctdb "$@"
797 [ $i -lt 5 ] || break
799 echo "That didn't seem to work - sleeping for a while..."
803 onnode -q 1 $CTDB_TEST_WRAPPER wait_until_healthy || return 1
805 echo "Setting RerecoveryTimeout to 1"
806 onnode -pq all "ctdb setvar RerecoveryTimeout 1"
808 # In recent versions of CTDB, forcing a recovery like this blocks
809 # until the recovery is complete. Hopefully this will help the
810 # cluster to stabilise before a subsequent test.
811 echo "Forcing a recovery..."
812 onnode -q 0 ctdb recover
814 echo "Forcing a recovery..."
815 onnode -q 0 ctdb recover
820 ctdb_restart_when_done ()
822 ctdb_test_restart_scheduled=true
825 #######################################
827 install_eventscript ()
829 local script_name="$1"
830 local script_contents="$2"
832 if [ -n "$CTDB_TEST_REAL_CLUSTER" ] ; then
833 # The quoting here is *very* fragile. However, we do
834 # experience the joy of installing a short script using
835 # onnode, and without needing to know the IP addresses of the
837 onnode all "f=\"\${CTDB_BASE:-/etc/ctdb}/events.d/${script_name}\" ; echo \"Installing \$f\" ; echo '${script_contents}' > \"\$f\" ; chmod 755 \"\$f\""
839 f="${CTDB_DIR}/tests/events.d/${script_name}"
840 echo "$script_contents" >"$f"
845 uninstall_eventscript ()
847 local script_name="$1"
849 if [ -n "$CTDB_TEST_REAL_CLUSTER" ] ; then
850 onnode all "rm -vf \"\${CTDB_BASE:-/etc/ctdb}/events.d/${script_name}\""
852 rm -vf "${CTDB_DIR}/tests/events.d/${script_name}"