# Hey Emacs, this is a -*- shell-script -*- !!! :-) # Augment PATH with relevant stubs/ directories. We do this by actually # setting PATH, and also by setting $EVENTSCRIPTS_PATH and then # prepending that to $PATH in rc.local to avoid the PATH reset in # functions. EVENTSCRIPTS_PATH="" if [ -d "${TEST_SUBDIR}/stubs" ] ; then EVENTSCRIPTS_PATH="${TEST_SUBDIR}/stubs" fi export EVENTSCRIPTS_PATH PATH="${EVENTSCRIPTS_PATH}:${PATH}" export EVENTSCRIPTS_TESTS_VAR_DIR="${TEST_VAR_DIR}/unit_eventscripts" if [ -d "$EVENTSCRIPTS_TESTS_VAR_DIR" -a \ "$EVENTSCRIPTS_TESTS_VAR_DIR" != "/unit_eventscripts" ] ; then rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR" fi mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR" export CTDB_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb" export CTDB_LOGFILE="${EVENTSCRIPTS_TESTS_VAR_DIR}/log.ctdb" touch "$CTDB_LOGFILE" || die "Unable to create CTDB_LOGFILE=$CTDB_LOGFILE" if [ -d "${TEST_SUBDIR}/etc" ] ; then cp -a "${TEST_SUBDIR}/etc" "$EVENTSCRIPTS_TESTS_VAR_DIR" export CTDB_ETCDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc" else die "Unable to setup \$CTDB_ETCDIR" fi if [ -d "${TEST_SUBDIR}/etc-ctdb" ] ; then cp -prL "${TEST_SUBDIR}/etc-ctdb" "$EVENTSCRIPTS_TESTS_VAR_DIR" export CTDB_BASE="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc-ctdb" else die "Unable to set \$CTDB_BASE" fi export CTDB_BASE if [ ! -d "${CTDB_BASE}/events.d" ] ; then cat </dev/null || true rmdir "$FAKE_IP_STATE"/* 2>/dev/null || true export CTDB_DBDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/db" mkdir -p "${CTDB_DBDIR}/persistent" export FAKE_TDBTOOL_SUPPORTS_CHECK="yes" export FAKE_TDB_IS_OK export FAKE_DATE_OUTPUT export FAKE_NETSTAT_TCP_ESTABLISHED FAKE_TCP_LISTEN FAKE_NETSTAT_UNIX_LISTEN export FAKE_NETSTAT_TCP_ESTABLISHED_FILE=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR") } tcp_port_down () { for _i ; do debug "Marking TCP port \"${_i}\" as not listening" FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g") done } shares_missing () { _fmt="$1" ; shift # Replace some shares with non-existent ones. _t="" _n=1 _nl=" " export MISSING_SHARES_TEXT="" for _i in $FAKE_SHARES ; do if [ $_n = "$1" ] ; then shift _i="${_i%_existing}_missing" debug "Replacing share $_n with missing share \"$_i\"" rmdir "$_i" 2>/dev/null || true MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}") fi _t="${_t}${_t:+ }${_i}" _n=$(($_n + 1)) done FAKE_SHARES="$_t" } # Setup some fake /proc/net/bonding files with just enough info for # the eventscripts. # arg1 is interface name, arg2 is currently active slave (use "None" # if none), arg3 is MII status ("up" or "down"). setup_bond () { _iface="$1" _slave="${2:-${_iface}_sl_0}" _mii_s="${3:-up}" _mii_subs="${4:-${_mii_s:-up}}" echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s" cat >"${FAKE_PROC_NET_BONDING}/$_iface" <> 8)) done echo "${_net}/${_maskbits}" } ###################################################################### # CTDB fakery # Evaluate an expression that probably calls functions or uses # variables from the CTDB functions file. This is used for test # initialisation. eventscript_call () { ( . "$CTDB_BASE/functions" "$@" ) } # Set output for ctdb command. Option 1st argument is return code. ctdb_set_output () { _out="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.out" cat >"$_out" _rc="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.rc" echo "${1:-0}" >"$_rc" eventscripts_test_add_cleanup "rm -f $_out $_rc" } setup_ctdb () { setup_generic export FAKE_CTDB_NUMNODES="${1:-3}" echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes" export FAKE_CTDB_PNN="${2:-0}" echo "Setting up CTDB with PNN ${FAKE_CTDB_PNN}" export CTDB_PUBLIC_ADDRESSES="${CTDB_BASE}/public_addresses" if [ -n "$3" ] ; then echo "Setting up CTDB_PUBLIC_ADDRESSES: $3" CTDB_PUBLIC_ADDRESSES=$(mktemp) for _i in $3 ; do _ip="${_i%@*}" _ifaces="${_i#*@}" echo "${_ip} ${_ifaces}" >>"$CTDB_PUBLIC_ADDRESSES" done eventscripts_test_add_cleanup "rm -f $CTDB_PUBLIC_ADDRESSES" fi export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb" export FAKE_CTDB_IFACES_DOWN="$FAKE_CTDB_STATE/ifaces-down" mkdir -p "$FAKE_CTDB_IFACES_DOWN" rm -f "$FAKE_CTDB_IFACES_DOWN"/* export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus" mkdir -p "$FAKE_CTDB_SCRIPTSTATUS" rm -f "$FAKE_CTDB_SCRIPTSTATUS"/* export CTDB_PARTIALLY_ONLINE_INTERFACES } setup_memcheck () { setup_ctdb _swap_total="5857276" if [ "$1" = "bad" ] ; then _swap_free=" 4352" _mem_cached=" 112" _mem_free=" 468" else _swap_free="$_swap_total" _mem_cached="1112" _mem_free="1468" fi export FAKE_PROC_MEMINFO="\ MemTotal: 3940712 kB MemFree: 225268 kB Buffers: 146120 kB Cached: 1139348 kB SwapCached: 56016 kB Active: 2422104 kB Inactive: 1019928 kB Active(anon): 1917580 kB Inactive(anon): 523080 kB Active(file): 504524 kB Inactive(file): 496848 kB Unevictable: 4844 kB Mlocked: 4844 kB SwapTotal: ${_swap_total} kB SwapFree: ${_swap_free} kB ..." export FAKE_FREE_M="\ total used free shared buffers cached Mem: 3848 3634 213 0 142 ${_mem_cached} -/+ buffers/cache: 2379 ${_mem_free} Swap: 5719 246 5473" export CTDB_MONITOR_FREE_MEMORY export CTDB_MONITOR_FREE_MEMORY_WARN export CTDB_CHECK_SWAP_IS_NOT_USED } ctdb_get_interfaces () { # The echo/subshell forces all the output onto 1 line. echo $(ctdb ifaces -Y | awk -F: 'FNR > 1 {print $2}') } ctdb_get_1_interface () { _t=$(ctdb_get_interfaces) echo ${_t%% *} } # Print all public addresses as: interface IP maskbits # Each line is suitable for passing to takeip/releaseip ctdb_get_all_public_addresses () { _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}" while IFS="/$IFS" read _ip _maskbits _ifaces ; do echo "$_ifaces $_ip $_maskbits" done <"$_f" } # Print public addresses on this node as: interface IP maskbits # Each line is suitable for passing to takeip/releaseip ctdb_get_my_public_addresses () { ctdb ip -v -Y | { read _x # skip header line while IFS=":" read _x _ip _x _iface _x ; do [ -n "$_iface" ] || continue while IFS="/$IFS" read _i _maskbits _x ; do if [ "$_ip" = "$_i" ] ; then echo $_iface $_ip $_maskbits break fi done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}" done } } # Prints the 1st public address as: interface IP maskbits # This is suitable for passing to takeip/releaseip ctdb_get_1_public_address () { ctdb_get_my_public_addresses | { head -n 1 ; cat >/dev/null ; } } ctdb_not_implemented () { export CTDB_NOT_IMPLEMENTED="$1" ctdb_not_implemented="\ DEBUG: ctdb: command \"$1\" not implemented in stub" } ctdb_fake_scriptstatus () { _code="$1" _status="$2" _err_out="$3" _d1=$(date '+%s.%N') _d2=$(date '+%s.%N') echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script" } ###################################################################### setup_ctdb_policy_routing () { service_name="per_ip_routing" export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing" export CTDB_PER_IP_ROUTING_RULE_PREF=100 export CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000 export CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000 # Tests need to create and populate this file rm -f "$CTDB_PER_IP_ROUTING_CONF" } # Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF. # $1 is the number of assigned IPs to use (, all), defaulting to # 1. If $2 is "default" then a default route is also added. create_policy_routing_config () { _num_ips="${1:-1}" _should_add_default="$2" ctdb_get_my_public_addresses | if [ "$_num_ips" = "all" ] ; then cat else { head -n "$_num_ips" ; cat >/dev/null ; } fi | while read _dev _ip _bits ; do _net=$(ipv4_host_addr_to_net "$_ip" "$_bits") _gw="${_net%.*}.1" # a dumb, calculated default echo "$_ip $_net" if [ "$_should_add_default" = "default" ] ; then echo "$_ip 0.0.0.0/0 $_gw" fi done >"$CTDB_PER_IP_ROUTING_CONF" } # Check the routes against those that are expected. $1 is the number # of assigned IPs to use (, all), defaulting to 1. If $2 is # "default" then expect default routes to have been added. check_routes () { _num_ips="${1:-1}" _should_add_default="$2" _policy_rules="" _policy_routes="" ctdb_get_my_public_addresses | if [ "$_num_ips" = "all" ] ; then cat else { head -n "$_num_ips" ; cat >/dev/null ; } fi | { while read _dev _ip _bits ; do _net=$(ipv4_host_addr_to_net "$_ip" "$_bits") _gw="${_net%.*}.1" # a dumb, calculated default _policy_rules="${_policy_rules} ${CTDB_PER_IP_ROUTING_RULE_PREF}: from $_ip lookup ctdb.$_ip " _policy_routes="${_policy_routes} # ip route show table ctdb.$_ip $_net dev $_dev scope link " if [ "$_should_add_default" = "default" ] ; then _policy_routes="${_policy_routes} default via $_gw dev $_dev " fi done ok <&1) result_check "$_extra_header" } simple_test_event () { # If something has previously failed then don't continue. : ${_passed:=true} $_passed || return 1 event="$1" ; shift echo "==================================================" simple_test "$@" } simple_test_command () { # If something has previously failed then don't continue. : ${_passed:=true} $_passed || return 1 echo "==================================================" echo "Running command \"$*\"" _out=$("$@" 2>&1) result_check } check_ctdb_logfile () { # If something has previously failed then don't continue. : ${_passed:=true} $_passed || return 1 echo "==================================================" echo "Checking CTDB_LOGFILE=\"${CTDB_LOGFILE}\"" _out=$(cat "$CTDB_LOGFILE" 2>&1) result_check } # Run an eventscript iteratively. # - 1st argument is the number of iterations. # - 2nd argument is something to eval to do setup for every iteration. # The easiest thing to do here is to define a function and pass it # here. # - Subsequent arguments come in pairs: an iteration number and # something to eval for that iteration. Each time an iteration # number is matched the associated argument is given to eval after # the default setup is done. The iteration numbers need to be given # in ascending order. # # Some optional args can be given *before* these, surrounded by extra # "--" args. These args are passed to the eventscript. Quoting is # lost. # # One use of the 2nd and further arguments is to call # required_result() to change what is expected of a particular # iteration. iterate_test () { [ -n "$event" ] || die 'simple_test: $event not set' args="" if [ "$1" = "--" ] ; then shift while [ "$1" != "--" ] ; do args="${args}${args:+ }$1" shift done shift fi _repeats="$1" _setup_default="$2" shift 2 echo "Running $_repeats iterations of \"$script $event\" $args" _result=true for iteration in $(seq 1 $_repeats) ; do # This is inefficient because the iteration-specific setup # might completely replace the default one. However, running # the default is good because it allows you to revert to a # particular result without needing to specify it explicitly. eval $_setup_default if [ $iteration = "$1" ] ; then eval $2 shift 2 fi _shell="" if $TEST_COMMAND_TRACE ; then _shell="sh -x" else _shell="sh" fi _out=$($_shell "${CTDB_BASE}/events.d/$script" "$event" $args 2>&1) _rc=$? _fout=$(echo "$_out" | result_filter) if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then _passed=true else _passed=false _result=false fi result_print "$_passed" "$_out" "$_rc" "Iteration $iteration" done result_footer "$_result" "$(_extra_header)" }