1 # Hey Emacs, this is a -*- shell-script -*- !!! :-)
3 # Print a message and exit.
4 die () { echo "$@" >&2 ; exit 1 ; }
6 # Augment PATH with relevant stubs/ directories. We do this by actually
7 # setting PATH, and also by setting $EVENTSCRIPTS_PATH and then
8 # prepending that to $PATH in rc.local to avoid the PATH reset in
13 if [ -d "${EVENTSCRIPTS_TESTS_DIR}/stubs" ] ; then
14 EVENTSCRIPTS_PATH="${EVENTSCRIPTS_TESTS_DIR}/stubs"
17 export EVENTSCRIPTS_TESTCASE_DIR=$(dirname "$0")
18 if [ $(basename "$EVENTSCRIPTS_TESTCASE_DIR") = "eventscripts" ] ; then
19 # Just a test script, no testcase subdirectory.
20 EVENTSCRIPTS_TESTCASE_DIR="$EVENTSCRIPTS_TESTS_DIR"
22 if [ -d "${EVENTSCRIPTS_TESTCASE_DIR}/stubs" ] ; then
23 EVENTSCRIPTS_PATH="${EVENTSCRIPTS_TESTCASE_DIR}/stubs:${EVENTSCRIPTS_PATH}"
27 export EVENTSCRIPTS_PATH
29 PATH="${EVENTSCRIPTS_PATH}:${PATH}"
31 if [ -d "${EVENTSCRIPTS_TESTCASE_DIR}/etc" ] ; then
32 CTDB_ETCDIR="${EVENTSCRIPTS_TESTCASE_DIR}/etc"
33 elif [ -d "${EVENTSCRIPTS_TESTS_DIR}/etc" ] ; then
34 CTDB_ETCDIR="${EVENTSCRIPTS_TESTS_DIR}/etc"
36 die "Unable to set \$CTDB_ETCDIR"
40 if [ -d "${EVENTSCRIPTS_TESTCASE_DIR}/etc-ctdb" ] ; then
41 CTDB_BASE="${EVENTSCRIPTS_TESTCASE_DIR}/etc-ctdb"
42 elif [ -d "${EVENTSCRIPTS_TESTCASE_DIR}/etc/ctdb" ] ; then
43 CTDB_BASE="${EVENTSCRIPTS_TESTCASE_DIR}/etc/ctdb"
44 elif [ -d "${EVENTSCRIPTS_TESTS_DIR}/etc-ctdb" ] ; then
45 CTDB_BASE="${EVENTSCRIPTS_TESTS_DIR}/etc-ctdb"
47 die "Unable to set \$CTDB_BASE"
51 export EVENTSCRIPTS_TESTS_VAR_DIR="${EVENTSCRIPTS_TESTS_DIR}/var"
52 if [ "$EVENTSCRIPTS_TESTS_VAR_DIR" != "/var" ] ; then
53 rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR"
55 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
56 export CTDB_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb"
58 ######################################################################
60 if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] ; then
61 debug () { echo "$@" ; }
66 eventscripts_tests_cleanup_hooks=""
69 eventscripts_test_add_cleanup ()
71 eventscripts_tests_cleanup_hooks="${eventscripts_tests_cleanup_hooks}${eventscripts_tests_cleanup_hooks:+ ; }$*"
74 trap 'eval $eventscripts_tests_cleanup_hooks' 0
77 ######################################################################
79 # General setup fakery
83 debug "Setting up shares (3 existing shares)"
84 # Create 3 fake shares/exports.
86 for i in $(seq 1 3) ; do
87 _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/${i}_existing"
89 FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
92 export FAKE_PROC_NET_BONDING="$EVENTSCRIPTS_TESTS_VAR_DIR/proc-net-bonding"
93 mkdir -p "$FAKE_PROC_NET_BONDING"
94 rm -f "$FAKE_PROC_NET_BONDING"/*
96 export FAKE_ETHTOOL_LINK_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/ethtool-link-down"
97 mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
98 rm -f "$FAKE_ETHTOOL_LINK_DOWN"/*
100 # This can only have 2 levels. We don't want to resort to usings
101 # something dangerous like "rm -r" setup time.
102 export FAKE_IP_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ip-state"
103 mkdir -p "$FAKE_IP_STATE"
104 rm -f "$FAKE_IP_STATE"/*/*
105 rm -f "$FAKE_IP_STATE"/* 2>/dev/null || true
106 rmdir "$FAKE_IP_STATE"/* 2>/dev/null || true
112 debug "Marking TCP port \"${_i}\" as not listening"
113 FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
121 # Replace some shares with non-existent ones.
126 export MISSING_SHARES_TEXT=""
127 for _i in $FAKE_SHARES ; do
128 if [ $_n = "$1" ] ; then
130 _i="${_i%_existing}_missing"
131 debug "Replacing share $_n with missing share \"$_i\""
132 rmdir "$_i" 2>/dev/null || true
133 MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}")
135 _t="${_t}${_t:+ }${_i}"
141 # Setup some fake /proc/net/bonding files with just enough info for
144 # arg1 is interface name, arg2 is currently active slave (use "None"
145 # if none), arg3 is MII status ("up" or "down").
149 _slave="${2:-${_iface}_sl_0}"
151 _mii_subs="${4:-${_mii_s:-up}}"
152 echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s"
153 cat >"${FAKE_PROC_NET_BONDING}/$_iface" <<EOF
154 Bonding Mode: IEEE 802.3ad Dynamic link aggregation
155 Currently Active Slave: $_slave
158 # Status of 1st pretend adapter
159 MII Status: $_mii_subs
160 # Status of 2nd pretend adapter
161 MII Status: $_mii_subs
165 ethtool_interfaces_down ()
168 echo "Marking interface $_i DOWN for ethtool"
169 touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
173 ethtool_interfaces_up ()
176 echo "Marking interface $_i UP for ethtool"
177 rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
181 setup_nmap_output_filter ()
183 OUT_FILTER="-e 's@^(DEBUG: # Nmap 5.21 scan initiated) .+ (as:)@\1 DATE \2@' -e 's@^(DEBUG: # Nmap done at) .+ (--)@\1 DATE \2@'"
186 ######################################################################
190 # Evaluate an expression that probably calls functions or uses
191 # variables from the CTDB functions file. This is used for test
196 . "$CTDB_BASE/functions"
201 # Set output for ctdb command. Option 1st argument is return code.
204 _out="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.out"
207 _rc="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.rc"
208 echo "${1:-0}" >"$_rc"
210 eventscripts_test_add_cleanup "rm -f $_out $_rc"
217 export FAKE_CTDB_NUMNODES="${1:-3}"
218 echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
220 export FAKE_CTDB_PNN="${2:-0}"
221 echo "Setting up CTDB with PNN ${FAKE_CTDB_PNN}"
223 if [ -n "$3" ] ; then
224 echo "Setting up CTDB_PUBLIC_ADDRESSES: $3"
225 export CTDB_PUBLIC_ADDRESSES=$(mktemp)
229 echo "${_ip} ${_ifaces}" >>"$CTDB_PUBLIC_ADDRESSES"
231 eventscripts_test_add_cleanup "rm -f $CTDB_PUBLIC_ADDRESSES"
234 export FAKE_CTDB_IFACES_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb/ifaces-down"
235 mkdir -p "$FAKE_CTDB_IFACES_DOWN"
236 rm -f "$FAKE_CTDB_IFACES_DOWN"/*
238 export FAKE_CTDB_NODES_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/nodes-down"
239 mkdir -p "$FAKE_CTDB_NODES_DOWN"
240 rm -f "$FAKE_CTDB_NODES_DOWN"/*
242 export FAKE_CTDB_SCRIPTSTATUS="$EVENTSCRIPTS_TESTS_VAR_DIR/scriptstatus"
243 mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
244 rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
252 rm -f "${FAKE_CTDB_NODES_DOWN}/${_i}"
259 touch "${FAKE_CTDB_NODES_DOWN}/${_i}"
263 ctdb_get_interfaces ()
265 # The echo/subshell forces all the output onto 1 line.
266 echo $(ctdb ifaces -Y | awk -F: 'FNR > 1 {print $2}')
269 ctdb_get_1_interface ()
271 _t=$(ctdb_get_interfaces)
275 ctdb_get_public_addresses ()
277 _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
278 echo $(if [ -r "$_f" ] ; then
279 while read _ip _iface ; do
280 echo "${_ip}@${_iface}"
285 # Prints the 1st public address as: interface IP maskbits
286 # This is suitable for passing to 10.interfaces takeip/releaseip
287 ctdb_get_1_public_address ()
289 _addrs=$(ctdb_get_public_addresses)
290 echo "${_addrs%% *}" | sed -r -e 's#(.*)/(.*)@(.*)#\3 \1 \2#g'
293 ctdb_not_implemented ()
295 export CTDB_NOT_IMPLEMENTED="$1"
296 ctdb_not_implemented="\
297 DEBUG: ctdb: command \"$1\" not implemented in stub"
300 ctdb_fake_scriptstatus ()
309 echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
312 ######################################################################
320 if [ "$1" != "down" ] ; then
322 debug "Marking Samba services as up, listening and managed by CTDB"
323 # Get into known state.
324 for i in "samba" "winbind" ; do
325 eventscript_call ctdb_service_managed "$i"
327 # All possible service names for all known distros.
328 for i in "smb" "nmb" "winbind" "samba" ; do
329 service "$i" force-started
332 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
333 export CTDB_MANAGED_SERVICES="foo samba winbind bar"
335 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
336 export FAKE_WBINFO_FAIL="no"
338 # Some things in 50.samba are backgrounded and waited for. If
339 # we don't sleep at all then timeouts can happen. This avoids
341 export FAKE_SLEEP_FORCE=0.1
343 debug "Marking Samba services as down, not listening and not managed by CTDB"
344 # Get into known state.
345 for i in "samba" "winbind" ; do
346 eventscript_call ctdb_service_unmanaged "$i"
348 # All possible service names for all known distros.
349 for i in "smb" "nmb" "winbind" "samba" ; do
350 service "$i" force-stopped
353 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
354 export CTDB_MANAGED_SERVICES="foo bar"
355 unset CTDB_MANAGES_SAMBA
356 unset CTDB_MANAGES_WINBIND
358 export FAKE_TCP_LISTEN=""
359 export FAKE_WBINFO_FAIL="yes"
362 # This is ugly but if this file isn't removed before each test
363 # then configuration changes between tests don't stick.
364 rm -f "$CTDB_VARDIR/state/samba/smb.conf.cache"
369 debug "Making wbinfo commands fail"
370 FAKE_WBINFO_FAIL="yes"
373 ######################################################################
381 export FAKE_RPCINFO_SERVICES=""
383 export CTDB_NFS_SKIP_SHARE_CHECK="no"
384 export CTDB_NFS_SKIP_KNFSD_ALIVE_CHECK="no"
386 # Reset the failcounts for nfs services.
387 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
389 rpc_fail_limits_file="${EVENTSCRIPTS_TESTS_VAR_DIR}/rpc_fail_limits"
391 # Force this file to exist so tests can be individually run.
392 if [ ! -f "$rpc_fail_limits_file" ] ; then
393 # This is gross... but is needed to fake through the nfs monitor event.
394 eventscript_call ctdb_service_managed "nfs"
395 service "nfs" force-started # might not be enough
396 CTDB_RC_LOCAL="$CTDB_BASE/rc.local.nfs.monitor.get-limits" \
397 CTDB_MANAGES_NFS="yes" \
398 "${CTDB_BASE}/events.d/60.nfs" "monitor" >"$rpc_fail_limits_file"
401 if [ "$1" != "down" ] ; then
402 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
404 eventscript_call ctdb_service_managed "nfs"
405 service "nfs" force-started # might not be enough
407 export CTDB_MANAGED_SERVICES="foo nfs bar"
409 rpc_services_up "nfs" "mountd" "rquotad" "nlockmgr" "status"
411 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
413 eventscript_call ctdb_service_unmanaged "nfs"
414 service "nfs" force-stopped # might not be enough
415 eventscript_call startstop_nfs stop
417 export CTDB_MANAGED_SERVICES="foo bar"
418 unset CTDB_MANAGES_NFS
425 debug "Marking RPC service \"${_i}\" as unavailable"
426 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
433 debug "Marking RPC service \"${_i}\" as available"
438 nlockmgr) _t="3:4" ;;
440 *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
443 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
447 # Set the required result for a particular RPC program having failed
448 # for a certain number of iterations. This is probably still a work
449 # in progress. Note that we could hook aggressively
450 # nfs_check_rpc_service() to try to implement this but we're better
451 # off testing nfs_check_rpc_service() using independent code... even
452 # if it is incomplete and hacky. So, if the 60.nfs eventscript
453 # changes and the tests start to fail then it may be due to this
454 # function being incomplete.
455 rpc_set_service_failure_response ()
458 # The number of failures defaults to the iteration number. This
459 # will be true when we fail from the 1st iteration... but we need
460 # the flexibility to set the number of failures.
461 _numfails="${2:-${iteration}}"
463 _c="${CTDB_ETCDIR}/sysconfig/nfs"
464 if [ -r "$_c" ] ; then
468 # A handy newline. :-)
475 _ts=$(sed -n -e "s@^${_progname} @@p" "$rpc_fail_limits_file")
477 while [ -n "$_ts" ] ; do
478 # Get the triple: operator, fail limit and actions.
479 _op="${_ts%% *}" ; _ts="${_ts#* }"
480 _li="${_ts%% *}" ; _ts="${_ts#* }"
481 # We've lost some of the quoting but we can simulate
482 # because we know an operator is always the first in a
485 while [ -n "$_ts" ] ; do
486 # If this is an operator then we've got all of the
492 _actions="${_actions}${_actions:+ }${_ts%% *}"
493 # Special case for end of list.
494 if [ "$_ts" != "${_ts#* }" ] ; then
501 if [ "$_numfails" "$_op" "$_li" ] ; then
504 for _action in $_actions ; do
510 knfsd) _ver=3 ; _pn="nfs" ;;
511 lockd) _ver=4 ; _pn="nlockmgr" ;;
512 statd) _pn="status" ;;
515 ERROR: $_pn failed RPC check:
516 rpcinfo: RPC: Program not registered
517 program $_pn version $_ver is not available"
521 _p="rpc.${_progname}"
524 _opts="${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
527 case "${_progname}${_action#restart}" in
530 Trying to restart NFS service
535 _t="Trying to restart NFS service"
539 Trying to restart lock manager service
541 Starting nfslock: OK"
544 _t="Trying to restart lock manager service"
547 _t="Trying to restart $_progname [${_p}${_opts}]"
549 _out="${_out}${_out:+${_nl}}${_t}"
555 required_result $_rc "$_out"
561 ######################################################################
567 if [ "$1" != "down" ] ; then
568 die "setup_vsftpd up not implemented!!!"
570 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
572 eventscript_call ctdb_service_unmanaged vsftpd
573 service vsftpd force-stopped
575 export CTDB_MANAGED_SERVICES="foo"
576 unset CTDB_MANAGES_VSFTPD
580 ######################################################################
586 if [ "$1" != "down" ] ; then
587 die "setup_httpd up not implemented!!!"
589 debug "Setting up HTTPD environment: service down, not managed by CTDB"
591 for i in "apache2" "httpd" ; do
592 eventscript_call ctdb_service_unmanaged "$i"
593 service "$i" force-stopped
596 export CTDB_MANAGED_SERVICES="foo"
597 unset CTDB_MANAGES_HTTPD
601 ######################################################################
603 # Result and test functions
605 # Set some globals and print the summary.
611 _f="${_f#./}" # strip leading ./
612 _f="${_f#simple/}" # strip leading simple/
613 _f="${_f#multievent/}" # strip leading multievent/
614 _f="${_f%%/*}" # if subdir, strip off file
615 _f="${_f%.sh}" # strip off .sh suffix if any
617 # Remaining format should be NN.service.event.NNN or NN.service.NNN:
630 die "Internal error - unknown testcase filename format"
633 printf "%-14s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
636 # Set the required result for a test.
637 # - Argument 1 is exit code.
638 # - Argument 2, if present is the required test output but "--"
639 # indicates empty output.
640 # If argument 2 is not present or null then read required test output
644 required_rc="${1:-0}"
645 if [ -n "$2" ] ; then
646 if [ "$2" = "--" ] ; then
653 required_output=$(cat)
662 required_result 0 "$@"
677 if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
678 if [ -n "$_iteration" ] ; then
681 ==================================================
682 Iteration $_iteration
687 --------------------------------------------------
688 Output (Exit status: ${_rc}):
689 --------------------------------------------------
696 --------------------------------------------------
697 Required output (Exit status: ${required_rc}):
698 --------------------------------------------------
708 if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
711 --------------------------------------------------
712 CTDB_BASE="$CTDB_BASE"
713 CTDB_ETCDIR="$CTDB_ETCDIR"
714 ctdb client is "$(which ctdb)"
715 --------------------------------------------------
729 # Run an eventscript once. The test passes if the return code and
730 # output match those required.
732 # Any args are passed to the eventscript.
734 # Eventscript tracing can be done by setting:
735 # EVENTSCRIPTS_TESTS_TRACE="sh -x"
737 # or similar. This will almost certainly make a test fail but is
738 # useful for debugging.
741 [ -n "$event" ] || die 'simple_test: $event not set'
743 echo "Running \"$script $event${1:+ }$*\""
744 _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
747 if [ -n "$OUT_FILTER" ] ; then
748 _fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
753 if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
759 result_print "$_passed" "$_out" "$_rc"
760 result_footer "$_passed"
765 # If something has previously failed then don't continue.
770 echo "##################################################"
774 # Run an eventscript iteratively.
775 # - 1st argument is the number of iterations.
776 # - 2nd argument is something to eval to do setup for every iteration.
777 # The easiest thing to do here is to define a function and pass it
779 # - Subsequent arguments come in pairs: an iteration number and
780 # something to eval for that iteration. Each time an iteration
781 # number is matched the associated argument is given to eval after
782 # the default setup is done. The iteration numbers need to be given
783 # in ascending order.
785 # Some optional args can be given *before* these, surrounded by extra
786 # "--" args. These args are passed to the eventscript. Quoting is
789 # One use of the 2nd and further arguments is to call
790 # required_result() to change what is expected of a particular
794 [ -n "$event" ] || die 'simple_test: $event not set'
797 if [ "$1" = "--" ] ; then
799 while [ "$1" != "--" ] ; do
800 args="${args}${args:+ }$1"
810 echo "Running $_repeats iterations of \"$script $event\" $args"
814 for iteration in $(seq 1 $_repeats) ; do
815 # This is inefficient because the iteration-specific setup
816 # might completely replace the default one. However, running
817 # the default is good because it allows you to revert to a
818 # particular result without needing to specify it explicitly.
820 if [ $iteration = "$1" ] ; then
825 _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" $args 2>&1)
828 if [ -n "$OUT_FILTER" ] ; then
829 _fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
834 if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
841 result_print "$_passed" "$_out" "$_rc" "$iteration"
844 result_footer "$_result"