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_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb"
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_ETCDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc"
38 die "Unable to setup \$CTDB_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_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb/${FAKE_CTDB_PNN}"
295 mkdir -p "$CTDB_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 _master _dev ; do
616 if [ "$_master" = "master" ] ; then
617 export FAKE_CTDB_NATGW_MASTER="$_ip"
619 done >"$CTDB_NATGW_NODES"
621 # Assume all of the nodes are on a /24 network and have IPv4
623 read _ip <"$CTDB_NATGW_NODES"
624 export CTDB_NATGW_PRIVATE_NETWORK="${_ip%.*}.0/24"
626 # These are fixed. Probably don't use the same network for the
627 # private node IPs. To unset the default gateway just set it to
629 export CTDB_NATGW_PUBLIC_IP="10.1.1.121/24"
630 export CTDB_NATGW_PUBLIC_IFACE="eth1"
631 export CTDB_NATGW_DEFAULT_GATEWAY="10.1.1.254"
632 export CTDB_NATGW_SLAVE_ONLY=""
635 ok_natgw_master_ip_addr_show ()
637 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
639 # This is based on CTDB_NATGW_PUBLIC_IP
643 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
644 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
645 inet ${CTDB_NATGW_PUBLIC_IP} brd ${_brd} scope global ${CTDB_NATGW_PUBLIC_IFACE}
646 valid_lft forever preferred_lft forever
650 ok_natgw_slave_ip_addr_show ()
652 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
654 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
655 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
659 ok_natgw_master_static_routes ()
664 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
665 # This is intentionally different to the code in 11.natgw ;-)
668 _net=$(echo "$_i" | sed -e 's|@.*||')
669 _gw=$(echo "$_i" | sed -e 's|.*@||')
673 _gw="$CTDB_NATGW_DEFAULT_GATEWAY"
676 [ -n "$_gw" ] || continue
677 _t="${_t}${_t:+${_nl}}"
678 _t="${_t}${_net} via ${_gw} dev ethXXX metric 10 "
680 _t=$(echo "$_t" | sort)
684 ok_natgw_slave_static_routes ()
689 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
690 # This is intentionally different to the code in 11.natgw ;-)
691 _net=$(echo "$_i" | sed -e 's|@.*||')
693 # The interface for the private network isn't specified as
694 # part of the NATGW configuration and isn't part of the
695 # command to add the route. It is implicitly added by "ip
696 # route" but our stub doesn't do this and adds "ethXXX".
697 _t="${_t}${_t:+${_nl}}"
698 _t="${_t}${_net} via ${FAKE_CTDB_NATGW_MASTER} dev ethXXX metric 10 "
700 _t=$(echo "$_t" | sort)
704 ######################################################################
706 # Samba/winbind fakery
714 if [ "$1" != "down" ] ; then
716 debug "Marking Samba services as up, listening and managed by CTDB"
717 # Get into known state.
718 eventscript_call ctdb_service_managed
720 # All possible service names for all known distros.
721 for i in "smb" "nmb" "samba" ; do
722 service "$i" force-started
725 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
726 export CTDB_MANAGED_SERVICES="foo samba bar"
728 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
729 export FAKE_WBINFO_FAIL="no"
731 # Some things in 50.samba are backgrounded and waited for. If
732 # we don't sleep at all then timeouts can happen. This avoids
734 export FAKE_SLEEP_FORCE=0.1
736 debug "Marking Samba services as down, not listening and not managed by CTDB"
737 # Get into known state.
738 eventscript_call ctdb_service_unmanaged
740 # All possible service names for all known distros.
741 for i in "smb" "nmb" "samba" ; do
742 service "$i" force-stopped
745 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
746 export CTDB_MANAGED_SERVICES="foo bar"
747 unset CTDB_MANAGES_SAMBA
749 export FAKE_TCP_LISTEN=""
750 export FAKE_WBINFO_FAIL="yes"
758 service_name="winbind"
760 if [ "$1" != "down" ] ; then
762 debug "Marking Winbind service as up and managed by CTDB"
763 # Get into known state.
764 eventscript_call ctdb_service_managed
766 service "winbind" force-started
768 export CTDB_MANAGED_SERVICES="foo winbind bar"
770 export FAKE_WBINFO_FAIL="no"
773 debug "Marking Winbind service as down and not managed by CTDB"
774 # Get into known state.
775 eventscript_call ctdb_service_unmanaged
777 service "winbind" force-stopped
779 export CTDB_MANAGED_SERVICES="foo bar"
780 unset CTDB_MANAGES_WINBIND
782 export FAKE_WBINFO_FAIL="yes"
788 debug "Making wbinfo commands fail"
789 FAKE_WBINFO_FAIL="yes"
792 ######################################################################
802 export FAKE_RPCINFO_SERVICES=""
804 export CTDB_NFS_SKIP_SHARE_CHECK="no"
808 # Reset the failcounts for nfs services.
809 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
811 if [ "$1" != "down" ] ; then
812 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
814 eventscript_call ctdb_service_managed
815 service "nfs" force-started
816 service "nfslock" force-started
818 export CTDB_MANAGED_SERVICES="foo nfs bar"
821 "portmapper" "nfs" "mountd" "rquotad" "nlockmgr" "status"
823 nfs_setup_fake_threads "nfsd"
824 nfs_setup_fake_threads "rpc.foobar" # Just set the variable to empty
826 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
828 eventscript_call ctdb_service_unmanaged
829 service "nfs" force-stopped
830 service "nfslock" force-stopped
832 export CTDB_MANAGED_SERVICES="foo bar"
833 unset CTDB_MANAGES_NFS
836 # This is really nasty. However, when we test NFS we don't
837 # actually test statd-callout. If we leave it there then left
838 # over, backgrounded instances of statd-callout will do horrible
839 # things with the "ctdb ip" stub and cause the actual
840 # statd-callout tests that follow to fail.
841 rm "${CTDB_BASE}/statd-callout"
847 export CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-ganesha-callout"
848 if [ "$1" != "down" ] ; then
849 export CTDB_MANAGES_NFS="yes"
852 export CTDB_NFS_SKIP_SHARE_CHECK="yes"
858 debug "Marking RPC service \"${_i}\" as unavailable"
859 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
866 debug "Marking RPC service \"${_i}\" as available"
868 portmapper) _t="2:4" ;;
872 nlockmgr) _t="3:4" ;;
874 *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
877 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
884 _etc="$CTDB_ETCDIR" # shortcut for readability
885 for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
886 if [ -r "$_c" ] ; then
893 nfs_setup_fake_threads ()
899 export PROCFS_PATH=$(mktemp -d --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
900 _threads="${PROCFS_PATH}/fs/nfsd/threads"
901 mkdir -p $(dirname "$_threads")
903 export FAKE_NFSD_THREAD_PIDS="$*"
906 export FAKE_RPC_THREAD_PIDS="$*"
911 program_stack_traces ()
917 for _pid in ${FAKE_NFSD_THREAD_PIDS:-$FAKE_RPC_THREAD_PIDS} ; do
918 [ $_count -le $_max ] || break
921 Stack trace for ${_prog}[${_pid}]:
922 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff
924 _count=$(($_count + 1))
931 $CTDB_NFS_CALLOUT\ start\ nlockmgr)
932 echo "&Starting nfslock: OK"
934 $CTDB_NFS_CALLOUT\ start\ nfs)
936 &Starting nfslock: OK
945 # Set the required result for a particular RPC program having failed
946 # for a certain number of iterations. This is probably still a work
947 # in progress. Note that we could hook aggressively
948 # nfs_check_rpc_service() to try to implement this but we're better
949 # off testing nfs_check_rpc_service() using independent code... even
950 # if it is incomplete and hacky. So, if the 60.nfs eventscript
951 # changes and the tests start to fail then it may be due to this
952 # function being incomplete.
953 rpc_set_service_failure_response ()
956 _numfails="${2:-1}" # default 1
960 if [ $_numfails -eq 0 ] ; then
966 # A handy newline. :-)
970 _dir="${CTDB_NFS_CHECKS_DIR:-${CTDB_BASE}/nfs-checks.d}"
972 _file=$(ls "$_dir"/[0-9][0-9]."${_rpc_service}.check")
973 [ -r "$_file" ] || die "RPC check file \"$_file\" does not exist or is not unique"
975 _out=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
976 _rc_file=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
979 # Subshell to restrict scope variables...
991 # Don't bother syntax checking, eventscript does that...
994 # Just use the first version, or use default. This is dumb but
995 # handles all the cases that we care about now...
996 if [ -n "$version" ] ; then
997 _ver="${version%% *}"
999 case "$_rpc_service" in
1000 portmapper) _ver="" ;;
1005 $_rpc_service failed RPC check:
1006 rpcinfo: RPC: Program not registered
1007 program $_rpc_service${_ver:+ version }${_ver} is not available"
1009 if [ $unhealthy_after -gt 0 -a $_numfails -ge $unhealthy_after ] ; then
1012 echo "ERROR: ${_rpc_check_out}" >>"$_out"
1018 if [ $restart_every -gt 0 -a $(($_numfails % $restart_every)) -eq 0 ] ; then
1019 if ! $_unhealthy ; then
1020 echo "WARNING: ${_rpc_check_out}" >>"$_out"
1023 echo "Trying to restart service \"${_rpc_service}\"..." >>"$_out"
1025 if [ -n "$service_debug_cmd" ] ; then
1026 $service_debug_cmd 2>&1 >>"$_out"
1029 guess_output "$service_start_cmd" >>"$_out"
1033 read _rc <"$_rc_file"
1034 required_result $_rc <"$_out"
1036 rm -f "$_out" "$_rc_file"
1039 ######################################################################
1045 service_name="vsftpd"
1047 if [ "$1" != "down" ] ; then
1048 die "setup_vsftpd up not implemented!!!"
1050 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
1052 eventscript_call ctdb_service_unmanaged
1053 service vsftpd force-stopped
1055 export CTDB_MANAGED_SERVICES="foo"
1056 unset CTDB_MANAGES_VSFTPD
1060 ######################################################################
1066 if [ "$1" != "down" ] ; then
1067 die "setup_httpd up not implemented!!!"
1069 debug "Setting up HTTPD environment: service down, not managed by CTDB"
1071 for service_name in "apache2" "httpd" ; do
1072 eventscript_call ctdb_service_unmanaged
1073 service "$service_name" force-stopped
1076 export CTDB_MANAGED_SERVICES="foo"
1077 unset CTDB_MANAGES_HTTPD
1081 ######################################################################
1091 echo "Marking ${_t} as having no active paths"
1092 FAKE_MULTIPATH_FAILURES="${FAKE_MULTIPATH_FAILURES}${FAKE_MULTIPATH+FAILURES:+ }${_t}"
1097 CTDB_MONITOR_MPDEVICES="${CTDB_MONITOR_MPDEVICES}${CTDB_MONITOR_MPDEVICES:+ }${_t}"
1100 export CTDB_MONITOR_MPDEVICES FAKE_MULTIPATH_FAILURES
1101 export FAKE_SLEEP_FORCE=0.1
1104 ######################################################################
1106 # Result and test functions
1108 # Set some globals and print the summary.
1113 _f=$(basename "$0" ".sh")
1115 # Remaining format should be NN.service.event.NNN or NN.service.NNN:
1123 script_dir="${CTDB_BASE}/events.d"
1128 script_dir="${CTDB_BASE}/events.d"
1133 script_dir="${CTDB_BASE}"
1138 script_dir="${CTDB_BASE}"
1141 [ -r "${script_dir}/${script}" ] || \
1142 die "Internal error - unable to find script \"${script_dir}/${script}\""
1144 printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
1147 # Run an eventscript once. The test passes if the return code and
1148 # output match those required.
1150 # Any args are passed to the eventscript.
1154 [ -n "$event" ] || die 'simple_test: $event not set'
1160 echo "Running script \"$script $event${args:+ }$args\""
1167 ##################################################
1168 CTDB_BASE="$CTDB_BASE"
1169 CTDB_ETCDIR="$CTDB_ETCDIR"
1170 ctdb client is "$(which ctdb)"
1171 ip command is "$(which ip)"
1175 script_test "${script_dir}/${script}" "$event" "$@"
1181 simple_test_event ()
1183 # If something has previously failed then don't continue.
1185 $_passed || return 1
1188 echo "=================================================="
1192 simple_test_command ()
1197 # Run an NFS eventscript iteratively.
1199 # - 1st argument is the number of iterations.
1201 # - 2nd argument is the NFS/RPC service being tested
1203 # rpcinfo (or $service_check_cmd) is used on each iteration to test
1204 # the availability of the service
1206 # If this is not set or null then no RPC service is checked and the
1207 # required output is not reset on each iteration. This is useful in
1208 # baseline tests to confirm that the eventscript and test
1209 # infrastructure is working correctly.
1211 # - Subsequent arguments come in pairs: an iteration number and
1212 # something to eval before that iteration. Each time an iteration
1213 # number is matched the associated argument is given to eval after
1214 # the default setup is done. The iteration numbers need to be given
1215 # in ascending order.
1217 # These arguments can allow a service to be started or stopped
1218 # before a particular iteration.
1224 if [ -n "$2" ] ; then
1230 echo "Running $_repeats iterations of \"$script $event\" $args"
1232 _iterate_failcount=0
1233 for _iteration in $(seq 1 $_repeats) ; do
1234 # This is not a numerical comparison because $1 will often not
1236 if [ "$_iteration" = "$1" ] ; then
1237 debug "##################################################"
1239 debug "##################################################"
1242 if [ -n "$_rpc_service" ] ; then
1244 if [ -n "$service_check_cmd" ] ; then
1245 if eval "$service_check_cmd" ; then
1249 if rpcinfo -T tcp localhost "$_rpc_service" >/dev/null 2>&1 ; then
1255 _iterate_failcount=0
1257 _iterate_failcount=$(($_iterate_failcount + 1))
1259 rpc_set_service_failure_response "$_rpc_service" $_iterate_failcount
1261 _out=$(simple_test 2>&1)
1263 if "$TEST_VERBOSE" || [ $_ret -ne 0 ] ; then
1264 echo "##################################################"
1265 echo "Iteration ${_iteration}:"
1268 if [ $_ret -ne 0 ] ; then