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 [ $
(($testfailures+0)) -eq 0 -a $status -ne 0 ] && testfailures
=$status
33 status
=$
(($testfailures+0))
35 # Avoid making a test fail from this point onwards. The test is
39 echo "*** TEST COMPLETED (RC=$status) AT $(date '+%F %T'), CLEANING UP..."
41 eval "$ctdb_test_exit_hook" || true
42 unset ctdb_test_exit_hook
44 if $ctdb_test_restart_scheduled ||
! cluster_is_healthy
; then
48 # This could be made unconditional but then we might get
49 # duplication from the recovery in restart_ctdb. We want to
50 # leave the recovery in restart_ctdb so that future tests that
51 # might do a manual restart mid-test will benefit.
52 echo "Forcing a recovery..."
53 onnode
0 $CTDB recover
59 ctdb_test_exit_hook_add
()
61 ctdb_test_exit_hook
="${ctdb_test_exit_hook}${ctdb_test_exit_hook:+ ; }$*"
66 scriptname
=$
(basename "$0")
68 ctdb_test_restart_scheduled
=false
70 trap "ctdb_test_exit" 0
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.
115 local num_lines
=$
(echo "$output" |
wc -l)
116 echo "There are $num_lines lines of output"
117 if [ $num_lines -lt $min_lines ] ; then
118 echo "BAD: that's less than the required number (${min_lines})"
123 local unexpected
# local doesn't pass through status of command on RHS.
124 unexpected
=$
(echo "$output" |
egrep -v "$regexp") || status
=$?
126 # Note that this is reversed.
127 if [ $status -eq 0 ] ; then
128 echo "BAD: unexpected lines in output:"
129 echo "$unexpected" |
cat -A
132 echo "Output lines look OK"
140 local ips
="$1" # list of "ip node" lines
142 echo "Sanity checking IPs..."
146 while read x ipp
; do
147 [ "$ipp" = "-1" ] && break
148 if [ -n "$prev" -a "$ipp" != "$prev" ] ; then
155 echo "BAD: a node was -1 or IPs are only assigned to one node:"
157 echo "Are you running an old version of CTDB?"
161 # This returns a list of "ip node" lines in $out
165 try_command_on_node
$node \
166 "$CTDB ip -X | awk -F'|' 'NR > 1 { print \$2, \$3 }'"
169 _select_test_node_and_ips
()
171 try_command_on_node any \
172 "$CTDB ip -X all | awk -F'|' 'NR > 1 { print \$2, \$3 }'"
174 test_node
="" # this matches no PNN
177 while read ip pnn
; do
178 if [ -z "$test_node" -a "$pnn" != "-1" ] ; then
181 if [ "$pnn" = "$test_node" ] ; then
182 test_node_ips
="${test_node_ips}${test_node_ips:+ }${ip}"
184 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
186 echo "Selected node ${test_node} with IPs: ${test_node_ips}."
187 test_ip
="${test_node_ips%% *}"
190 *:*) test_prefix
="${test_ip}/128" ;;
191 *) test_prefix
="${test_ip}/32" ;;
194 [ -n "$test_node" ] ||
return 1
197 select_test_node_and_ips
()
200 while ! _select_test_node_and_ips
; do
201 echo "Unable to find a test node with IPs assigned"
202 if [ $timeout -le 0 ] ; then
203 echo "BAD: Too many attempts"
207 timeout
=$
(($timeout - 1))
214 get_test_ip_mask_and_iface
()
217 try_command_on_node
$test_node "$CTDB ip -v -X | awk -F'|' -v ip=$test_ip '\$2 == ip { print \$4 }'"
220 if [ -z "$TEST_LOCAL_DAEMONS" ] ; then
222 try_command_on_node
$test_node ip addr show to
$test_ip
229 echo "$test_ip/$mask is on $iface"
234 try_command_on_node
-q all
"$CTDB pnn"
238 # The subtlety is that "ctdb delip" will fail if the IP address isn't
239 # configured on a node...
240 delete_ip_from_all_nodes
()
248 for _pnn
in $all_pnns ; do
249 all_ips_on_node
$_pnn
250 while read _i _n
; do
251 if [ "$_ip" = "$_i" ] ; then
252 _nodes
="${_nodes}${_nodes:+,}${_pnn}"
254 done <<<"$out" # bashism
257 try_command_on_node
-pq "$_nodes" "$CTDB delip $_ip"
260 #######################################
265 for i
in $
(seq 1 $1) ; do
272 _cluster_is_healthy
()
274 $CTDB nodestatus all
>/dev
/null
277 _cluster_is_recovered
()
279 node_has_status
0 recovered
284 _cluster_is_healthy
&& _cluster_is_recovered
287 cluster_is_healthy
()
289 if onnode
0 $CTDB_TEST_WRAPPER _cluster_is_healthy
; then
290 echo "Cluster is HEALTHY"
291 if ! onnode
0 $CTDB_TEST_WRAPPER _cluster_is_recovered
; then
292 echo "WARNING: cluster in recovery mode!"
296 echo "Cluster is UNHEALTHY"
297 if ! ${ctdb_test_restart_scheduled:-false} ; then
298 echo "DEBUG AT $(date '+%F %T'):"
300 for i
in "onnode -q 0 $CTDB status" "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\)$' ;;
340 echo "node_has_status: unknown status \"$status\""
344 if [ -n "$bits" ] ; then
347 out
=$
($CTDB -X status
2>&1) ||
return 1
352 # This needs to be done in 2 steps to avoid false matches.
353 local line_bits
="${line#|${pnn}|*|}"
354 [ "$line_bits" = "$line" ] && continue
355 [ "${line_bits#${bits}}" != "$line_bits" ] && return 0
358 } <<<"$out" # Yay bash!
359 elif [ -n "$fpat" ] ; then
360 $CTDB statistics
-n "$pnn" |
egrep -q "$fpat"
361 elif [ -n "$rpat" ] ; then
362 ! $CTDB status
-n "$pnn" |
egrep -q "$rpat"
364 echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
369 wait_until_node_has_status
()
373 local timeout
="${3:-30}"
374 local proxy_pnn
="${4:-any}"
376 echo "Waiting until node $pnn has status \"$status\"..."
378 if ! wait_until
$timeout onnode
$proxy_pnn $CTDB_TEST_WRAPPER node_has_status
"$pnn" "$status" ; then
379 for i
in "onnode -q any $CTDB status" "onnode -q any onnode all $CTDB scriptstatus" ; do
389 # Useful for superficially testing IP failover.
390 # IPs must be on the given node.
391 # If the first argument is '!' then the IPs must not be on the given node.
395 if [ "$1" = "!" ] ; then
396 negating
=true
; shift
398 local node
="$1" ; shift
403 all_ips_on_node
$node
406 for check
in $ips ; do
408 while read ip pnn
; do
409 if [ "$check" = "$ip" ] ; then
410 if [ "$pnn" = "$node" ] ; then
411 if $negating ; then return 1 ; fi
413 if ! $negating ; then return 1 ; fi
415 ips
="${ips/${ip}}" # Remove from list
418 # If we're negating and we didn't see the address then it
419 # isn't hosted by anyone!
421 ips
="${ips/${check}}"
423 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
426 ips
="${ips// }" # Remove any spaces.
430 wait_until_ips_are_on_node
()
432 # Go to some trouble to print a use description of what is happening
434 if [ "$1" == "!" ] ; then
441 [ "$i" != "!" ] ||
continue
442 if [ -z "$node" ] ; then
446 ips
="${ips}${ips:+, }${i}"
448 echo "Waiting for ${ips} to ${not}be assigned to node ${node}"
450 wait_until
60 ips_are_on_node
"$@"
459 all_ips_on_node
$node
461 while read ip pnn
; do
462 if [ "$node" = "$pnn" ] ; then
465 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
470 wait_until_node_has_some_ips
()
472 echo "Waiting for some IPs to be assigned to node ${test_node}"
474 wait_until
60 node_has_some_ips
"$@"
477 #######################################
483 if [ -e /etc
/redhat-release
] ; then
486 /etc
/init.d
/ctdb
"$cmd"
490 # Restart CTDB on all nodes. Override for local daemons.
493 onnode
-p all
$CTDB_TEST_WRAPPER _service_ctdb restart
496 # Nothing needed for a cluster. Override for local daemons.
504 onnode
"$1" $CTDB_TEST_WRAPPER _service_ctdb start
509 onnode
"$1" $CTDB_TEST_WRAPPER _service_ctdb stop
514 onnode
"$1" $CTDB_TEST_WRAPPER _service_ctdb restart
519 echo -n "Restarting CTDB"
520 if $ctdb_test_restart_scheduled ; then
521 echo -n " (scheduled)"
526 for i
in $
(seq 1 5) ; do
527 _restart_ctdb_all ||
{
528 echo "Restart failed. Trying again in a few seconds..."
533 wait_until_ready ||
{
534 echo "Cluster didn't become ready. Restarting..."
538 echo "Setting RerecoveryTimeout to 1"
539 onnode
-pq all
"$CTDB setvar RerecoveryTimeout 1"
541 # In recent versions of CTDB, forcing a recovery like this
542 # blocks until the recovery is complete. Hopefully this will
543 # help the cluster to stabilise before a subsequent test.
544 echo "Forcing a recovery..."
545 onnode
-q 0 $CTDB recover
548 if ! onnode
-q any
$CTDB_TEST_WRAPPER _cluster_is_recovered
; then
549 echo "Cluster has gone into recovery again, waiting..."
550 wait_until
30/2 onnode
-q any
$CTDB_TEST_WRAPPER _cluster_is_recovered
554 # Cluster is still healthy. Good, we're done!
555 if ! onnode
0 $CTDB_TEST_WRAPPER _cluster_is_healthy
; then
556 echo "Cluster became UNHEALTHY again [$(date)]"
557 onnode
-p all ctdb status
-X 2>&1
558 onnode
-p all ctdb scriptstatus
2>&1
563 echo "Doing a sync..."
564 onnode
-q 0 $CTDB sync
570 echo "Cluster UNHEALTHY... too many attempts..."
571 onnode
-p all ctdb status
-X 2>&1
572 onnode
-p all ctdb scriptstatus
2>&1
574 # Try to make the calling test fail
579 # Does nothing on cluster and should be overridden for local daemons
585 ctdb_restart_when_done
()
587 ctdb_test_restart_scheduled
=true
590 get_ctdbd_command_line_option
()
595 try_command_on_node
"$pnn" "$CTDB getpid" || \
596 die
"Unable to get PID of ctdbd on node $pnn"
598 local pid
="${out#*:}"
599 try_command_on_node
"$pnn" "ps -p $pid -o args hww" || \
600 die
"Unable to get command-line of PID $pid"
602 # Strip everything up to and including --option
603 local t
="${out#*--${option}}"
604 # Strip leading '=' or space if present
607 # Strip any following options and print
611 #######################################
613 wait_for_monitor_event
()
618 echo "Waiting for a monitor event on node ${pnn}..."
620 try_command_on_node
"$pnn" $CTDB scriptstatus ||
{
621 echo "Unable to get scriptstatus from node $pnn"
625 local ctdb_scriptstatus_original
="$out"
626 wait_until
120 _ctdb_scriptstatus_changed
629 _ctdb_scriptstatus_changed
()
631 try_command_on_node
"$pnn" $CTDB scriptstatus ||
{
632 echo "Unable to get scriptstatus from node $pnn"
636 [ "$out" != "$ctdb_scriptstatus_original" ]
639 #######################################
643 select_test_node_and_ips
645 nfs_first_export
=$
(showmount
-e $test_ip |
sed -n -e '2s/ .*//p')
647 echo "Creating test subdirectory..."
648 try_command_on_node
$test_node "mktemp -d --tmpdir=$nfs_first_export"
650 try_command_on_node
$test_node "chmod 777 $nfs_test_dir"
652 nfs_mnt_d
=$
(mktemp
-d)
653 nfs_local_file
="${nfs_mnt_d}/${nfs_test_dir##*/}/TEST_FILE"
654 nfs_remote_file
="${nfs_test_dir}/TEST_FILE"
656 ctdb_test_exit_hook_add nfs_test_cleanup
658 echo "Mounting ${test_ip}:${nfs_first_export} on ${nfs_mnt_d} ..."
659 mount
-o timeo
=1,hard
,intr
,vers
=3 \
660 "[${test_ip}]:${nfs_first_export}" ${nfs_mnt_d}
665 rm -f "$nfs_local_file"
666 umount -f "$nfs_mnt_d"
668 onnode -q $test_node rmdir "$nfs_test_dir"
671 #######################################
673 # If the given IP is hosted then print 2 items: maskbits and iface
679 *:*) _family="inet6
" ; _bits=128 ;;
680 *) _family="inet
" ; _bits=32 ;;
683 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
684 awk -v family="${_family}" \
685 'NR == 1 { iface = $2; sub(":$
", "", iface) } \
686 $1 ~ /inet/ { mask = $2; sub(".
*/", "", mask); \
687 print mask, iface, family }'
692 _addr="${1%/*}" # Remove optional maskbits
694 set -- $(ip_maskbits_iface $_addr)
695 if [ -n "$1" ] ; then
698 echo "Removing public address
$_addr/$_maskbits from device
$_iface"
699 ip addr del "$_ip/$_maskbits" dev "$_iface" >/dev/null 2>&1 || true
710 #######################################
712 # $1: pnn, $2: DB name
715 try_command_on_node -v $1 $CTDB getdbstatus "$2" |
716 sed -n -e "s@^path
: @@p
"
719 # $1: pnn, $2: DB name
720 db_ctdb_cattdb_count_records ()
722 try_command_on_node -v $1 $CTDB cattdb "$2" |
723 grep '^key' | grep -v '__db_sequence_number__' |
727 # $1: pnn, $2: DB name, $3: key string, $4: value string, $5: RSN (default 7)
730 _tdb=$(db_get_path $1 "$2")
732 try_command_on_node $1 $CTDB tstore "$_tdb" "$3" "$4" "$_rsn"
735 # $1: pnn, $2: DB name, $3: dbseqnum (must be < 255!!!!!)
736 db_ctdb_tstore_dbseqnum ()
738 # "__db_sequence_number__
" + trailing 0x00
739 _key='0x5f5f64625f73657175656e63655f6e756d6265725f5f00'
741 # Construct 8 byte (unit64_t) database sequence number. This
742 # probably breaks if $3 > 255
743 _value=$(printf "0x
%02x
%014x
" $3 0)
745 db_ctdb_tstore $1 "$2" "$_key" "$_value"
748 #######################################
750 # Make sure that $CTDB is set.
753 local="${TEST_SUBDIR}/scripts
/local.bash
"
754 if [ -r "$local" ] ; then