1 # Hey Emacs, this is a -*- shell-script -*- !!! :-)
9 ######################################################################
11 #. /root/SOFS/autosofs/scripts/tester.bash
17 teststarttime=$(date '+%s')
20 echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
21 echo "Running test $name ($(date '+%T'))"
22 echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
27 local name="$1" ; shift
28 local status="$1" ; shift
29 # "$@" is command-line
31 local interp="SKIPPED"
32 local statstr=" (reason $*)"
33 if [ -n "$status" ] ; then
34 if [ $status -eq 0 ] ; then
40 statstr=" (status $status)"
41 testfailures=$(($testfailures+1))
45 testduration=$(($(date +%s)-$teststarttime))
47 echo "=========================================================================="
48 echo "TEST ${interp}: ${name}${statstr} (duration: ${testduration}s)"
49 echo "=========================================================================="
55 exit $(($testfailures+0))
64 [ $(($testfailures+0)) -eq 0 -a $status -ne 0 ] && testfailures=$status
66 eval "$ctdb_test_exit_hook"
68 if ! onnode 0 $CTDB_TEST_WRAPPER cluster_is_healthy ; then
69 echo "Restarting ctdb on all nodes to get back into known state..."
78 local name="$1" ; shift
80 [ -n "$1" ] || set -- "$name"
82 ctdb_test_begin "$name"
87 ctdb_test_end "$name" "$status" "$*"
100 -h, --help show this screen.
101 -v, --version show test case version.
102 --category show the test category (ACL, CTDB, Samba ...).
103 -d, --description show test case description.
104 --summary show short test case summary.
112 [ -n "$CTDB_DIR" ] || fail "Can not determine version."
114 (cd "$CTDB_DIR" && git describe)
117 ctdb_test_cmd_options()
119 [ -n "$1" ] || return 0
122 -h|--help) ctdb_test_usage 0 ;;
123 -v|--version) ctdb_test_version ;;
124 --category) echo "CTDB" ;;
125 -d|--description) test_info ;;
126 --summary) test_info | head -1 ;;
128 echo "Error: Unknown parameter = $1"
139 scriptname=$(basename "$0")
142 ctdb_test_cmd_options $@
144 trap "ctdb_test_exit" 0
147 ########################################
150 try_command_on_node ()
152 local nodespec="$1" ; shift
155 if [ "$nodespec" = "-v" ] ; then
157 nodespec="$1" ; shift
161 out=$(onnode -q "$nodespec" "$cmd" 2>&1) || {
163 echo "Failed to execute \"$cmd\" on node(s) \"$nodespec\""
169 echo "Output of \"$cmd\":"
174 sanity_check_output ()
177 local regexp="$2" # Should be anchored as necessary.
182 local num_lines=$(echo "$output" | wc -l)
183 echo "There are $num_lines lines of output"
184 if [ $num_lines -lt $min_lines ] ; then
185 echo "BAD: that's less than the required number (${min_lines})"
190 local unexpected # local doesn't pass through status of command on RHS.
191 unexpected=$(echo "$output" | egrep -v "$regexp") || status=$?
193 # Note that this is reversed.
194 if [ $status -eq 0 ] ; then
195 echo "BAD: unexpected lines in output:"
196 echo "$unexpected" | cat -A
199 echo "Output lines look OK"
205 #######################################
207 # Wait until either timeout expires or command succeeds. The command
208 # will be tried once per second.
211 local timeout="$1" ; shift # "$@" is the command...
213 echo -n "<${timeout}|"
214 while [ $timeout -gt 0 ] ; do
221 timeout=$(($timeout - 1))
233 for i in $(seq 1 $1) ; do
240 _cluster_is_healthy ()
242 local out x count line
244 out=$(ctdb -Y status 2>&1) || return 1
250 count=$(($count + 1))
251 [ "${line#:*:*:}" != "0:0:0:0:" ] && return 1
253 [ $count -gt 0 ] && return $?
254 } <<<"$out" # Yay bash!
257 cluster_is_healthy ()
259 if _cluster_is_healthy ; then
260 echo "Cluster is HEALTHY"
263 echo "Cluster is UNHEALTHY"
268 wait_until_healthy ()
270 local timeout="${1:-120}"
272 echo "Waiting for cluster to become healthy..."
274 wait_until 120 _cluster_is_healthy
277 # This function is becoming nicely overloaded. Soon it will collapse! :-)
285 (unhealthy) bits="?:?:?:1" ;;
286 (healthy) bits="?:?:?:0" ;;
287 (disconnected) bits="1:?:?:?" ;;
288 (connected) bits="0:?:?:?" ;;
289 (banned) bits="?:1:?:?" ;;
290 (unbanned) bits="?:0:?:?" ;;
291 (disabled) bits="?:?:1:?" ;;
292 (enabled) bits="?:?:0:?" ;;
293 (frozen) fpat='^[[:space:]]+frozen[[:space:]]+1$' ;;
294 (unfrozen) fpat='^[[:space:]]+frozen[[:space:]]+0$' ;;
295 (monon) mpat='^Monitoring mode:ACTIVE \(0\)$' ;;
296 (monoff) mpat='^Monitoring mode:DISABLED \(1\)$' ;;
298 echo "node_has_status: unknown status \"$status\""
302 if [ -n "$bits" ] ; then
305 out=$(ctdb -Y status 2>&1) || return 1
310 [ "${line#:${pnn}:*:${bits}:}" = "" ] && return 0
313 } <<<"$out" # Yay bash!
314 elif [ -n "$fpat" ] ; then
315 ctdb statistics -n "$pnn" | egrep -q "$fpat"
316 elif [ -n "$mpat" ] ; then
317 ctdb getmonmode -n "$pnn" | egrep -q "$mpat"
319 echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
324 wait_until_node_has_status ()
328 local timeout="${3:-30}"
330 echo "Waiting until node $pnn has status \"$status\"..."
332 wait_until $timeout node_has_status "$pnn" "$status"
335 # Useful for superficially testing IP failover.
336 # IPs must be on nodes matching nodeglob.
337 ips_are_on_nodeglob ()
339 local nodeglob="$1" ; shift
344 try_command_on_node 1 ctdb ip -n all
346 while read ip pnn ; do
347 for check in $ips ; do
348 if [ "$check" = "$ip" ] ; then
353 ips="${ips/${ip}}" # Remove from list
356 done <<<"$out" # bashism to avoid problem setting variable in pipeline.
358 ips="${ips// }" # Remove any spaces.
362 wait_until_ips_are_on_nodeglob ()
364 echo "Waiting for IPs to fail over..."
366 wait_until 60 ips_are_on_nodeglob "$@"
369 #######################################
373 echo "Attempting to politely shutdown daemons..."
374 onnode 1 ctdb shutdown -n all || true
376 echo "Sleeping for a while..."
379 if pidof $CTDB_DIR/bin/ctdbd >/dev/null ; then
380 echo "Killing remaining daemons..."
381 killall $CTDB_DIR/bin/ctdbd
383 if pidof $CTDB_DIR/bin/ctdbd >/dev/null ; then
384 echo "Once more with feeling.."
385 killall -9 $CTDB_DIR/bin/ctdbd
389 var_dir=$CTDB_DIR/tests/var
390 rm -rf $var_dir/test.db
395 local num_nodes="${1:-2}" # default is 2 nodes
397 local var_dir=$CTDB_DIR/tests/var
399 mkdir -p $var_dir/test.db/persistent
401 local nodes=$var_dir/nodes.txt
402 local public_addresses=$var_dir/public_addresses.txt
403 local no_public_addresses=$var_dir/no_public_addresses.txt
404 rm -f $nodes $public_addresses $no_public_addresses
406 # If there are (strictly) greater than 2 nodes then we'll randomly
407 # choose a node to have no public addresses.
408 local no_public_ips=-1
409 [ $num_nodes -gt 2 ] && no_public_ips=$(($RANDOM % $num_nodes))
410 echo "$no_public_ips" >$no_public_addresses
413 for i in $(seq 1 $num_nodes) ; do
414 if [ "${CTDB_USE_IPV6}x" != "x" ]; then
416 ip addr add ::$i/128 dev lo
418 echo 127.0.0.$i >> $nodes
419 # 2 public addresses on most nodes, just to make things interesting.
420 if [ $(($i - 1)) -ne $no_public_ips ] ; then
421 echo "192.0.2.$i/24 lo" >> $public_addresses
422 echo "192.0.2.$(($i + $num_nodes))/24 lo" >> $public_addresses
430 local num_nodes="${1:-2}" # default is 2 nodes
431 shift # "$@" gets passed to ctdbd
433 local var_dir=$CTDB_DIR/tests/var
435 local nodes=$var_dir/nodes.txt
436 local public_addresses=$var_dir/public_addresses.txt
437 local no_public_addresses=$var_dir/no_public_addresses.txt
439 local no_public_ips=-1
440 [ -r $no_public_addresses ] && read no_public_ips <$no_public_addresses
442 local ctdb_options="--reclock=$var_dir/rec.lock --nlist $nodes --nopublicipcheck --event-script-dir=tests/events.d --logfile=$var_dir/daemons.log -d 0 --dbdir=$var_dir/test.db --dbdir-persistent=$var_dir/test.db/persistent"
444 echo "Starting $num_nodes ctdb daemons..."
445 if [ "$no_public_ips" != -1 ] ; then
446 echo "Node $no_public_ips will have no public IPs."
449 for i in $(seq 0 $(($num_nodes - 1))) ; do
450 if [ $(id -u) -eq 0 ]; then
451 ctdb_options="$ctdb_options --public-interface=lo"
454 if [ $i -eq $no_public_ips ] ; then
455 ctdb_options="$ctdb_options --public-addresses=/dev/null"
457 ctdb_options="$ctdb_options --public-addresses=$public_addresses"
460 $VALGRIND bin/ctdbd --socket=$var_dir/sock.$i $ctdb_options "$@" || return 1
463 if [ -L /tmp/ctdb.socket -o ! -S /tmp/ctdb.socket ] ; then
464 ln -sf $var_dir/sock.0 /tmp/ctdb.socket || return 1
468 #######################################
472 if [ -e /etc/redhat-release ] ; then
475 /etc/init.d/ctdb restart
481 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
482 daemons_setup $CTDB_TEST_NUM_DAEMONS
488 if [ -n "$CTDB_NODES_SOCKETS" ] ; then
490 daemons_start $CTDB_TEST_NUM_DAEMONS
492 onnode -pq all $CTDB_TEST_WRAPPER _restart_ctdb
495 onnode -q 1 $CTDB_TEST_WRAPPER wait_until_healthy || return 1
497 echo "Setting RerecoveryTimeout to 1"
498 onnode -pq all "ctdb setvar RerecoveryTimeout 1"
500 # In recent versions of CTDB, forcing a recovery like this blocks
501 # until the recovery is complete. Hopefully this will help the
502 # cluster to stabilise before a subsequent test.
503 echo "Forcing a recovery..."
504 onnode -q 0 ctdb recover
506 #echo "Sleeping to allow ctdb to settle..."
512 #######################################
514 install_eventscript ()
516 local script_name="$1"
517 local script_contents="$2"
519 if [ -n "$CTDB_TEST_REAL_CLUSTER" ] ; then
520 # The quoting here is *very* fragile. However, we do
521 # experience the joy of installing a short script using
522 # onnode, and without needing to know the IP addresses of the
524 onnode all "f=\"\${CTDB_BASE:-/etc/ctdb}/events.d/${script_name}\" ; echo \"Installing \$f\" ; echo '${script_contents}' > \"\$f\" ; chmod 755 \"\$f\""
526 f="${CTDB_DIR}/tests/events.d/${script_name}"
527 echo "$script_contents" >"$f"
532 uninstall_eventscript ()
534 local script_name="$1"
536 if [ -n "$CTDB_TEST_REAL_CLUSTER" ] ; then
537 onnode all "rm -vf \"\${CTDB_BASE:-/etc/ctdb}/events.d/${script_name}\""
539 rm -vf "${CTDB_DIR}/tests/events.d/${script_name}"