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}"
25 export EVENTSCRIPTS_TESTS_VAR_DIR="${TEST_VAR_DIR}/unit_eventscripts"
26 if [ -d "$EVENTSCRIPTS_TESTS_VAR_DIR" -a \
27 "$EVENTSCRIPTS_TESTS_VAR_DIR" != "/unit_eventscripts" ] ; then
28 rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR"
30 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
31 export CTDB_SCRIPT_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/script-state"
33 export CTDB_LOGGING="file:${EVENTSCRIPTS_TESTS_VAR_DIR}/log.ctdb"
34 touch "${CTDB_LOGGING#file:}" || \
35 die "Unable to setup logging for \"$CTDB_LOGGING\""
37 if [ -d "${TEST_SUBDIR}/etc" ] ; then
38 cp -a "${TEST_SUBDIR}/etc" "$EVENTSCRIPTS_TESTS_VAR_DIR"
39 export CTDB_SYS_ETCDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc"
41 die "Unable to setup \$CTDB_SYS_ETCDIR"
44 if [ -d "${TEST_SUBDIR}/etc-ctdb" ] ; then
45 cp -prL "${TEST_SUBDIR}/etc-ctdb" "$EVENTSCRIPTS_TESTS_VAR_DIR"
46 export CTDB_BASE="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc-ctdb"
48 die "Unable to set \$CTDB_BASE"
52 if [ ! -d "${CTDB_BASE}/events.d" ] ; then
54 ERROR: Directory ${CTDB_BASE}/events.d does not exist.
56 That means that no eventscripts can be tested.
58 One possible explanation:
60 You have CTDB installed via RPMs (or similar), so the regular
61 CTDB_BASE directory is in /etc/ctdb/
65 You have done a regular "configure" and "make install" so the tests
66 are installed under /usr/local/.
68 If so, one possible hack to fix this is to create a symlink:
70 ln -s /etc/ctdb /usr/local/etc/ctdb
72 This is nasty but it works... :-)
77 ######################################################################
79 if "$TEST_VERBOSE" ; then
80 debug () { echo "$@" ; }
85 ######################################################################
87 # General setup fakery
91 debug "Setting up shares (3 existing shares)"
92 # Create 3 fake shares/exports.
94 for i in $(seq 1 3) ; do
95 _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/${i}_existing"
97 FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
100 export FAKE_PROC_NET_BONDING="$EVENTSCRIPTS_TESTS_VAR_DIR/proc-net-bonding"
101 mkdir -p "$FAKE_PROC_NET_BONDING"
102 rm -f "$FAKE_PROC_NET_BONDING"/*
104 export FAKE_ETHTOOL_LINK_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/ethtool-link-down"
105 mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
106 rm -f "$FAKE_ETHTOOL_LINK_DOWN"/*
108 # This can only have 2 levels. We don't want to resort to usings
109 # something dangerous like "rm -r" setup time.
110 export FAKE_IP_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ip-state"
111 mkdir -p "$FAKE_IP_STATE"
112 rm -f "$FAKE_IP_STATE"/*/*
113 rm -f "$FAKE_IP_STATE"/* 2>/dev/null || true
114 rmdir "$FAKE_IP_STATE"/* 2>/dev/null || true
117 export CTDB_DBDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/db"
118 export CTDB_DBDIR_PERSISTENT="${CTDB_DBDIR}/persistent"
119 export CTDB_DBDIR_STATE="${CTDB_DBDIR}/state"
120 mkdir -p "$CTDB_DBDIR_PERSISTENT"
121 mkdir -p "$CTDB_DBDIR_STATE"
123 export FAKE_TDBTOOL_SUPPORTS_CHECK="yes"
124 export FAKE_TDB_IS_OK
125 export FAKE_DATE_OUTPUT
127 export FAKE_NETSTAT_TCP_ESTABLISHED FAKE_TCP_LISTEN FAKE_NETSTAT_UNIX_LISTEN
128 export FAKE_NETSTAT_TCP_ESTABLISHED_FILE=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
134 debug "Marking TCP port \"${_i}\" as not listening"
135 FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
143 # Replace some shares with non-existent ones.
148 export MISSING_SHARES_TEXT=""
149 for _i in $FAKE_SHARES ; do
150 if [ $_n = "$1" ] ; then
152 _i="${_i%_existing}_missing"
153 debug "Replacing share $_n with missing share \"$_i\""
154 rmdir "$_i" 2>/dev/null || true
155 MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}")
157 _t="${_t}${_t:+ }${_i}"
163 # Setup some fake /proc/net/bonding files with just enough info for
166 # arg1 is interface name, arg2 is currently active slave (use "None"
167 # if none), arg3 is MII status ("up" or "down").
171 _slave="${2:-${_iface}_sl_0}"
173 _mii_subs="${4:-${_mii_s:-up}}"
174 echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s"
175 cat >"${FAKE_PROC_NET_BONDING}/$_iface" <<EOF
176 Bonding Mode: IEEE 802.3ad Dynamic link aggregation
177 Currently Active Slave: $_slave
180 # Status of 1st pretend adapter
181 MII Status: $_mii_subs
182 # Status of 2nd pretend adapter
183 MII Status: $_mii_subs
187 ethtool_interfaces_down ()
190 echo "Marking interface $_i DOWN for ethtool"
191 touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
195 ethtool_interfaces_up ()
198 echo "Marking interface $_i UP for ethtool"
199 rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
205 echo "# ip rule show"
209 while read _p _x _i _x _t ; do
210 # Remove trailing colon after priority/preference.
212 # Only remove rules that match our priority/preference.
213 [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
215 echo "# ip route show table $_t"
216 ip route show table "$_t"
220 # Copied from 13.per_ip_routing for now... so this is lazy testing :-(
221 ipv4_host_addr_to_net ()
226 # Convert the host address to an unsigned long by splitting out
227 # the octets and doing the math.
229 for _o in $(export IFS="." ; echo $_host) ; do
230 _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
233 # Calculate the mask and apply it.
234 _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
235 _net_ul=$(( $_host_ul & $_mask_ul ))
237 # Now convert to a network address one byte at a time.
239 for _o in $(seq 1 4) ; do
240 _net="$(($_net_ul & 255))${_net:+.}${_net}"
241 _net_ul=$(($_net_ul >> 8))
244 echo "${_net}/${_maskbits}"
247 ######################################################################
251 # Evaluate an expression that probably calls functions or uses
252 # variables from the CTDB functions file. This is used for test
257 . "$CTDB_BASE/functions"
262 # For now this creates the same public addresses each time. However,
263 # it could be made more flexible.
264 setup_public_addresses ()
266 if [ -f "$CTDB_PUBLIC_ADDRESSES" -a \
267 "${CTDB_PUBLIC_ADDRESSES%/*}" = "$EVENTSCRIPTS_TESTS_VAR_DIR" ] ; then
268 rm "$CTDB_PUBLIC_ADDRESSES"
271 export CTDB_PUBLIC_ADDRESSES=$(mktemp \
272 --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR" \
273 "public-addresses-XXXXXXXX")
275 echo "Setting up CTDB_PUBLIC_ADDRESSES=${CTDB_PUBLIC_ADDRESSES}"
276 cat >"$CTDB_PUBLIC_ADDRESSES" <<EOF
289 # Need to cope with ctdb_get_pnn(). If a test changes PNN then it
290 # needs to be using a different state directory, otherwise the wrong
291 # PNN can already be cached in the state directory.
294 export FAKE_CTDB_PNN="$1"
295 echo "Setting up PNN ${FAKE_CTDB_PNN}"
297 export CTDB_SCRIPT_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/script-state/${FAKE_CTDB_PNN}"
298 mkdir -p "$CTDB_SCRIPT_VARDIR"
305 export FAKE_CTDB_NUMNODES="${1:-3}"
306 echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
308 ctdb_set_pnn "${2:-0}"
310 setup_public_addresses
312 export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
314 export FAKE_CTDB_EXTRA_CONFIG="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-config.sh"
315 rm -f "$FAKE_CTDB_EXTRA_CONFIG"
317 export FAKE_CTDB_IFACES_DOWN="$FAKE_CTDB_STATE/ifaces-down"
318 mkdir -p "$FAKE_CTDB_IFACES_DOWN"
319 rm -f "$FAKE_CTDB_IFACES_DOWN"/*
321 export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus"
322 mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
323 rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
325 export CTDB_PARTIALLY_ONLINE_INTERFACES
327 export FAKE_CTDB_TUNABLES_OK="MonitorInterval TDBMutexEnabled DatabaseHashSize"
328 export FAKE_CTDB_TUNABLES_OBSOLETE="EventScriptUnhealthyOnTimeout"
333 cat >"$FAKE_CTDB_EXTRA_CONFIG"
336 validate_percentage ()
339 [0-9]|[0-9][0-9]|100) return 0 ;;
340 *) echo "WARNING: ${1} is an invalid percentage${2:+\" in }${2}${2:+\"}"
347 _mem_usage="${1:-10}" # Default is 10%
348 _swap_usage="${2:-0}" # Default is 0%
353 _swap_free=$(( (100 - $_swap_usage) * $_swap_total / 100 ))
358 _mem_cached=$(( $_mem_total * (100 - $_mem_usage) / 100 - $_mem_free - $_mem_buffers ))
360 export FAKE_PROC_MEMINFO="\
361 MemTotal: ${_mem_total} kB
362 MemFree: ${_mem_free} kB
363 Buffers: ${_mem_buffers} kB
364 Cached: ${_mem_cached} kB
368 Active(anon): 1917580 kB
369 Inactive(anon): 523080 kB
370 Active(file): 504524 kB
371 Inactive(file): 496848 kB
374 SwapTotal: ${_swap_total} kB
375 SwapFree: ${_swap_free} kB
378 export CTDB_MONITOR_MEMORY_USAGE
379 export CTDB_MONITOR_SWAP_USAGE
384 export FAKE_FS_USE="${1:-10}" # Default is 10% usage
386 # Causes some variables to be exported
389 export CTDB_MONITOR_FILESYSTEM_USAGE
392 ctdb_get_interfaces ()
394 # The echo/subshell forces all the output onto 1 line.
395 echo $(ctdb ifaces -X | awk -F'|' 'FNR > 1 {print $2}')
398 ctdb_get_1_interface ()
400 _t=$(ctdb_get_interfaces)
404 # Print all public addresses as: interface IP maskbits
405 # Each line is suitable for passing to takeip/releaseip
406 ctdb_get_all_public_addresses ()
408 _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
409 while IFS="/$IFS" read _ip _maskbits _ifaces ; do
410 echo "$_ifaces $_ip $_maskbits"
414 # Print public addresses on this node as: interface IP maskbits
415 # Each line is suitable for passing to takeip/releaseip
416 ctdb_get_my_public_addresses ()
419 read _x # skip header line
421 while IFS="|" read _x _ip _x _iface _x ; do
422 [ -n "$_iface" ] || continue
423 while IFS="/$IFS" read _i _maskbits _x ; do
424 if [ "$_ip" = "$_i" ] ; then
425 echo $_iface $_ip $_maskbits
428 done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
433 # Prints the 1st public address as: interface IP maskbits
434 # This is suitable for passing to takeip/releaseip
435 ctdb_get_1_public_address ()
437 ctdb_get_my_public_addresses | { head -n 1 ; cat >/dev/null ; }
440 ctdb_not_implemented ()
442 export CTDB_NOT_IMPLEMENTED="$1"
443 ctdb_not_implemented="\
444 DEBUG: ctdb: command \"$1\" not implemented in stub"
447 ctdb_fake_scriptstatus ()
456 echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
459 ######################################################################
461 setup_ctdb_policy_routing ()
463 service_name="per_ip_routing"
465 export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing"
466 export CTDB_PER_IP_ROUTING_RULE_PREF=100
467 export CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000
468 export CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000
470 # Tests need to create and populate this file
471 rm -f "$CTDB_PER_IP_ROUTING_CONF"
474 # Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF.
475 # $1 is the number of assigned IPs to use (<num>, all), defaulting to
476 # 1. If $2 is "default" then a default route is also added.
477 create_policy_routing_config ()
480 _should_add_default="$2"
482 ctdb_get_my_public_addresses |
483 if [ "$_num_ips" = "all" ] ; then
486 { head -n "$_num_ips" ; cat >/dev/null ; }
488 while read _dev _ip _bits ; do
489 _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
490 _gw="${_net%.*}.254" # a dumb, calculated default
494 if [ "$_should_add_default" = "default" ] ; then
495 echo "$_ip 0.0.0.0/0 $_gw"
497 done >"$CTDB_PER_IP_ROUTING_CONF"
500 # Check the routes against those that are expected. $1 is the number
501 # of assigned IPs to use (<num>, all), defaulting to 1. If $2 is
502 # "default" then expect default routes to have been added.
506 _should_add_default="$2"
511 ctdb_get_my_public_addresses |
512 if [ "$_num_ips" = "all" ] ; then
515 { head -n "$_num_ips" ; cat >/dev/null ; }
517 while read _dev _ip _bits ; do
518 _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
519 _gw="${_net%.*}.254" # a dumb, calculated default
521 _policy_rules="${_policy_rules}
522 ${CTDB_PER_IP_ROUTING_RULE_PREF}: from $_ip lookup ctdb.$_ip "
523 _policy_routes="${_policy_routes}
524 # ip route show table ctdb.$_ip
525 $_net dev $_dev scope link "
527 if [ "$_should_add_default" = "default" ] ; then
528 _policy_routes="${_policy_routes}
529 default via $_gw dev $_dev "
535 0: from all lookup local ${_policy_rules}
536 32766: from all lookup main
537 32767: from all lookup default ${_policy_routes}
540 simple_test_command dump_routes
544 ######################################################################
548 lvs_state_dir="${EVENTSCRIPTS_TESTS_VAR_DIR}/lvs"
549 mkdir -p "$lvs_state_dir"
551 export FAKE_LVS_STATE_DIR="${lvs_state_dir}/state"
552 mkdir "$FAKE_LVS_STATE_DIR"
554 lvs_header=$(ipvsadm -l -n)
556 export CTDB_LVS_PUBLIC_IP="$1"
557 export CTDB_LVS_PUBLIC_IFACE="$2"
559 [ -n "$CTDB_LVS_PUBLIC_IP" ] || return 0
560 [ -n "$CTDB_LVS_PUBLIC_IFACE" ] || return 0
562 export CTDB_LVS_NODES=$(mktemp --tmpdir="$lvs_state_dir")
563 export FAKE_CTDB_LVS_MASTER=""
567 while read _ip _opts ; do
570 FAKE_CTDB_LVS_MASTER="$_pnn"
574 printf "%s\tslave-only\n" "$_ip"
581 done >"$CTDB_LVS_NODES"
586 if [ "$1" = "NULL" ] ; then
587 required_result 0 <<EOF
591 required_result 0 <<EOF
597 simple_test_command ipvsadm -l -n
604 if [ "$_scope" = "NULL" ] ; then
605 required_result 0 <<EOF
606 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
607 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
610 required_result 0 <<EOF
611 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
612 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
613 inet ${CTDB_LVS_PUBLIC_IP}/32 scope ${_scope} lo
614 valid_lft forever preferred_lft forever
618 simple_test_command ip addr show dev lo
621 ######################################################################
623 ctdb_catdb_format_pairs ()
627 while read _k _v ; do
628 _kn=$(echo -n "$_k" | wc -c)
629 _vn=$(echo -n "$_v" | wc -c)
631 key(${_kn}) = "${_k}"
634 data(${_vn}) = "${_v}"
637 _count=$(($_count + 1))
640 echo "Dumped ${_count} records"
643 check_ctdb_tdb_statd_state ()
645 ctdb_get_my_public_addresses |
646 while read _x _sip _x ; do
648 echo "statd-state@${_sip}@${_cip}" "$FAKE_DATE_OUTPUT"
651 ctdb_catdb_format_pairs | {
653 simple_test_command ctdb catdb ctdb.tdb
657 check_statd_callout_smnotify ()
659 _state_even=$(( $(date '+%s') / 2 * 2))
660 _state_odd=$(($_state_even + 1))
664 ctdb_get_my_public_addresses |
665 while read _x _sip _x ; do
668 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_even}
669 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_even}
670 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_odd}
671 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_odd}
676 simple_test_event "notify"
680 ######################################################################
684 debug "Setting up NAT gateway"
686 natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
687 mkdir -p "$natgw_config_dir"
689 # These will accumulate, 1 per test... but will be cleaned up at
691 export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
694 while read _ip _opts ; do
697 export FAKE_CTDB_NATGW_MASTER="$_ip"
701 printf "%s\tslave-only\n" "$_ip"
707 done >"$CTDB_NATGW_NODES"
709 # Assume all of the nodes are on a /24 network and have IPv4
711 read _ip <"$CTDB_NATGW_NODES"
712 export CTDB_NATGW_PRIVATE_NETWORK="${_ip%.*}.0/24"
714 # These are fixed. Probably don't use the same network for the
715 # private node IPs. To unset the default gateway just set it to
717 export CTDB_NATGW_PUBLIC_IP="10.1.1.121/24"
718 export CTDB_NATGW_PUBLIC_IFACE="eth1"
719 export CTDB_NATGW_DEFAULT_GATEWAY="10.1.1.254"
720 export CTDB_NATGW_SLAVE_ONLY=""
723 ok_natgw_master_ip_addr_show ()
725 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
727 # This is based on CTDB_NATGW_PUBLIC_IP
731 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
732 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
733 inet ${CTDB_NATGW_PUBLIC_IP} brd ${_brd} scope global ${CTDB_NATGW_PUBLIC_IFACE}
734 valid_lft forever preferred_lft forever
738 ok_natgw_slave_ip_addr_show ()
740 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
742 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
743 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
747 ok_natgw_master_static_routes ()
752 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
753 # This is intentionally different to the code in 11.natgw ;-)
756 _net=$(echo "$_i" | sed -e 's|@.*||')
757 _gw=$(echo "$_i" | sed -e 's|.*@||')
761 _gw="$CTDB_NATGW_DEFAULT_GATEWAY"
764 [ -n "$_gw" ] || continue
765 _t="${_t}${_t:+${_nl}}"
766 _t="${_t}${_net} via ${_gw} dev ethXXX metric 10 "
768 _t=$(echo "$_t" | sort)
772 ok_natgw_slave_static_routes ()
777 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
778 # This is intentionally different to the code in 11.natgw ;-)
779 _net=$(echo "$_i" | sed -e 's|@.*||')
781 # The interface for the private network isn't specified as
782 # part of the NATGW configuration and isn't part of the
783 # command to add the route. It is implicitly added by "ip
784 # route" but our stub doesn't do this and adds "ethXXX".
785 _t="${_t}${_t:+${_nl}}"
786 _t="${_t}${_net} via ${FAKE_CTDB_NATGW_MASTER} dev ethXXX metric 10 "
788 _t=$(echo "$_t" | sort)
792 ######################################################################
794 # Samba/winbind fakery
802 if [ "$1" != "down" ] ; then
804 debug "Marking Samba services as up, listening and managed by CTDB"
805 # Get into known state.
806 eventscript_call ctdb_service_managed
808 # All possible service names for all known distros.
809 for i in "smb" "nmb" "samba" ; do
810 service "$i" force-started
813 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
814 export CTDB_MANAGED_SERVICES="foo samba bar"
816 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
817 export FAKE_WBINFO_FAIL="no"
819 # Some things in 50.samba are backgrounded and waited for. If
820 # we don't sleep at all then timeouts can happen. This avoids
822 export FAKE_SLEEP_FORCE=0.1
824 debug "Marking Samba services as down, not listening and not managed by CTDB"
825 # Get into known state.
826 eventscript_call ctdb_service_unmanaged
828 # All possible service names for all known distros.
829 for i in "smb" "nmb" "samba" ; do
830 service "$i" force-stopped
833 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
834 export CTDB_MANAGED_SERVICES="foo bar"
835 unset CTDB_MANAGES_SAMBA
837 export FAKE_TCP_LISTEN=""
838 export FAKE_WBINFO_FAIL="yes"
846 service_name="winbind"
848 if [ "$1" != "down" ] ; then
850 debug "Marking Winbind service as up and managed by CTDB"
851 # Get into known state.
852 eventscript_call ctdb_service_managed
854 service "winbind" force-started
856 export CTDB_MANAGED_SERVICES="foo winbind bar"
858 export FAKE_WBINFO_FAIL="no"
861 debug "Marking Winbind service as down and not managed by CTDB"
862 # Get into known state.
863 eventscript_call ctdb_service_unmanaged
865 service "winbind" force-stopped
867 export CTDB_MANAGED_SERVICES="foo bar"
868 unset CTDB_MANAGES_WINBIND
870 export FAKE_WBINFO_FAIL="yes"
876 debug "Making wbinfo commands fail"
877 FAKE_WBINFO_FAIL="yes"
880 ######################################################################
890 export FAKE_RPCINFO_SERVICES=""
892 export CTDB_NFS_SKIP_SHARE_CHECK="no"
896 # This doesn't even need to exist
897 export CTDB_NFS_EXPORTS_FILE="$EVENTSCRIPTS_TESTS_VAR_DIR/etc-exports"
899 # Reset the failcounts for nfs services.
900 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
902 if [ "$1" != "down" ] ; then
903 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
905 eventscript_call ctdb_service_managed
906 service "nfs" force-started
907 service "nfslock" force-started
909 export CTDB_MANAGED_SERVICES="foo nfs bar"
912 "portmapper" "nfs" "mountd" "rquotad" "nlockmgr" "status"
914 nfs_setup_fake_threads "nfsd"
915 nfs_setup_fake_threads "rpc.foobar" # Just set the variable to empty
917 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
919 eventscript_call ctdb_service_unmanaged
920 service "nfs" force-stopped
921 service "nfslock" force-stopped
923 export CTDB_MANAGED_SERVICES="foo bar"
924 unset CTDB_MANAGES_NFS
927 # This is really nasty. However, when we test NFS we don't
928 # actually test statd-callout. If we leave it there then left
929 # over, backgrounded instances of statd-callout will do horrible
930 # things with the "ctdb ip" stub and cause the actual
931 # statd-callout tests that follow to fail.
932 rm "${CTDB_BASE}/statd-callout"
938 export CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-ganesha-callout"
939 if [ "$1" != "down" ] ; then
940 export CTDB_MANAGES_NFS="yes"
943 export CTDB_NFS_SKIP_SHARE_CHECK="yes"
949 debug "Marking RPC service \"${_i}\" as unavailable"
950 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
957 debug "Marking RPC service \"${_i}\" as available"
959 portmapper) _t="2:4" ;;
963 nlockmgr) _t="3:4" ;;
965 *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
968 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
975 _etc="$CTDB_SYS_ETCDIR" # shortcut for readability
976 for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
977 if [ -r "$_c" ] ; then
984 nfs_setup_fake_threads ()
990 export PROCFS_PATH=$(mktemp -d --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
991 _threads="${PROCFS_PATH}/fs/nfsd/threads"
992 mkdir -p $(dirname "$_threads")
994 export FAKE_NFSD_THREAD_PIDS="$*"
997 export FAKE_RPC_THREAD_PIDS="$*"
1002 program_stack_traces ()
1008 for _pid in ${FAKE_NFSD_THREAD_PIDS:-$FAKE_RPC_THREAD_PIDS} ; do
1009 [ $_count -le $_max ] || break
1012 Stack trace for ${_prog}[${_pid}]:
1013 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff
1015 _count=$(($_count + 1))
1022 $CTDB_NFS_CALLOUT\ start\ nlockmgr)
1023 echo "&Starting nfslock: OK"
1025 $CTDB_NFS_CALLOUT\ start\ nfs)
1027 &Starting nfslock: OK
1036 # Set the required result for a particular RPC program having failed
1037 # for a certain number of iterations. This is probably still a work
1038 # in progress. Note that we could hook aggressively
1039 # nfs_check_rpc_service() to try to implement this but we're better
1040 # off testing nfs_check_rpc_service() using independent code... even
1041 # if it is incomplete and hacky. So, if the 60.nfs eventscript
1042 # changes and the tests start to fail then it may be due to this
1043 # function being incomplete.
1044 rpc_set_service_failure_response ()
1047 _numfails="${2:-1}" # default 1
1051 if [ $_numfails -eq 0 ] ; then
1057 # A handy newline. :-)
1061 _dir="${CTDB_NFS_CHECKS_DIR:-${CTDB_BASE}/nfs-checks.d}"
1063 _file=$(ls "$_dir"/[0-9][0-9]."${_rpc_service}.check")
1064 [ -r "$_file" ] || die "RPC check file \"$_file\" does not exist or is not unique"
1066 _out=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1067 _rc_file=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1070 # Subshell to restrict scope variables...
1078 service_start_cmd=""
1079 service_check_cmd=""
1080 service_debug_cmd=""
1082 # Don't bother syntax checking, eventscript does that...
1085 # Just use the first version, or use default. This is dumb but
1086 # handles all the cases that we care about now...
1087 if [ -n "$version" ] ; then
1088 _ver="${version%% *}"
1090 case "$_rpc_service" in
1091 portmapper) _ver="" ;;
1096 $_rpc_service failed RPC check:
1097 rpcinfo: RPC: Program not registered
1098 program $_rpc_service${_ver:+ version }${_ver} is not available"
1100 if [ $unhealthy_after -gt 0 -a $_numfails -ge $unhealthy_after ] ; then
1103 echo "ERROR: ${_rpc_check_out}" >>"$_out"
1109 if [ $restart_every -gt 0 -a $(($_numfails % $restart_every)) -eq 0 ] ; then
1110 if ! $_unhealthy ; then
1111 echo "WARNING: ${_rpc_check_out}" >>"$_out"
1114 echo "Trying to restart service \"${_rpc_service}\"..." >>"$_out"
1116 if [ -n "$service_debug_cmd" ] ; then
1117 $service_debug_cmd 2>&1 >>"$_out"
1120 guess_output "$service_start_cmd" >>"$_out"
1124 read _rc <"$_rc_file"
1125 required_result $_rc <"$_out"
1127 rm -f "$_out" "$_rc_file"
1130 ######################################################################
1132 # Recovery lock fakery
1136 CTDB_RECOVERY_LOCK=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1137 export CTDB_RECOVERY_LOCK
1140 ######################################################################
1146 service_name="vsftpd"
1148 if [ "$1" != "down" ] ; then
1149 die "setup_vsftpd up not implemented!!!"
1151 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
1153 eventscript_call ctdb_service_unmanaged
1154 service vsftpd force-stopped
1156 export CTDB_MANAGED_SERVICES="foo"
1157 unset CTDB_MANAGES_VSFTPD
1161 ######################################################################
1167 if [ "$1" != "down" ] ; then
1168 die "setup_httpd up not implemented!!!"
1170 debug "Setting up HTTPD environment: service down, not managed by CTDB"
1172 for service_name in "apache2" "httpd" ; do
1173 eventscript_call ctdb_service_unmanaged
1174 service "$service_name" force-stopped
1177 export CTDB_MANAGED_SERVICES="foo"
1178 unset CTDB_MANAGES_HTTPD
1182 ######################################################################
1192 echo "Marking ${_t} as having no active paths"
1193 FAKE_MULTIPATH_FAILURES="${FAKE_MULTIPATH_FAILURES}${FAKE_MULTIPATH+FAILURES:+ }${_t}"
1198 CTDB_MONITOR_MPDEVICES="${CTDB_MONITOR_MPDEVICES}${CTDB_MONITOR_MPDEVICES:+ }${_t}"
1201 export CTDB_MONITOR_MPDEVICES FAKE_MULTIPATH_FAILURES
1202 export FAKE_SLEEP_FORCE=0.1
1205 ######################################################################
1207 # Result and test functions
1209 # Set some globals and print the summary.
1214 _f=$(basename "$0" ".sh")
1216 # Remaining format should be NN.service.event.NNN or NN.service.NNN:
1224 script_dir="${CTDB_BASE}/events.d"
1229 script_dir="${CTDB_BASE}/events.d"
1234 script_dir="${CTDB_BASE}"
1239 script_dir="${CTDB_BASE}"
1242 [ -r "${script_dir}/${script}" ] || \
1243 die "Internal error - unable to find script \"${script_dir}/${script}\""
1245 printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
1248 # Run an eventscript once. The test passes if the return code and
1249 # output match those required.
1251 # Any args are passed to the eventscript.
1255 [ -n "$event" ] || die 'simple_test: $event not set'
1261 echo "Running script \"$script $event${args:+ }$args\""
1268 ##################################################
1269 CTDB_BASE="$CTDB_BASE"
1270 CTDB_SYS_ETCDIR="$CTDB_SYS_ETCDIR"
1271 ctdb client is "$(which ctdb)"
1272 ip command is "$(which ip)"
1276 script_test "${script_dir}/${script}" "$event" "$@"
1282 simple_test_event ()
1284 # If something has previously failed then don't continue.
1286 $_passed || return 1
1289 echo "=================================================="
1293 simple_test_command ()
1298 # Run an NFS eventscript iteratively.
1300 # - 1st argument is the number of iterations.
1302 # - 2nd argument is the NFS/RPC service being tested
1304 # rpcinfo (or $service_check_cmd) is used on each iteration to test
1305 # the availability of the service
1307 # If this is not set or null then no RPC service is checked and the
1308 # required output is not reset on each iteration. This is useful in
1309 # baseline tests to confirm that the eventscript and test
1310 # infrastructure is working correctly.
1312 # - Subsequent arguments come in pairs: an iteration number and
1313 # something to eval before that iteration. Each time an iteration
1314 # number is matched the associated argument is given to eval after
1315 # the default setup is done. The iteration numbers need to be given
1316 # in ascending order.
1318 # These arguments can allow a service to be started or stopped
1319 # before a particular iteration.
1325 if [ -n "$2" ] ; then
1331 echo "Running $_repeats iterations of \"$script $event\" $args"
1333 _iterate_failcount=0
1334 for _iteration in $(seq 1 $_repeats) ; do
1335 # This is not a numerical comparison because $1 will often not
1337 if [ "$_iteration" = "$1" ] ; then
1338 debug "##################################################"
1340 debug "##################################################"
1343 if [ -n "$_rpc_service" ] ; then
1345 if [ -n "$service_check_cmd" ] ; then
1346 if eval "$service_check_cmd" ; then
1350 if rpcinfo -T tcp localhost "$_rpc_service" >/dev/null 2>&1 ; then
1356 _iterate_failcount=0
1358 _iterate_failcount=$(($_iterate_failcount + 1))
1360 rpc_set_service_failure_response "$_rpc_service" $_iterate_failcount
1362 _out=$(simple_test 2>&1)
1364 if "$TEST_VERBOSE" || [ $_ret -ne 0 ] ; then
1365 echo "##################################################"
1366 echo "Iteration ${_iteration}:"
1369 if [ $_ret -ne 0 ] ; then