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_NETSTAT_TCP_LISTEN=$(echo "$FAKE_NETSTAT_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_NETSTAT_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
307 export FAKE_WBINFO_FAIL="no"
309 debug "Marking Samba services as down, not listening and not managed by CTDB"
310 # Get into known state.
311 for i in "samba" "winbind" ; do
312 eventscript_call ctdb_service_unmanaged "$i"
314 # All possible service names for all known distros.
315 for i in "smb" "nmb" "winbind" "samba" ; do
316 service "$i" force-stopped
319 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
320 export CTDB_MANAGED_SERVICES="foo bar"
321 unset CTDB_MANAGES_SAMBA
322 unset CTDB_MANAGES_WINBIND
324 export FAKE_NETSTAT_TCP_LISTEN=""
325 export FAKE_WBINFO_FAIL="yes"
328 # This is ugly but if this file isn't removed before each test
329 # then configuration changes between tests don't stick.
330 rm -f "$CTDB_VARDIR/state/samba/smb.conf.cache"
335 debug "Making wbinfo commands fail"
336 FAKE_WBINFO_FAIL="yes"
339 ######################################################################
347 export FAKE_RPCINFO_SERVICES=""
349 export CTDB_NFS_SKIP_SHARE_CHECK="no"
350 export CTDB_NFS_SKIP_KNFSD_ALIVE_CHECK="no"
352 # Reset the failcounts for nfs services.
353 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
355 rpc_fail_limits_file="${EVENTSCRIPTS_TESTS_VAR_DIR}/rpc_fail_limits"
357 # Force this file to exist so tests can be individually run.
358 if [ ! -f "$rpc_fail_limits_file" ] ; then
359 # This is gross... but is needed to fake through the nfs monitor event.
360 eventscript_call ctdb_service_managed "nfs"
361 service "nfs" force-started # might not be enough
362 CTDB_RC_LOCAL="$CTDB_BASE/rc.local.nfs.monitor.get-limits" \
363 CTDB_MANAGES_NFS="yes" \
364 "${CTDB_BASE}/events.d/60.nfs" "monitor" >"$rpc_fail_limits_file"
367 if [ "$1" != "down" ] ; then
368 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
370 eventscript_call ctdb_service_managed "nfs"
371 service "nfs" force-started # might not be enough
373 export CTDB_MANAGED_SERVICES="foo nfs bar"
375 rpc_services_up "nfs" "mountd" "rquotad" "nlockmgr" "status"
377 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
379 eventscript_call ctdb_service_unmanaged "nfs"
380 service "nfs" force-stopped # might not be enough
381 eventscript_call startstop_nfs stop
383 export CTDB_MANAGED_SERVICES="foo bar"
384 unset CTDB_MANAGES_NFS
391 debug "Marking RPC service \"${_i}\" as unavailable"
392 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
399 debug "Marking RPC service \"${_i}\" as available"
404 nlockmgr) _t="3:4" ;;
407 echo "Internal error - unsupported RPC service \"${_i}\""
411 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
415 # Set the required result for a particular RPC program having failed
416 # for a certain number of iterations. This is probably still a work
417 # in progress. Note that we could hook aggressively
418 # nfs_check_rpc_service() to try to implement this but we're better
419 # off testing nfs_check_rpc_service() using independent code... even
420 # if it is incomplete and hacky. So, if the 60.nfs eventscript
421 # changes and the tests start to fail then it may be due to this
422 # function being incomplete.
423 rpc_set_service_failure_response ()
426 # The number of failures defaults to the iteration number. This
427 # will be true when we fail from the 1st iteration... but we need
428 # the flexibility to set the number of failures.
429 _numfails="${2:-${iteration}}"
431 _c="${CTDB_ETCDIR}/sysconfig/nfs"
432 if [ -r "$_c" ] ; then
436 # A handy newline. :-)
443 _ts=$(sed -n -e "s@^${_progname} @@p" "$rpc_fail_limits_file")
445 while [ -n "$_ts" ] ; do
446 # Get the triple: operator, fail limit and actions.
447 _op="${_ts%% *}" ; _ts="${_ts#* }"
448 _li="${_ts%% *}" ; _ts="${_ts#* }"
449 # We've lost some of the quoting but we can simulate
450 # because we know an operator is always the first in a
453 while [ -n "$_ts" ] ; do
454 # If this is an operator then we've got all of the
460 _actions="${_actions}${_actions:+ }${_ts%% *}"
461 # Special case for end of list.
462 if [ "$_ts" != "${_ts#* }" ] ; then
469 if [ "$_numfails" "$_op" "$_li" ] ; then
472 for _action in $_actions ; do
478 knfsd) _ver=3 ; _pn="nfs" ;;
479 lockd) _ver=4 ; _pn="nlockmgr" ;;
480 statd) _pn="status" ;;
483 ERROR: $_pn failed RPC check:
484 rpcinfo: RPC: Program not registered
485 program $_pn version $_ver is not available"
489 _p="rpc.${_progname}"
492 _opts="${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
495 case "${_progname}${_action#restart}" in
498 Trying to restart NFS service
503 _t="Trying to restart NFS service"
507 Trying to restart lock manager service
509 Starting nfslock: OK"
512 _t="Trying to restart lock manager service"
515 _t="Trying to restart $_progname [${_p}${_opts}]"
517 _out="${_out}${_out:+${_nl}}${_t}"
523 required_result $_rc "$_out"
529 ######################################################################
535 if [ "$1" != "down" ] ; then
536 echo "setup_vsftpd up not implemented!!!"
539 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
541 eventscript_call ctdb_service_unmanaged vsftpd
542 service vsftpd force-stopped
544 export CTDB_MANAGED_SERVICES="foo"
545 unset CTDB_MANAGES_VSFTPD
549 ######################################################################
555 if [ "$1" != "down" ] ; then
556 echo "setup_httpd up not implemented!!!"
559 debug "Setting up HTTPD environment: service down, not managed by CTDB"
561 for i in "apache2" "httpd" ; do
562 eventscript_call ctdb_service_unmanaged "$i"
563 service "$i" force-stopped
566 export CTDB_MANAGED_SERVICES="foo"
567 unset CTDB_MANAGES_HTTPD
571 ######################################################################
573 # Result and test functions
575 # Set some globals and print the summary.
581 _f="${_f#./}" # strip leading ./
582 _f="${_f#simple/}" # strip leading simple/
583 _f="${_f%%/*}" # if subdir, strip off file
584 _f="${_f%.sh}" # strip off .sh suffix if any
586 # Remaining format should be NN.service.event.NNN:
592 printf "%-14s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
595 # Set the required result for a test.
596 # - Argument 1 is exit code.
597 # - Argument 2, if present is the required test output but "--"
598 # indicates empty output.
599 # If argument 2 is not present or null then read required test output
603 required_rc="${1:-0}"
604 if [ -n "$2" ] ; then
605 if [ "$2" = "--" ] ; then
612 required_output=$(cat)
621 required_result 0 "$@"
636 if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
637 if [ -n "$_iteration" ] ; then
640 ##################################################
641 ##################################################
642 Iteration $_iteration
647 ##################################################
648 Output (Exit status: ${_rc}):
649 ##################################################
656 ##################################################
657 Required output (Exit status: ${required_rc}):
658 ##################################################
668 if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
671 ##################################################
672 CTDB_BASE="$CTDB_BASE"
673 CTDB_ETCDIR="$CTDB_ETCDIR"
674 ctdb client is "$(which ctdb)"
675 ##################################################
690 # Run an eventscript once. The test passes if the return code and
691 # output match those required.
693 # Any args are passed to the eventscript.
695 # Eventscript tracing can be done by setting:
696 # EVENTSCRIPTS_TESTS_TRACE="sh -x"
698 # or similar. This will almost certainly make a test fail but is
699 # useful for debugging.
702 echo "Running \"${CTDB_BASE}/events.d/$script $event\""
703 _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
706 if [ "$_out" = "$required_output" -a $_rc = $required_rc ] ; then
712 result_print "$_passed" "$_out" "$_rc"
713 result_footer "$_passed"
716 # Run an eventscript iteratively.
717 # - 1st argument is the number of iterations.
718 # - 2nd argument is something to eval to do setup for every iteration.
719 # The easiest thing to do here is to define a function and pass it
721 # - Subsequent arguments come in pairs: an iteration number and
722 # something to eval for that iteration. Each time an iteration
723 # number is matched the associated argument is given to eval after
724 # the default setup is done. The iteration numbers need to be given
725 # in ascending order.
727 # Some optional args can be given *before* these, surrounded by extra
728 # "--" args. These args are passed to the eventscript. Quoting is
731 # One use of the 2nd and further arguments is to call
732 # required_result() to change what is expected of a particular
737 if [ "$1" = "--" ] ; then
739 while [ "$1" != "--" ] ; do
740 args="${args}${args:+ }$1"
750 echo "Running $_repeats iterations of \"${CTDB_BASE}/events.d/$script $event\" $args"
754 for iteration in $(seq 1 $_repeats) ; do
755 # This is inefficient because the iteration-specific setup
756 # might completely replace the default one. However, running
757 # the default is good because it allows you to revert to a
758 # particular result without needing to specify it explicitly.
760 if [ $iteration = "$1" ] ; then
765 _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" $args 2>&1)
768 if [ "$_out" = "$required_output" -a $_rc = $required_rc ] ; then
775 result_print "$_passed" "$_out" "$_rc" "$iteration"
778 result_footer "$_result"