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 "${TEST_SUBDIR}/stubs" ] ; then
11 EVENTSCRIPTS_PATH="${TEST_SUBDIR}/stubs"
14 export EVENTSCRIPTS_PATH
16 PATH="${EVENTSCRIPTS_PATH}:${PATH}"
18 if [ -d "${TEST_SUBDIR}/etc" ] ; then
19 CTDB_ETCDIR="${TEST_SUBDIR}/etc"
21 die "Unable to set \$CTDB_ETCDIR"
25 if [ -d "${TEST_SUBDIR}/etc-ctdb" ] ; then
26 CTDB_BASE="${TEST_SUBDIR}/etc-ctdb"
28 die "Unable to set \$CTDB_BASE"
32 export EVENTSCRIPTS_TESTS_VAR_DIR="${TEST_VAR_DIR}/unit_eventscripts"
33 if [ "$EVENTSCRIPTS_TESTS_VAR_DIR" != "/unit_eventscripts" ] ; then
34 rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR"
36 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
37 export CTDB_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb"
39 ######################################################################
41 if "$TEST_VERBOSE" ; then
42 debug () { echo "$@" ; }
47 eventscripts_tests_cleanup_hooks=""
50 eventscripts_test_add_cleanup ()
52 eventscripts_tests_cleanup_hooks="${eventscripts_tests_cleanup_hooks}${eventscripts_tests_cleanup_hooks:+ ; }$*"
55 trap 'eval $eventscripts_tests_cleanup_hooks' 0
58 ######################################################################
60 # General setup fakery
64 debug "Setting up shares (3 existing shares)"
65 # Create 3 fake shares/exports.
67 for i in $(seq 1 3) ; do
68 _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/${i}_existing"
70 FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
73 export FAKE_PROC_NET_BONDING="$EVENTSCRIPTS_TESTS_VAR_DIR/proc-net-bonding"
74 mkdir -p "$FAKE_PROC_NET_BONDING"
75 rm -f "$FAKE_PROC_NET_BONDING"/*
77 export FAKE_ETHTOOL_LINK_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/ethtool-link-down"
78 mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
79 rm -f "$FAKE_ETHTOOL_LINK_DOWN"/*
81 # This can only have 2 levels. We don't want to resort to usings
82 # something dangerous like "rm -r" setup time.
83 export FAKE_IP_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ip-state"
84 mkdir -p "$FAKE_IP_STATE"
85 rm -f "$FAKE_IP_STATE"/*/*
86 rm -f "$FAKE_IP_STATE"/* 2>/dev/null || true
87 rmdir "$FAKE_IP_STATE"/* 2>/dev/null || true
93 debug "Marking TCP port \"${_i}\" as not listening"
94 FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
102 # Replace some shares with non-existent ones.
107 export MISSING_SHARES_TEXT=""
108 for _i in $FAKE_SHARES ; do
109 if [ $_n = "$1" ] ; then
111 _i="${_i%_existing}_missing"
112 debug "Replacing share $_n with missing share \"$_i\""
113 rmdir "$_i" 2>/dev/null || true
114 MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}")
116 _t="${_t}${_t:+ }${_i}"
122 # Setup some fake /proc/net/bonding files with just enough info for
125 # arg1 is interface name, arg2 is currently active slave (use "None"
126 # if none), arg3 is MII status ("up" or "down").
130 _slave="${2:-${_iface}_sl_0}"
132 _mii_subs="${4:-${_mii_s:-up}}"
133 echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s"
134 cat >"${FAKE_PROC_NET_BONDING}/$_iface" <<EOF
135 Bonding Mode: IEEE 802.3ad Dynamic link aggregation
136 Currently Active Slave: $_slave
139 # Status of 1st pretend adapter
140 MII Status: $_mii_subs
141 # Status of 2nd pretend adapter
142 MII Status: $_mii_subs
146 ethtool_interfaces_down ()
149 echo "Marking interface $_i DOWN for ethtool"
150 touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
154 ethtool_interfaces_up ()
157 echo "Marking interface $_i UP for ethtool"
158 rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
162 setup_nmap_output_filter ()
164 OUT_FILTER="-e 's@^(DEBUG: # Nmap 5.21 scan initiated) .+ (as:)@\1 DATE \2@' -e 's@^(DEBUG: # Nmap done at) .+ (--)@\1 DATE \2@'"
169 echo "# ip rule show"
173 while read _p _x _i _x _t ; do
174 # Remove trailing colon after priority/preference.
176 # Only remove rules that match our priority/preference.
177 [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
179 echo "# ip route show table $_t"
180 ip route show table "$_t"
184 # Copied from 13.per_ip_routing for now... so this is lazy testing :-(
185 ipv4_host_addr_to_net ()
190 # Convert the host address to an unsigned long by splitting out
191 # the octets and doing the math.
193 for _o in $(export IFS="." ; echo $_host) ; do
194 _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
197 # Calculate the mask and apply it.
198 _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
199 _net_ul=$(( $_host_ul & $_mask_ul ))
201 # Now convert to a network address one byte at a time.
203 for _o in $(seq 1 4) ; do
204 _net="$(($_net_ul & 255))${_net:+.}${_net}"
205 _net_ul=$(($_net_ul >> 8))
208 echo "${_net}/${_maskbits}"
211 ######################################################################
215 # Evaluate an expression that probably calls functions or uses
216 # variables from the CTDB functions file. This is used for test
221 . "$CTDB_BASE/functions"
226 # Set output for ctdb command. Option 1st argument is return code.
229 _out="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.out"
232 _rc="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.rc"
233 echo "${1:-0}" >"$_rc"
235 eventscripts_test_add_cleanup "rm -f $_out $_rc"
242 export FAKE_CTDB_NUMNODES="${1:-3}"
243 echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
245 export FAKE_CTDB_PNN="${2:-0}"
246 echo "Setting up CTDB with PNN ${FAKE_CTDB_PNN}"
248 if [ -n "$3" ] ; then
249 echo "Setting up CTDB_PUBLIC_ADDRESSES: $3"
250 export CTDB_PUBLIC_ADDRESSES=$(mktemp)
254 echo "${_ip} ${_ifaces}" >>"$CTDB_PUBLIC_ADDRESSES"
256 eventscripts_test_add_cleanup "rm -f $CTDB_PUBLIC_ADDRESSES"
259 export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
261 export FAKE_CTDB_IFACES_DOWN="$FAKE_CTDB_STATE/ifaces-down"
262 mkdir -p "$FAKE_CTDB_IFACES_DOWN"
263 rm -f "$FAKE_CTDB_IFACES_DOWN"/*
265 export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus"
266 mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
267 rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
270 ctdb_get_interfaces ()
272 # The echo/subshell forces all the output onto 1 line.
273 echo $(ctdb ifaces -Y | awk -F: 'FNR > 1 {print $2}')
276 ctdb_get_1_interface ()
278 _t=$(ctdb_get_interfaces)
282 # Print all public addresses as: interface IP maskbits
283 # Each line is suitable for passing to takeip/releaseip
284 ctdb_get_all_public_addresses ()
286 _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
287 while IFS="/$IFS" read _ip _maskbits _ifaces ; do
288 echo "$_ifaces $_ip $_maskbits"
292 # Print public addresses on this node as: interface IP maskbits
293 # Each line is suitable for passing to takeip/releaseip
294 ctdb_get_my_public_addresses ()
297 read _x # skip header line
299 while IFS=":" read _x _ip _x _iface _x ; do
300 [ -n "$_iface" ] || continue
301 while IFS="/$IFS" read _i _maskbits _x ; do
302 if [ "$_ip" = "$_i" ] ; then
303 echo $_iface $_ip $_maskbits
306 done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
311 # Prints the 1st public address as: interface IP maskbits
312 # This is suitable for passing to takeip/releaseip
313 ctdb_get_1_public_address ()
315 ctdb_get_my_public_addresses | head -n 1
318 ctdb_not_implemented ()
320 export CTDB_NOT_IMPLEMENTED="$1"
321 ctdb_not_implemented="\
322 DEBUG: ctdb: command \"$1\" not implemented in stub"
325 ctdb_fake_scriptstatus ()
334 echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
337 setup_ctdb_policy_routing ()
339 export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing"
340 export CTDB_PER_IP_ROUTING_RULE_PREF=100
341 export CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000
342 export CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000
344 # Tests need to create and populate this file
345 rm -f "$CTDB_PER_IP_ROUTING_CONF"
348 ######################################################################
356 if [ "$1" != "down" ] ; then
358 debug "Marking Samba services as up, listening and managed by CTDB"
359 # Get into known state.
360 for i in "samba" "winbind" ; do
361 eventscript_call ctdb_service_managed "$i"
363 # All possible service names for all known distros.
364 for i in "smb" "nmb" "winbind" "samba" ; do
365 service "$i" force-started
368 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
369 export CTDB_MANAGED_SERVICES="foo samba winbind bar"
371 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
372 export FAKE_WBINFO_FAIL="no"
374 # Some things in 50.samba are backgrounded and waited for. If
375 # we don't sleep at all then timeouts can happen. This avoids
377 export FAKE_SLEEP_FORCE=0.1
379 debug "Marking Samba services as down, not listening and not managed by CTDB"
380 # Get into known state.
381 for i in "samba" "winbind" ; do
382 eventscript_call ctdb_service_unmanaged "$i"
384 # All possible service names for all known distros.
385 for i in "smb" "nmb" "winbind" "samba" ; do
386 service "$i" force-stopped
389 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
390 export CTDB_MANAGED_SERVICES="foo bar"
391 unset CTDB_MANAGES_SAMBA
392 unset CTDB_MANAGES_WINBIND
394 export FAKE_TCP_LISTEN=""
395 export FAKE_WBINFO_FAIL="yes"
398 # This is ugly but if this file isn't removed before each test
399 # then configuration changes between tests don't stick.
400 rm -f "$CTDB_VARDIR/state/samba/smb.conf.cache"
405 debug "Making wbinfo commands fail"
406 FAKE_WBINFO_FAIL="yes"
409 ######################################################################
417 export FAKE_RPCINFO_SERVICES=""
419 export CTDB_NFS_SKIP_SHARE_CHECK="no"
420 export CTDB_NFS_SKIP_KNFSD_ALIVE_CHECK="no"
422 # Reset the failcounts for nfs services.
423 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
425 rpc_fail_limits_file="${EVENTSCRIPTS_TESTS_VAR_DIR}/rpc_fail_limits"
427 # Force this file to exist so tests can be individually run.
428 if [ ! -f "$rpc_fail_limits_file" ] ; then
429 # This is gross... but is needed to fake through the nfs monitor event.
430 eventscript_call ctdb_service_managed "nfs"
431 service "nfs" force-started # might not be enough
432 CTDB_RC_LOCAL="$CTDB_BASE/rc.local.nfs.monitor.get-limits" \
433 CTDB_MANAGES_NFS="yes" \
434 "${CTDB_BASE}/events.d/60.nfs" "monitor" >"$rpc_fail_limits_file"
437 if [ "$1" != "down" ] ; then
438 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
440 eventscript_call ctdb_service_managed "nfs"
441 service "nfs" force-started # might not be enough
443 export CTDB_MANAGED_SERVICES="foo nfs bar"
445 rpc_services_up "nfs" "mountd" "rquotad" "nlockmgr" "status"
447 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
449 eventscript_call ctdb_service_unmanaged "nfs"
450 service "nfs" force-stopped # might not be enough
451 eventscript_call startstop_nfs stop
453 export CTDB_MANAGED_SERVICES="foo bar"
454 unset CTDB_MANAGES_NFS
461 debug "Marking RPC service \"${_i}\" as unavailable"
462 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
469 debug "Marking RPC service \"${_i}\" as available"
474 nlockmgr) _t="3:4" ;;
476 *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
479 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
483 # Set the required result for a particular RPC program having failed
484 # for a certain number of iterations. This is probably still a work
485 # in progress. Note that we could hook aggressively
486 # nfs_check_rpc_service() to try to implement this but we're better
487 # off testing nfs_check_rpc_service() using independent code... even
488 # if it is incomplete and hacky. So, if the 60.nfs eventscript
489 # changes and the tests start to fail then it may be due to this
490 # function being incomplete.
491 rpc_set_service_failure_response ()
494 # The number of failures defaults to the iteration number. This
495 # will be true when we fail from the 1st iteration... but we need
496 # the flexibility to set the number of failures.
497 _numfails="${2:-${iteration}}"
499 _etc="$CTDB_ETCDIR" # shortcut for readability
500 for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
501 if [ -r "$_c" ] ; then
507 # A handy newline. :-)
514 _ts=$(sed -n -e "s@^${_progname} @@p" "$rpc_fail_limits_file")
516 while [ -n "$_ts" ] ; do
517 # Get the triple: operator, fail limit and actions.
518 _op="${_ts%% *}" ; _ts="${_ts#* }"
519 _li="${_ts%% *}" ; _ts="${_ts#* }"
520 # We've lost some of the quoting but we can simulate
521 # because we know an operator is always the first in a
524 while [ -n "$_ts" ] ; do
525 # If this is an operator then we've got all of the
531 _actions="${_actions}${_actions:+ }${_ts%% *}"
532 # Special case for end of list.
533 if [ "$_ts" != "${_ts#* }" ] ; then
540 if [ "$_numfails" "$_op" "$_li" ] ; then
543 for _action in $_actions ; do
549 knfsd) _ver=3 ; _pn="nfs" ;;
550 lockd) _ver=4 ; _pn="nlockmgr" ;;
551 statd) _pn="status" ;;
554 ERROR: $_pn failed RPC check:
555 rpcinfo: RPC: Program not registered
556 program $_pn version $_ver is not available"
560 _p="rpc.${_progname}"
563 _opts="${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
566 case "${_progname}${_action#restart}" in
569 Trying to restart NFS service
574 _t="Trying to restart NFS service"
578 Trying to restart lock manager service
580 Starting nfslock: OK"
583 _t="Trying to restart lock manager service"
586 _t="Trying to restart $_progname [${_p}${_opts}]"
588 _out="${_out}${_out:+${_nl}}${_t}"
594 required_result $_rc "$_out"
600 ######################################################################
606 if [ "$1" != "down" ] ; then
607 die "setup_vsftpd up not implemented!!!"
609 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
611 eventscript_call ctdb_service_unmanaged vsftpd
612 service vsftpd force-stopped
614 export CTDB_MANAGED_SERVICES="foo"
615 unset CTDB_MANAGES_VSFTPD
619 ######################################################################
625 if [ "$1" != "down" ] ; then
626 die "setup_httpd up not implemented!!!"
628 debug "Setting up HTTPD environment: service down, not managed by CTDB"
630 for i in "apache2" "httpd" ; do
631 eventscript_call ctdb_service_unmanaged "$i"
632 service "$i" force-stopped
635 export CTDB_MANAGED_SERVICES="foo"
636 unset CTDB_MANAGES_HTTPD
640 ######################################################################
642 # Result and test functions
644 # Set some globals and print the summary.
649 _f=$(basename "$0" ".sh")
651 # Remaining format should be NN.service.event.NNN or NN.service.NNN:
664 die "Internal error - unknown testcase filename format"
667 printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
673 CTDB_BASE="$CTDB_BASE"
674 CTDB_ETCDIR="$CTDB_ETCDIR"
675 ctdb client is "$(which ctdb)"
679 # Run an eventscript once. The test passes if the return code and
680 # output match those required.
682 # Any args are passed to the eventscript.
684 # Eventscript tracing can be done by setting:
685 # EVENTSCRIPTS_TESTS_TRACE="sh -x"
687 # or similar. This will almost certainly make a test fail but is
688 # useful for debugging.
691 [ -n "$event" ] || die 'simple_test: $event not set'
693 _extra_header=$(_extra_header)
695 echo "Running eventscript \"$script $event${1:+ }$*\""
697 if $TEST_COMMAND_TRACE ; then
700 _out=$($_trace "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
702 result_check "$_extra_header"
707 # If something has previously failed then don't continue.
712 echo "##################################################"
716 simple_test_command ()
718 # If something has previously failed then don't continue.
722 echo "##################################################"
723 echo "Running command \"$*\""
729 # Run an eventscript iteratively.
730 # - 1st argument is the number of iterations.
731 # - 2nd argument is something to eval to do setup for every iteration.
732 # The easiest thing to do here is to define a function and pass it
734 # - Subsequent arguments come in pairs: an iteration number and
735 # something to eval for that iteration. Each time an iteration
736 # number is matched the associated argument is given to eval after
737 # the default setup is done. The iteration numbers need to be given
738 # in ascending order.
740 # Some optional args can be given *before* these, surrounded by extra
741 # "--" args. These args are passed to the eventscript. Quoting is
744 # One use of the 2nd and further arguments is to call
745 # required_result() to change what is expected of a particular
749 [ -n "$event" ] || die 'simple_test: $event not set'
752 if [ "$1" = "--" ] ; then
754 while [ "$1" != "--" ] ; do
755 args="${args}${args:+ }$1"
765 echo "Running $_repeats iterations of \"$script $event\" $args"
769 for iteration in $(seq 1 $_repeats) ; do
770 # This is inefficient because the iteration-specific setup
771 # might completely replace the default one. However, running
772 # the default is good because it allows you to revert to a
773 # particular result without needing to specify it explicitly.
775 if [ $iteration = "$1" ] ; then
780 _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" $args 2>&1)
783 if [ -n "$OUT_FILTER" ] ; then
784 _fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
789 if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
796 result_print "$_passed" "$_out" "$_rc" "Iteration $iteration"
799 result_footer "$_result" "$(_extra_header)"