1 # Hey Emacs, this is a -*- shell-script -*- !!! :-)
3 # Augment PATH with relevant stubs/ directories. We do this by actually
4 # setting PATH, and also by setting $EVENTSCRIPTS_PATH and then
5 # prepending that to $PATH in rc.local to avoid the PATH reset in
10 if [ -d "${EVENTSCRIPTS_TESTS_DIR}/stubs" ] ; then
11 EVENTSCRIPTS_PATH="${EVENTSCRIPTS_TESTS_DIR}/stubs"
14 export EVENTSCRIPTS_TESTCASE_DIR=$(dirname "$0")
15 if [ $(basename "$EVENTSCRIPTS_TESTCASE_DIR") = "eventscripts" ] ; then
16 # Just a test script, no testcase subdirectory.
17 EVENTSCRIPTS_TESTCASE_DIR="$EVENTSCRIPTS_TESTS_DIR"
19 if [ -d "${EVENTSCRIPTS_TESTCASE_DIR}/stubs" ] ; then
20 EVENTSCRIPTS_PATH="${EVENTSCRIPTS_TESTCASE_DIR}/stubs:${EVENTSCRIPTS_PATH}"
24 export EVENTSCRIPTS_PATH
26 PATH="${EVENTSCRIPTS_PATH}:${PATH}"
28 if [ -d "${EVENTSCRIPTS_TESTCASE_DIR}/etc" ] ; then
29 CTDB_ETCDIR="${EVENTSCRIPTS_TESTCASE_DIR}/etc"
30 elif [ -d "${EVENTSCRIPTS_TESTS_DIR}/etc" ] ; then
31 CTDB_ETCDIR="${EVENTSCRIPTS_TESTS_DIR}/etc"
33 echo "Unable to set \$CTDB_ETCDIR" >&2
38 if [ -d "${EVENTSCRIPTS_TESTCASE_DIR}/etc-ctdb" ] ; then
39 CTDB_BASE="${EVENTSCRIPTS_TESTCASE_DIR}/etc-ctdb"
40 elif [ -d "${EVENTSCRIPTS_TESTCASE_DIR}/etc/ctdb" ] ; then
41 CTDB_BASE="${EVENTSCRIPTS_TESTCASE_DIR}/etc/ctdb"
42 elif [ -d "${EVENTSCRIPTS_TESTS_DIR}/etc-ctdb" ] ; then
43 CTDB_BASE="${EVENTSCRIPTS_TESTS_DIR}/etc-ctdb"
45 echo "Unable to set \$CTDB_BASE" >&2
50 export EVENTSCRIPTS_TESTS_VAR_DIR="${EVENTSCRIPTS_TESTS_DIR}/var"
51 if [ "$EVENTSCRIPTS_TESTS_VAR_DIR" != "/var" ] ; then
52 rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR"
54 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
55 export CTDB_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb"
57 ######################################################################
59 if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] ; then
60 debug () { echo "$@" ; }
65 eventscripts_tests_cleanup_hooks=""
68 eventscripts_test_add_cleanup ()
70 eventscripts_tests_cleanup_hooks="${eventscripts_tests_cleanup_hooks}${eventscripts_tests_cleanup_hooks:+ ; }$*"
73 trap 'eval $eventscripts_tests_cleanup_hooks' 0
76 ######################################################################
78 # General setup fakery
82 debug "Setting up shares (3 existing shares)"
83 # Create 3 fake shares/exports.
85 for i in $(seq 1 3) ; do
86 _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/${i}_existing"
88 FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
91 export FAKE_PROC_NET_BONDING="$EVENTSCRIPTS_TESTS_VAR_DIR/proc-net-bonding"
92 mkdir -p "$FAKE_PROC_NET_BONDING"
93 rm -f "$FAKE_PROC_NET_BONDING"/*
95 export FAKE_ETHTOOL_LINK_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/ethtool-link-down"
96 mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
97 rm -f "$FAKE_ETHTOOL_LINK_DOWN"/*
99 # This can only have 2 levels. We don't want to resort to usings
100 # something dangerous like "rm -r" setup time.
101 export FAKE_IP_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ip-state"
102 mkdir -p "$FAKE_IP_STATE"
103 rm -f "$FAKE_IP_STATE"/*/*
104 rm -f "$FAKE_IP_STATE"/* 2>/dev/null || true
105 rmdir "$FAKE_IP_STATE"/* 2>/dev/null || true
111 debug "Marking TCP port \"${_i}\" as not listening"
112 FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
120 # Replace some shares with non-existent ones.
125 export MISSING_SHARES_TEXT=""
126 for _i in $FAKE_SHARES ; do
127 if [ $_n = "$1" ] ; then
129 _i="${_i%_existing}_missing"
130 debug "Replacing share $_n with missing share \"$_i\""
131 rmdir "$_i" 2>/dev/null || true
132 MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}")
134 _t="${_t}${_t:+ }${_i}"
140 # Setup some fake /proc/net/bonding files with just enough info for
143 # arg1 is interface name, arg2 is currently active slave (use "None"
144 # if none), arg3 is MII status ("up" or "down").
148 _slave="${2:-${_iface}_sl_0}"
150 _mii_subs="${4:-${_mii_s:-up}}"
151 echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s"
152 cat >"${FAKE_PROC_NET_BONDING}/$_iface" <<EOF
153 Bonding Mode: IEEE 802.3ad Dynamic link aggregation
154 Currently Active Slave: $_slave
157 # Status of 1st pretend adapter
158 MII Status: $_mii_subs
159 # Status of 2nd pretend adapter
160 MII Status: $_mii_subs
164 ethtool_interfaces_down ()
167 echo "Marking interface $_i DOWN for ethtool"
168 touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
172 ethtool_interfaces_up ()
175 echo "Marking interface $_i UP for ethtool"
176 rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
180 ######################################################################
184 # Evaluate an expression that probably calls functions or uses
185 # variables from the CTDB functions file. This is used for test
190 . "$CTDB_BASE/functions"
195 # Set output for ctdb command. Option 1st argument is return code.
198 _out="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.out"
201 _rc="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.rc"
202 echo "${1:-0}" >"$_rc"
204 eventscripts_test_add_cleanup "rm -f $_out $_rc"
211 export FAKE_CTDB_NUMNODES="${1:-3}"
212 echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
214 export FAKE_CTDB_PNN="${2:-0}"
215 echo "Setting up CTDB with PNN ${FAKE_CTDB_PNN}"
217 if [ -n "$3" ] ; then
218 echo "Setting up CTDB_PUBLIC_ADDRESSES: $3"
219 export CTDB_PUBLIC_ADDRESSES=$(mktemp)
223 echo "${_ip} ${_ifaces}" >>"$CTDB_PUBLIC_ADDRESSES"
225 eventscripts_test_add_cleanup "rm -f $CTDB_PUBLIC_ADDRESSES"
228 export FAKE_CTDB_IFACES_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb/ifaces-down"
229 mkdir -p "$FAKE_CTDB_IFACES_DOWN"
230 rm -f "$FAKE_CTDB_IFACES_DOWN"/*
232 export FAKE_CTDB_NODES_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/nodes-down"
233 mkdir -p "$FAKE_CTDB_NODES_DOWN"
234 rm -f "$FAKE_CTDB_NODES_DOWN"/*
242 rm -f "${FAKE_CTDB_NODES_DOWN}/${_i}"
249 touch "${FAKE_CTDB_NODES_DOWN}/${_i}"
253 ctdb_get_interfaces ()
255 # The echo/subshell forces all the output onto 1 line.
256 echo $(ctdb ifaces -Y | awk -F: 'FNR > 1 {print $2}')
259 ctdb_get_1_interface ()
261 _t=$(ctdb_get_interfaces)
265 ctdb_get_public_addresses ()
267 _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
268 echo $(if [ -r "$_f" ] ; then
269 while read _ip _iface ; do
270 echo "${_ip}@${_iface}"
275 # Prints the 1st public address as: interface IP maskbits
276 # This is suitable for passing to 10.interfaces takeip/releaseip
277 ctdb_get_1_public_address ()
279 _addrs=$(ctdb_get_public_addresses)
280 echo "${_addrs%% *}" | sed -r -e 's#(.*)/(.*)@(.*)#\3 \1 \2#g'
283 ######################################################################
291 if [ "$1" != "down" ] ; then
293 debug "Marking Samba services as up, listening and managed by CTDB"
294 # Get into known state.
295 for i in "samba" "winbind" ; do
296 eventscript_call ctdb_service_managed "$i"
298 # All possible service names for all known distros.
299 for i in "smb" "nmb" "winbind" "samba" ; do
300 service "$i" force-started
303 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
304 export CTDB_MANAGED_SERVICES="foo samba winbind bar"
306 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
307 export FAKE_WBINFO_FAIL="no"
309 # Some things in 50.samba are backgrounded and waited for. If
310 # we don't sleep at all then timeouts can happen. This avoids
312 export FAKE_SLEEP_FORCE=0.1
314 debug "Marking Samba services as down, not listening and not managed by CTDB"
315 # Get into known state.
316 for i in "samba" "winbind" ; do
317 eventscript_call ctdb_service_unmanaged "$i"
319 # All possible service names for all known distros.
320 for i in "smb" "nmb" "winbind" "samba" ; do
321 service "$i" force-stopped
324 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
325 export CTDB_MANAGED_SERVICES="foo bar"
326 unset CTDB_MANAGES_SAMBA
327 unset CTDB_MANAGES_WINBIND
329 export FAKE_TCP_LISTEN=""
330 export FAKE_WBINFO_FAIL="yes"
333 # This is ugly but if this file isn't removed before each test
334 # then configuration changes between tests don't stick.
335 rm -f "$CTDB_VARDIR/state/samba/smb.conf.cache"
340 debug "Making wbinfo commands fail"
341 FAKE_WBINFO_FAIL="yes"
344 ######################################################################
352 export FAKE_RPCINFO_SERVICES=""
354 export CTDB_NFS_SKIP_SHARE_CHECK="no"
355 export CTDB_NFS_SKIP_KNFSD_ALIVE_CHECK="no"
357 # Reset the failcounts for nfs services.
358 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
360 rpc_fail_limits_file="${EVENTSCRIPTS_TESTS_VAR_DIR}/rpc_fail_limits"
362 # Force this file to exist so tests can be individually run.
363 if [ ! -f "$rpc_fail_limits_file" ] ; then
364 # This is gross... but is needed to fake through the nfs monitor event.
365 eventscript_call ctdb_service_managed "nfs"
366 service "nfs" force-started # might not be enough
367 CTDB_RC_LOCAL="$CTDB_BASE/rc.local.nfs.monitor.get-limits" \
368 CTDB_MANAGES_NFS="yes" \
369 "${CTDB_BASE}/events.d/60.nfs" "monitor" >"$rpc_fail_limits_file"
372 if [ "$1" != "down" ] ; then
373 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
375 eventscript_call ctdb_service_managed "nfs"
376 service "nfs" force-started # might not be enough
378 export CTDB_MANAGED_SERVICES="foo nfs bar"
380 rpc_services_up "nfs" "mountd" "rquotad" "nlockmgr" "status"
382 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
384 eventscript_call ctdb_service_unmanaged "nfs"
385 service "nfs" force-stopped # might not be enough
386 eventscript_call startstop_nfs stop
388 export CTDB_MANAGED_SERVICES="foo bar"
389 unset CTDB_MANAGES_NFS
396 debug "Marking RPC service \"${_i}\" as unavailable"
397 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
404 debug "Marking RPC service \"${_i}\" as available"
409 nlockmgr) _t="3:4" ;;
412 echo "Internal error - unsupported RPC service \"${_i}\""
416 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
420 # Set the required result for a particular RPC program having failed
421 # for a certain number of iterations. This is probably still a work
422 # in progress. Note that we could hook aggressively
423 # nfs_check_rpc_service() to try to implement this but we're better
424 # off testing nfs_check_rpc_service() using independent code... even
425 # if it is incomplete and hacky. So, if the 60.nfs eventscript
426 # changes and the tests start to fail then it may be due to this
427 # function being incomplete.
428 rpc_set_service_failure_response ()
431 # The number of failures defaults to the iteration number. This
432 # will be true when we fail from the 1st iteration... but we need
433 # the flexibility to set the number of failures.
434 _numfails="${2:-${iteration}}"
436 _c="${CTDB_ETCDIR}/sysconfig/nfs"
437 if [ -r "$_c" ] ; then
441 # A handy newline. :-)
448 _ts=$(sed -n -e "s@^${_progname} @@p" "$rpc_fail_limits_file")
450 while [ -n "$_ts" ] ; do
451 # Get the triple: operator, fail limit and actions.
452 _op="${_ts%% *}" ; _ts="${_ts#* }"
453 _li="${_ts%% *}" ; _ts="${_ts#* }"
454 # We've lost some of the quoting but we can simulate
455 # because we know an operator is always the first in a
458 while [ -n "$_ts" ] ; do
459 # If this is an operator then we've got all of the
465 _actions="${_actions}${_actions:+ }${_ts%% *}"
466 # Special case for end of list.
467 if [ "$_ts" != "${_ts#* }" ] ; then
474 if [ "$_numfails" "$_op" "$_li" ] ; then
477 for _action in $_actions ; do
483 knfsd) _ver=3 ; _pn="nfs" ;;
484 lockd) _ver=4 ; _pn="nlockmgr" ;;
485 statd) _pn="status" ;;
488 ERROR: $_pn failed RPC check:
489 rpcinfo: RPC: Program not registered
490 program $_pn version $_ver is not available"
494 _p="rpc.${_progname}"
497 _opts="${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
500 case "${_progname}${_action#restart}" in
503 Trying to restart NFS service
508 _t="Trying to restart NFS service"
512 Trying to restart lock manager service
514 Starting nfslock: OK"
517 _t="Trying to restart lock manager service"
520 _t="Trying to restart $_progname [${_p}${_opts}]"
522 _out="${_out}${_out:+${_nl}}${_t}"
528 required_result $_rc "$_out"
534 ######################################################################
540 if [ "$1" != "down" ] ; then
541 echo "setup_vsftpd up not implemented!!!"
544 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
546 eventscript_call ctdb_service_unmanaged vsftpd
547 service vsftpd force-stopped
549 export CTDB_MANAGED_SERVICES="foo"
550 unset CTDB_MANAGES_VSFTPD
554 ######################################################################
560 if [ "$1" != "down" ] ; then
561 echo "setup_httpd up not implemented!!!"
564 debug "Setting up HTTPD environment: service down, not managed by CTDB"
566 for i in "apache2" "httpd" ; do
567 eventscript_call ctdb_service_unmanaged "$i"
568 service "$i" force-stopped
571 export CTDB_MANAGED_SERVICES="foo"
572 unset CTDB_MANAGES_HTTPD
576 ######################################################################
578 # Result and test functions
580 # Set some globals and print the summary.
586 _f="${_f#./}" # strip leading ./
587 _f="${_f#simple/}" # strip leading simple/
588 _f="${_f%%/*}" # if subdir, strip off file
589 _f="${_f%.sh}" # strip off .sh suffix if any
591 # Remaining format should be NN.service.event.NNN:
597 printf "%-14s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
600 # Set the required result for a test.
601 # - Argument 1 is exit code.
602 # - Argument 2, if present is the required test output but "--"
603 # indicates empty output.
604 # If argument 2 is not present or null then read required test output
608 required_rc="${1:-0}"
609 if [ -n "$2" ] ; then
610 if [ "$2" = "--" ] ; then
617 required_output=$(cat)
626 required_result 0 "$@"
641 if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
642 if [ -n "$_iteration" ] ; then
645 ##################################################
646 ##################################################
647 Iteration $_iteration
652 ##################################################
653 Output (Exit status: ${_rc}):
654 ##################################################
661 ##################################################
662 Required output (Exit status: ${required_rc}):
663 ##################################################
673 if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
676 ##################################################
677 CTDB_BASE="$CTDB_BASE"
678 CTDB_ETCDIR="$CTDB_ETCDIR"
679 ctdb client is "$(which ctdb)"
680 ##################################################
695 # Run an eventscript once. The test passes if the return code and
696 # output match those required.
698 # Any args are passed to the eventscript.
700 # Eventscript tracing can be done by setting:
701 # EVENTSCRIPTS_TESTS_TRACE="sh -x"
703 # or similar. This will almost certainly make a test fail but is
704 # useful for debugging.
707 echo "Running \"${CTDB_BASE}/events.d/$script $event\""
708 _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
711 if [ "$_out" = "$required_output" -a $_rc = $required_rc ] ; then
717 result_print "$_passed" "$_out" "$_rc"
718 result_footer "$_passed"
721 # Run an eventscript iteratively.
722 # - 1st argument is the number of iterations.
723 # - 2nd argument is something to eval to do setup for every iteration.
724 # The easiest thing to do here is to define a function and pass it
726 # - Subsequent arguments come in pairs: an iteration number and
727 # something to eval for that iteration. Each time an iteration
728 # number is matched the associated argument is given to eval after
729 # the default setup is done. The iteration numbers need to be given
730 # in ascending order.
732 # Some optional args can be given *before* these, surrounded by extra
733 # "--" args. These args are passed to the eventscript. Quoting is
736 # One use of the 2nd and further arguments is to call
737 # required_result() to change what is expected of a particular
742 if [ "$1" = "--" ] ; then
744 while [ "$1" != "--" ] ; do
745 args="${args}${args:+ }$1"
755 echo "Running $_repeats iterations of \"${CTDB_BASE}/events.d/$script $event\" $args"
759 for iteration in $(seq 1 $_repeats) ; do
760 # This is inefficient because the iteration-specific setup
761 # might completely replace the default one. However, running
762 # the default is good because it allows you to revert to a
763 # particular result without needing to specify it explicitly.
765 if [ $iteration = "$1" ] ; then
770 _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" $args 2>&1)
773 if [ "$_out" = "$required_output" -a $_rc = $required_rc ] ; then
780 result_print "$_passed" "$_out" "$_rc" "$iteration"
783 result_footer "$_result"