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}" ;;
18 export EVENTSCRIPTS_PATH
20 PATH="${EVENTSCRIPTS_PATH}:${PATH}"
22 export EVENTSCRIPTS_TESTS_VAR_DIR="${TEST_VAR_DIR}/unit_eventscripts"
23 if [ -d "$EVENTSCRIPTS_TESTS_VAR_DIR" -a \
24 "$EVENTSCRIPTS_TESTS_VAR_DIR" != "/unit_eventscripts" ] ; then
25 rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR"
27 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
28 export CTDB_SCRIPT_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/script-state"
30 export CTDB_LOGGING="file:${EVENTSCRIPTS_TESTS_VAR_DIR}/log.ctdb"
31 touch "${CTDB_LOGGING#file:}" || \
32 die "Unable to setup logging for \"$CTDB_LOGGING\""
34 if [ -d "${TEST_SUBDIR}/etc" ] ; then
35 cp -a "${TEST_SUBDIR}/etc" "$EVENTSCRIPTS_TESTS_VAR_DIR"
36 export CTDB_SYS_ETCDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc"
38 die "Unable to setup \$CTDB_SYS_ETCDIR"
41 if [ -d "${TEST_SUBDIR}/etc-ctdb" ] ; then
42 cp -prL "${TEST_SUBDIR}/etc-ctdb" "$EVENTSCRIPTS_TESTS_VAR_DIR"
43 export CTDB_BASE="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc-ctdb"
45 die "Unable to set \$CTDB_BASE"
49 if [ ! -d "${CTDB_BASE}/events.d" ] ; then
51 ERROR: Directory ${CTDB_BASE}/events.d does not exist.
53 That means that no eventscripts can be tested.
55 One possible explanation:
57 You have CTDB installed via RPMs (or similar), so the regular
58 CTDB_BASE directory is in /etc/ctdb/
62 You have done a regular "configure" and "make install" so the tests
63 are installed under /usr/local/.
65 If so, one possible hack to fix this is to create a symlink:
67 ln -s /etc/ctdb /usr/local/etc/ctdb
69 This is nasty but it works... :-)
74 ######################################################################
76 if "$TEST_VERBOSE" ; then
77 debug () { echo "$@" ; }
82 ######################################################################
84 # General setup fakery
88 debug "Setting up shares (3 existing shares)"
89 # Create 3 fake shares/exports.
91 for i in $(seq 1 3) ; do
92 _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/${i}_existing"
94 FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
97 export FAKE_PROC_NET_BONDING="$EVENTSCRIPTS_TESTS_VAR_DIR/proc-net-bonding"
98 mkdir -p "$FAKE_PROC_NET_BONDING"
99 rm -f "$FAKE_PROC_NET_BONDING"/*
101 export FAKE_ETHTOOL_LINK_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/ethtool-link-down"
102 mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
103 rm -f "$FAKE_ETHTOOL_LINK_DOWN"/*
105 # This can only have 2 levels. We don't want to resort to usings
106 # something dangerous like "rm -r" setup time.
107 export FAKE_IP_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ip-state"
108 mkdir -p "$FAKE_IP_STATE"
109 rm -f "$FAKE_IP_STATE"/*/*
110 rm -f "$FAKE_IP_STATE"/* 2>/dev/null || true
111 rmdir "$FAKE_IP_STATE"/* 2>/dev/null || true
114 export CTDB_DBDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/db"
115 export CTDB_DBDIR_PERSISTENT="${CTDB_DBDIR}/persistent"
116 export CTDB_DBDIR_STATE="${CTDB_DBDIR}/state"
117 mkdir -p "$CTDB_DBDIR_PERSISTENT"
118 mkdir -p "$CTDB_DBDIR_STATE"
120 export FAKE_TDBTOOL_SUPPORTS_CHECK="yes"
121 export FAKE_TDB_IS_OK
122 export FAKE_DATE_OUTPUT
124 export FAKE_NETSTAT_TCP_ESTABLISHED FAKE_TCP_LISTEN FAKE_NETSTAT_UNIX_LISTEN
125 export FAKE_NETSTAT_TCP_ESTABLISHED_FILE=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
131 debug "Marking TCP port \"${_i}\" as not listening"
132 FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
140 # Replace some shares with non-existent ones.
145 export MISSING_SHARES_TEXT=""
146 for _i in $FAKE_SHARES ; do
147 if [ $_n = "$1" ] ; then
149 _i="${_i%_existing}_missing"
150 debug "Replacing share $_n with missing share \"$_i\""
151 rmdir "$_i" 2>/dev/null || true
152 MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}")
154 _t="${_t}${_t:+ }${_i}"
160 # Setup some fake /proc/net/bonding files with just enough info for
163 # arg1 is interface name, arg2 is currently active slave (use "None"
164 # if none), arg3 is MII status ("up" or "down").
168 _slave="${2:-${_iface}_sl_0}"
170 _mii_subs="${4:-${_mii_s:-up}}"
171 echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s"
172 cat >"${FAKE_PROC_NET_BONDING}/$_iface" <<EOF
173 Bonding Mode: IEEE 802.3ad Dynamic link aggregation
174 Currently Active Slave: $_slave
177 # Status of 1st pretend adapter
178 MII Status: $_mii_subs
179 # Status of 2nd pretend adapter
180 MII Status: $_mii_subs
184 ethtool_interfaces_down ()
187 echo "Marking interface $_i DOWN for ethtool"
188 touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
192 ethtool_interfaces_up ()
195 echo "Marking interface $_i UP for ethtool"
196 rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
202 echo "# ip rule show"
206 while read _p _x _i _x _t ; do
207 # Remove trailing colon after priority/preference.
209 # Only remove rules that match our priority/preference.
210 [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
212 echo "# ip route show table $_t"
213 ip route show table "$_t"
217 # Copied from 13.per_ip_routing for now... so this is lazy testing :-(
218 ipv4_host_addr_to_net ()
223 # Convert the host address to an unsigned long by splitting out
224 # the octets and doing the math.
226 for _o in $(export IFS="." ; echo $_host) ; do
227 _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
230 # Calculate the mask and apply it.
231 _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
232 _net_ul=$(( $_host_ul & $_mask_ul ))
234 # Now convert to a network address one byte at a time.
236 for _o in $(seq 1 4) ; do
237 _net="$(($_net_ul & 255))${_net:+.}${_net}"
238 _net_ul=$(($_net_ul >> 8))
241 echo "${_net}/${_maskbits}"
244 ######################################################################
248 # Evaluate an expression that probably calls functions or uses
249 # variables from the CTDB functions file. This is used for test
254 . "$CTDB_BASE/functions"
259 # For now this creates the same public addresses each time. However,
260 # it could be made more flexible.
261 setup_public_addresses ()
263 if [ -f "$CTDB_PUBLIC_ADDRESSES" -a \
264 "${CTDB_PUBLIC_ADDRESSES%/*}" = "$EVENTSCRIPTS_TESTS_VAR_DIR" ] ; then
265 rm "$CTDB_PUBLIC_ADDRESSES"
268 export CTDB_PUBLIC_ADDRESSES=$(mktemp \
269 --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR" \
270 "public-addresses-XXXXXXXX")
272 echo "Setting up CTDB_PUBLIC_ADDRESSES=${CTDB_PUBLIC_ADDRESSES}"
273 cat >"$CTDB_PUBLIC_ADDRESSES" <<EOF
286 # Need to cope with ctdb_get_pnn(). If a test changes PNN then it
287 # needs to be using a different state directory, otherwise the wrong
288 # PNN can already be cached in the state directory.
291 export FAKE_CTDB_PNN="$1"
292 echo "Setting up PNN ${FAKE_CTDB_PNN}"
294 export CTDB_SCRIPT_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/script-state/${FAKE_CTDB_PNN}"
295 mkdir -p "$CTDB_SCRIPT_VARDIR"
302 export FAKE_CTDB_NUMNODES="${1:-3}"
303 echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
305 ctdb_set_pnn "${2:-0}"
307 setup_public_addresses
309 export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
311 export FAKE_CTDB_EXTRA_CONFIG="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-config.sh"
312 rm -f "$FAKE_CTDB_EXTRA_CONFIG"
314 export FAKE_CTDB_IFACES_DOWN="$FAKE_CTDB_STATE/ifaces-down"
315 mkdir -p "$FAKE_CTDB_IFACES_DOWN"
316 rm -f "$FAKE_CTDB_IFACES_DOWN"/*
318 export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus"
319 mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
320 rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
322 export CTDB_PARTIALLY_ONLINE_INTERFACES
324 export FAKE_CTDB_TUNABLES_OK="MonitorInterval TDBMutexEnabled DatabaseHashSize"
325 export FAKE_CTDB_TUNABLES_OBSOLETE="EventScriptUnhealthyOnTimeout"
330 cat >"$FAKE_CTDB_EXTRA_CONFIG"
333 validate_percentage ()
336 [0-9]|[0-9][0-9]|100) return 0 ;;
337 *) echo "WARNING: ${1} is an invalid percentage${2:+\" in }${2}${2:+\"}"
344 _mem_usage="${1:-10}" # Default is 10%
345 _swap_usage="${2:-0}" # Default is 0%
350 _swap_free=$(( (100 - $_swap_usage) * $_swap_total / 100 ))
355 _mem_cached=$(( $_mem_total * (100 - $_mem_usage) / 100 - $_mem_free - $_mem_buffers ))
357 export FAKE_PROC_MEMINFO="\
358 MemTotal: ${_mem_total} kB
359 MemFree: ${_mem_free} kB
360 Buffers: ${_mem_buffers} kB
361 Cached: ${_mem_cached} kB
365 Active(anon): 1917580 kB
366 Inactive(anon): 523080 kB
367 Active(file): 504524 kB
368 Inactive(file): 496848 kB
371 SwapTotal: ${_swap_total} kB
372 SwapFree: ${_swap_free} kB
375 export CTDB_MONITOR_MEMORY_USAGE
376 export CTDB_MONITOR_SWAP_USAGE
381 export FAKE_FS_USE="${1:-10}" # Default is 10% usage
383 # Causes some variables to be exported
386 export CTDB_MONITOR_FILESYSTEM_USAGE
389 ctdb_get_interfaces ()
391 # The echo/subshell forces all the output onto 1 line.
392 echo $(ctdb ifaces -X | awk -F'|' 'FNR > 1 {print $2}')
395 ctdb_get_1_interface ()
397 _t=$(ctdb_get_interfaces)
401 # Print all public addresses as: interface IP maskbits
402 # Each line is suitable for passing to takeip/releaseip
403 ctdb_get_all_public_addresses ()
405 _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
406 while IFS="/$IFS" read _ip _maskbits _ifaces ; do
407 echo "$_ifaces $_ip $_maskbits"
411 # Print public addresses on this node as: interface IP maskbits
412 # Each line is suitable for passing to takeip/releaseip
413 ctdb_get_my_public_addresses ()
416 read _x # skip header line
418 while IFS="|" read _x _ip _x _iface _x ; do
419 [ -n "$_iface" ] || continue
420 while IFS="/$IFS" read _i _maskbits _x ; do
421 if [ "$_ip" = "$_i" ] ; then
422 echo $_iface $_ip $_maskbits
425 done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
430 # Prints the 1st public address as: interface IP maskbits
431 # This is suitable for passing to takeip/releaseip
432 ctdb_get_1_public_address ()
434 ctdb_get_my_public_addresses | { head -n 1 ; cat >/dev/null ; }
437 ctdb_not_implemented ()
439 export CTDB_NOT_IMPLEMENTED="$1"
440 ctdb_not_implemented="\
441 DEBUG: ctdb: command \"$1\" not implemented in stub"
444 ctdb_fake_scriptstatus ()
453 echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
456 ######################################################################
458 setup_ctdb_policy_routing ()
460 service_name="per_ip_routing"
462 export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing"
463 export CTDB_PER_IP_ROUTING_RULE_PREF=100
464 export CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000
465 export CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000
467 # Tests need to create and populate this file
468 rm -f "$CTDB_PER_IP_ROUTING_CONF"
471 # Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF.
472 # $1 is the number of assigned IPs to use (<num>, all), defaulting to
473 # 1. If $2 is "default" then a default route is also added.
474 create_policy_routing_config ()
477 _should_add_default="$2"
479 ctdb_get_my_public_addresses |
480 if [ "$_num_ips" = "all" ] ; then
483 { head -n "$_num_ips" ; cat >/dev/null ; }
485 while read _dev _ip _bits ; do
486 _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
487 _gw="${_net%.*}.254" # a dumb, calculated default
491 if [ "$_should_add_default" = "default" ] ; then
492 echo "$_ip 0.0.0.0/0 $_gw"
494 done >"$CTDB_PER_IP_ROUTING_CONF"
497 # Check the routes against those that are expected. $1 is the number
498 # of assigned IPs to use (<num>, all), defaulting to 1. If $2 is
499 # "default" then expect default routes to have been added.
503 _should_add_default="$2"
508 ctdb_get_my_public_addresses |
509 if [ "$_num_ips" = "all" ] ; then
512 { head -n "$_num_ips" ; cat >/dev/null ; }
514 while read _dev _ip _bits ; do
515 _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
516 _gw="${_net%.*}.254" # a dumb, calculated default
518 _policy_rules="${_policy_rules}
519 ${CTDB_PER_IP_ROUTING_RULE_PREF}: from $_ip lookup ctdb.$_ip "
520 _policy_routes="${_policy_routes}
521 # ip route show table ctdb.$_ip
522 $_net dev $_dev scope link "
524 if [ "$_should_add_default" = "default" ] ; then
525 _policy_routes="${_policy_routes}
526 default via $_gw dev $_dev "
532 0: from all lookup local ${_policy_rules}
533 32766: from all lookup main
534 32767: from all lookup default ${_policy_routes}
537 simple_test_command dump_routes
541 ######################################################################
543 ctdb_catdb_format_pairs ()
547 while read _k _v ; do
548 _kn=$(echo -n "$_k" | wc -c)
549 _vn=$(echo -n "$_v" | wc -c)
551 key(${_kn}) = "${_k}"
554 data(${_vn}) = "${_v}"
557 _count=$(($_count + 1))
560 echo "Dumped ${_count} records"
563 check_ctdb_tdb_statd_state ()
565 ctdb_get_my_public_addresses |
566 while read _x _sip _x ; do
568 echo "statd-state@${_sip}@${_cip}" "$FAKE_DATE_OUTPUT"
571 ctdb_catdb_format_pairs | {
573 simple_test_command ctdb catdb ctdb.tdb
577 check_statd_callout_smnotify ()
579 _state_even=$(( $(date '+%s') / 2 * 2))
580 _state_odd=$(($_state_even + 1))
584 ctdb_get_my_public_addresses |
585 while read _x _sip _x ; do
588 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_even}
589 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_even}
590 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_odd}
591 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_odd}
596 simple_test_event "notify"
600 ######################################################################
604 debug "Setting up NAT gateway"
606 natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
607 mkdir -p "$natgw_config_dir"
609 # These will accumulate, 1 per test... but will be cleaned up at
611 export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
614 while read _ip _opts ; do
617 export FAKE_CTDB_NATGW_MASTER="$_ip"
621 printf "%s\tslave-only\n" "$_ip"
627 done >"$CTDB_NATGW_NODES"
629 # Assume all of the nodes are on a /24 network and have IPv4
631 read _ip <"$CTDB_NATGW_NODES"
632 export CTDB_NATGW_PRIVATE_NETWORK="${_ip%.*}.0/24"
634 # These are fixed. Probably don't use the same network for the
635 # private node IPs. To unset the default gateway just set it to
637 export CTDB_NATGW_PUBLIC_IP="10.1.1.121/24"
638 export CTDB_NATGW_PUBLIC_IFACE="eth1"
639 export CTDB_NATGW_DEFAULT_GATEWAY="10.1.1.254"
640 export CTDB_NATGW_SLAVE_ONLY=""
643 ok_natgw_master_ip_addr_show ()
645 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
647 # This is based on CTDB_NATGW_PUBLIC_IP
651 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
652 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
653 inet ${CTDB_NATGW_PUBLIC_IP} brd ${_brd} scope global ${CTDB_NATGW_PUBLIC_IFACE}
654 valid_lft forever preferred_lft forever
658 ok_natgw_slave_ip_addr_show ()
660 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
662 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
663 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
667 ok_natgw_master_static_routes ()
672 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
673 # This is intentionally different to the code in 11.natgw ;-)
676 _net=$(echo "$_i" | sed -e 's|@.*||')
677 _gw=$(echo "$_i" | sed -e 's|.*@||')
681 _gw="$CTDB_NATGW_DEFAULT_GATEWAY"
684 [ -n "$_gw" ] || continue
685 _t="${_t}${_t:+${_nl}}"
686 _t="${_t}${_net} via ${_gw} dev ethXXX metric 10 "
688 _t=$(echo "$_t" | sort)
692 ok_natgw_slave_static_routes ()
697 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
698 # This is intentionally different to the code in 11.natgw ;-)
699 _net=$(echo "$_i" | sed -e 's|@.*||')
701 # The interface for the private network isn't specified as
702 # part of the NATGW configuration and isn't part of the
703 # command to add the route. It is implicitly added by "ip
704 # route" but our stub doesn't do this and adds "ethXXX".
705 _t="${_t}${_t:+${_nl}}"
706 _t="${_t}${_net} via ${FAKE_CTDB_NATGW_MASTER} dev ethXXX metric 10 "
708 _t=$(echo "$_t" | sort)
712 ######################################################################
714 # Samba/winbind fakery
722 if [ "$1" != "down" ] ; then
724 debug "Marking Samba services as up, listening and managed by CTDB"
725 # Get into known state.
726 eventscript_call ctdb_service_managed
728 # All possible service names for all known distros.
729 for i in "smb" "nmb" "samba" ; do
730 service "$i" force-started
733 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
734 export CTDB_MANAGED_SERVICES="foo samba bar"
736 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
737 export FAKE_WBINFO_FAIL="no"
739 # Some things in 50.samba are backgrounded and waited for. If
740 # we don't sleep at all then timeouts can happen. This avoids
742 export FAKE_SLEEP_FORCE=0.1
744 debug "Marking Samba services as down, not listening and not managed by CTDB"
745 # Get into known state.
746 eventscript_call ctdb_service_unmanaged
748 # All possible service names for all known distros.
749 for i in "smb" "nmb" "samba" ; do
750 service "$i" force-stopped
753 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
754 export CTDB_MANAGED_SERVICES="foo bar"
755 unset CTDB_MANAGES_SAMBA
757 export FAKE_TCP_LISTEN=""
758 export FAKE_WBINFO_FAIL="yes"
766 service_name="winbind"
768 if [ "$1" != "down" ] ; then
770 debug "Marking Winbind service as up and managed by CTDB"
771 # Get into known state.
772 eventscript_call ctdb_service_managed
774 service "winbind" force-started
776 export CTDB_MANAGED_SERVICES="foo winbind bar"
778 export FAKE_WBINFO_FAIL="no"
781 debug "Marking Winbind service as down and not managed by CTDB"
782 # Get into known state.
783 eventscript_call ctdb_service_unmanaged
785 service "winbind" force-stopped
787 export CTDB_MANAGED_SERVICES="foo bar"
788 unset CTDB_MANAGES_WINBIND
790 export FAKE_WBINFO_FAIL="yes"
796 debug "Making wbinfo commands fail"
797 FAKE_WBINFO_FAIL="yes"
800 ######################################################################
810 export FAKE_RPCINFO_SERVICES=""
812 export CTDB_NFS_SKIP_SHARE_CHECK="no"
816 # This doesn't even need to exist
817 export CTDB_NFS_EXPORTS_FILE="$EVENTSCRIPTS_TESTS_VAR_DIR/etc-exports"
819 # Reset the failcounts for nfs services.
820 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
822 if [ "$1" != "down" ] ; then
823 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
825 eventscript_call ctdb_service_managed
826 service "nfs" force-started
827 service "nfslock" force-started
829 export CTDB_MANAGED_SERVICES="foo nfs bar"
832 "portmapper" "nfs" "mountd" "rquotad" "nlockmgr" "status"
834 nfs_setup_fake_threads "nfsd"
835 nfs_setup_fake_threads "rpc.foobar" # Just set the variable to empty
837 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
839 eventscript_call ctdb_service_unmanaged
840 service "nfs" force-stopped
841 service "nfslock" force-stopped
843 export CTDB_MANAGED_SERVICES="foo bar"
844 unset CTDB_MANAGES_NFS
847 # This is really nasty. However, when we test NFS we don't
848 # actually test statd-callout. If we leave it there then left
849 # over, backgrounded instances of statd-callout will do horrible
850 # things with the "ctdb ip" stub and cause the actual
851 # statd-callout tests that follow to fail.
852 rm "${CTDB_BASE}/statd-callout"
858 export CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-ganesha-callout"
859 if [ "$1" != "down" ] ; then
860 export CTDB_MANAGES_NFS="yes"
863 export CTDB_NFS_SKIP_SHARE_CHECK="yes"
869 debug "Marking RPC service \"${_i}\" as unavailable"
870 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
877 debug "Marking RPC service \"${_i}\" as available"
879 portmapper) _t="2:4" ;;
883 nlockmgr) _t="3:4" ;;
885 *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
888 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
895 _etc="$CTDB_SYS_ETCDIR" # shortcut for readability
896 for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
897 if [ -r "$_c" ] ; then
904 nfs_setup_fake_threads ()
910 export PROCFS_PATH=$(mktemp -d --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
911 _threads="${PROCFS_PATH}/fs/nfsd/threads"
912 mkdir -p $(dirname "$_threads")
914 export FAKE_NFSD_THREAD_PIDS="$*"
917 export FAKE_RPC_THREAD_PIDS="$*"
922 program_stack_traces ()
928 for _pid in ${FAKE_NFSD_THREAD_PIDS:-$FAKE_RPC_THREAD_PIDS} ; do
929 [ $_count -le $_max ] || break
932 Stack trace for ${_prog}[${_pid}]:
933 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff
935 _count=$(($_count + 1))
942 $CTDB_NFS_CALLOUT\ start\ nlockmgr)
943 echo "&Starting nfslock: OK"
945 $CTDB_NFS_CALLOUT\ start\ nfs)
947 &Starting nfslock: OK
956 # Set the required result for a particular RPC program having failed
957 # for a certain number of iterations. This is probably still a work
958 # in progress. Note that we could hook aggressively
959 # nfs_check_rpc_service() to try to implement this but we're better
960 # off testing nfs_check_rpc_service() using independent code... even
961 # if it is incomplete and hacky. So, if the 60.nfs eventscript
962 # changes and the tests start to fail then it may be due to this
963 # function being incomplete.
964 rpc_set_service_failure_response ()
967 _numfails="${2:-1}" # default 1
971 if [ $_numfails -eq 0 ] ; then
977 # A handy newline. :-)
981 _dir="${CTDB_NFS_CHECKS_DIR:-${CTDB_BASE}/nfs-checks.d}"
983 _file=$(ls "$_dir"/[0-9][0-9]."${_rpc_service}.check")
984 [ -r "$_file" ] || die "RPC check file \"$_file\" does not exist or is not unique"
986 _out=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
987 _rc_file=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
990 # Subshell to restrict scope variables...
1000 service_debug_cmd=""
1002 # Don't bother syntax checking, eventscript does that...
1005 # Just use the first version, or use default. This is dumb but
1006 # handles all the cases that we care about now...
1007 if [ -n "$version" ] ; then
1008 _ver="${version%% *}"
1010 case "$_rpc_service" in
1011 portmapper) _ver="" ;;
1016 $_rpc_service failed RPC check:
1017 rpcinfo: RPC: Program not registered
1018 program $_rpc_service${_ver:+ version }${_ver} is not available"
1020 if [ $unhealthy_after -gt 0 -a $_numfails -ge $unhealthy_after ] ; then
1023 echo "ERROR: ${_rpc_check_out}" >>"$_out"
1029 if [ $restart_every -gt 0 -a $(($_numfails % $restart_every)) -eq 0 ] ; then
1030 if ! $_unhealthy ; then
1031 echo "WARNING: ${_rpc_check_out}" >>"$_out"
1034 echo "Trying to restart service \"${_rpc_service}\"..." >>"$_out"
1036 if [ -n "$service_debug_cmd" ] ; then
1037 $service_debug_cmd 2>&1 >>"$_out"
1040 guess_output "$service_start_cmd" >>"$_out"
1044 read _rc <"$_rc_file"
1045 required_result $_rc <"$_out"
1047 rm -f "$_out" "$_rc_file"
1050 ######################################################################
1056 service_name="vsftpd"
1058 if [ "$1" != "down" ] ; then
1059 die "setup_vsftpd up not implemented!!!"
1061 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
1063 eventscript_call ctdb_service_unmanaged
1064 service vsftpd force-stopped
1066 export CTDB_MANAGED_SERVICES="foo"
1067 unset CTDB_MANAGES_VSFTPD
1071 ######################################################################
1077 if [ "$1" != "down" ] ; then
1078 die "setup_httpd up not implemented!!!"
1080 debug "Setting up HTTPD environment: service down, not managed by CTDB"
1082 for service_name in "apache2" "httpd" ; do
1083 eventscript_call ctdb_service_unmanaged
1084 service "$service_name" force-stopped
1087 export CTDB_MANAGED_SERVICES="foo"
1088 unset CTDB_MANAGES_HTTPD
1092 ######################################################################
1102 echo "Marking ${_t} as having no active paths"
1103 FAKE_MULTIPATH_FAILURES="${FAKE_MULTIPATH_FAILURES}${FAKE_MULTIPATH+FAILURES:+ }${_t}"
1108 CTDB_MONITOR_MPDEVICES="${CTDB_MONITOR_MPDEVICES}${CTDB_MONITOR_MPDEVICES:+ }${_t}"
1111 export CTDB_MONITOR_MPDEVICES FAKE_MULTIPATH_FAILURES
1112 export FAKE_SLEEP_FORCE=0.1
1115 ######################################################################
1117 # Result and test functions
1119 # Set some globals and print the summary.
1124 _f=$(basename "$0" ".sh")
1126 # Remaining format should be NN.service.event.NNN or NN.service.NNN:
1134 script_dir="${CTDB_BASE}/events.d"
1139 script_dir="${CTDB_BASE}/events.d"
1144 script_dir="${CTDB_BASE}"
1149 script_dir="${CTDB_BASE}"
1152 [ -r "${script_dir}/${script}" ] || \
1153 die "Internal error - unable to find script \"${script_dir}/${script}\""
1155 printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
1158 # Run an eventscript once. The test passes if the return code and
1159 # output match those required.
1161 # Any args are passed to the eventscript.
1165 [ -n "$event" ] || die 'simple_test: $event not set'
1171 echo "Running script \"$script $event${args:+ }$args\""
1178 ##################################################
1179 CTDB_BASE="$CTDB_BASE"
1180 CTDB_SYS_ETCDIR="$CTDB_SYS_ETCDIR"
1181 ctdb client is "$(which ctdb)"
1182 ip command is "$(which ip)"
1186 script_test "${script_dir}/${script}" "$event" "$@"
1192 simple_test_event ()
1194 # If something has previously failed then don't continue.
1196 $_passed || return 1
1199 echo "=================================================="
1203 simple_test_command ()
1208 # Run an NFS eventscript iteratively.
1210 # - 1st argument is the number of iterations.
1212 # - 2nd argument is the NFS/RPC service being tested
1214 # rpcinfo (or $service_check_cmd) is used on each iteration to test
1215 # the availability of the service
1217 # If this is not set or null then no RPC service is checked and the
1218 # required output is not reset on each iteration. This is useful in
1219 # baseline tests to confirm that the eventscript and test
1220 # infrastructure is working correctly.
1222 # - Subsequent arguments come in pairs: an iteration number and
1223 # something to eval before that iteration. Each time an iteration
1224 # number is matched the associated argument is given to eval after
1225 # the default setup is done. The iteration numbers need to be given
1226 # in ascending order.
1228 # These arguments can allow a service to be started or stopped
1229 # before a particular iteration.
1235 if [ -n "$2" ] ; then
1241 echo "Running $_repeats iterations of \"$script $event\" $args"
1243 _iterate_failcount=0
1244 for _iteration in $(seq 1 $_repeats) ; do
1245 # This is not a numerical comparison because $1 will often not
1247 if [ "$_iteration" = "$1" ] ; then
1248 debug "##################################################"
1250 debug "##################################################"
1253 if [ -n "$_rpc_service" ] ; then
1255 if [ -n "$service_check_cmd" ] ; then
1256 if eval "$service_check_cmd" ; then
1260 if rpcinfo -T tcp localhost "$_rpc_service" >/dev/null 2>&1 ; then
1266 _iterate_failcount=0
1268 _iterate_failcount=$(($_iterate_failcount + 1))
1270 rpc_set_service_failure_response "$_rpc_service" $_iterate_failcount
1272 _out=$(simple_test 2>&1)
1274 if "$TEST_VERBOSE" || [ $_ret -ne 0 ] ; then
1275 echo "##################################################"
1276 echo "Iteration ${_iteration}:"
1279 if [ $_ret -ne 0 ] ; then