ctdb-scripts: Compact server-end TCP connection killing output
[samba.git] / ctdb / tests / eventscripts / scripts / local.sh
1 # Hey Emacs, this is a -*- shell-script -*- !!!  :-)
2
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
6 # functions.
7
8 EVENTSCRIPTS_PATH=""
9
10 if [ -d "${TEST_SUBDIR}/stubs" ] ; then
11     EVENTSCRIPTS_PATH="${TEST_SUBDIR}/stubs"
12     case "$EVENTSCRIPTS_PATH" in
13         /*) : ;;
14         *) EVENTSCRIPTS_PATH="${PWD}/${EVENTSCRIPTS_PATH}" ;;
15     esac
16     export CTDB_HELPER_BINDIR="$EVENTSCRIPTS_PATH"
17 fi
18
19 export EVENTSCRIPTS_PATH
20
21 PATH="${EVENTSCRIPTS_PATH}:${PATH}"
22
23 export CTDB="ctdb"
24
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"
29 fi
30 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
31 export CTDB_SCRIPT_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/script-state"
32
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\""
36
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"
40 else
41     die "Unable to setup \$CTDB_SYS_ETCDIR"
42 fi
43
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"
47 else
48     die "Unable to set \$CTDB_BASE"
49 fi
50 export CTDB_BASE
51
52 if [ ! -d "${CTDB_BASE}/events.d" ] ; then
53     cat <<EOF
54 ERROR: Directory ${CTDB_BASE}/events.d does not exist.
55
56 That means that no eventscripts can be tested.
57
58 One possible explanation:
59
60   You have CTDB installed via RPMs (or similar), so the regular
61   CTDB_BASE directory is in /etc/ctdb/
62
63   BUT
64
65   You have done a regular "configure" and "make install" so the tests
66   are installed under /usr/local/.
67
68 If so, one possible hack to fix this is to create a symlink:
69
70   ln -s /etc/ctdb /usr/local/etc/ctdb
71
72 This is nasty but it works...  :-)
73 EOF
74     exit 1
75 fi
76
77 ######################################################################
78
79 if "$TEST_VERBOSE" ; then
80     debug () { echo "$@" ; }
81 else
82     debug () { : ; }
83 fi
84
85 ######################################################################
86
87 # General setup fakery
88
89 setup_generic ()
90 {
91     debug "Setting up shares (3 existing shares)"
92     # Create 3 fake shares/exports.
93     export FAKE_SHARES=""
94     for i in $(seq 1 3) ; do
95         _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/${i}_existing"
96         mkdir -p "$_s"
97         FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
98     done
99
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"/*
103
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"/*
107
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
115
116
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"
122
123     export FAKE_TDBTOOL_SUPPORTS_CHECK="yes"
124     export FAKE_TDB_IS_OK
125     export FAKE_DATE_OUTPUT
126
127     export FAKE_TCP_LISTEN FAKE_NETSTAT_UNIX_LISTEN
128 }
129
130 tcp_port_down ()
131 {
132     for _i ; do
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")
135     done
136 }
137
138 _tcp_connections ()
139 {
140         _count="$1"
141         _sip="$2"
142         _sport="$3"
143         _cip_base="$4"
144         _cport_base="$5"
145
146         _cip_prefix="${_cip_base%.*}"
147         _cip_suffix="${_cip_base##*.}"
148
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}"
154         done
155 }
156
157 setup_tcp_connections ()
158 {
159         _t==$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
160         export FAKE_NETSTAT_TCP_ESTABLISHED_FILE"$_t"
161         _tcp_connections "$@" >"$FAKE_NETSTAT_TCP_ESTABLISHED_FILE"
162 }
163
164 setup_tcp_connections_unkillable ()
165 {
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
169         # out.
170         _t=$(_tcp_connections "$@" | sed -e 's/ /|/g')
171         export FAKE_NETSTAT_TCP_ESTABLISHED="$_t"
172 }
173
174 shares_missing ()
175 {
176     _fmt="$1" ; shift
177
178     # Replace some shares with non-existent ones.
179     _t=""
180     _n=1
181     _nl="
182 "
183     export MISSING_SHARES_TEXT=""
184     for _i in $FAKE_SHARES ; do
185         if [ $_n = "$1" ] ; then
186             shift
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}")
191         fi
192         _t="${_t}${_t:+ }${_i}"
193         _n=$(($_n + 1))
194     done
195     FAKE_SHARES="$_t"
196 }
197
198 # Setup some fake /proc/net/bonding files with just enough info for
199 # the eventscripts.
200
201 # arg1 is interface name, arg2 is currently active slave (use "None"
202 # if none), arg3 is MII status ("up" or "down").
203 setup_bond ()
204 {
205     _iface="$1"
206     _slave="${2:-${_iface}_sl_0}"
207     _mii_s="${3:-up}"
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
213 # Status of the bond
214 MII Status: $_mii_s
215 # Status of 1st pretend adapter
216 MII Status: $_mii_subs
217 # Status of 2nd pretend adapter
218 MII Status: $_mii_subs
219 EOF
220 }
221
222 ethtool_interfaces_down ()
223 {
224     for _i ; do
225         echo "Marking interface $_i DOWN for ethtool"
226         touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
227     done
228 }
229
230 ethtool_interfaces_up ()
231 {
232     for _i ; do
233         echo "Marking interface $_i UP for ethtool"
234         rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
235     done
236 }
237
238 dump_routes ()
239 {
240     echo "# ip rule show"
241     ip rule show
242
243     ip rule show |
244     while read _p _x _i _x _t ; do
245         # Remove trailing colon after priority/preference.
246         _p="${_p%:}"
247         # Only remove rules that match our priority/preference.
248         [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
249
250         echo "# ip route show table $_t"
251         ip route show table "$_t"
252     done
253 }
254
255 # Copied from 13.per_ip_routing for now... so this is lazy testing  :-(
256 ipv4_host_addr_to_net ()
257 {
258     _host="$1"
259     _maskbits="$2"
260
261     # Convert the host address to an unsigned long by splitting out
262     # the octets and doing the math.
263     _host_ul=0
264     for _o in $(export IFS="." ; echo $_host) ; do
265         _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
266     done
267
268     # Calculate the mask and apply it.
269     _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
270     _net_ul=$(( $_host_ul & $_mask_ul ))
271
272     # Now convert to a network address one byte at a time.
273     _net=""
274     for _o in $(seq 1 4) ; do
275         _net="$(($_net_ul & 255))${_net:+.}${_net}"
276         _net_ul=$(($_net_ul >> 8))
277     done
278
279     echo "${_net}/${_maskbits}"
280 }
281
282 ######################################################################
283
284 # CTDB fakery
285
286 # Evaluate an expression that probably calls functions or uses
287 # variables from the CTDB functions file.  This is used for test
288 # initialisation.
289 eventscript_call ()
290 {
291     (
292         . "$CTDB_BASE/functions"
293         "$@"
294     )
295 }
296
297 # For now this creates the same public addresses each time.  However,
298 # it could be made more flexible.
299 setup_public_addresses ()
300 {
301     if [ -f "$CTDB_PUBLIC_ADDRESSES" -a \
302             "${CTDB_PUBLIC_ADDRESSES%/*}" = "$EVENTSCRIPTS_TESTS_VAR_DIR" ] ; then
303         rm "$CTDB_PUBLIC_ADDRESSES"
304     fi
305
306     export CTDB_PUBLIC_ADDRESSES=$(mktemp \
307                                        --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR" \
308                                        "public-addresses-XXXXXXXX")
309
310     echo "Setting up CTDB_PUBLIC_ADDRESSES=${CTDB_PUBLIC_ADDRESSES}"
311     cat >"$CTDB_PUBLIC_ADDRESSES" <<EOF
312 10.0.0.1/24 dev123
313 10.0.0.2/24 dev123
314 10.0.0.3/24 dev123
315 10.0.0.4/24 dev123
316 10.0.0.5/24 dev123
317 10.0.0.6/24 dev123
318 10.0.1.1/24 dev456
319 10.0.1.2/24 dev456
320 10.0.1.3/24 dev456
321 EOF
322 }
323
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.
327 ctdb_set_pnn ()
328 {
329     export FAKE_CTDB_PNN="$1"
330     echo "Setting up PNN ${FAKE_CTDB_PNN}"
331
332     export CTDB_SCRIPT_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/script-state/${FAKE_CTDB_PNN}"
333     mkdir -p "$CTDB_SCRIPT_VARDIR"
334 }
335
336 setup_ctdb ()
337 {
338     setup_generic
339
340     export FAKE_CTDB_NUMNODES="${1:-3}"
341     echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
342
343     ctdb_set_pnn "${2:-0}"
344
345     setup_public_addresses
346
347     export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
348
349     export FAKE_CTDB_EXTRA_CONFIG="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-config.sh"
350     rm -f "$FAKE_CTDB_EXTRA_CONFIG"
351
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"/*
355
356     export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus"
357     mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
358     rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
359
360     export CTDB_PARTIALLY_ONLINE_INTERFACES
361
362     export FAKE_CTDB_TUNABLES_OK="MonitorInterval TDBMutexEnabled DatabaseHashSize"
363     export FAKE_CTDB_TUNABLES_OBSOLETE="EventScriptUnhealthyOnTimeout"
364 }
365
366 setup_config ()
367 {
368     cat >"$FAKE_CTDB_EXTRA_CONFIG"
369 }
370
371 validate_percentage ()
372 {
373     case "$1" in
374         [0-9]|[0-9][0-9]|100) return 0 ;;
375         *) echo "WARNING: ${1} is an invalid percentage${2:+\" in }${2}${2:+\"}"
376            return 1
377     esac
378 }
379
380 setup_memcheck ()
381 {
382     _mem_usage="${1:-10}" # Default is 10%
383     _swap_usage="${2:-0}" # Default is  0%
384
385     setup_ctdb
386
387     _swap_total=5857276
388     _swap_free=$(( (100 - $_swap_usage) * $_swap_total / 100 ))
389
390     _mem_total=3940712
391     _mem_free=225268
392     _mem_buffers=146120
393     _mem_cached=$(( $_mem_total * (100 - $_mem_usage) / 100 - $_mem_free - $_mem_buffers ))
394
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
400 SwapCached:        56016 kB
401 Active:          2422104 kB
402 Inactive:        1019928 kB
403 Active(anon):    1917580 kB
404 Inactive(anon):   523080 kB
405 Active(file):     504524 kB
406 Inactive(file):   496848 kB
407 Unevictable:        4844 kB
408 Mlocked:            4844 kB
409 SwapTotal:       ${_swap_total} kB
410 SwapFree:        ${_swap_free} kB
411 ..."
412
413     export CTDB_MONITOR_MEMORY_USAGE
414     export CTDB_MONITOR_SWAP_USAGE
415 }
416
417 setup_fscheck ()
418 {
419     export FAKE_FS_USE="${1:-10}"  # Default is 10% usage
420
421     # Causes some variables to be exported
422     setup_ctdb
423
424     export CTDB_MONITOR_FILESYSTEM_USAGE
425 }
426
427 ctdb_get_interfaces ()
428 {
429     # The echo/subshell forces all the output onto 1 line.
430     echo $(ctdb ifaces -X | awk -F'|' 'FNR > 1 {print $2}')
431 }
432
433 ctdb_get_1_interface ()
434 {
435     _t=$(ctdb_get_interfaces)
436     echo ${_t%% *}
437 }
438
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 ()
442 {
443     _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
444     while IFS="/$IFS" read _ip _maskbits _ifaces ; do
445         echo "$_ifaces $_ip $_maskbits"
446     done <"$_f"
447 }
448
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 ()
452 {
453     ctdb ip -v -X | {
454         read _x # skip header line
455
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
461                     break
462                 fi
463             done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
464         done
465     }
466 }
467
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 ()
471 {
472     ctdb_get_my_public_addresses | { head -n 1 ; cat >/dev/null ; }
473 }
474
475 ctdb_not_implemented ()
476 {
477     export CTDB_NOT_IMPLEMENTED="$1"
478     ctdb_not_implemented="\
479 DEBUG: ctdb: command \"$1\" not implemented in stub"
480 }
481
482 ctdb_fake_scriptstatus ()
483 {
484     _code="$1"
485     _status="$2"
486     _err_out="$3"
487
488     _d1=$(date '+%s.%N')
489     _d2=$(date '+%s.%N')
490
491     echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
492 }
493
494 ######################################################################
495
496 setup_ctdb_policy_routing ()
497 {
498     service_name="per_ip_routing"
499
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
504
505     # Tests need to create and populate this file
506     rm -f "$CTDB_PER_IP_ROUTING_CONF"
507 }
508
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 ()
513 {
514     _num_ips="${1:-1}"
515     _should_add_default="$2"
516
517     ctdb_get_my_public_addresses |
518     if [ "$_num_ips" = "all" ] ; then
519         cat
520     else
521         { head -n "$_num_ips" ; cat >/dev/null ; }
522     fi |
523     while read _dev _ip _bits ; do
524         _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
525         _gw="${_net%.*}.254" # a dumb, calculated default
526
527         echo "$_ip $_net"
528
529         if [ "$_should_add_default" = "default" ] ; then
530             echo "$_ip 0.0.0.0/0 $_gw"
531         fi
532     done >"$CTDB_PER_IP_ROUTING_CONF"
533 }
534
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.
538 check_routes ()
539 {
540     _num_ips="${1:-1}"
541     _should_add_default="$2"
542
543     _policy_rules=""
544     _policy_routes=""
545
546     ctdb_get_my_public_addresses |
547     if [ "$_num_ips" = "all" ] ; then
548         cat
549     else
550         { head -n "$_num_ips" ; cat >/dev/null ; }
551     fi | {
552         while read _dev _ip _bits ; do
553             _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
554             _gw="${_net%.*}.254" # a dumb, calculated default
555
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 "
561
562             if [ "$_should_add_default" = "default" ] ; then
563                 _policy_routes="${_policy_routes}
564 default via $_gw dev $_dev "
565             fi
566         done
567
568         ok <<EOF
569 # ip rule show
570 0:      from all lookup local ${_policy_rules}
571 32766:  from all lookup main 
572 32767:  from all lookup default ${_policy_routes}
573 EOF
574
575         simple_test_command dump_routes
576     } || test_fail
577 }
578
579 ######################################################################
580
581 setup_ctdb_lvs ()
582 {
583         lvs_state_dir="${EVENTSCRIPTS_TESTS_VAR_DIR}/lvs"
584         mkdir -p "$lvs_state_dir"
585
586         export FAKE_LVS_STATE_DIR="${lvs_state_dir}/state"
587         mkdir "$FAKE_LVS_STATE_DIR"
588
589         lvs_header=$(ipvsadm -l -n)
590
591         export CTDB_LVS_PUBLIC_IP="$1"
592         export CTDB_LVS_PUBLIC_IFACE="$2"
593
594         [ -n "$CTDB_LVS_PUBLIC_IP" ] || return 0
595         [ -n "$CTDB_LVS_PUBLIC_IFACE" ] || return 0
596
597         export CTDB_LVS_NODES=$(mktemp --tmpdir="$lvs_state_dir")
598         export FAKE_CTDB_LVS_MASTER=""
599
600         # Read from stdin
601         _pnn=0
602         while read _ip _opts ; do
603                 case "$_opts" in
604                 master)
605                         FAKE_CTDB_LVS_MASTER="$_pnn"
606                         echo "$_ip"
607                         ;;
608                 slave-only)
609                         printf "%s\tslave-only\n" "$_ip"
610                         ;;
611                 *)
612                         echo "$_ip"
613                         ;;
614                 esac
615                 _pnn=$(($_pnn + 1))
616         done >"$CTDB_LVS_NODES"
617 }
618
619 check_ipvsadm ()
620 {
621         if [ "$1" = "NULL" ] ; then
622                 required_result 0 <<EOF
623 $lvs_header
624 EOF
625         else
626                 required_result 0 <<EOF
627 $lvs_header
628 $(cat)
629 EOF
630         fi
631
632         simple_test_command ipvsadm -l -n
633 }
634
635 check_lvs_ip ()
636 {
637         _scope="$1"
638
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
643 EOF
644         else
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
650 EOF
651         fi
652
653         simple_test_command ip addr show dev lo
654 }
655
656 ######################################################################
657
658 ctdb_catdb_format_pairs ()
659 {
660     _count=0
661
662     while read _k _v ; do
663         _kn=$(echo -n "$_k" | wc -c)
664         _vn=$(echo -n "$_v" | wc -c)
665         cat <<EOF
666 key(${_kn}) = "${_k}"
667 dmaster: 0
668 rsn: 1
669 data(${_vn}) = "${_v}"
670
671 EOF
672         _count=$(($_count + 1))
673     done
674
675     echo "Dumped ${_count} records"
676 }
677
678 check_ctdb_tdb_statd_state ()
679 {
680     ctdb_get_my_public_addresses |
681     while read _x _sip _x ; do
682         for _cip ; do
683             echo "statd-state@${_sip}@${_cip}" "$FAKE_DATE_OUTPUT"
684         done
685     done |
686     ctdb_catdb_format_pairs | {
687         ok
688         simple_test_command ctdb catdb ctdb.tdb
689     } || test_fail
690 }
691
692 check_statd_callout_smnotify ()
693 {
694     _state_even=$(( $(date '+%s') / 2 * 2))
695     _state_odd=$(($_state_even + 1))
696
697     nfs_load_config
698
699     ctdb_get_my_public_addresses |
700     while read _x _sip _x ; do
701         for _cip ; do
702             cat <<EOF
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}
707 EOF
708         done
709     done | {
710         ok
711         simple_test_event "notify"
712     } || test_fail
713 }
714
715 ######################################################################
716
717 setup_ctdb_natgw ()
718 {
719         debug "Setting up NAT gateway"
720
721         natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
722         mkdir -p "$natgw_config_dir"
723
724         # These will accumulate, 1 per test... but will be cleaned up at
725         # the end.
726         export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
727
728         # Read from stdin
729         while read _ip _opts ; do
730                 case "$_opts" in
731                 master)
732                         export FAKE_CTDB_NATGW_MASTER="$_ip"
733                         echo "$_ip"
734                         ;;
735                 slave-only)
736                         printf "%s\tslave-only\n" "$_ip"
737                         ;;
738                 *)
739                         echo "$_ip"
740                         ;;
741                 esac
742         done >"$CTDB_NATGW_NODES"
743
744         # Assume all of the nodes are on a /24 network and have IPv4
745         # addresses:
746         read _ip <"$CTDB_NATGW_NODES"
747         export CTDB_NATGW_PRIVATE_NETWORK="${_ip%.*}.0/24"
748
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
751         # "".  :-)
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=""
756 }
757
758 ok_natgw_master_ip_addr_show ()
759 {
760     _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
761
762     # This is based on CTDB_NATGW_PUBLIC_IP
763     _brd="10.1.1.255"
764
765 ok <<EOF
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
770 EOF
771 }
772
773 ok_natgw_slave_ip_addr_show ()
774 {
775     _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
776 ok <<EOF
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
779 EOF
780 }
781
782 ok_natgw_master_static_routes ()
783 {
784     _nl="
785 "
786     _t=""
787     for _i in $CTDB_NATGW_STATIC_ROUTES ; do
788         # This is intentionally different to the code in 11.natgw ;-)
789         case "$_i" in
790             *@*)
791                 _net=$(echo "$_i" | sed -e 's|@.*||')
792                 _gw=$(echo "$_i" | sed -e 's|.*@||')
793                 ;;
794             *)
795                 _net="$_i"
796                 _gw="$CTDB_NATGW_DEFAULT_GATEWAY"
797         esac
798
799         [ -n "$_gw" ] || continue
800         _t="${_t}${_t:+${_nl}}"
801         _t="${_t}${_net} via ${_gw} dev ethXXX  metric 10 "
802     done
803     _t=$(echo "$_t" | sort)
804     ok "$_t"
805 }
806
807 ok_natgw_slave_static_routes ()
808 {
809     _nl="
810 "
811     _t=""
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|@.*||')
815
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 "
822     done
823     _t=$(echo "$_t" | sort)
824     ok "$_t"
825 }
826
827 ######################################################################
828
829 # Samba/winbind fakery
830
831 setup_samba ()
832 {
833     setup_ctdb
834
835     service_name="samba"
836
837     if [ "$1" != "down" ] ; then
838
839         debug "Marking Samba services as up, listening and managed by CTDB"
840
841         # All possible service names for all known distros.
842         for i in "smb" "nmb" "samba" "smbd" "nmbd" ; do
843             service "$i" force-started
844         done
845
846         export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
847         export CTDB_MANAGED_SERVICES="foo samba bar"
848
849         export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
850         export FAKE_WBINFO_FAIL="no"
851
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
854         # that...  :-)
855         export FAKE_SLEEP_FORCE=0.1
856     else
857         debug "Marking Samba services as down, not listening and not managed by CTDB"
858
859         # All possible service names for all known distros.
860         for i in "smb" "nmb" "samba" "smbd" "nmbd" ; do
861             service "$i" force-stopped
862         done
863
864         export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
865         export CTDB_MANAGED_SERVICES="foo bar"
866         unset CTDB_MANAGES_SAMBA
867
868         export FAKE_TCP_LISTEN=""
869         export FAKE_WBINFO_FAIL="yes"
870     fi
871 }
872
873 samba_setup_fake_threads ()
874 {
875         export FAKE_SMBD_THREAD_PIDS="$*"
876
877         _nl="
878 "
879         _out=""
880         _count=0
881         for _pid ; do
882                 [ "$_count" -lt 5 ] || break
883                 _t=$(program_stack_trace "smbd" $_pid)
884                 _out="${_out:+${_out}${_nl}}${_t}"
885                 _count=$((_count + 1))
886         done
887         SAMBA_STACK_TRACES="$_out"
888 }
889
890 setup_winbind ()
891 {
892     setup_ctdb
893
894     service_name="winbind"
895
896     if [ "$1" != "down" ] ; then
897
898         debug "Marking Winbind service as up and managed by CTDB"
899
900         service "winbind" force-started
901
902         export CTDB_MANAGED_SERVICES="foo winbind bar"
903
904         export FAKE_WBINFO_FAIL="no"
905
906     else
907         debug "Marking Winbind service as down and not managed by CTDB"
908
909         service "winbind" force-stopped
910
911         export CTDB_MANAGED_SERVICES="foo bar"
912         unset CTDB_MANAGES_WINBIND
913
914         export FAKE_WBINFO_FAIL="yes"
915     fi
916 }
917
918 wbinfo_down ()
919 {
920     debug "Making wbinfo commands fail"
921     FAKE_WBINFO_FAIL="yes"
922 }
923
924 ######################################################################
925
926 # NFS fakery
927
928 setup_nfs ()
929 {
930     setup_ctdb
931
932     service_name="nfs"
933
934     export FAKE_RPCINFO_SERVICES=""
935
936     export CTDB_NFS_SKIP_SHARE_CHECK="no"
937
938     export RPCNFSDCOUNT
939
940     # This doesn't even need to exist
941     export CTDB_NFS_EXPORTS_FILE="$EVENTSCRIPTS_TESTS_VAR_DIR/etc-exports"
942
943     # Reset the failcounts for nfs services.
944     eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
945
946     if [ "$1" != "down" ] ; then
947         debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
948
949         service "nfs" force-started
950         service "nfslock" force-started
951
952         export CTDB_MANAGED_SERVICES="foo nfs bar"
953
954         rpc_services_up \
955             "portmapper" "nfs" "mountd" "rquotad" "nlockmgr" "status"
956
957         nfs_setup_fake_threads "nfsd"
958         nfs_setup_fake_threads "rpc.foobar"  # Just set the variable to empty
959     else
960         debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
961
962         service "nfs" force-stopped
963         service "nfslock" force-stopped
964
965         export CTDB_MANAGED_SERVICES="foo bar"
966         unset CTDB_MANAGES_NFS
967     fi
968
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"
975 }
976
977 setup_nfs_ganesha ()
978 {
979     setup_nfs "$@"
980     export CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-ganesha-callout"
981     if [ "$1" != "down" ] ; then
982         export CTDB_MANAGES_NFS="yes"
983     fi
984
985     export CTDB_NFS_SKIP_SHARE_CHECK="yes"
986 }
987
988 rpc_services_down ()
989 {
990     for _i ; do
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")
993     done
994 }
995
996 rpc_services_up ()
997 {
998     for _i ; do
999         debug "Marking RPC service \"${_i}\" as available"
1000         case "$_i" in
1001             portmapper) _t="2:4" ;;
1002             nfs)        _t="2:3" ;;
1003             mountd)     _t="1:3" ;;
1004             rquotad)    _t="1:2" ;;
1005             nlockmgr)   _t="3:4" ;;
1006             status)     _t="1:1" ;;
1007             *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
1008         esac
1009
1010         FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
1011     done
1012 }
1013
1014
1015 nfs_load_config ()
1016 {
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
1020             . "$_c"
1021             break
1022         fi
1023     done
1024 }
1025
1026 nfs_setup_fake_threads ()
1027 {
1028     _prog="$1" ; shift
1029
1030     case "$_prog" in
1031         nfsd)
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="$*"
1037             ;;
1038         *)
1039             export FAKE_RPC_THREAD_PIDS="$*"
1040             ;;
1041     esac
1042 }
1043
1044 program_stack_trace ()
1045 {
1046         _prog="$1"
1047         _pid="$2"
1048
1049         cat <<EOF
1050 Stack trace for ${_prog}[${_pid}]:
1051 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff
1052 EOF
1053 }
1054
1055 program_stack_traces ()
1056 {
1057     _prog="$1"
1058     _max="${2:-1}"
1059
1060     _count=1
1061     for _pid in ${FAKE_NFSD_THREAD_PIDS:-$FAKE_RPC_THREAD_PIDS} ; do
1062         [ $_count -le $_max ] || break
1063
1064         program_stack_trace "$_prog" "$_pid"
1065         _count=$(($_count + 1))
1066     done
1067 }
1068
1069 guess_output ()
1070 {
1071     case "$1" in
1072         $CTDB_NFS_CALLOUT\ start\ nlockmgr)
1073             echo "&Starting nfslock: OK"
1074             ;;
1075         $CTDB_NFS_CALLOUT\ start\ nfs)
1076             cat <<EOF
1077 &Starting nfslock: OK
1078 &Starting nfs: OK
1079 EOF
1080             ;;
1081         *)
1082             : # Nothing
1083     esac
1084 }
1085
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 ()
1095 {
1096     _rpc_service="$1"
1097     _numfails="${2:-1}" # default 1
1098
1099     # Default
1100     ok_null
1101     if [ $_numfails -eq 0 ] ; then
1102         return
1103     fi
1104
1105     nfs_load_config
1106
1107     # A handy newline.  :-)
1108     _nl="
1109 "
1110
1111     _dir="${CTDB_NFS_CHECKS_DIR:-${CTDB_BASE}/nfs-checks.d}"
1112
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"
1115
1116     _out=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1117     _rc_file=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1118
1119     (
1120         # Subshell to restrict scope variables...
1121
1122         # Defaults
1123         family="tcp"
1124         version=""
1125         unhealthy_after=1
1126         restart_every=0
1127         service_stop_cmd=""
1128         service_start_cmd=""
1129         service_check_cmd=""
1130         service_debug_cmd=""
1131
1132         # Don't bother syntax checking, eventscript does that...
1133         . "$_file"
1134
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%% *}"
1139         else
1140             case "$_rpc_service" in
1141                 portmapper) _ver="" ;;
1142                 *)          _ver=1  ;;
1143             esac
1144         fi
1145         _rpc_check_out="\
1146 $_rpc_service failed RPC check:
1147 rpcinfo: RPC: Program not registered
1148 program $_rpc_service${_ver:+ version }${_ver} is not available"
1149
1150         if [ $unhealthy_after -gt 0 -a $_numfails -ge $unhealthy_after ] ; then
1151             _unhealthy=true
1152             echo 1 >"$_rc_file"
1153             echo "ERROR: ${_rpc_check_out}" >>"$_out"
1154         else
1155             _unhealthy=false
1156             echo 0 >"$_rc_file"
1157         fi
1158
1159         if [ $restart_every -gt 0 ] && \
1160                    [ $(($_numfails % $restart_every)) -eq 0 ] ; then
1161             if ! $_unhealthy ; then
1162                 echo "WARNING: ${_rpc_check_out}" >>"$_out"
1163             fi
1164
1165             echo "Trying to restart service \"${_rpc_service}\"..." >>"$_out"
1166
1167             if [ -n "$service_debug_cmd" ] ; then
1168                 $service_debug_cmd 2>&1 >>"$_out"
1169             fi
1170
1171             guess_output "$service_start_cmd" >>"$_out"
1172         fi
1173     )
1174
1175     read _rc <"$_rc_file"
1176     required_result $_rc <"$_out"
1177
1178     rm -f "$_out" "$_rc_file"
1179 }
1180
1181 ######################################################################
1182
1183 # Recovery lock fakery
1184
1185 cleanup_reclock ()
1186 {
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)
1191         done
1192 }
1193
1194 setup_reclock ()
1195 {
1196         CTDB_RECOVERY_LOCK=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
1197         export CTDB_RECOVERY_LOCK
1198
1199         test_cleanup cleanup_reclock
1200 }
1201
1202 ######################################################################
1203
1204 # VSFTPD fakery
1205
1206 setup_vsftpd ()
1207 {
1208     service_name="vsftpd"
1209
1210     if [ "$1" != "down" ] ; then
1211         die "setup_vsftpd up not implemented!!!"
1212     else
1213         debug "Setting up VSFTPD environment: service down, not managed by CTDB"
1214
1215         service vsftpd force-stopped
1216
1217         export CTDB_MANAGED_SERVICES="foo"
1218         unset CTDB_MANAGES_VSFTPD
1219     fi
1220 }
1221
1222 ######################################################################
1223
1224 # HTTPD fakery
1225
1226 setup_httpd ()
1227 {
1228     if [ "$1" != "down" ] ; then
1229         die "setup_httpd up not implemented!!!"
1230     else
1231         debug "Setting up HTTPD environment: service down, not managed by CTDB"
1232
1233         for service_name in "apache2" "httpd" ; do
1234             service "$service_name" force-stopped
1235         done
1236
1237         export CTDB_MANAGED_SERVICES="foo"
1238         unset CTDB_MANAGES_HTTPD
1239     fi
1240 }
1241
1242 ######################################################################
1243
1244 # multipathd fakery
1245
1246 setup_multipathd ()
1247 {
1248     for i ; do
1249         case "$i" in
1250             \!*)
1251                 _t="${i#!}"
1252                 echo "Marking ${_t} as having no active paths"
1253                 FAKE_MULTIPATH_FAILURES="${FAKE_MULTIPATH_FAILURES}${FAKE_MULTIPATH+FAILURES:+ }${_t}"
1254                 ;;
1255             *)
1256                 _t="$i"         
1257         esac
1258         CTDB_MONITOR_MPDEVICES="${CTDB_MONITOR_MPDEVICES}${CTDB_MONITOR_MPDEVICES:+ }${_t}"
1259     done
1260
1261     export CTDB_MONITOR_MPDEVICES FAKE_MULTIPATH_FAILURES
1262     export FAKE_SLEEP_FORCE=0.1
1263 }
1264
1265 ######################################################################
1266
1267 # Result and test functions
1268
1269 # Set some globals and print the summary.
1270 define_test ()
1271 {
1272     desc="$1"
1273
1274     _f=$(basename "$0" ".sh")
1275
1276     # Remaining format should be NN.service.event.NNN or NN.service.NNN:
1277     _num="${_f##*.}"
1278     _f="${_f%.*}"
1279
1280     case "$_f" in
1281         [0-9][0-9].*.*)
1282             script="${_f%.*}"
1283             event="${_f##*.}"
1284             script_dir="${CTDB_BASE}/events.d"
1285             ;;
1286         [0-9][0-9].*)
1287             script="$_f"
1288             unset event
1289             script_dir="${CTDB_BASE}/events.d"
1290             ;;
1291         *.*)
1292             script="${_f%.*}"
1293             event="${_f##*.}"
1294             script_dir="${CTDB_BASE}"
1295             ;;
1296         *)
1297             script="${_f%.*}"
1298             unset event
1299             script_dir="${CTDB_BASE}"
1300     esac
1301
1302     [ -r "${script_dir}/${script}" ] || \
1303         die "Internal error - unable to find script \"${script_dir}/${script}\""
1304
1305     printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
1306 }
1307
1308 # Run an eventscript once.  The test passes if the return code and
1309 # output match those required.
1310
1311 # Any args are passed to the eventscript.
1312
1313 simple_test ()
1314 {
1315     [ -n "$event" ] || die 'simple_test: $event not set'
1316
1317     args="$@"
1318
1319     test_header ()
1320     {
1321         echo "Running script \"$script $event${args:+ }$args\""
1322     }
1323
1324     extra_header ()
1325     {
1326         cat <<EOF
1327
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)"
1333 EOF
1334     }
1335
1336     script_test "${script_dir}/${script}" "$event" "$@"
1337
1338     reset_test_header
1339     reset_extra_header
1340 }
1341
1342 simple_test_event ()
1343 {
1344     # If something has previously failed then don't continue.
1345     : ${_passed:=true}
1346     $_passed || return 1
1347
1348     event="$1" ; shift
1349     echo "=================================================="
1350     simple_test "$@"
1351 }
1352
1353 simple_test_command ()
1354 {
1355     unit_test "$@"
1356 }
1357
1358 # Run an NFS eventscript iteratively.
1359 #
1360 # - 1st argument is the number of iterations.
1361 #
1362 # - 2nd argument is the NFS/RPC service being tested
1363 #
1364 #   rpcinfo (or $service_check_cmd) is used on each iteration to test
1365 #   the availability of the service
1366 #
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.
1371 #
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.
1377 #
1378 #   These arguments can allow a service to be started or stopped
1379 #   before a particular iteration.
1380 #
1381 nfs_iterate_test ()
1382 {
1383     _repeats="$1"
1384     _rpc_service="$2"
1385     if [ -n "$2" ] ; then
1386         shift 2
1387     else
1388         shift
1389     fi
1390
1391     echo "Running $_repeats iterations of \"$script $event\" $args"
1392
1393     _iterate_failcount=0
1394     for _iteration in $(seq 1 $_repeats) ; do
1395         # This is not a numerical comparison because $1 will often not
1396         # be set.
1397         if [ "$_iteration" = "$1" ] ; then
1398             debug "##################################################"
1399             eval "$2"
1400             debug "##################################################"
1401             shift 2
1402         fi
1403         if [ -n "$_rpc_service" ] ; then
1404             _ok=false
1405             if [ -n "$service_check_cmd" ] ; then
1406                 if eval "$service_check_cmd" ; then
1407                     _ok=true
1408                 fi
1409             else
1410                 if rpcinfo -T tcp localhost "$_rpc_service" >/dev/null 2>&1 ; then
1411                     _ok=true
1412                 fi
1413             fi
1414
1415             if $_ok ; then
1416                 _iterate_failcount=0
1417             else
1418                 _iterate_failcount=$(($_iterate_failcount + 1))
1419             fi
1420             rpc_set_service_failure_response "$_rpc_service" $_iterate_failcount
1421         fi
1422         _out=$(simple_test 2>&1)
1423         _ret=$?
1424         if "$TEST_VERBOSE" || [ $_ret -ne 0 ] ; then
1425             echo "##################################################"
1426             echo "Iteration ${_iteration}:"
1427             echo "$_out"
1428         fi
1429         if [ $_ret -ne 0 ] ; then
1430             exit $_ret
1431         fi
1432     done
1433 }