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 export EVENTSCRIPTS_TESTS_VAR_DIR="${TEST_VAR_DIR}/unit_eventscripts"
19 if [ -d "$EVENTSCRIPTS_TESTS_VAR_DIR" -a \
20 "$EVENTSCRIPTS_TESTS_VAR_DIR" != "/unit_eventscripts" ] ; then
21 rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR"
23 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
24 export CTDB_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb"
26 export CTDB_LOGFILE="${EVENTSCRIPTS_TESTS_VAR_DIR}/log.ctdb"
27 touch "$CTDB_LOGFILE" || die "Unable to create CTDB_LOGFILE=$CTDB_LOGFILE"
29 if [ -d "${TEST_SUBDIR}/etc" ] ; then
30 cp -a "${TEST_SUBDIR}/etc" "$EVENTSCRIPTS_TESTS_VAR_DIR"
31 export CTDB_ETCDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc"
33 die "Unable to setup \$CTDB_ETCDIR"
36 if [ -d "${TEST_SUBDIR}/etc-ctdb" ] ; then
37 cp -prL "${TEST_SUBDIR}/etc-ctdb" "$EVENTSCRIPTS_TESTS_VAR_DIR"
38 export CTDB_BASE="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc-ctdb"
40 die "Unable to set \$CTDB_BASE"
44 if [ ! -d "${CTDB_BASE}/events.d" ] ; then
46 ERROR: Directory ${CTDB_BASE}/events.d does not exist.
48 That means that no eventscripts can be tested.
50 One possible explanation:
52 You have CTDB installed via RPMs (or similar), so the regular
53 CTDB_BASE directory is in /etc/ctdb/
57 You have done a regular "configure" and "make install" so the tests
58 are installed under /usr/local/.
60 If so, one possible hack to fix this is to create a symlink:
62 ln -s /etc/ctdb /usr/local/etc/ctdb
64 This is nasty but it works... :-)
69 ######################################################################
71 if "$TEST_VERBOSE" ; then
72 debug () { echo "$@" ; }
77 eventscripts_tests_cleanup_hooks=""
80 eventscripts_test_add_cleanup ()
82 eventscripts_tests_cleanup_hooks="${eventscripts_tests_cleanup_hooks}${eventscripts_tests_cleanup_hooks:+ ; }$*"
85 trap 'eval $eventscripts_tests_cleanup_hooks' 0
88 ######################################################################
90 # General setup fakery
94 debug "Setting up shares (3 existing shares)"
95 # Create 3 fake shares/exports.
97 for i in $(seq 1 3) ; do
98 _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/${i}_existing"
100 FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
103 export FAKE_PROC_NET_BONDING="$EVENTSCRIPTS_TESTS_VAR_DIR/proc-net-bonding"
104 mkdir -p "$FAKE_PROC_NET_BONDING"
105 rm -f "$FAKE_PROC_NET_BONDING"/*
107 export FAKE_ETHTOOL_LINK_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/ethtool-link-down"
108 mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
109 rm -f "$FAKE_ETHTOOL_LINK_DOWN"/*
111 # This can only have 2 levels. We don't want to resort to usings
112 # something dangerous like "rm -r" setup time.
113 export FAKE_IP_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ip-state"
114 mkdir -p "$FAKE_IP_STATE"
115 rm -f "$FAKE_IP_STATE"/*/*
116 rm -f "$FAKE_IP_STATE"/* 2>/dev/null || true
117 rmdir "$FAKE_IP_STATE"/* 2>/dev/null || true
123 debug "Marking TCP port \"${_i}\" as not listening"
124 FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
132 # Replace some shares with non-existent ones.
137 export MISSING_SHARES_TEXT=""
138 for _i in $FAKE_SHARES ; do
139 if [ $_n = "$1" ] ; then
141 _i="${_i%_existing}_missing"
142 debug "Replacing share $_n with missing share \"$_i\""
143 rmdir "$_i" 2>/dev/null || true
144 MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}")
146 _t="${_t}${_t:+ }${_i}"
152 # Setup some fake /proc/net/bonding files with just enough info for
155 # arg1 is interface name, arg2 is currently active slave (use "None"
156 # if none), arg3 is MII status ("up" or "down").
160 _slave="${2:-${_iface}_sl_0}"
162 _mii_subs="${4:-${_mii_s:-up}}"
163 echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s"
164 cat >"${FAKE_PROC_NET_BONDING}/$_iface" <<EOF
165 Bonding Mode: IEEE 802.3ad Dynamic link aggregation
166 Currently Active Slave: $_slave
169 # Status of 1st pretend adapter
170 MII Status: $_mii_subs
171 # Status of 2nd pretend adapter
172 MII Status: $_mii_subs
176 ethtool_interfaces_down ()
179 echo "Marking interface $_i DOWN for ethtool"
180 touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
184 ethtool_interfaces_up ()
187 echo "Marking interface $_i UP for ethtool"
188 rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
192 setup_nmap_output_filter ()
194 OUT_FILTER="-e 's@^(DEBUG: # Nmap 5.21 scan initiated) .+ (as:)@\1 DATE \2@' -e 's@^(DEBUG: # Nmap done at) .+ (--)@\1 DATE \2@'"
199 echo "# ip rule show"
203 while read _p _x _i _x _t ; do
204 # Remove trailing colon after priority/preference.
206 # Only remove rules that match our priority/preference.
207 [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
209 echo "# ip route show table $_t"
210 ip route show table "$_t"
214 # Copied from 13.per_ip_routing for now... so this is lazy testing :-(
215 ipv4_host_addr_to_net ()
220 # Convert the host address to an unsigned long by splitting out
221 # the octets and doing the math.
223 for _o in $(export IFS="." ; echo $_host) ; do
224 _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
227 # Calculate the mask and apply it.
228 _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
229 _net_ul=$(( $_host_ul & $_mask_ul ))
231 # Now convert to a network address one byte at a time.
233 for _o in $(seq 1 4) ; do
234 _net="$(($_net_ul & 255))${_net:+.}${_net}"
235 _net_ul=$(($_net_ul >> 8))
238 echo "${_net}/${_maskbits}"
241 ######################################################################
245 # Evaluate an expression that probably calls functions or uses
246 # variables from the CTDB functions file. This is used for test
251 . "$CTDB_BASE/functions"
256 # Set output for ctdb command. Option 1st argument is return code.
259 _out="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.out"
262 _rc="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.rc"
263 echo "${1:-0}" >"$_rc"
265 eventscripts_test_add_cleanup "rm -f $_out $_rc"
272 export FAKE_CTDB_NUMNODES="${1:-3}"
273 echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
275 export FAKE_CTDB_PNN="${2:-0}"
276 echo "Setting up CTDB with PNN ${FAKE_CTDB_PNN}"
278 if [ -n "$3" ] ; then
279 echo "Setting up CTDB_PUBLIC_ADDRESSES: $3"
280 export CTDB_PUBLIC_ADDRESSES=$(mktemp)
284 echo "${_ip} ${_ifaces}" >>"$CTDB_PUBLIC_ADDRESSES"
286 eventscripts_test_add_cleanup "rm -f $CTDB_PUBLIC_ADDRESSES"
289 export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
291 export FAKE_CTDB_IFACES_DOWN="$FAKE_CTDB_STATE/ifaces-down"
292 mkdir -p "$FAKE_CTDB_IFACES_DOWN"
293 rm -f "$FAKE_CTDB_IFACES_DOWN"/*
295 export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus"
296 mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
297 rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
300 ctdb_get_interfaces ()
302 # The echo/subshell forces all the output onto 1 line.
303 echo $(ctdb ifaces -Y | awk -F: 'FNR > 1 {print $2}')
306 ctdb_get_1_interface ()
308 _t=$(ctdb_get_interfaces)
312 # Print all public addresses as: interface IP maskbits
313 # Each line is suitable for passing to takeip/releaseip
314 ctdb_get_all_public_addresses ()
316 _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
317 while IFS="/$IFS" read _ip _maskbits _ifaces ; do
318 echo "$_ifaces $_ip $_maskbits"
322 # Print public addresses on this node as: interface IP maskbits
323 # Each line is suitable for passing to takeip/releaseip
324 ctdb_get_my_public_addresses ()
327 read _x # skip header line
329 while IFS=":" read _x _ip _x _iface _x ; do
330 [ -n "$_iface" ] || continue
331 while IFS="/$IFS" read _i _maskbits _x ; do
332 if [ "$_ip" = "$_i" ] ; then
333 echo $_iface $_ip $_maskbits
336 done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
341 # Prints the 1st public address as: interface IP maskbits
342 # This is suitable for passing to takeip/releaseip
343 ctdb_get_1_public_address ()
345 ctdb_get_my_public_addresses | head -n 1
348 ctdb_not_implemented ()
350 export CTDB_NOT_IMPLEMENTED="$1"
351 ctdb_not_implemented="\
352 DEBUG: ctdb: command \"$1\" not implemented in stub"
355 ctdb_fake_scriptstatus ()
364 echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
367 ######################################################################
369 setup_ctdb_policy_routing ()
371 service_name="per_ip_routing"
373 export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing"
374 export CTDB_PER_IP_ROUTING_RULE_PREF=100
375 export CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000
376 export CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000
378 # Tests need to create and populate this file
379 rm -f "$CTDB_PER_IP_ROUTING_CONF"
382 # Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF.
383 # $1 is the number of assigned IPs to use (<num>, all), defaulting to
384 # 1. If $2 is "default" then a default route is also added.
385 create_policy_routing_config ()
388 _should_add_default="$2"
390 ctdb_get_my_public_addresses |
391 if [ "$_num_ips" = "all" ] ; then
396 while read _dev _ip _bits ; do
397 _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
398 _gw="${_net%.*}.1" # a dumb, calculated default
402 if [ "$_should_add_default" = "default" ] ; then
403 echo "$_ip 0.0.0.0/0 $_gw"
405 done >"$CTDB_PER_IP_ROUTING_CONF"
408 # Check the routes against those that are expected. $1 is the number
409 # of assigned IPs to use (<num>, all), defaulting to 1. If $2 is
410 # "default" then expect default routes to have been added.
414 _should_add_default="$2"
419 ctdb_get_my_public_addresses |
420 if [ "$_num_ips" = "all" ] ; then
425 while read _dev _ip _bits ; do
426 _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
427 _gw="${_net%.*}.1" # a dumb, calculated default
429 _policy_rules="${_policy_rules}
430 ${CTDB_PER_IP_ROUTING_RULE_PREF}: from $_ip lookup ctdb.$_ip "
431 _policy_routes="${_policy_routes}
432 # ip route show table ctdb.$_ip
433 $_net dev $_dev scope link "
435 if [ "$_should_add_default" = "default" ] ; then
436 _policy_routes="${_policy_routes}
437 default via $_gw dev $_dev "
443 0: from all lookup local ${_policy_rules}
444 32766: from all lookup main
445 32767: from all lookup default ${_policy_routes}
448 simple_test_command dump_routes
452 ######################################################################
454 # Samba/winbind fakery
462 if [ "$1" != "down" ] ; then
464 debug "Marking Samba services as up, listening and managed by CTDB"
465 # Get into known state.
466 eventscript_call ctdb_service_managed
468 # All possible service names for all known distros.
469 for i in "smb" "nmb" "samba" ; do
470 service "$i" force-started
473 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
474 export CTDB_MANAGED_SERVICES="foo samba bar"
476 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
477 export FAKE_WBINFO_FAIL="no"
479 # Some things in 50.samba are backgrounded and waited for. If
480 # we don't sleep at all then timeouts can happen. This avoids
482 export FAKE_SLEEP_FORCE=0.1
484 debug "Marking Samba services as down, not listening and not managed by CTDB"
485 # Get into known state.
486 eventscript_call ctdb_service_unmanaged
488 # All possible service names for all known distros.
489 for i in "smb" "nmb" "samba" ; do
490 service "$i" force-stopped
493 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
494 export CTDB_MANAGED_SERVICES="foo bar"
495 unset CTDB_MANAGES_SAMBA
497 export FAKE_TCP_LISTEN=""
498 export FAKE_WBINFO_FAIL="yes"
501 # This is ugly but if this file isn't removed before each test
502 # then configuration changes between tests don't stick.
503 rm -f "$CTDB_VARDIR/state/samba/smb.conf.cache"
510 service_name="winbind"
512 if [ "$1" != "down" ] ; then
514 debug "Marking Winbind service as up and managed by CTDB"
515 # Get into known state.
516 eventscript_call ctdb_service_managed
518 service "winbind" force-started
520 export CTDB_MANAGED_SERVICES="foo winbind bar"
522 export FAKE_WBINFO_FAIL="no"
525 debug "Marking Winbind service as down and not managed by CTDB"
526 # Get into known state.
527 eventscript_call ctdb_service_unmanaged
529 service "winbind" force-stopped
531 export CTDB_MANAGED_SERVICES="foo bar"
532 unset CTDB_MANAGES_WINBIND
534 export FAKE_WBINFO_FAIL="yes"
540 debug "Making wbinfo commands fail"
541 FAKE_WBINFO_FAIL="yes"
544 ######################################################################
554 export FAKE_RPCINFO_SERVICES=""
556 export CTDB_NFS_SKIP_SHARE_CHECK="no"
558 export CTDB_MONITOR_NFS_THREAD_COUNT RPCNFSDCOUNT FAKE_NFSD_THREAD_PIDS
560 # Reset the failcounts for nfs services.
561 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
563 if [ "$1" != "down" ] ; then
564 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
566 eventscript_call ctdb_service_managed
567 service "nfs" force-started # might not be enough
569 export CTDB_MANAGED_SERVICES="foo nfs bar"
571 rpc_services_up "nfs" "mountd" "rquotad" "nlockmgr" "status"
573 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
575 eventscript_call ctdb_service_unmanaged
576 service "nfs" force-stopped # might not be enough
577 eventscript_call startstop_nfs stop
579 export CTDB_MANAGED_SERVICES="foo bar"
580 unset CTDB_MANAGES_NFS
587 debug "Marking RPC service \"${_i}\" as unavailable"
588 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
595 debug "Marking RPC service \"${_i}\" as available"
600 nlockmgr) _t="3:4" ;;
602 *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
605 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
609 # Set the required result for a particular RPC program having failed
610 # for a certain number of iterations. This is probably still a work
611 # in progress. Note that we could hook aggressively
612 # nfs_check_rpc_service() to try to implement this but we're better
613 # off testing nfs_check_rpc_service() using independent code... even
614 # if it is incomplete and hacky. So, if the 60.nfs eventscript
615 # changes and the tests start to fail then it may be due to this
616 # function being incomplete.
617 rpc_set_service_failure_response ()
620 # The number of failures defaults to the iteration number. This
621 # will be true when we fail from the 1st iteration... but we need
622 # the flexibility to set the number of failures.
623 _numfails="${2:-${iteration}}"
625 _etc="$CTDB_ETCDIR" # shortcut for readability
626 for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
627 if [ -r "$_c" ] ; then
633 # A handy newline. :-)
640 _file=$(ls "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9]."${_progname}.check")
641 [ -r "$_file" ] || die "RPC check file \"$_file\" does not exist or is not unique"
643 while read _op _li _actions ; do
649 if [ "$_numfails" "$_op" "$_li" ] ; then
652 for _action in $_actions ; do
658 nfsd) _ver=3 ; _pn="nfs" ;;
659 lockd) _ver=4 ; _pn="nlockmgr" ;;
660 statd) _pn="status" ;;
663 ERROR: $_pn failed RPC check:
664 rpcinfo: RPC: Program not registered
665 program $_pn version $_ver is not available"
669 _p="rpc.${_progname}"
672 _opts="${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
675 case "${_progname}${_action#restart}" in
678 Trying to restart NFS service
683 _t="Trying to restart NFS service"
687 Trying to restart lock manager service
689 Starting nfslock: OK"
692 _t="Trying to restart lock manager service"
695 _t="Trying to restart $_progname [${_p}${_opts}]"
697 _out="${_out}${_out:+${_nl}}${_t}"
703 required_result $_rc "$_out"
709 ######################################################################
715 service_name="vsftpd"
717 if [ "$1" != "down" ] ; then
718 die "setup_vsftpd up not implemented!!!"
720 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
722 eventscript_call ctdb_service_unmanaged
723 service vsftpd force-stopped
725 export CTDB_MANAGED_SERVICES="foo"
726 unset CTDB_MANAGES_VSFTPD
730 ######################################################################
736 if [ "$1" != "down" ] ; then
737 die "setup_httpd up not implemented!!!"
739 debug "Setting up HTTPD environment: service down, not managed by CTDB"
741 for service_name in "apache2" "httpd" ; do
742 eventscript_call ctdb_service_unmanaged
743 service "$service_name" force-stopped
746 export CTDB_MANAGED_SERVICES="foo"
747 unset CTDB_MANAGES_HTTPD
751 ######################################################################
753 # Result and test functions
755 # Set some globals and print the summary.
760 _f=$(basename "$0" ".sh")
762 # Remaining format should be NN.service.event.NNN or NN.service.NNN:
775 die "Internal error - unknown testcase filename format"
778 printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
784 CTDB_BASE="$CTDB_BASE"
785 CTDB_ETCDIR="$CTDB_ETCDIR"
786 ctdb client is "$(which ctdb)"
790 # Run an eventscript once. The test passes if the return code and
791 # output match those required.
793 # Any args are passed to the eventscript.
795 # Eventscript tracing can be done by setting:
796 # EVENTSCRIPTS_TESTS_TRACE="sh -x"
798 # or similar. This will almost certainly make a test fail but is
799 # useful for debugging.
802 [ -n "$event" ] || die 'simple_test: $event not set'
804 _extra_header=$(_extra_header)
806 echo "Running eventscript \"$script $event${1:+ }$*\""
808 if $TEST_COMMAND_TRACE ; then
811 _out=$($_trace "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
813 result_check "$_extra_header"
818 # If something has previously failed then don't continue.
823 echo "=================================================="
827 simple_test_command ()
829 # If something has previously failed then don't continue.
833 echo "=================================================="
834 echo "Running command \"$*\""
840 check_ctdb_logfile ()
842 # If something has previously failed then don't continue.
846 echo "=================================================="
847 echo "Checking CTDB_LOGFILE=\"${CTDB_LOGFILE}\""
848 _out=$(cat "$CTDB_LOGFILE" 2>&1)
853 # Run an eventscript iteratively.
854 # - 1st argument is the number of iterations.
855 # - 2nd argument is something to eval to do setup for every iteration.
856 # The easiest thing to do here is to define a function and pass it
858 # - Subsequent arguments come in pairs: an iteration number and
859 # something to eval for that iteration. Each time an iteration
860 # number is matched the associated argument is given to eval after
861 # the default setup is done. The iteration numbers need to be given
862 # in ascending order.
864 # Some optional args can be given *before* these, surrounded by extra
865 # "--" args. These args are passed to the eventscript. Quoting is
868 # One use of the 2nd and further arguments is to call
869 # required_result() to change what is expected of a particular
873 [ -n "$event" ] || die 'simple_test: $event not set'
876 if [ "$1" = "--" ] ; then
878 while [ "$1" != "--" ] ; do
879 args="${args}${args:+ }$1"
889 echo "Running $_repeats iterations of \"$script $event\" $args"
893 for iteration in $(seq 1 $_repeats) ; do
894 # This is inefficient because the iteration-specific setup
895 # might completely replace the default one. However, running
896 # the default is good because it allows you to revert to a
897 # particular result without needing to specify it explicitly.
899 if [ $iteration = "$1" ] ; then
904 _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" $args 2>&1)
907 if [ -n "$OUT_FILTER" ] ; then
908 _fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
913 if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
920 result_print "$_passed" "$_out" "$_rc" "Iteration $iteration"
923 result_footer "$_result" "$(_extra_header)"