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"
12 case "$EVENTSCRIPTS_PATH" in
14 *) EVENTSCRIPTS_PATH="${PWD}/${EVENTSCRIPTS_PATH}" ;;
16 export CTDB_HELPER_BINDIR="$EVENTSCRIPTS_PATH"
19 export EVENTSCRIPTS_PATH
21 PATH="${EVENTSCRIPTS_PATH}:${PATH}"
23 export EVENTSCRIPTS_TESTS_VAR_DIR="${TEST_VAR_DIR}/unit_eventscripts"
24 if [ -d "$EVENTSCRIPTS_TESTS_VAR_DIR" -a \
25 "$EVENTSCRIPTS_TESTS_VAR_DIR" != "/unit_eventscripts" ] ; then
26 rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR"
28 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
29 export CTDB_SCRIPT_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/script-state"
31 export CTDB_LOGGING="file:${EVENTSCRIPTS_TESTS_VAR_DIR}/log.ctdb"
32 touch "${CTDB_LOGGING#file:}" || \
33 die "Unable to setup logging for \"$CTDB_LOGGING\""
35 if [ -d "${TEST_SUBDIR}/etc" ] ; then
36 cp -a "${TEST_SUBDIR}/etc" "$EVENTSCRIPTS_TESTS_VAR_DIR"
37 export CTDB_SYS_ETCDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc"
39 die "Unable to setup \$CTDB_SYS_ETCDIR"
42 if [ -d "${TEST_SUBDIR}/etc-ctdb" ] ; then
43 cp -prL "${TEST_SUBDIR}/etc-ctdb" "$EVENTSCRIPTS_TESTS_VAR_DIR"
44 export CTDB_BASE="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc-ctdb"
46 die "Unable to set \$CTDB_BASE"
50 if [ ! -d "${CTDB_BASE}/events.d" ] ; then
52 ERROR: Directory ${CTDB_BASE}/events.d does not exist.
54 That means that no eventscripts can be tested.
56 One possible explanation:
58 You have CTDB installed via RPMs (or similar), so the regular
59 CTDB_BASE directory is in /etc/ctdb/
63 You have done a regular "configure" and "make install" so the tests
64 are installed under /usr/local/.
66 If so, one possible hack to fix this is to create a symlink:
68 ln -s /etc/ctdb /usr/local/etc/ctdb
70 This is nasty but it works... :-)
75 ######################################################################
77 if "$TEST_VERBOSE" ; then
78 debug () { echo "$@" ; }
83 ######################################################################
85 # General setup fakery
89 debug "Setting up shares (3 existing shares)"
90 # Create 3 fake shares/exports.
92 for i in $(seq 1 3) ; do
93 _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/${i}_existing"
95 FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
98 export FAKE_PROC_NET_BONDING="$EVENTSCRIPTS_TESTS_VAR_DIR/proc-net-bonding"
99 mkdir -p "$FAKE_PROC_NET_BONDING"
100 rm -f "$FAKE_PROC_NET_BONDING"/*
102 export FAKE_ETHTOOL_LINK_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/ethtool-link-down"
103 mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
104 rm -f "$FAKE_ETHTOOL_LINK_DOWN"/*
106 # This can only have 2 levels. We don't want to resort to usings
107 # something dangerous like "rm -r" setup time.
108 export FAKE_IP_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ip-state"
109 mkdir -p "$FAKE_IP_STATE"
110 rm -f "$FAKE_IP_STATE"/*/*
111 rm -f "$FAKE_IP_STATE"/* 2>/dev/null || true
112 rmdir "$FAKE_IP_STATE"/* 2>/dev/null || true
115 export CTDB_DBDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/db"
116 export CTDB_DBDIR_PERSISTENT="${CTDB_DBDIR}/persistent"
117 export CTDB_DBDIR_STATE="${CTDB_DBDIR}/state"
118 mkdir -p "$CTDB_DBDIR_PERSISTENT"
119 mkdir -p "$CTDB_DBDIR_STATE"
121 export FAKE_TDBTOOL_SUPPORTS_CHECK="yes"
122 export FAKE_TDB_IS_OK
123 export FAKE_DATE_OUTPUT
125 export FAKE_NETSTAT_TCP_ESTABLISHED FAKE_TCP_LISTEN FAKE_NETSTAT_UNIX_LISTEN
126 export FAKE_NETSTAT_TCP_ESTABLISHED_FILE=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
132 debug "Marking TCP port \"${_i}\" as not listening"
133 FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
141 # Replace some shares with non-existent ones.
146 export MISSING_SHARES_TEXT=""
147 for _i in $FAKE_SHARES ; do
148 if [ $_n = "$1" ] ; then
150 _i="${_i%_existing}_missing"
151 debug "Replacing share $_n with missing share \"$_i\""
152 rmdir "$_i" 2>/dev/null || true
153 MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}")
155 _t="${_t}${_t:+ }${_i}"
161 # Setup some fake /proc/net/bonding files with just enough info for
164 # arg1 is interface name, arg2 is currently active slave (use "None"
165 # if none), arg3 is MII status ("up" or "down").
169 _slave="${2:-${_iface}_sl_0}"
171 _mii_subs="${4:-${_mii_s:-up}}"
172 echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s"
173 cat >"${FAKE_PROC_NET_BONDING}/$_iface" <<EOF
174 Bonding Mode: IEEE 802.3ad Dynamic link aggregation
175 Currently Active Slave: $_slave
178 # Status of 1st pretend adapter
179 MII Status: $_mii_subs
180 # Status of 2nd pretend adapter
181 MII Status: $_mii_subs
185 ethtool_interfaces_down ()
188 echo "Marking interface $_i DOWN for ethtool"
189 touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
193 ethtool_interfaces_up ()
196 echo "Marking interface $_i UP for ethtool"
197 rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
203 echo "# ip rule show"
207 while read _p _x _i _x _t ; do
208 # Remove trailing colon after priority/preference.
210 # Only remove rules that match our priority/preference.
211 [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
213 echo "# ip route show table $_t"
214 ip route show table "$_t"
218 # Copied from 13.per_ip_routing for now... so this is lazy testing :-(
219 ipv4_host_addr_to_net ()
224 # Convert the host address to an unsigned long by splitting out
225 # the octets and doing the math.
227 for _o in $(export IFS="." ; echo $_host) ; do
228 _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
231 # Calculate the mask and apply it.
232 _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
233 _net_ul=$(( $_host_ul & $_mask_ul ))
235 # Now convert to a network address one byte at a time.
237 for _o in $(seq 1 4) ; do
238 _net="$(($_net_ul & 255))${_net:+.}${_net}"
239 _net_ul=$(($_net_ul >> 8))
242 echo "${_net}/${_maskbits}"
245 ######################################################################
249 # Evaluate an expression that probably calls functions or uses
250 # variables from the CTDB functions file. This is used for test
255 . "$CTDB_BASE/functions"
260 # For now this creates the same public addresses each time. However,
261 # it could be made more flexible.
262 setup_public_addresses ()
264 if [ -f "$CTDB_PUBLIC_ADDRESSES" -a \
265 "${CTDB_PUBLIC_ADDRESSES%/*}" = "$EVENTSCRIPTS_TESTS_VAR_DIR" ] ; then
266 rm "$CTDB_PUBLIC_ADDRESSES"
269 export CTDB_PUBLIC_ADDRESSES=$(mktemp \
270 --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR" \
271 "public-addresses-XXXXXXXX")
273 echo "Setting up CTDB_PUBLIC_ADDRESSES=${CTDB_PUBLIC_ADDRESSES}"
274 cat >"$CTDB_PUBLIC_ADDRESSES" <<EOF
287 # Need to cope with ctdb_get_pnn(). If a test changes PNN then it
288 # needs to be using a different state directory, otherwise the wrong
289 # PNN can already be cached in the state directory.
292 export FAKE_CTDB_PNN="$1"
293 echo "Setting up PNN ${FAKE_CTDB_PNN}"
295 export CTDB_SCRIPT_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/script-state/${FAKE_CTDB_PNN}"
296 mkdir -p "$CTDB_SCRIPT_VARDIR"
303 export FAKE_CTDB_NUMNODES="${1:-3}"
304 echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
306 ctdb_set_pnn "${2:-0}"
308 setup_public_addresses
310 export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
312 export FAKE_CTDB_EXTRA_CONFIG="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-config.sh"
313 rm -f "$FAKE_CTDB_EXTRA_CONFIG"
315 export FAKE_CTDB_IFACES_DOWN="$FAKE_CTDB_STATE/ifaces-down"
316 mkdir -p "$FAKE_CTDB_IFACES_DOWN"
317 rm -f "$FAKE_CTDB_IFACES_DOWN"/*
319 export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus"
320 mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
321 rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
323 export CTDB_PARTIALLY_ONLINE_INTERFACES
325 export FAKE_CTDB_TUNABLES_OK="MonitorInterval TDBMutexEnabled DatabaseHashSize"
326 export FAKE_CTDB_TUNABLES_OBSOLETE="EventScriptUnhealthyOnTimeout"
331 cat >"$FAKE_CTDB_EXTRA_CONFIG"
334 validate_percentage ()
337 [0-9]|[0-9][0-9]|100) return 0 ;;
338 *) echo "WARNING: ${1} is an invalid percentage${2:+\" in }${2}${2:+\"}"
345 _mem_usage="${1:-10}" # Default is 10%
346 _swap_usage="${2:-0}" # Default is 0%
351 _swap_free=$(( (100 - $_swap_usage) * $_swap_total / 100 ))
356 _mem_cached=$(( $_mem_total * (100 - $_mem_usage) / 100 - $_mem_free - $_mem_buffers ))
358 export FAKE_PROC_MEMINFO="\
359 MemTotal: ${_mem_total} kB
360 MemFree: ${_mem_free} kB
361 Buffers: ${_mem_buffers} kB
362 Cached: ${_mem_cached} kB
366 Active(anon): 1917580 kB
367 Inactive(anon): 523080 kB
368 Active(file): 504524 kB
369 Inactive(file): 496848 kB
372 SwapTotal: ${_swap_total} kB
373 SwapFree: ${_swap_free} kB
376 export CTDB_MONITOR_MEMORY_USAGE
377 export CTDB_MONITOR_SWAP_USAGE
382 export FAKE_FS_USE="${1:-10}" # Default is 10% usage
384 # Causes some variables to be exported
387 export CTDB_MONITOR_FILESYSTEM_USAGE
390 ctdb_get_interfaces ()
392 # The echo/subshell forces all the output onto 1 line.
393 echo $(ctdb ifaces -X | awk -F'|' 'FNR > 1 {print $2}')
396 ctdb_get_1_interface ()
398 _t=$(ctdb_get_interfaces)
402 # Print all public addresses as: interface IP maskbits
403 # Each line is suitable for passing to takeip/releaseip
404 ctdb_get_all_public_addresses ()
406 _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
407 while IFS="/$IFS" read _ip _maskbits _ifaces ; do
408 echo "$_ifaces $_ip $_maskbits"
412 # Print public addresses on this node as: interface IP maskbits
413 # Each line is suitable for passing to takeip/releaseip
414 ctdb_get_my_public_addresses ()
417 read _x # skip header line
419 while IFS="|" read _x _ip _x _iface _x ; do
420 [ -n "$_iface" ] || continue
421 while IFS="/$IFS" read _i _maskbits _x ; do
422 if [ "$_ip" = "$_i" ] ; then
423 echo $_iface $_ip $_maskbits
426 done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
431 # Prints the 1st public address as: interface IP maskbits
432 # This is suitable for passing to takeip/releaseip
433 ctdb_get_1_public_address ()
435 ctdb_get_my_public_addresses | { head -n 1 ; cat >/dev/null ; }
438 ctdb_not_implemented ()
440 export CTDB_NOT_IMPLEMENTED="$1"
441 ctdb_not_implemented="\
442 DEBUG: ctdb: command \"$1\" not implemented in stub"
445 ctdb_fake_scriptstatus ()
454 echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
457 ######################################################################
459 setup_ctdb_policy_routing ()
461 service_name="per_ip_routing"
463 export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing"
464 export CTDB_PER_IP_ROUTING_RULE_PREF=100
465 export CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000
466 export CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000
468 # Tests need to create and populate this file
469 rm -f "$CTDB_PER_IP_ROUTING_CONF"
472 # Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF.
473 # $1 is the number of assigned IPs to use (<num>, all), defaulting to
474 # 1. If $2 is "default" then a default route is also added.
475 create_policy_routing_config ()
478 _should_add_default="$2"
480 ctdb_get_my_public_addresses |
481 if [ "$_num_ips" = "all" ] ; then
484 { head -n "$_num_ips" ; cat >/dev/null ; }
486 while read _dev _ip _bits ; do
487 _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
488 _gw="${_net%.*}.254" # a dumb, calculated default
492 if [ "$_should_add_default" = "default" ] ; then
493 echo "$_ip 0.0.0.0/0 $_gw"
495 done >"$CTDB_PER_IP_ROUTING_CONF"
498 # Check the routes against those that are expected. $1 is the number
499 # of assigned IPs to use (<num>, all), defaulting to 1. If $2 is
500 # "default" then expect default routes to have been added.
504 _should_add_default="$2"
509 ctdb_get_my_public_addresses |
510 if [ "$_num_ips" = "all" ] ; then
513 { head -n "$_num_ips" ; cat >/dev/null ; }
515 while read _dev _ip _bits ; do
516 _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
517 _gw="${_net%.*}.254" # a dumb, calculated default
519 _policy_rules="${_policy_rules}
520 ${CTDB_PER_IP_ROUTING_RULE_PREF}: from $_ip lookup ctdb.$_ip "
521 _policy_routes="${_policy_routes}
522 # ip route show table ctdb.$_ip
523 $_net dev $_dev scope link "
525 if [ "$_should_add_default" = "default" ] ; then
526 _policy_routes="${_policy_routes}
527 default via $_gw dev $_dev "
533 0: from all lookup local ${_policy_rules}
534 32766: from all lookup main
535 32767: from all lookup default ${_policy_routes}
538 simple_test_command dump_routes
542 ######################################################################
544 ctdb_catdb_format_pairs ()
548 while read _k _v ; do
549 _kn=$(echo -n "$_k" | wc -c)
550 _vn=$(echo -n "$_v" | wc -c)
552 key(${_kn}) = "${_k}"
555 data(${_vn}) = "${_v}"
558 _count=$(($_count + 1))
561 echo "Dumped ${_count} records"
564 check_ctdb_tdb_statd_state ()
566 ctdb_get_my_public_addresses |
567 while read _x _sip _x ; do
569 echo "statd-state@${_sip}@${_cip}" "$FAKE_DATE_OUTPUT"
572 ctdb_catdb_format_pairs | {
574 simple_test_command ctdb catdb ctdb.tdb
578 check_statd_callout_smnotify ()
580 _state_even=$(( $(date '+%s') / 2 * 2))
581 _state_odd=$(($_state_even + 1))
585 ctdb_get_my_public_addresses |
586 while read _x _sip _x ; do
589 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_even}
590 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_even}
591 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_odd}
592 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_odd}
597 simple_test_event "notify"
601 ######################################################################
605 debug "Setting up NAT gateway"
607 natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
608 mkdir -p "$natgw_config_dir"
610 # These will accumulate, 1 per test... but will be cleaned up at
612 export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
615 while read _ip _opts ; do
618 export FAKE_CTDB_NATGW_MASTER="$_ip"
622 printf "%s\tslave-only\n" "$_ip"
628 done >"$CTDB_NATGW_NODES"
630 # Assume all of the nodes are on a /24 network and have IPv4
632 read _ip <"$CTDB_NATGW_NODES"
633 export CTDB_NATGW_PRIVATE_NETWORK="${_ip%.*}.0/24"
635 # These are fixed. Probably don't use the same network for the
636 # private node IPs. To unset the default gateway just set it to
638 export CTDB_NATGW_PUBLIC_IP="10.1.1.121/24"
639 export CTDB_NATGW_PUBLIC_IFACE="eth1"
640 export CTDB_NATGW_DEFAULT_GATEWAY="10.1.1.254"
641 export CTDB_NATGW_SLAVE_ONLY=""
644 ok_natgw_master_ip_addr_show ()
646 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
648 # This is based on CTDB_NATGW_PUBLIC_IP
652 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
653 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
654 inet ${CTDB_NATGW_PUBLIC_IP} brd ${_brd} scope global ${CTDB_NATGW_PUBLIC_IFACE}
655 valid_lft forever preferred_lft forever
659 ok_natgw_slave_ip_addr_show ()
661 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
663 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
664 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
668 ok_natgw_master_static_routes ()
673 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
674 # This is intentionally different to the code in 11.natgw ;-)
677 _net=$(echo "$_i" | sed -e 's|@.*||')
678 _gw=$(echo "$_i" | sed -e 's|.*@||')
682 _gw="$CTDB_NATGW_DEFAULT_GATEWAY"
685 [ -n "$_gw" ] || continue
686 _t="${_t}${_t:+${_nl}}"
687 _t="${_t}${_net} via ${_gw} dev ethXXX metric 10 "
689 _t=$(echo "$_t" | sort)
693 ok_natgw_slave_static_routes ()
698 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
699 # This is intentionally different to the code in 11.natgw ;-)
700 _net=$(echo "$_i" | sed -e 's|@.*||')
702 # The interface for the private network isn't specified as
703 # part of the NATGW configuration and isn't part of the
704 # command to add the route. It is implicitly added by "ip
705 # route" but our stub doesn't do this and adds "ethXXX".
706 _t="${_t}${_t:+${_nl}}"
707 _t="${_t}${_net} via ${FAKE_CTDB_NATGW_MASTER} dev ethXXX metric 10 "
709 _t=$(echo "$_t" | sort)
713 ######################################################################
715 # Samba/winbind fakery
723 if [ "$1" != "down" ] ; then
725 debug "Marking Samba services as up, listening and managed by CTDB"
726 # Get into known state.
727 eventscript_call ctdb_service_managed
729 # All possible service names for all known distros.
730 for i in "smb" "nmb" "samba" ; do
731 service "$i" force-started
734 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
735 export CTDB_MANAGED_SERVICES="foo samba bar"
737 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
738 export FAKE_WBINFO_FAIL="no"
740 # Some things in 50.samba are backgrounded and waited for. If
741 # we don't sleep at all then timeouts can happen. This avoids
743 export FAKE_SLEEP_FORCE=0.1
745 debug "Marking Samba services as down, not listening and not managed by CTDB"
746 # Get into known state.
747 eventscript_call ctdb_service_unmanaged
749 # All possible service names for all known distros.
750 for i in "smb" "nmb" "samba" ; do
751 service "$i" force-stopped
754 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
755 export CTDB_MANAGED_SERVICES="foo bar"
756 unset CTDB_MANAGES_SAMBA
758 export FAKE_TCP_LISTEN=""
759 export FAKE_WBINFO_FAIL="yes"
767 service_name="winbind"
769 if [ "$1" != "down" ] ; then
771 debug "Marking Winbind service as up and managed by CTDB"
772 # Get into known state.
773 eventscript_call ctdb_service_managed
775 service "winbind" force-started
777 export CTDB_MANAGED_SERVICES="foo winbind bar"
779 export FAKE_WBINFO_FAIL="no"
782 debug "Marking Winbind service as down and not managed by CTDB"
783 # Get into known state.
784 eventscript_call ctdb_service_unmanaged
786 service "winbind" force-stopped
788 export CTDB_MANAGED_SERVICES="foo bar"
789 unset CTDB_MANAGES_WINBIND
791 export FAKE_WBINFO_FAIL="yes"
797 debug "Making wbinfo commands fail"
798 FAKE_WBINFO_FAIL="yes"
801 ######################################################################
811 export FAKE_RPCINFO_SERVICES=""
813 export CTDB_NFS_SKIP_SHARE_CHECK="no"
817 # This doesn't even need to exist
818 export CTDB_NFS_EXPORTS_FILE="$EVENTSCRIPTS_TESTS_VAR_DIR/etc-exports"
820 # Reset the failcounts for nfs services.
821 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
823 if [ "$1" != "down" ] ; then
824 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
826 eventscript_call ctdb_service_managed
827 service "nfs" force-started
828 service "nfslock" force-started
830 export CTDB_MANAGED_SERVICES="foo nfs bar"
833 "portmapper" "nfs" "mountd" "rquotad" "nlockmgr" "status"
835 nfs_setup_fake_threads "nfsd"
836 nfs_setup_fake_threads "rpc.foobar" # Just set the variable to empty
838 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
840 eventscript_call ctdb_service_unmanaged
841 service "nfs" force-stopped
842 service "nfslock" force-stopped
844 export CTDB_MANAGED_SERVICES="foo bar"
845 unset CTDB_MANAGES_NFS
848 # This is really nasty. However, when we test NFS we don't
849 # actually test statd-callout. If we leave it there then left
850 # over, backgrounded instances of statd-callout will do horrible
851 # things with the "ctdb ip" stub and cause the actual
852 # statd-callout tests that follow to fail.
853 rm "${CTDB_BASE}/statd-callout"
859 export CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-ganesha-callout"
860 if [ "$1" != "down" ] ; then
861 export CTDB_MANAGES_NFS="yes"
864 export CTDB_NFS_SKIP_SHARE_CHECK="yes"
870 debug "Marking RPC service \"${_i}\" as unavailable"
871 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
878 debug "Marking RPC service \"${_i}\" as available"
880 portmapper) _t="2:4" ;;
884 nlockmgr) _t="3:4" ;;
886 *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
889 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
896 _etc="$CTDB_SYS_ETCDIR" # shortcut for readability
897 for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
898 if [ -r "$_c" ] ; then
905 nfs_setup_fake_threads ()
911 export PROCFS_PATH=$(mktemp -d --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
912 _threads="${PROCFS_PATH}/fs/nfsd/threads"
913 mkdir -p $(dirname "$_threads")
915 export FAKE_NFSD_THREAD_PIDS="$*"
918 export FAKE_RPC_THREAD_PIDS="$*"
923 program_stack_traces ()
929 for _pid in ${FAKE_NFSD_THREAD_PIDS:-$FAKE_RPC_THREAD_PIDS} ; do
930 [ $_count -le $_max ] || break
933 Stack trace for ${_prog}[${_pid}]:
934 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff
936 _count=$(($_count + 1))
943 $CTDB_NFS_CALLOUT\ start\ nlockmgr)
944 echo "&Starting nfslock: OK"
946 $CTDB_NFS_CALLOUT\ start\ nfs)
948 &Starting nfslock: OK
957 # Set the required result for a particular RPC program having failed
958 # for a certain number of iterations. This is probably still a work
959 # in progress. Note that we could hook aggressively
960 # nfs_check_rpc_service() to try to implement this but we're better
961 # off testing nfs_check_rpc_service() using independent code... even
962 # if it is incomplete and hacky. So, if the 60.nfs eventscript
963 # changes and the tests start to fail then it may be due to this
964 # function being incomplete.
965 rpc_set_service_failure_response ()
968 _numfails="${2:-1}" # default 1
972 if [ $_numfails -eq 0 ] ; then
978 # A handy newline. :-)
982 _dir="${CTDB_NFS_CHECKS_DIR:-${CTDB_BASE}/nfs-checks.d}"
984 _file=$(ls "$_dir"/[0-9][0-9]."${_rpc_service}.check")
985 [ -r "$_file" ] || die "RPC check file \"$_file\" does not exist or is not unique"
987 _out=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
988 _rc_file=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
991 # Subshell to restrict scope variables...
1000 service_check_cmd=""
1001 service_debug_cmd=""
1003 # Don't bother syntax checking, eventscript does that...
1006 # Just use the first version, or use default. This is dumb but
1007 # handles all the cases that we care about now...
1008 if [ -n "$version" ] ; then
1009 _ver="${version%% *}"
1011 case "$_rpc_service" in
1012 portmapper) _ver="" ;;
1017 $_rpc_service failed RPC check:
1018 rpcinfo: RPC: Program not registered
1019 program $_rpc_service${_ver:+ version }${_ver} is not available"
1021 if [ $unhealthy_after -gt 0 -a $_numfails -ge $unhealthy_after ] ; then
1024 echo "ERROR: ${_rpc_check_out}" >>"$_out"
1030 if [ $restart_every -gt 0 -a $(($_numfails % $restart_every)) -eq 0 ] ; then
1031 if ! $_unhealthy ; then
1032 echo "WARNING: ${_rpc_check_out}" >>"$_out"
1035 echo "Trying to restart service \"${_rpc_service}\"..." >>"$_out"
1037 if [ -n "$service_debug_cmd" ] ; then
1038 $service_debug_cmd 2>&1 >>"$_out"
1041 guess_output "$service_start_cmd" >>"$_out"
1045 read _rc <"$_rc_file"
1046 required_result $_rc <"$_out"
1048 rm -f "$_out" "$_rc_file"
1051 ######################################################################
1057 service_name="vsftpd"
1059 if [ "$1" != "down" ] ; then
1060 die "setup_vsftpd up not implemented!!!"
1062 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
1064 eventscript_call ctdb_service_unmanaged
1065 service vsftpd force-stopped
1067 export CTDB_MANAGED_SERVICES="foo"
1068 unset CTDB_MANAGES_VSFTPD
1072 ######################################################################
1078 if [ "$1" != "down" ] ; then
1079 die "setup_httpd up not implemented!!!"
1081 debug "Setting up HTTPD environment: service down, not managed by CTDB"
1083 for service_name in "apache2" "httpd" ; do
1084 eventscript_call ctdb_service_unmanaged
1085 service "$service_name" force-stopped
1088 export CTDB_MANAGED_SERVICES="foo"
1089 unset CTDB_MANAGES_HTTPD
1093 ######################################################################
1103 echo "Marking ${_t} as having no active paths"
1104 FAKE_MULTIPATH_FAILURES="${FAKE_MULTIPATH_FAILURES}${FAKE_MULTIPATH+FAILURES:+ }${_t}"
1109 CTDB_MONITOR_MPDEVICES="${CTDB_MONITOR_MPDEVICES}${CTDB_MONITOR_MPDEVICES:+ }${_t}"
1112 export CTDB_MONITOR_MPDEVICES FAKE_MULTIPATH_FAILURES
1113 export FAKE_SLEEP_FORCE=0.1
1116 ######################################################################
1118 # Result and test functions
1120 # Set some globals and print the summary.
1125 _f=$(basename "$0" ".sh")
1127 # Remaining format should be NN.service.event.NNN or NN.service.NNN:
1135 script_dir="${CTDB_BASE}/events.d"
1140 script_dir="${CTDB_BASE}/events.d"
1145 script_dir="${CTDB_BASE}"
1150 script_dir="${CTDB_BASE}"
1153 [ -r "${script_dir}/${script}" ] || \
1154 die "Internal error - unable to find script \"${script_dir}/${script}\""
1156 printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
1159 # Run an eventscript once. The test passes if the return code and
1160 # output match those required.
1162 # Any args are passed to the eventscript.
1166 [ -n "$event" ] || die 'simple_test: $event not set'
1172 echo "Running script \"$script $event${args:+ }$args\""
1179 ##################################################
1180 CTDB_BASE="$CTDB_BASE"
1181 CTDB_SYS_ETCDIR="$CTDB_SYS_ETCDIR"
1182 ctdb client is "$(which ctdb)"
1183 ip command is "$(which ip)"
1187 script_test "${script_dir}/${script}" "$event" "$@"
1193 simple_test_event ()
1195 # If something has previously failed then don't continue.
1197 $_passed || return 1
1200 echo "=================================================="
1204 simple_test_command ()
1209 # Run an NFS eventscript iteratively.
1211 # - 1st argument is the number of iterations.
1213 # - 2nd argument is the NFS/RPC service being tested
1215 # rpcinfo (or $service_check_cmd) is used on each iteration to test
1216 # the availability of the service
1218 # If this is not set or null then no RPC service is checked and the
1219 # required output is not reset on each iteration. This is useful in
1220 # baseline tests to confirm that the eventscript and test
1221 # infrastructure is working correctly.
1223 # - Subsequent arguments come in pairs: an iteration number and
1224 # something to eval before that iteration. Each time an iteration
1225 # number is matched the associated argument is given to eval after
1226 # the default setup is done. The iteration numbers need to be given
1227 # in ascending order.
1229 # These arguments can allow a service to be started or stopped
1230 # before a particular iteration.
1236 if [ -n "$2" ] ; then
1242 echo "Running $_repeats iterations of \"$script $event\" $args"
1244 _iterate_failcount=0
1245 for _iteration in $(seq 1 $_repeats) ; do
1246 # This is not a numerical comparison because $1 will often not
1248 if [ "$_iteration" = "$1" ] ; then
1249 debug "##################################################"
1251 debug "##################################################"
1254 if [ -n "$_rpc_service" ] ; then
1256 if [ -n "$service_check_cmd" ] ; then
1257 if eval "$service_check_cmd" ; then
1261 if rpcinfo -T tcp localhost "$_rpc_service" >/dev/null 2>&1 ; then
1267 _iterate_failcount=0
1269 _iterate_failcount=$(($_iterate_failcount + 1))
1271 rpc_set_service_failure_response "$_rpc_service" $_iterate_failcount
1273 _out=$(simple_test 2>&1)
1275 if "$TEST_VERBOSE" || [ $_ret -ne 0 ] ; then
1276 echo "##################################################"
1277 echo "Iteration ${_iteration}:"
1280 if [ $_ret -ne 0 ] ; then