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 ######################################################################
546 export CTDB_LVS_PUBLIC_IP="$1"
547 export CTDB_PUBLIC_INTERFACE="$2"
549 lvs_state_dir="${EVENTSCRIPTS_TESTS_VAR_DIR}/lvs"
550 mkdir -p "$lvs_state_dir"
552 export FAKE_LVS_STATE_DIR="${lvs_state_dir}/state"
553 mkdir "$FAKE_LVS_STATE_DIR"
555 lvs_header=$(ipvsadm -l -n)
557 # Not an official configuration file, just used by the ctdb
559 export CTDB_LVS_NODES=$(mktemp --tmpdir="$lvs_state_dir")
560 export FAKE_CTDB_LVS_MASTER=""
564 while read _ip _opts ; do
567 FAKE_CTDB_LVS_MASTER="$_pnn"
571 printf "%s\tslave-only\n" "$_ip"
578 done >"$CTDB_LVS_NODES"
583 if [ "$1" = "NULL" ] ; then
584 required_result 0 <<EOF
588 required_result 0 <<EOF
594 simple_test_command ipvsadm -l -n
601 if [ "$_scope" = "NULL" ] ; then
602 required_result 0 <<EOF
603 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
604 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
607 required_result 0 <<EOF
608 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
609 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
610 inet ${CTDB_LVS_PUBLIC_IP}/32 scope ${_scope} lo
611 valid_lft forever preferred_lft forever
615 simple_test_command ip addr show dev lo
618 ######################################################################
620 ctdb_catdb_format_pairs ()
624 while read _k _v ; do
625 _kn=$(echo -n "$_k" | wc -c)
626 _vn=$(echo -n "$_v" | wc -c)
628 key(${_kn}) = "${_k}"
631 data(${_vn}) = "${_v}"
634 _count=$(($_count + 1))
637 echo "Dumped ${_count} records"
640 check_ctdb_tdb_statd_state ()
642 ctdb_get_my_public_addresses |
643 while read _x _sip _x ; do
645 echo "statd-state@${_sip}@${_cip}" "$FAKE_DATE_OUTPUT"
648 ctdb_catdb_format_pairs | {
650 simple_test_command ctdb catdb ctdb.tdb
654 check_statd_callout_smnotify ()
656 _state_even=$(( $(date '+%s') / 2 * 2))
657 _state_odd=$(($_state_even + 1))
661 ctdb_get_my_public_addresses |
662 while read _x _sip _x ; do
665 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_even}
666 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_even}
667 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_odd}
668 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_odd}
673 simple_test_event "notify"
677 ######################################################################
681 debug "Setting up NAT gateway"
683 natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
684 mkdir -p "$natgw_config_dir"
686 # These will accumulate, 1 per test... but will be cleaned up at
688 export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
691 while read _ip _opts ; do
694 export FAKE_CTDB_NATGW_MASTER="$_ip"
698 printf "%s\tslave-only\n" "$_ip"
704 done >"$CTDB_NATGW_NODES"
706 # Assume all of the nodes are on a /24 network and have IPv4
708 read _ip <"$CTDB_NATGW_NODES"
709 export CTDB_NATGW_PRIVATE_NETWORK="${_ip%.*}.0/24"
711 # These are fixed. Probably don't use the same network for the
712 # private node IPs. To unset the default gateway just set it to
714 export CTDB_NATGW_PUBLIC_IP="10.1.1.121/24"
715 export CTDB_NATGW_PUBLIC_IFACE="eth1"
716 export CTDB_NATGW_DEFAULT_GATEWAY="10.1.1.254"
717 export CTDB_NATGW_SLAVE_ONLY=""
720 ok_natgw_master_ip_addr_show ()
722 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
724 # This is based on CTDB_NATGW_PUBLIC_IP
728 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
729 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
730 inet ${CTDB_NATGW_PUBLIC_IP} brd ${_brd} scope global ${CTDB_NATGW_PUBLIC_IFACE}
731 valid_lft forever preferred_lft forever
735 ok_natgw_slave_ip_addr_show ()
737 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
739 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
740 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
744 ok_natgw_master_static_routes ()
749 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
750 # This is intentionally different to the code in 11.natgw ;-)
753 _net=$(echo "$_i" | sed -e 's|@.*||')
754 _gw=$(echo "$_i" | sed -e 's|.*@||')
758 _gw="$CTDB_NATGW_DEFAULT_GATEWAY"
761 [ -n "$_gw" ] || continue
762 _t="${_t}${_t:+${_nl}}"
763 _t="${_t}${_net} via ${_gw} dev ethXXX metric 10 "
765 _t=$(echo "$_t" | sort)
769 ok_natgw_slave_static_routes ()
774 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
775 # This is intentionally different to the code in 11.natgw ;-)
776 _net=$(echo "$_i" | sed -e 's|@.*||')
778 # The interface for the private network isn't specified as
779 # part of the NATGW configuration and isn't part of the
780 # command to add the route. It is implicitly added by "ip
781 # route" but our stub doesn't do this and adds "ethXXX".
782 _t="${_t}${_t:+${_nl}}"
783 _t="${_t}${_net} via ${FAKE_CTDB_NATGW_MASTER} dev ethXXX metric 10 "
785 _t=$(echo "$_t" | sort)
789 ######################################################################
791 # Samba/winbind fakery
799 if [ "$1" != "down" ] ; then
801 debug "Marking Samba services as up, listening and managed by CTDB"
802 # Get into known state.
803 eventscript_call ctdb_service_managed
805 # All possible service names for all known distros.
806 for i in "smb" "nmb" "samba" ; do
807 service "$i" force-started
810 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
811 export CTDB_MANAGED_SERVICES="foo samba bar"
813 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
814 export FAKE_WBINFO_FAIL="no"
816 # Some things in 50.samba are backgrounded and waited for. If
817 # we don't sleep at all then timeouts can happen. This avoids
819 export FAKE_SLEEP_FORCE=0.1
821 debug "Marking Samba services as down, not listening and not managed by CTDB"
822 # Get into known state.
823 eventscript_call ctdb_service_unmanaged
825 # All possible service names for all known distros.
826 for i in "smb" "nmb" "samba" ; do
827 service "$i" force-stopped
830 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
831 export CTDB_MANAGED_SERVICES="foo bar"
832 unset CTDB_MANAGES_SAMBA
834 export FAKE_TCP_LISTEN=""
835 export FAKE_WBINFO_FAIL="yes"
843 service_name="winbind"
845 if [ "$1" != "down" ] ; then
847 debug "Marking Winbind service as up and managed by CTDB"
848 # Get into known state.
849 eventscript_call ctdb_service_managed
851 service "winbind" force-started
853 export CTDB_MANAGED_SERVICES="foo winbind bar"
855 export FAKE_WBINFO_FAIL="no"
858 debug "Marking Winbind service as down and not managed by CTDB"
859 # Get into known state.
860 eventscript_call ctdb_service_unmanaged
862 service "winbind" force-stopped
864 export CTDB_MANAGED_SERVICES="foo bar"
865 unset CTDB_MANAGES_WINBIND
867 export FAKE_WBINFO_FAIL="yes"
873 debug "Making wbinfo commands fail"
874 FAKE_WBINFO_FAIL="yes"
877 ######################################################################
887 export FAKE_RPCINFO_SERVICES=""
889 export CTDB_NFS_SKIP_SHARE_CHECK="no"
893 # This doesn't even need to exist
894 export CTDB_NFS_EXPORTS_FILE="$EVENTSCRIPTS_TESTS_VAR_DIR/etc-exports"
896 # Reset the failcounts for nfs services.
897 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
899 if [ "$1" != "down" ] ; then
900 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
902 eventscript_call ctdb_service_managed
903 service "nfs" force-started
904 service "nfslock" force-started
906 export CTDB_MANAGED_SERVICES="foo nfs bar"
909 "portmapper" "nfs" "mountd" "rquotad" "nlockmgr" "status"
911 nfs_setup_fake_threads "nfsd"
912 nfs_setup_fake_threads "rpc.foobar" # Just set the variable to empty
914 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
916 eventscript_call ctdb_service_unmanaged
917 service "nfs" force-stopped
918 service "nfslock" force-stopped
920 export CTDB_MANAGED_SERVICES="foo bar"
921 unset CTDB_MANAGES_NFS
924 # This is really nasty. However, when we test NFS we don't
925 # actually test statd-callout. If we leave it there then left
926 # over, backgrounded instances of statd-callout will do horrible
927 # things with the "ctdb ip" stub and cause the actual
928 # statd-callout tests that follow to fail.
929 rm "${CTDB_BASE}/statd-callout"
935 export CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-ganesha-callout"
936 if [ "$1" != "down" ] ; then
937 export CTDB_MANAGES_NFS="yes"
940 export CTDB_NFS_SKIP_SHARE_CHECK="yes"
946 debug "Marking RPC service \"${_i}\" as unavailable"
947 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
954 debug "Marking RPC service \"${_i}\" as available"
956 portmapper) _t="2:4" ;;
960 nlockmgr) _t="3:4" ;;
962 *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
965 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
972 _etc="$CTDB_SYS_ETCDIR" # shortcut for readability
973 for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
974 if [ -r "$_c" ] ; then
981 nfs_setup_fake_threads ()
987 export PROCFS_PATH=$(mktemp -d --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
988 _threads="${PROCFS_PATH}/fs/nfsd/threads"
989 mkdir -p $(dirname "$_threads")
991 export FAKE_NFSD_THREAD_PIDS="$*"
994 export FAKE_RPC_THREAD_PIDS="$*"
999 program_stack_traces ()
1005 for _pid in ${FAKE_NFSD_THREAD_PIDS:-$FAKE_RPC_THREAD_PIDS} ; do
1006 [ $_count -le $_max ] || break
1009 Stack trace for ${_prog}[${_pid}]:
1010 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff
1012 _count=$(($_count + 1))
1019 $CTDB_NFS_CALLOUT\ start\ nlockmgr)
1020 echo "&Starting nfslock: OK"
1022 $CTDB_NFS_CALLOUT\ start\ nfs)
1024 &Starting nfslock: OK
1033 # Set the required result for a particular RPC program having failed
1034 # for a certain number of iterations. This is probably still a work
1035 # in progress. Note that we could hook aggressively
1036 # nfs_check_rpc_service() to try to implement this but we're better
1037 # off testing nfs_check_rpc_service() using independent code... even
1038 # if it is incomplete and hacky. So, if the 60.nfs eventscript
1039 # changes and the tests start to fail then it may be due to this
1040 # function being incomplete.
1041 rpc_set_service_failure_response ()
1044 _numfails="${2:-1}" # default 1
1048 if [ $_numfails -eq 0 ] ; then
1054 # A handy newline. :-)
1058 _dir="${CTDB_NFS_CHECKS_DIR:-${CTDB_BASE}/nfs-checks.d}"
1060 _file=$(ls "$_dir"/[0-9][0-9]."${_rpc_service}.check")
1061 [ -r "$_file" ] || die "RPC check file \"$_file\" does not exist or is not unique"
1063 _out=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1064 _rc_file=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1067 # Subshell to restrict scope variables...
1075 service_start_cmd=""
1076 service_check_cmd=""
1077 service_debug_cmd=""
1079 # Don't bother syntax checking, eventscript does that...
1082 # Just use the first version, or use default. This is dumb but
1083 # handles all the cases that we care about now...
1084 if [ -n "$version" ] ; then
1085 _ver="${version%% *}"
1087 case "$_rpc_service" in
1088 portmapper) _ver="" ;;
1093 $_rpc_service failed RPC check:
1094 rpcinfo: RPC: Program not registered
1095 program $_rpc_service${_ver:+ version }${_ver} is not available"
1097 if [ $unhealthy_after -gt 0 -a $_numfails -ge $unhealthy_after ] ; then
1100 echo "ERROR: ${_rpc_check_out}" >>"$_out"
1106 if [ $restart_every -gt 0 -a $(($_numfails % $restart_every)) -eq 0 ] ; then
1107 if ! $_unhealthy ; then
1108 echo "WARNING: ${_rpc_check_out}" >>"$_out"
1111 echo "Trying to restart service \"${_rpc_service}\"..." >>"$_out"
1113 if [ -n "$service_debug_cmd" ] ; then
1114 $service_debug_cmd 2>&1 >>"$_out"
1117 guess_output "$service_start_cmd" >>"$_out"
1121 read _rc <"$_rc_file"
1122 required_result $_rc <"$_out"
1124 rm -f "$_out" "$_rc_file"
1127 ######################################################################
1133 service_name="vsftpd"
1135 if [ "$1" != "down" ] ; then
1136 die "setup_vsftpd up not implemented!!!"
1138 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
1140 eventscript_call ctdb_service_unmanaged
1141 service vsftpd force-stopped
1143 export CTDB_MANAGED_SERVICES="foo"
1144 unset CTDB_MANAGES_VSFTPD
1148 ######################################################################
1154 if [ "$1" != "down" ] ; then
1155 die "setup_httpd up not implemented!!!"
1157 debug "Setting up HTTPD environment: service down, not managed by CTDB"
1159 for service_name in "apache2" "httpd" ; do
1160 eventscript_call ctdb_service_unmanaged
1161 service "$service_name" force-stopped
1164 export CTDB_MANAGED_SERVICES="foo"
1165 unset CTDB_MANAGES_HTTPD
1169 ######################################################################
1179 echo "Marking ${_t} as having no active paths"
1180 FAKE_MULTIPATH_FAILURES="${FAKE_MULTIPATH_FAILURES}${FAKE_MULTIPATH+FAILURES:+ }${_t}"
1185 CTDB_MONITOR_MPDEVICES="${CTDB_MONITOR_MPDEVICES}${CTDB_MONITOR_MPDEVICES:+ }${_t}"
1188 export CTDB_MONITOR_MPDEVICES FAKE_MULTIPATH_FAILURES
1189 export FAKE_SLEEP_FORCE=0.1
1192 ######################################################################
1194 # Result and test functions
1196 # Set some globals and print the summary.
1201 _f=$(basename "$0" ".sh")
1203 # Remaining format should be NN.service.event.NNN or NN.service.NNN:
1211 script_dir="${CTDB_BASE}/events.d"
1216 script_dir="${CTDB_BASE}/events.d"
1221 script_dir="${CTDB_BASE}"
1226 script_dir="${CTDB_BASE}"
1229 [ -r "${script_dir}/${script}" ] || \
1230 die "Internal error - unable to find script \"${script_dir}/${script}\""
1232 printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
1235 # Run an eventscript once. The test passes if the return code and
1236 # output match those required.
1238 # Any args are passed to the eventscript.
1242 [ -n "$event" ] || die 'simple_test: $event not set'
1248 echo "Running script \"$script $event${args:+ }$args\""
1255 ##################################################
1256 CTDB_BASE="$CTDB_BASE"
1257 CTDB_SYS_ETCDIR="$CTDB_SYS_ETCDIR"
1258 ctdb client is "$(which ctdb)"
1259 ip command is "$(which ip)"
1263 script_test "${script_dir}/${script}" "$event" "$@"
1269 simple_test_event ()
1271 # If something has previously failed then don't continue.
1273 $_passed || return 1
1276 echo "=================================================="
1280 simple_test_command ()
1285 # Run an NFS eventscript iteratively.
1287 # - 1st argument is the number of iterations.
1289 # - 2nd argument is the NFS/RPC service being tested
1291 # rpcinfo (or $service_check_cmd) is used on each iteration to test
1292 # the availability of the service
1294 # If this is not set or null then no RPC service is checked and the
1295 # required output is not reset on each iteration. This is useful in
1296 # baseline tests to confirm that the eventscript and test
1297 # infrastructure is working correctly.
1299 # - Subsequent arguments come in pairs: an iteration number and
1300 # something to eval before that iteration. Each time an iteration
1301 # number is matched the associated argument is given to eval after
1302 # the default setup is done. The iteration numbers need to be given
1303 # in ascending order.
1305 # These arguments can allow a service to be started or stopped
1306 # before a particular iteration.
1312 if [ -n "$2" ] ; then
1318 echo "Running $_repeats iterations of \"$script $event\" $args"
1320 _iterate_failcount=0
1321 for _iteration in $(seq 1 $_repeats) ; do
1322 # This is not a numerical comparison because $1 will often not
1324 if [ "$_iteration" = "$1" ] ; then
1325 debug "##################################################"
1327 debug "##################################################"
1330 if [ -n "$_rpc_service" ] ; then
1332 if [ -n "$service_check_cmd" ] ; then
1333 if eval "$service_check_cmd" ; then
1337 if rpcinfo -T tcp localhost "$_rpc_service" >/dev/null 2>&1 ; then
1343 _iterate_failcount=0
1345 _iterate_failcount=$(($_iterate_failcount + 1))
1347 rpc_set_service_failure_response "$_rpc_service" $_iterate_failcount
1349 _out=$(simple_test 2>&1)
1351 if "$TEST_VERBOSE" || [ $_ret -ne 0 ] ; then
1352 echo "##################################################"
1353 echo "Iteration ${_iteration}:"
1356 if [ $_ret -ne 0 ] ; then