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"/*
248 rm -f "${FAKE_CTDB_NODES_DOWN}/${_i}"
255 touch "${FAKE_CTDB_NODES_DOWN}/${_i}"
259 ctdb_get_interfaces ()
261 # The echo/subshell forces all the output onto 1 line.
262 echo $(ctdb ifaces -Y | awk -F: 'FNR > 1 {print $2}')
265 ctdb_get_1_interface ()
267 _t=$(ctdb_get_interfaces)
271 ctdb_get_public_addresses ()
273 _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
274 echo $(if [ -r "$_f" ] ; then
275 while read _ip _iface ; do
276 echo "${_ip}@${_iface}"
281 # Prints the 1st public address as: interface IP maskbits
282 # This is suitable for passing to 10.interfaces takeip/releaseip
283 ctdb_get_1_public_address ()
285 _addrs=$(ctdb_get_public_addresses)
286 echo "${_addrs%% *}" | sed -r -e 's#(.*)/(.*)@(.*)#\3 \1 \2#g'
289 ctdb_not_implemented ()
291 export CTDB_NOT_IMPLEMENTED="$1"
292 ctdb_not_implemented="\
293 DEBUG: ctdb: command \"$1\" not implemented
294 DEBUG: Usage: ctdb [-Y] cmd
296 DEBUG: A fake CTDB stub that prints items depending on the variables
297 DEBUG: FAKE_CTDB_PNN (default 0) depending on command-line options.
299 DEBUG: Note that -Y is ignored."
302 ######################################################################
310 if [ "$1" != "down" ] ; then
312 debug "Marking Samba services as up, listening and managed by CTDB"
313 # Get into known state.
314 for i in "samba" "winbind" ; do
315 eventscript_call ctdb_service_managed "$i"
317 # All possible service names for all known distros.
318 for i in "smb" "nmb" "winbind" "samba" ; do
319 service "$i" force-started
322 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
323 export CTDB_MANAGED_SERVICES="foo samba winbind bar"
325 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
326 export FAKE_WBINFO_FAIL="no"
328 # Some things in 50.samba are backgrounded and waited for. If
329 # we don't sleep at all then timeouts can happen. This avoids
331 export FAKE_SLEEP_FORCE=0.1
333 debug "Marking Samba services as down, not listening and not managed by CTDB"
334 # Get into known state.
335 for i in "samba" "winbind" ; do
336 eventscript_call ctdb_service_unmanaged "$i"
338 # All possible service names for all known distros.
339 for i in "smb" "nmb" "winbind" "samba" ; do
340 service "$i" force-stopped
343 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
344 export CTDB_MANAGED_SERVICES="foo bar"
345 unset CTDB_MANAGES_SAMBA
346 unset CTDB_MANAGES_WINBIND
348 export FAKE_TCP_LISTEN=""
349 export FAKE_WBINFO_FAIL="yes"
352 # This is ugly but if this file isn't removed before each test
353 # then configuration changes between tests don't stick.
354 rm -f "$CTDB_VARDIR/state/samba/smb.conf.cache"
359 debug "Making wbinfo commands fail"
360 FAKE_WBINFO_FAIL="yes"
363 ######################################################################
371 export FAKE_RPCINFO_SERVICES=""
373 export CTDB_NFS_SKIP_SHARE_CHECK="no"
374 export CTDB_NFS_SKIP_KNFSD_ALIVE_CHECK="no"
376 # Reset the failcounts for nfs services.
377 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
379 rpc_fail_limits_file="${EVENTSCRIPTS_TESTS_VAR_DIR}/rpc_fail_limits"
381 # Force this file to exist so tests can be individually run.
382 if [ ! -f "$rpc_fail_limits_file" ] ; then
383 # This is gross... but is needed to fake through the nfs monitor event.
384 eventscript_call ctdb_service_managed "nfs"
385 service "nfs" force-started # might not be enough
386 CTDB_RC_LOCAL="$CTDB_BASE/rc.local.nfs.monitor.get-limits" \
387 CTDB_MANAGES_NFS="yes" \
388 "${CTDB_BASE}/events.d/60.nfs" "monitor" >"$rpc_fail_limits_file"
391 if [ "$1" != "down" ] ; then
392 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
394 eventscript_call ctdb_service_managed "nfs"
395 service "nfs" force-started # might not be enough
397 export CTDB_MANAGED_SERVICES="foo nfs bar"
399 rpc_services_up "nfs" "mountd" "rquotad" "nlockmgr" "status"
401 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
403 eventscript_call ctdb_service_unmanaged "nfs"
404 service "nfs" force-stopped # might not be enough
405 eventscript_call startstop_nfs stop
407 export CTDB_MANAGED_SERVICES="foo bar"
408 unset CTDB_MANAGES_NFS
415 debug "Marking RPC service \"${_i}\" as unavailable"
416 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
423 debug "Marking RPC service \"${_i}\" as available"
428 nlockmgr) _t="3:4" ;;
430 *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
433 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
437 # Set the required result for a particular RPC program having failed
438 # for a certain number of iterations. This is probably still a work
439 # in progress. Note that we could hook aggressively
440 # nfs_check_rpc_service() to try to implement this but we're better
441 # off testing nfs_check_rpc_service() using independent code... even
442 # if it is incomplete and hacky. So, if the 60.nfs eventscript
443 # changes and the tests start to fail then it may be due to this
444 # function being incomplete.
445 rpc_set_service_failure_response ()
448 # The number of failures defaults to the iteration number. This
449 # will be true when we fail from the 1st iteration... but we need
450 # the flexibility to set the number of failures.
451 _numfails="${2:-${iteration}}"
453 _c="${CTDB_ETCDIR}/sysconfig/nfs"
454 if [ -r "$_c" ] ; then
458 # A handy newline. :-)
465 _ts=$(sed -n -e "s@^${_progname} @@p" "$rpc_fail_limits_file")
467 while [ -n "$_ts" ] ; do
468 # Get the triple: operator, fail limit and actions.
469 _op="${_ts%% *}" ; _ts="${_ts#* }"
470 _li="${_ts%% *}" ; _ts="${_ts#* }"
471 # We've lost some of the quoting but we can simulate
472 # because we know an operator is always the first in a
475 while [ -n "$_ts" ] ; do
476 # If this is an operator then we've got all of the
482 _actions="${_actions}${_actions:+ }${_ts%% *}"
483 # Special case for end of list.
484 if [ "$_ts" != "${_ts#* }" ] ; then
491 if [ "$_numfails" "$_op" "$_li" ] ; then
494 for _action in $_actions ; do
500 knfsd) _ver=3 ; _pn="nfs" ;;
501 lockd) _ver=4 ; _pn="nlockmgr" ;;
502 statd) _pn="status" ;;
505 ERROR: $_pn failed RPC check:
506 rpcinfo: RPC: Program not registered
507 program $_pn version $_ver is not available"
511 _p="rpc.${_progname}"
514 _opts="${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
517 case "${_progname}${_action#restart}" in
520 Trying to restart NFS service
525 _t="Trying to restart NFS service"
529 Trying to restart lock manager service
531 Starting nfslock: OK"
534 _t="Trying to restart lock manager service"
537 _t="Trying to restart $_progname [${_p}${_opts}]"
539 _out="${_out}${_out:+${_nl}}${_t}"
545 required_result $_rc "$_out"
551 ######################################################################
557 if [ "$1" != "down" ] ; then
558 die "setup_vsftpd up not implemented!!!"
560 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
562 eventscript_call ctdb_service_unmanaged vsftpd
563 service vsftpd force-stopped
565 export CTDB_MANAGED_SERVICES="foo"
566 unset CTDB_MANAGES_VSFTPD
570 ######################################################################
576 if [ "$1" != "down" ] ; then
577 die "setup_httpd up not implemented!!!"
579 debug "Setting up HTTPD environment: service down, not managed by CTDB"
581 for i in "apache2" "httpd" ; do
582 eventscript_call ctdb_service_unmanaged "$i"
583 service "$i" force-stopped
586 export CTDB_MANAGED_SERVICES="foo"
587 unset CTDB_MANAGES_HTTPD
591 ######################################################################
593 # Result and test functions
595 # Set some globals and print the summary.
601 _f="${_f#./}" # strip leading ./
602 _f="${_f#simple/}" # strip leading simple/
603 _f="${_f#multievent/}" # strip leading multievent/
604 _f="${_f%%/*}" # if subdir, strip off file
605 _f="${_f%.sh}" # strip off .sh suffix if any
607 # Remaining format should be NN.service.event.NNN or NN.service.NNN:
620 die "Internal error - unknown testcase filename format"
623 printf "%-14s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
626 # Set the required result for a test.
627 # - Argument 1 is exit code.
628 # - Argument 2, if present is the required test output but "--"
629 # indicates empty output.
630 # If argument 2 is not present or null then read required test output
634 required_rc="${1:-0}"
635 if [ -n "$2" ] ; then
636 if [ "$2" = "--" ] ; then
643 required_output=$(cat)
652 required_result 0 "$@"
667 if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
668 if [ -n "$_iteration" ] ; then
671 ##################################################
672 ##################################################
673 Iteration $_iteration
678 ##################################################
679 Output (Exit status: ${_rc}):
680 ##################################################
687 ##################################################
688 Required output (Exit status: ${required_rc}):
689 ##################################################
699 if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
702 ##################################################
703 CTDB_BASE="$CTDB_BASE"
704 CTDB_ETCDIR="$CTDB_ETCDIR"
705 ctdb client is "$(which ctdb)"
706 ##################################################
721 # Run an eventscript once. The test passes if the return code and
722 # output match those required.
724 # Any args are passed to the eventscript.
726 # Eventscript tracing can be done by setting:
727 # EVENTSCRIPTS_TESTS_TRACE="sh -x"
729 # or similar. This will almost certainly make a test fail but is
730 # useful for debugging.
733 [ -n "$event" ] || die 'simple_test: $event not set'
735 echo "Running \"${CTDB_BASE}/events.d/$script $event\""
736 _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
739 if [ -n "$OUT_FILTER" ] ; then
740 _fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
745 if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
751 result_print "$_passed" "$_out" "$_rc"
752 result_footer "$_passed"
755 # Run an eventscript iteratively.
756 # - 1st argument is the number of iterations.
757 # - 2nd argument is something to eval to do setup for every iteration.
758 # The easiest thing to do here is to define a function and pass it
760 # - Subsequent arguments come in pairs: an iteration number and
761 # something to eval for that iteration. Each time an iteration
762 # number is matched the associated argument is given to eval after
763 # the default setup is done. The iteration numbers need to be given
764 # in ascending order.
766 # Some optional args can be given *before* these, surrounded by extra
767 # "--" args. These args are passed to the eventscript. Quoting is
770 # One use of the 2nd and further arguments is to call
771 # required_result() to change what is expected of a particular
775 [ -n "$event" ] || die 'simple_test: $event not set'
778 if [ "$1" = "--" ] ; then
780 while [ "$1" != "--" ] ; do
781 args="${args}${args:+ }$1"
791 echo "Running $_repeats iterations of \"${CTDB_BASE}/events.d/$script $event\" $args"
795 for iteration in $(seq 1 $_repeats) ; do
796 # This is inefficient because the iteration-specific setup
797 # might completely replace the default one. However, running
798 # the default is good because it allows you to revert to a
799 # particular result without needing to specify it explicitly.
801 if [ $iteration = "$1" ] ; then
806 _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" $args 2>&1)
809 if [ -n "$OUT_FILTER" ] ; then
810 _fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
815 if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
822 result_print "$_passed" "$_out" "$_rc" "$iteration"
825 result_footer "$_result"