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_TCP_LISTEN FAKE_NETSTAT_UNIX_LISTEN
133 debug "Marking TCP port \"${_i}\" as not listening"
134 FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
146 _cip_prefix="${_cip_base%.*}"
147 _cip_suffix="${_cip_base##*.}"
149 for _i in $(seq 1 $_count) ; do
150 _cip_last=$((_cip_suffix + _i))
151 _cip="${_cip_prefix}.${_cip_last}"
152 _cport=$((_cport_base + _i))
153 echo "${_sip}:${_sport} ${_cip}:${_cport}"
157 setup_tcp_connections ()
159 _t==$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
160 export FAKE_NETSTAT_TCP_ESTABLISHED_FILE"$_t"
161 _tcp_connections "$@" >"$FAKE_NETSTAT_TCP_ESTABLISHED_FILE"
164 setup_tcp_connections_unkillable ()
166 # These connections are listed by the "ss" stub but are not
167 # killed by the "ctdb killtcp" stub. So killing these
168 # connections will never succeed... and will look like a time
170 _t=$(_tcp_connections "$@" | sed -e 's/ /|/g')
171 export FAKE_NETSTAT_TCP_ESTABLISHED="$_t"
178 # Replace some shares with non-existent ones.
183 export MISSING_SHARES_TEXT=""
184 for _i in $FAKE_SHARES ; do
185 if [ $_n = "$1" ] ; then
187 _i="${_i%_existing}_missing"
188 debug "Replacing share $_n with missing share \"$_i\""
189 rmdir "$_i" 2>/dev/null || true
190 MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}")
192 _t="${_t}${_t:+ }${_i}"
198 # Setup some fake /proc/net/bonding files with just enough info for
201 # arg1 is interface name, arg2 is currently active slave (use "None"
202 # if none), arg3 is MII status ("up" or "down").
206 _slave="${2:-${_iface}_sl_0}"
208 _mii_subs="${4:-${_mii_s:-up}}"
209 echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s"
210 cat >"${FAKE_PROC_NET_BONDING}/$_iface" <<EOF
211 Bonding Mode: IEEE 802.3ad Dynamic link aggregation
212 Currently Active Slave: $_slave
215 # Status of 1st pretend adapter
216 MII Status: $_mii_subs
217 # Status of 2nd pretend adapter
218 MII Status: $_mii_subs
222 ethtool_interfaces_down ()
225 echo "Marking interface $_i DOWN for ethtool"
226 touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
230 ethtool_interfaces_up ()
233 echo "Marking interface $_i UP for ethtool"
234 rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
240 echo "# ip rule show"
244 while read _p _x _i _x _t ; do
245 # Remove trailing colon after priority/preference.
247 # Only remove rules that match our priority/preference.
248 [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
250 echo "# ip route show table $_t"
251 ip route show table "$_t"
255 # Copied from 13.per_ip_routing for now... so this is lazy testing :-(
256 ipv4_host_addr_to_net ()
261 # Convert the host address to an unsigned long by splitting out
262 # the octets and doing the math.
264 for _o in $(export IFS="." ; echo $_host) ; do
265 _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
268 # Calculate the mask and apply it.
269 _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
270 _net_ul=$(( $_host_ul & $_mask_ul ))
272 # Now convert to a network address one byte at a time.
274 for _o in $(seq 1 4) ; do
275 _net="$(($_net_ul & 255))${_net:+.}${_net}"
276 _net_ul=$(($_net_ul >> 8))
279 echo "${_net}/${_maskbits}"
282 ######################################################################
286 # Evaluate an expression that probably calls functions or uses
287 # variables from the CTDB functions file. This is used for test
292 . "$CTDB_BASE/functions"
297 # For now this creates the same public addresses each time. However,
298 # it could be made more flexible.
299 setup_public_addresses ()
301 if [ -f "$CTDB_PUBLIC_ADDRESSES" -a \
302 "${CTDB_PUBLIC_ADDRESSES%/*}" = "$EVENTSCRIPTS_TESTS_VAR_DIR" ] ; then
303 rm "$CTDB_PUBLIC_ADDRESSES"
306 export CTDB_PUBLIC_ADDRESSES=$(mktemp \
307 --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR" \
308 "public-addresses-XXXXXXXX")
310 echo "Setting up CTDB_PUBLIC_ADDRESSES=${CTDB_PUBLIC_ADDRESSES}"
311 cat >"$CTDB_PUBLIC_ADDRESSES" <<EOF
324 # Need to cope with ctdb_get_pnn(). If a test changes PNN then it
325 # needs to be using a different state directory, otherwise the wrong
326 # PNN can already be cached in the state directory.
329 export FAKE_CTDB_PNN="$1"
330 echo "Setting up PNN ${FAKE_CTDB_PNN}"
332 export CTDB_SCRIPT_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/script-state/${FAKE_CTDB_PNN}"
333 mkdir -p "$CTDB_SCRIPT_VARDIR"
340 export FAKE_CTDB_NUMNODES="${1:-3}"
341 echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
343 ctdb_set_pnn "${2:-0}"
345 setup_public_addresses
347 export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
349 export FAKE_CTDB_EXTRA_CONFIG="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-config.sh"
350 rm -f "$FAKE_CTDB_EXTRA_CONFIG"
352 export FAKE_CTDB_IFACES_DOWN="$FAKE_CTDB_STATE/ifaces-down"
353 mkdir -p "$FAKE_CTDB_IFACES_DOWN"
354 rm -f "$FAKE_CTDB_IFACES_DOWN"/*
356 export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus"
357 mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
358 rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
360 export CTDB_PARTIALLY_ONLINE_INTERFACES
362 export FAKE_CTDB_TUNABLES_OK="MonitorInterval TDBMutexEnabled DatabaseHashSize"
363 export FAKE_CTDB_TUNABLES_OBSOLETE="EventScriptUnhealthyOnTimeout"
368 cat >"$FAKE_CTDB_EXTRA_CONFIG"
371 validate_percentage ()
374 [0-9]|[0-9][0-9]|100) return 0 ;;
375 *) echo "WARNING: ${1} is an invalid percentage${2:+\" in }${2}${2:+\"}"
382 _mem_usage="${1:-10}" # Default is 10%
383 _swap_usage="${2:-0}" # Default is 0%
388 _swap_free=$(( (100 - $_swap_usage) * $_swap_total / 100 ))
393 _mem_cached=$(( $_mem_total * (100 - $_mem_usage) / 100 - $_mem_free - $_mem_buffers ))
395 export FAKE_PROC_MEMINFO="\
396 MemTotal: ${_mem_total} kB
397 MemFree: ${_mem_free} kB
398 Buffers: ${_mem_buffers} kB
399 Cached: ${_mem_cached} kB
403 Active(anon): 1917580 kB
404 Inactive(anon): 523080 kB
405 Active(file): 504524 kB
406 Inactive(file): 496848 kB
409 SwapTotal: ${_swap_total} kB
410 SwapFree: ${_swap_free} kB
413 export CTDB_MONITOR_MEMORY_USAGE
414 export CTDB_MONITOR_SWAP_USAGE
419 export FAKE_FS_USE="${1:-10}" # Default is 10% usage
421 # Causes some variables to be exported
424 export CTDB_MONITOR_FILESYSTEM_USAGE
427 ctdb_get_interfaces ()
429 # The echo/subshell forces all the output onto 1 line.
430 echo $(ctdb ifaces -X | awk -F'|' 'FNR > 1 {print $2}')
433 ctdb_get_1_interface ()
435 _t=$(ctdb_get_interfaces)
439 # Print all public addresses as: interface IP maskbits
440 # Each line is suitable for passing to takeip/releaseip
441 ctdb_get_all_public_addresses ()
443 _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
444 while IFS="/$IFS" read _ip _maskbits _ifaces ; do
445 echo "$_ifaces $_ip $_maskbits"
449 # Print public addresses on this node as: interface IP maskbits
450 # Each line is suitable for passing to takeip/releaseip
451 ctdb_get_my_public_addresses ()
454 read _x # skip header line
456 while IFS="|" read _x _ip _x _iface _x ; do
457 [ -n "$_iface" ] || continue
458 while IFS="/$IFS" read _i _maskbits _x ; do
459 if [ "$_ip" = "$_i" ] ; then
460 echo $_iface $_ip $_maskbits
463 done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
468 # Prints the 1st public address as: interface IP maskbits
469 # This is suitable for passing to takeip/releaseip
470 ctdb_get_1_public_address ()
472 ctdb_get_my_public_addresses | { head -n 1 ; cat >/dev/null ; }
475 ctdb_not_implemented ()
477 export CTDB_NOT_IMPLEMENTED="$1"
478 ctdb_not_implemented="\
479 DEBUG: ctdb: command \"$1\" not implemented in stub"
482 ctdb_fake_scriptstatus ()
491 echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
494 ######################################################################
496 setup_ctdb_policy_routing ()
498 service_name="per_ip_routing"
500 export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing"
501 export CTDB_PER_IP_ROUTING_RULE_PREF=100
502 export CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000
503 export CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000
505 # Tests need to create and populate this file
506 rm -f "$CTDB_PER_IP_ROUTING_CONF"
509 # Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF.
510 # $1 is the number of assigned IPs to use (<num>, all), defaulting to
511 # 1. If $2 is "default" then a default route is also added.
512 create_policy_routing_config ()
515 _should_add_default="$2"
517 ctdb_get_my_public_addresses |
518 if [ "$_num_ips" = "all" ] ; then
521 { head -n "$_num_ips" ; cat >/dev/null ; }
523 while read _dev _ip _bits ; do
524 _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
525 _gw="${_net%.*}.254" # a dumb, calculated default
529 if [ "$_should_add_default" = "default" ] ; then
530 echo "$_ip 0.0.0.0/0 $_gw"
532 done >"$CTDB_PER_IP_ROUTING_CONF"
535 # Check the routes against those that are expected. $1 is the number
536 # of assigned IPs to use (<num>, all), defaulting to 1. If $2 is
537 # "default" then expect default routes to have been added.
541 _should_add_default="$2"
546 ctdb_get_my_public_addresses |
547 if [ "$_num_ips" = "all" ] ; then
550 { head -n "$_num_ips" ; cat >/dev/null ; }
552 while read _dev _ip _bits ; do
553 _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
554 _gw="${_net%.*}.254" # a dumb, calculated default
556 _policy_rules="${_policy_rules}
557 ${CTDB_PER_IP_ROUTING_RULE_PREF}: from $_ip lookup ctdb.$_ip "
558 _policy_routes="${_policy_routes}
559 # ip route show table ctdb.$_ip
560 $_net dev $_dev scope link "
562 if [ "$_should_add_default" = "default" ] ; then
563 _policy_routes="${_policy_routes}
564 default via $_gw dev $_dev "
570 0: from all lookup local ${_policy_rules}
571 32766: from all lookup main
572 32767: from all lookup default ${_policy_routes}
575 simple_test_command dump_routes
579 ######################################################################
583 lvs_state_dir="${EVENTSCRIPTS_TESTS_VAR_DIR}/lvs"
584 mkdir -p "$lvs_state_dir"
586 export FAKE_LVS_STATE_DIR="${lvs_state_dir}/state"
587 mkdir "$FAKE_LVS_STATE_DIR"
589 lvs_header=$(ipvsadm -l -n)
591 export CTDB_LVS_PUBLIC_IP="$1"
592 export CTDB_LVS_PUBLIC_IFACE="$2"
594 [ -n "$CTDB_LVS_PUBLIC_IP" ] || return 0
595 [ -n "$CTDB_LVS_PUBLIC_IFACE" ] || return 0
597 export CTDB_LVS_NODES=$(mktemp --tmpdir="$lvs_state_dir")
598 export FAKE_CTDB_LVS_MASTER=""
602 while read _ip _opts ; do
605 FAKE_CTDB_LVS_MASTER="$_pnn"
609 printf "%s\tslave-only\n" "$_ip"
616 done >"$CTDB_LVS_NODES"
621 if [ "$1" = "NULL" ] ; then
622 required_result 0 <<EOF
626 required_result 0 <<EOF
632 simple_test_command ipvsadm -l -n
639 if [ "$_scope" = "NULL" ] ; then
640 required_result 0 <<EOF
641 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
642 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
645 required_result 0 <<EOF
646 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
647 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
648 inet ${CTDB_LVS_PUBLIC_IP}/32 scope ${_scope} lo
649 valid_lft forever preferred_lft forever
653 simple_test_command ip addr show dev lo
656 ######################################################################
658 ctdb_catdb_format_pairs ()
662 while read _k _v ; do
663 _kn=$(echo -n "$_k" | wc -c)
664 _vn=$(echo -n "$_v" | wc -c)
666 key(${_kn}) = "${_k}"
669 data(${_vn}) = "${_v}"
672 _count=$(($_count + 1))
675 echo "Dumped ${_count} records"
678 check_ctdb_tdb_statd_state ()
680 ctdb_get_my_public_addresses |
681 while read _x _sip _x ; do
683 echo "statd-state@${_sip}@${_cip}" "$FAKE_DATE_OUTPUT"
686 ctdb_catdb_format_pairs | {
688 simple_test_command ctdb catdb ctdb.tdb
692 check_statd_callout_smnotify ()
694 _state_even=$(( $(date '+%s') / 2 * 2))
695 _state_odd=$(($_state_even + 1))
699 ctdb_get_my_public_addresses |
700 while read _x _sip _x ; do
703 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_even}
704 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_even}
705 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_odd}
706 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_odd}
711 simple_test_event "notify"
715 ######################################################################
719 debug "Setting up NAT gateway"
721 natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
722 mkdir -p "$natgw_config_dir"
724 # These will accumulate, 1 per test... but will be cleaned up at
726 export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
729 while read _ip _opts ; do
732 export FAKE_CTDB_NATGW_MASTER="$_ip"
736 printf "%s\tslave-only\n" "$_ip"
742 done >"$CTDB_NATGW_NODES"
744 # Assume all of the nodes are on a /24 network and have IPv4
746 read _ip <"$CTDB_NATGW_NODES"
747 export CTDB_NATGW_PRIVATE_NETWORK="${_ip%.*}.0/24"
749 # These are fixed. Probably don't use the same network for the
750 # private node IPs. To unset the default gateway just set it to
752 export CTDB_NATGW_PUBLIC_IP="10.1.1.121/24"
753 export CTDB_NATGW_PUBLIC_IFACE="eth1"
754 export CTDB_NATGW_DEFAULT_GATEWAY="10.1.1.254"
755 export CTDB_NATGW_SLAVE_ONLY=""
758 ok_natgw_master_ip_addr_show ()
760 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
762 # This is based on CTDB_NATGW_PUBLIC_IP
766 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
767 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
768 inet ${CTDB_NATGW_PUBLIC_IP} brd ${_brd} scope global ${CTDB_NATGW_PUBLIC_IFACE}
769 valid_lft forever preferred_lft forever
773 ok_natgw_slave_ip_addr_show ()
775 _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
777 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
778 link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
782 ok_natgw_master_static_routes ()
787 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
788 # This is intentionally different to the code in 11.natgw ;-)
791 _net=$(echo "$_i" | sed -e 's|@.*||')
792 _gw=$(echo "$_i" | sed -e 's|.*@||')
796 _gw="$CTDB_NATGW_DEFAULT_GATEWAY"
799 [ -n "$_gw" ] || continue
800 _t="${_t}${_t:+${_nl}}"
801 _t="${_t}${_net} via ${_gw} dev ethXXX metric 10 "
803 _t=$(echo "$_t" | sort)
807 ok_natgw_slave_static_routes ()
812 for _i in $CTDB_NATGW_STATIC_ROUTES ; do
813 # This is intentionally different to the code in 11.natgw ;-)
814 _net=$(echo "$_i" | sed -e 's|@.*||')
816 # The interface for the private network isn't specified as
817 # part of the NATGW configuration and isn't part of the
818 # command to add the route. It is implicitly added by "ip
819 # route" but our stub doesn't do this and adds "ethXXX".
820 _t="${_t}${_t:+${_nl}}"
821 _t="${_t}${_net} via ${FAKE_CTDB_NATGW_MASTER} dev ethXXX metric 10 "
823 _t=$(echo "$_t" | sort)
827 ######################################################################
829 # Samba/winbind fakery
837 if [ "$1" != "down" ] ; then
839 debug "Marking Samba services as up, listening and managed by CTDB"
841 # All possible service names for all known distros.
842 for i in "smb" "nmb" "samba" "smbd" "nmbd" ; do
843 service "$i" force-started
846 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
847 export CTDB_MANAGED_SERVICES="foo samba bar"
849 export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
850 export FAKE_WBINFO_FAIL="no"
852 # Some things in 50.samba are backgrounded and waited for. If
853 # we don't sleep at all then timeouts can happen. This avoids
855 export FAKE_SLEEP_FORCE=0.1
857 debug "Marking Samba services as down, not listening and not managed by CTDB"
859 # All possible service names for all known distros.
860 for i in "smb" "nmb" "samba" "smbd" "nmbd" ; do
861 service "$i" force-stopped
864 export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
865 export CTDB_MANAGED_SERVICES="foo bar"
866 unset CTDB_MANAGES_SAMBA
868 export FAKE_TCP_LISTEN=""
869 export FAKE_WBINFO_FAIL="yes"
873 samba_setup_fake_threads ()
875 export FAKE_SMBD_THREAD_PIDS="$*"
882 [ "$_count" -lt 5 ] || break
883 _t=$(program_stack_trace "smbd" $_pid)
884 _out="${_out:+${_out}${_nl}}${_t}"
885 _count=$((_count + 1))
887 SAMBA_STACK_TRACES="$_out"
894 service_name="winbind"
896 if [ "$1" != "down" ] ; then
898 debug "Marking Winbind service as up and managed by CTDB"
900 service "winbind" force-started
902 export CTDB_MANAGED_SERVICES="foo winbind bar"
904 export FAKE_WBINFO_FAIL="no"
907 debug "Marking Winbind service as down and not managed by CTDB"
909 service "winbind" force-stopped
911 export CTDB_MANAGED_SERVICES="foo bar"
912 unset CTDB_MANAGES_WINBIND
914 export FAKE_WBINFO_FAIL="yes"
920 debug "Making wbinfo commands fail"
921 FAKE_WBINFO_FAIL="yes"
924 ######################################################################
934 export FAKE_RPCINFO_SERVICES=""
936 export CTDB_NFS_SKIP_SHARE_CHECK="no"
940 # This doesn't even need to exist
941 export CTDB_NFS_EXPORTS_FILE="$EVENTSCRIPTS_TESTS_VAR_DIR/etc-exports"
943 # Reset the failcounts for nfs services.
944 eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
946 if [ "$1" != "down" ] ; then
947 debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
949 service "nfs" force-started
950 service "nfslock" force-started
952 export CTDB_MANAGED_SERVICES="foo nfs bar"
955 "portmapper" "nfs" "mountd" "rquotad" "nlockmgr" "status"
957 nfs_setup_fake_threads "nfsd"
958 nfs_setup_fake_threads "rpc.foobar" # Just set the variable to empty
960 debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
962 service "nfs" force-stopped
963 service "nfslock" force-stopped
965 export CTDB_MANAGED_SERVICES="foo bar"
966 unset CTDB_MANAGES_NFS
969 # This is really nasty. However, when we test NFS we don't
970 # actually test statd-callout. If we leave it there then left
971 # over, backgrounded instances of statd-callout will do horrible
972 # things with the "ctdb ip" stub and cause the actual
973 # statd-callout tests that follow to fail.
974 rm "${CTDB_BASE}/statd-callout"
980 export CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-ganesha-callout"
981 if [ "$1" != "down" ] ; then
982 export CTDB_MANAGES_NFS="yes"
985 export CTDB_NFS_SKIP_SHARE_CHECK="yes"
991 debug "Marking RPC service \"${_i}\" as unavailable"
992 FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
999 debug "Marking RPC service \"${_i}\" as available"
1001 portmapper) _t="2:4" ;;
1004 rquotad) _t="1:2" ;;
1005 nlockmgr) _t="3:4" ;;
1007 *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
1010 FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
1017 _etc="$CTDB_SYS_ETCDIR" # shortcut for readability
1018 for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
1019 if [ -r "$_c" ] ; then
1026 nfs_setup_fake_threads ()
1032 export PROCFS_PATH=$(mktemp -d --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1033 _threads="${PROCFS_PATH}/fs/nfsd/threads"
1034 mkdir -p $(dirname "$_threads")
1035 echo $# >"$_threads"
1036 export FAKE_NFSD_THREAD_PIDS="$*"
1039 export FAKE_RPC_THREAD_PIDS="$*"
1044 program_stack_trace ()
1050 Stack trace for ${_prog}[${_pid}]:
1051 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff
1055 program_stack_traces ()
1061 for _pid in ${FAKE_NFSD_THREAD_PIDS:-$FAKE_RPC_THREAD_PIDS} ; do
1062 [ $_count -le $_max ] || break
1064 program_stack_trace "$_prog" "$_pid"
1065 _count=$(($_count + 1))
1072 $CTDB_NFS_CALLOUT\ start\ nlockmgr)
1073 echo "&Starting nfslock: OK"
1075 $CTDB_NFS_CALLOUT\ start\ nfs)
1077 &Starting nfslock: OK
1086 # Set the required result for a particular RPC program having failed
1087 # for a certain number of iterations. This is probably still a work
1088 # in progress. Note that we could hook aggressively
1089 # nfs_check_rpc_service() to try to implement this but we're better
1090 # off testing nfs_check_rpc_service() using independent code... even
1091 # if it is incomplete and hacky. So, if the 60.nfs eventscript
1092 # changes and the tests start to fail then it may be due to this
1093 # function being incomplete.
1094 rpc_set_service_failure_response ()
1097 _numfails="${2:-1}" # default 1
1101 if [ $_numfails -eq 0 ] ; then
1107 # A handy newline. :-)
1111 _dir="${CTDB_NFS_CHECKS_DIR:-${CTDB_BASE}/nfs-checks.d}"
1113 _file=$(ls "$_dir"/[0-9][0-9]."${_rpc_service}.check")
1114 [ -r "$_file" ] || die "RPC check file \"$_file\" does not exist or is not unique"
1116 _out=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1117 _rc_file=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1120 # Subshell to restrict scope variables...
1128 service_start_cmd=""
1129 service_check_cmd=""
1130 service_debug_cmd=""
1132 # Don't bother syntax checking, eventscript does that...
1135 # Just use the first version, or use default. This is dumb but
1136 # handles all the cases that we care about now...
1137 if [ -n "$version" ] ; then
1138 _ver="${version%% *}"
1140 case "$_rpc_service" in
1141 portmapper) _ver="" ;;
1146 $_rpc_service failed RPC check:
1147 rpcinfo: RPC: Program not registered
1148 program $_rpc_service${_ver:+ version }${_ver} is not available"
1150 if [ $unhealthy_after -gt 0 -a $_numfails -ge $unhealthy_after ] ; then
1153 echo "ERROR: ${_rpc_check_out}" >>"$_out"
1159 if [ $restart_every -gt 0 ] && \
1160 [ $(($_numfails % $restart_every)) -eq 0 ] ; then
1161 if ! $_unhealthy ; then
1162 echo "WARNING: ${_rpc_check_out}" >>"$_out"
1165 echo "Trying to restart service \"${_rpc_service}\"..." >>"$_out"
1167 if [ -n "$service_debug_cmd" ] ; then
1168 $service_debug_cmd 2>&1 >>"$_out"
1171 guess_output "$service_start_cmd" >>"$_out"
1175 read _rc <"$_rc_file"
1176 required_result $_rc <"$_out"
1178 rm -f "$_out" "$_rc_file"
1181 ######################################################################
1183 # Recovery lock fakery
1187 _pattern="${script_dir}/${script}"
1188 while pgrep -f "$_pattern" >/dev/null ; do
1189 echo "Waiting for backgrounded ${script} to exit..."
1190 (FAKE_SLEEP_REALLY=yes sleep 1)
1196 CTDB_RECOVERY_LOCK=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1197 export CTDB_RECOVERY_LOCK
1199 test_cleanup cleanup_reclock
1202 ######################################################################
1208 service_name="vsftpd"
1210 if [ "$1" != "down" ] ; then
1211 die "setup_vsftpd up not implemented!!!"
1213 debug "Setting up VSFTPD environment: service down, not managed by CTDB"
1215 service vsftpd force-stopped
1217 export CTDB_MANAGED_SERVICES="foo"
1218 unset CTDB_MANAGES_VSFTPD
1222 ######################################################################
1228 if [ "$1" != "down" ] ; then
1229 die "setup_httpd up not implemented!!!"
1231 debug "Setting up HTTPD environment: service down, not managed by CTDB"
1233 for service_name in "apache2" "httpd" ; do
1234 service "$service_name" force-stopped
1237 export CTDB_MANAGED_SERVICES="foo"
1238 unset CTDB_MANAGES_HTTPD
1242 ######################################################################
1252 echo "Marking ${_t} as having no active paths"
1253 FAKE_MULTIPATH_FAILURES="${FAKE_MULTIPATH_FAILURES}${FAKE_MULTIPATH+FAILURES:+ }${_t}"
1258 CTDB_MONITOR_MPDEVICES="${CTDB_MONITOR_MPDEVICES}${CTDB_MONITOR_MPDEVICES:+ }${_t}"
1261 export CTDB_MONITOR_MPDEVICES FAKE_MULTIPATH_FAILURES
1262 export FAKE_SLEEP_FORCE=0.1
1265 ######################################################################
1267 # Result and test functions
1269 # Set some globals and print the summary.
1274 _f=$(basename "$0" ".sh")
1276 # Remaining format should be NN.service.event.NNN or NN.service.NNN:
1284 script_dir="${CTDB_BASE}/events.d"
1289 script_dir="${CTDB_BASE}/events.d"
1294 script_dir="${CTDB_BASE}"
1299 script_dir="${CTDB_BASE}"
1302 [ -r "${script_dir}/${script}" ] || \
1303 die "Internal error - unable to find script \"${script_dir}/${script}\""
1305 printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
1308 # Run an eventscript once. The test passes if the return code and
1309 # output match those required.
1311 # Any args are passed to the eventscript.
1315 [ -n "$event" ] || die 'simple_test: $event not set'
1321 echo "Running script \"$script $event${args:+ }$args\""
1328 ##################################################
1329 CTDB_BASE="$CTDB_BASE"
1330 CTDB_SYS_ETCDIR="$CTDB_SYS_ETCDIR"
1331 ctdb client is "$(which ctdb)"
1332 ip command is "$(which ip)"
1336 script_test "${script_dir}/${script}" "$event" "$@"
1342 simple_test_event ()
1344 # If something has previously failed then don't continue.
1346 $_passed || return 1
1349 echo "=================================================="
1353 simple_test_command ()
1358 # Run an NFS eventscript iteratively.
1360 # - 1st argument is the number of iterations.
1362 # - 2nd argument is the NFS/RPC service being tested
1364 # rpcinfo (or $service_check_cmd) is used on each iteration to test
1365 # the availability of the service
1367 # If this is not set or null then no RPC service is checked and the
1368 # required output is not reset on each iteration. This is useful in
1369 # baseline tests to confirm that the eventscript and test
1370 # infrastructure is working correctly.
1372 # - Subsequent arguments come in pairs: an iteration number and
1373 # something to eval before that iteration. Each time an iteration
1374 # number is matched the associated argument is given to eval after
1375 # the default setup is done. The iteration numbers need to be given
1376 # in ascending order.
1378 # These arguments can allow a service to be started or stopped
1379 # before a particular iteration.
1385 if [ -n "$2" ] ; then
1391 echo "Running $_repeats iterations of \"$script $event\" $args"
1393 _iterate_failcount=0
1394 for _iteration in $(seq 1 $_repeats) ; do
1395 # This is not a numerical comparison because $1 will often not
1397 if [ "$_iteration" = "$1" ] ; then
1398 debug "##################################################"
1400 debug "##################################################"
1403 if [ -n "$_rpc_service" ] ; then
1405 if [ -n "$service_check_cmd" ] ; then
1406 if eval "$service_check_cmd" ; then
1410 if rpcinfo -T tcp localhost "$_rpc_service" >/dev/null 2>&1 ; then
1416 _iterate_failcount=0
1418 _iterate_failcount=$(($_iterate_failcount + 1))
1420 rpc_set_service_failure_response "$_rpc_service" $_iterate_failcount
1422 _out=$(simple_test 2>&1)
1424 if "$TEST_VERBOSE" || [ $_ret -ne 0 ] ; then
1425 echo "##################################################"
1426 echo "Iteration ${_iteration}:"
1429 if [ $_ret -ne 0 ] ; then