tests/eventscripts: Add unit tests for $CTDB_MONITOR_NFS_THREAD_COUNT
[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 fi
13
14 export EVENTSCRIPTS_PATH
15
16 PATH="${EVENTSCRIPTS_PATH}:${PATH}"
17
18 export EVENTSCRIPTS_TESTS_VAR_DIR="${TEST_VAR_DIR}/unit_eventscripts"
19 if [ -d "$EVENTSCRIPTS_TESTS_VAR_DIR" -a \
20     "$EVENTSCRIPTS_TESTS_VAR_DIR" != "/unit_eventscripts" ] ; then
21     rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR"
22 fi
23 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
24 export CTDB_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb"
25
26 export CTDB_LOGFILE="${EVENTSCRIPTS_TESTS_VAR_DIR}/log.ctdb"
27 touch "$CTDB_LOGFILE" || die "Unable to create CTDB_LOGFILE=$CTDB_LOGFILE"
28
29 if [ -d "${TEST_SUBDIR}/etc" ] ; then    
30     cp -a "${TEST_SUBDIR}/etc" "$EVENTSCRIPTS_TESTS_VAR_DIR"
31     export CTDB_ETCDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc"
32 else
33     die "Unable to setup \$CTDB_ETCDIR"
34 fi
35
36 if [ -d "${TEST_SUBDIR}/etc-ctdb" ] ; then
37     cp -prL "${TEST_SUBDIR}/etc-ctdb" "$EVENTSCRIPTS_TESTS_VAR_DIR"
38     export CTDB_BASE="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc-ctdb"
39 else
40     die "Unable to set \$CTDB_BASE"
41 fi
42 export CTDB_BASE
43
44 if [ ! -d "${CTDB_BASE}/events.d" ] ; then
45     cat <<EOF
46 ERROR: Directory ${CTDB_BASE}/events.d does not exist.
47
48 That means that no eventscripts can be tested.
49
50 One possible explanation:
51
52   You have CTDB installed via RPMs (or similar), so the regular
53   CTDB_BASE directory is in /etc/ctdb/
54
55   BUT
56
57   You have done a regular "configure" and "make install" so the tests
58   are installed under /usr/local/.
59
60 If so, one possible hack to fix this is to create a symlink:
61
62   ln -s /etc/ctdb /usr/local/etc/ctdb
63
64 This is nasty but it works...  :-)
65 EOF
66     exit 1
67 fi
68
69 ######################################################################
70
71 if "$TEST_VERBOSE" ; then
72     debug () { echo "$@" ; }
73 else
74     debug () { : ; }
75 fi
76
77 eventscripts_tests_cleanup_hooks=""
78
79 # This loses quoting!
80 eventscripts_test_add_cleanup ()
81 {
82     eventscripts_tests_cleanup_hooks="${eventscripts_tests_cleanup_hooks}${eventscripts_tests_cleanup_hooks:+ ; }$*"
83 }
84
85 trap 'eval $eventscripts_tests_cleanup_hooks' 0
86
87
88 ######################################################################
89
90 # General setup fakery
91
92 setup_generic ()
93 {
94     debug "Setting up shares (3 existing shares)"
95     # Create 3 fake shares/exports.
96     export FAKE_SHARES=""
97     for i in $(seq 1 3) ; do
98         _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/${i}_existing"
99         mkdir -p "$_s"
100         FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
101     done
102
103     export FAKE_PROC_NET_BONDING="$EVENTSCRIPTS_TESTS_VAR_DIR/proc-net-bonding"
104     mkdir -p "$FAKE_PROC_NET_BONDING"
105     rm -f "$FAKE_PROC_NET_BONDING"/*
106
107     export FAKE_ETHTOOL_LINK_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/ethtool-link-down"
108     mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
109     rm -f "$FAKE_ETHTOOL_LINK_DOWN"/*
110
111     # This can only have 2 levels.  We don't want to resort to usings
112     # something dangerous like "rm -r" setup time.
113     export FAKE_IP_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ip-state"
114     mkdir -p "$FAKE_IP_STATE"
115     rm -f "$FAKE_IP_STATE"/*/*
116     rm -f "$FAKE_IP_STATE"/* 2>/dev/null || true
117     rmdir "$FAKE_IP_STATE"/* 2>/dev/null || true
118 }
119
120 tcp_port_down ()
121 {
122     for _i ; do
123         debug "Marking TCP port \"${_i}\" as not listening"
124         FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
125     done
126 }
127
128 shares_missing ()
129 {
130     _fmt="$1" ; shift
131
132     # Replace some shares with non-existent ones.
133     _t=""
134     _n=1
135     _nl="
136 "
137     export MISSING_SHARES_TEXT=""
138     for _i in $FAKE_SHARES ; do
139         if [ $_n = "$1" ] ; then
140             shift
141             _i="${_i%_existing}_missing"
142             debug "Replacing share $_n with missing share \"$_i\""
143             rmdir "$_i" 2>/dev/null || true
144             MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}")
145         fi
146         _t="${_t}${_t:+ }${_i}"
147         _n=$(($_n + 1))
148     done
149     FAKE_SHARES="$_t"
150 }
151
152 # Setup some fake /proc/net/bonding files with just enough info for
153 # the eventscripts.
154
155 # arg1 is interface name, arg2 is currently active slave (use "None"
156 # if none), arg3 is MII status ("up" or "down").
157 setup_bond ()
158 {
159     _iface="$1"
160     _slave="${2:-${_iface}_sl_0}"
161     _mii_s="${3:-up}"
162     _mii_subs="${4:-${_mii_s:-up}}"
163     echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s"
164     cat >"${FAKE_PROC_NET_BONDING}/$_iface" <<EOF
165 Bonding Mode: IEEE 802.3ad Dynamic link aggregation
166 Currently Active Slave: $_slave
167 # Status of the bond
168 MII Status: $_mii_s
169 # Status of 1st pretend adapter
170 MII Status: $_mii_subs
171 # Status of 2nd pretend adapter
172 MII Status: $_mii_subs
173 EOF
174 }
175
176 ethtool_interfaces_down ()
177 {
178     for _i ; do
179         echo "Marking interface $_i DOWN for ethtool"
180         touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
181     done
182 }
183
184 ethtool_interfaces_up ()
185 {
186     for _i ; do
187         echo "Marking interface $_i UP for ethtool"
188         rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
189     done
190 }
191
192 setup_nmap_output_filter ()
193 {
194     OUT_FILTER="-e 's@^(DEBUG: # Nmap 5.21 scan initiated) .+ (as:)@\1 DATE \2@' -e 's@^(DEBUG: # Nmap done at) .+ (--)@\1 DATE \2@'"
195 }
196
197 dump_routes ()
198 {
199     echo "# ip rule show"
200     ip rule show
201
202     ip rule show |
203     while read _p _x _i _x _t ; do
204         # Remove trailing colon after priority/preference.
205         _p="${_p%:}"
206         # Only remove rules that match our priority/preference.
207         [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
208
209         echo "# ip route show table $_t"
210         ip route show table "$_t"
211     done
212 }
213
214 # Copied from 13.per_ip_routing for now... so this is lazy testing  :-(
215 ipv4_host_addr_to_net ()
216 {
217     _host="$1"
218     _maskbits="$2"
219
220     # Convert the host address to an unsigned long by splitting out
221     # the octets and doing the math.
222     _host_ul=0
223     for _o in $(export IFS="." ; echo $_host) ; do
224         _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
225     done
226
227     # Calculate the mask and apply it.
228     _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
229     _net_ul=$(( $_host_ul & $_mask_ul ))
230
231     # Now convert to a network address one byte at a time.
232     _net=""
233     for _o in $(seq 1 4) ; do
234         _net="$(($_net_ul & 255))${_net:+.}${_net}"
235         _net_ul=$(($_net_ul >> 8))
236     done
237
238     echo "${_net}/${_maskbits}"
239 }
240
241 ######################################################################
242
243 # CTDB fakery
244
245 # Evaluate an expression that probably calls functions or uses
246 # variables from the CTDB functions file.  This is used for test
247 # initialisation.
248 eventscript_call ()
249 {
250     (
251         . "$CTDB_BASE/functions"
252         "$@"
253     )
254 }
255
256 # Set output for ctdb command.  Option 1st argument is return code.
257 ctdb_set_output ()
258 {
259     _out="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.out"
260     cat >"$_out"
261
262     _rc="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.rc"
263     echo "${1:-0}" >"$_rc"
264
265     eventscripts_test_add_cleanup "rm -f $_out $_rc"
266 }
267
268 setup_ctdb ()
269 {
270     setup_generic
271
272     export FAKE_CTDB_NUMNODES="${1:-3}"
273     echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
274
275     export FAKE_CTDB_PNN="${2:-0}"
276     echo "Setting up CTDB with PNN ${FAKE_CTDB_PNN}"
277
278     if [ -n "$3" ] ; then
279         echo "Setting up CTDB_PUBLIC_ADDRESSES: $3"
280         export CTDB_PUBLIC_ADDRESSES=$(mktemp)
281         for _i in $3 ; do
282             _ip="${_i%@*}"
283             _ifaces="${_i#*@}"
284             echo "${_ip} ${_ifaces}" >>"$CTDB_PUBLIC_ADDRESSES"
285         done
286         eventscripts_test_add_cleanup "rm -f $CTDB_PUBLIC_ADDRESSES"
287     fi
288
289     export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
290
291     export FAKE_CTDB_IFACES_DOWN="$FAKE_CTDB_STATE/ifaces-down"
292     mkdir -p "$FAKE_CTDB_IFACES_DOWN"
293     rm -f "$FAKE_CTDB_IFACES_DOWN"/*
294
295     export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus"
296     mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
297     rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
298 }
299
300 ctdb_get_interfaces ()
301 {
302     # The echo/subshell forces all the output onto 1 line.
303     echo $(ctdb ifaces -Y | awk -F: 'FNR > 1 {print $2}')
304 }
305
306 ctdb_get_1_interface ()
307 {
308     _t=$(ctdb_get_interfaces)
309     echo ${_t%% *}
310 }
311
312 # Print all public addresses as: interface IP maskbits
313 # Each line is suitable for passing to takeip/releaseip
314 ctdb_get_all_public_addresses ()
315 {
316     _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
317     while IFS="/$IFS" read _ip _maskbits _ifaces ; do
318         echo "$_ifaces $_ip $_maskbits"
319     done <"$_f"
320 }
321
322 # Print public addresses on this node as: interface IP maskbits
323 # Each line is suitable for passing to takeip/releaseip
324 ctdb_get_my_public_addresses ()
325 {
326     ctdb ip -v -Y | {
327         read _x # skip header line
328
329         while IFS=":" read _x _ip _x _iface _x ; do
330             [ -n "$_iface" ] || continue
331             while IFS="/$IFS" read _i _maskbits _x ; do
332                 if [ "$_ip" = "$_i" ] ; then
333                     echo $_iface $_ip $_maskbits
334                     break
335                 fi
336             done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
337         done
338     }
339 }
340
341 # Prints the 1st public address as: interface IP maskbits
342 # This is suitable for passing to takeip/releaseip
343 ctdb_get_1_public_address ()
344 {
345     ctdb_get_my_public_addresses | head -n 1
346 }
347
348 ctdb_not_implemented ()
349 {
350     export CTDB_NOT_IMPLEMENTED="$1"
351     ctdb_not_implemented="\
352 DEBUG: ctdb: command \"$1\" not implemented in stub"
353 }
354
355 ctdb_fake_scriptstatus ()
356 {
357     _code="$1"
358     _status="$2"
359     _err_out="$3"
360
361     _d1=$(date '+%s.%N')
362     _d2=$(date '+%s.%N')
363
364     echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
365 }
366
367 ######################################################################
368
369 setup_ctdb_policy_routing ()
370 {
371     service_name="per_ip_routing"
372
373     export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing"
374     export CTDB_PER_IP_ROUTING_RULE_PREF=100
375     export CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000
376     export CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000
377
378     # Tests need to create and populate this file
379     rm -f "$CTDB_PER_IP_ROUTING_CONF"
380 }
381
382 # Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF.
383 # $1 is the number of assigned IPs to use (<num>, all), defaulting to
384 # 1.  If $2 is "default" then a default route is also added.
385 create_policy_routing_config ()
386 {
387     _num_ips="${1:-1}"
388     _should_add_default="$2"
389
390     ctdb_get_my_public_addresses |
391     if [ "$_num_ips" = "all" ] ; then
392         cat
393     else
394         head -n "$_num_ips"
395     fi |
396     while read _dev _ip _bits ; do
397         _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
398         _gw="${_net%.*}.1" # a dumb, calculated default
399
400         echo "$_ip $_net"
401
402         if [ "$_should_add_default" = "default" ] ; then
403             echo "$_ip 0.0.0.0/0 $_gw"
404         fi
405     done >"$CTDB_PER_IP_ROUTING_CONF"
406 }
407
408 # Check the routes against those that are expected.  $1 is the number
409 # of assigned IPs to use (<num>, all), defaulting to 1.  If $2 is
410 # "default" then expect default routes to have been added.
411 check_routes ()
412 {
413     _num_ips="${1:-1}"
414     _should_add_default="$2"
415
416     _policy_rules=""
417     _policy_routes=""
418
419     ctdb_get_my_public_addresses |
420     if [ "$_num_ips" = "all" ] ; then
421         cat
422     else
423         head -n "$_num_ips"
424     fi | {
425         while read _dev _ip _bits ; do
426             _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
427             _gw="${_net%.*}.1" # a dumb, calculated default
428
429             _policy_rules="${_policy_rules}
430 ${CTDB_PER_IP_ROUTING_RULE_PREF}:       from $_ip lookup ctdb.$_ip "
431             _policy_routes="${_policy_routes}
432 # ip route show table ctdb.$_ip
433 $_net dev $_dev  scope link "
434
435             if [ "$_should_add_default" = "default" ] ; then
436                 _policy_routes="${_policy_routes}
437 default via $_gw dev $_dev "
438             fi
439         done
440
441         ok <<EOF
442 # ip rule show
443 0:      from all lookup local ${_policy_rules}
444 32766:  from all lookup main 
445 32767:  from all lookup default ${_policy_routes}
446 EOF
447
448         simple_test_command dump_routes
449     }
450 }
451
452 ######################################################################
453
454 # Samba/winbind fakery
455
456 setup_samba ()
457 {
458     setup_ctdb
459
460     service_name="samba"
461
462     if [ "$1" != "down" ] ; then
463
464         debug "Marking Samba services as up, listening and managed by CTDB"
465         # Get into known state.
466         eventscript_call ctdb_service_managed
467
468         # All possible service names for all known distros.
469         for i in "smb" "nmb" "samba" ; do
470             service "$i" force-started
471         done
472
473         export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
474         export CTDB_MANAGED_SERVICES="foo samba bar"
475
476         export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
477         export FAKE_WBINFO_FAIL="no"
478
479         # Some things in 50.samba are backgrounded and waited for.  If
480         # we don't sleep at all then timeouts can happen.  This avoids
481         # that...  :-)
482         export FAKE_SLEEP_FORCE=0.1
483     else
484         debug "Marking Samba services as down, not listening and not managed by CTDB"
485         # Get into known state.
486         eventscript_call ctdb_service_unmanaged
487
488         # All possible service names for all known distros.
489         for i in "smb" "nmb" "samba" ; do
490             service "$i" force-stopped
491         done
492
493         export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
494         export CTDB_MANAGED_SERVICES="foo bar"
495         unset CTDB_MANAGES_SAMBA
496
497         export FAKE_TCP_LISTEN=""
498         export FAKE_WBINFO_FAIL="yes"
499     fi
500
501     # This is ugly but if this file isn't removed before each test
502     # then configuration changes between tests don't stick.
503     rm -f "$CTDB_VARDIR/state/samba/smb.conf.cache"
504 }
505
506 setup_winbind ()
507 {
508     setup_ctdb
509
510     service_name="winbind"
511
512     if [ "$1" != "down" ] ; then
513
514         debug "Marking Winbind service as up and managed by CTDB"
515         # Get into known state.
516         eventscript_call ctdb_service_managed
517
518         service "winbind" force-started
519
520         export CTDB_MANAGED_SERVICES="foo winbind bar"
521
522         export FAKE_WBINFO_FAIL="no"
523
524     else
525         debug "Marking Winbind service as down and not managed by CTDB"
526         # Get into known state.
527         eventscript_call ctdb_service_unmanaged
528
529         service "winbind" force-stopped
530
531         export CTDB_MANAGED_SERVICES="foo bar"
532         unset CTDB_MANAGES_WINBIND
533
534         export FAKE_WBINFO_FAIL="yes"
535     fi
536 }
537
538 wbinfo_down ()
539 {
540     debug "Making wbinfo commands fail"
541     FAKE_WBINFO_FAIL="yes"
542 }
543
544 ######################################################################
545
546 # NFS fakery
547
548 setup_nfs ()
549 {
550     setup_ctdb
551
552     service_name="nfs"
553
554     export FAKE_RPCINFO_SERVICES=""
555
556     export CTDB_NFS_SKIP_SHARE_CHECK="no"
557
558     export CTDB_MONITOR_NFS_THREAD_COUNT RPCNFSDCOUNT FAKE_NFSD_THREAD_PIDS
559
560     # Reset the failcounts for nfs services.
561     eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
562
563     if [ "$1" != "down" ] ; then
564         debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
565
566         eventscript_call ctdb_service_managed
567         service "nfs" force-started  # might not be enough
568
569         export CTDB_MANAGED_SERVICES="foo nfs bar"
570
571         rpc_services_up "nfs" "mountd" "rquotad" "nlockmgr" "status"
572     else
573         debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
574
575         eventscript_call ctdb_service_unmanaged
576         service "nfs" force-stopped  # might not be enough
577         eventscript_call startstop_nfs stop
578
579         export CTDB_MANAGED_SERVICES="foo bar"
580         unset CTDB_MANAGES_NFS
581     fi
582 }
583
584 rpc_services_down ()
585 {
586     for _i ; do
587         debug "Marking RPC service \"${_i}\" as unavailable"
588         FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
589     done
590 }
591
592 rpc_services_up ()
593 {
594     for _i ; do
595         debug "Marking RPC service \"${_i}\" as available"
596         case "$_i" in
597             nfs)      _t="2:3" ;;
598             mountd)   _t="1:3" ;;
599             rquotad)  _t="1:2" ;;
600             nlockmgr) _t="3:4" ;;
601             status)   _t="1:1" ;;
602             *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
603         esac
604
605         FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
606     done
607 }
608
609 # Set the required result for a particular RPC program having failed
610 # for a certain number of iterations.  This is probably still a work
611 # in progress.  Note that we could hook aggressively
612 # nfs_check_rpc_service() to try to implement this but we're better
613 # off testing nfs_check_rpc_service() using independent code...  even
614 # if it is incomplete and hacky.  So, if the 60.nfs eventscript
615 # changes and the tests start to fail then it may be due to this
616 # function being incomplete.
617 rpc_set_service_failure_response ()
618 {
619     _progname="$1"
620     # The number of failures defaults to the iteration number.  This
621     # will be true when we fail from the 1st iteration... but we need
622     # the flexibility to set the number of failures.
623     _numfails="${2:-${iteration}}"
624
625     _etc="$CTDB_ETCDIR" # shortcut for readability
626     for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
627         if [ -r "$_c" ] ; then
628             . "$_c"
629             break
630         fi
631     done
632
633     # A handy newline.  :-)
634     _nl="
635 "
636
637     # Default
638     ok_null
639
640     _file=$(ls "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9]."${_progname}.check")
641     [ -r "$_file" ] || die "RPC check file \"$_file\" does not exist or is not unique"
642
643     while read _op _li _actions ; do
644         # Skip comments
645         case "$_op" in
646             \#*) continue ;;
647         esac
648
649         if [ "$_numfails" "$_op" "$_li" ] ; then
650             _out=""
651             _rc=0
652             for _action in $_actions ; do
653                 case "$_action" in
654                     verbose)
655                         _ver=1
656                         _pn="$_progname"
657                         case "$_progname" in
658                             nfsd) _ver=3 ; _pn="nfs" ;;
659                             lockd) _ver=4 ; _pn="nlockmgr" ;;
660                             statd) _pn="status" ;;
661                         esac
662                         _out="\
663 ERROR: $_pn failed RPC check:
664 rpcinfo: RPC: Program not registered
665 program $_pn version $_ver is not available"
666                         ;;
667                     restart|restart:*)
668                         _opts=""
669                         _p="rpc.${_progname}"
670                         case "$_progname" in
671                             statd)
672                                 _opts="${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
673                                 ;;
674                         esac
675                         case "${_progname}${_action#restart}" in
676                             nfsd)
677                                 _t="\
678 Trying to restart NFS service
679 Starting nfslock: OK
680 Starting nfs: OK"
681                                 ;;
682                             nfsd:bs)
683                                 _t="Trying to restart NFS service"
684                                 ;;
685                             lockd|lockd:b)
686                                 _t="\
687 Trying to restart lock manager service
688 Stopping nfslock: OK
689 Starting nfslock: OK"
690                                 ;;
691                             lockd:*)
692                                 _t="Trying to restart lock manager service"
693                                 ;;
694                             *)
695                                 _t="Trying to restart $_progname [${_p}${_opts}]"
696                         esac
697                         _out="${_out}${_out:+${_nl}}${_t}"
698                         ;;
699                     unhealthy)
700                         _rc=1
701                 esac
702             done
703             required_result $_rc "$_out"
704             return
705         fi
706     done <"$_file"
707 }
708
709 ######################################################################
710
711 # VSFTPD fakery
712
713 setup_vsftpd ()
714 {
715     service_name="vsftpd"
716
717     if [ "$1" != "down" ] ; then
718         die "setup_vsftpd up not implemented!!!"
719     else
720         debug "Setting up VSFTPD environment: service down, not managed by CTDB"
721
722         eventscript_call ctdb_service_unmanaged
723         service vsftpd force-stopped
724
725         export CTDB_MANAGED_SERVICES="foo"
726         unset CTDB_MANAGES_VSFTPD
727     fi
728 }
729
730 ######################################################################
731
732 # HTTPD fakery
733
734 setup_httpd ()
735 {
736     if [ "$1" != "down" ] ; then
737         die "setup_httpd up not implemented!!!"
738     else
739         debug "Setting up HTTPD environment: service down, not managed by CTDB"
740
741         for service_name in "apache2" "httpd" ; do
742             eventscript_call ctdb_service_unmanaged
743             service "$service_name" force-stopped
744         done
745
746         export CTDB_MANAGED_SERVICES="foo"
747         unset CTDB_MANAGES_HTTPD
748     fi
749 }
750
751 ######################################################################
752
753 # Result and test functions
754
755 # Set some globals and print the summary.
756 define_test ()
757 {
758     desc="$1"
759
760     _f=$(basename "$0" ".sh")
761
762     # Remaining format should be NN.service.event.NNN or NN.service.NNN:
763     _num="${_f##*.}"
764     _f="${_f%.*}"
765     case "$_f" in
766         *.*.*)
767             script="${_f%.*}"
768             event="${_f##*.}"
769             ;;
770         *.*)
771             script="$_f"
772             unset event
773             ;;
774         *)
775             die "Internal error - unknown testcase filename format"
776     esac
777
778     printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
779 }
780
781 _extra_header ()
782 {
783     cat <<EOF
784 CTDB_BASE="$CTDB_BASE"
785 CTDB_ETCDIR="$CTDB_ETCDIR"
786 ctdb client is "$(which ctdb)"
787 EOF
788 }
789
790 # Run an eventscript once.  The test passes if the return code and
791 # output match those required.
792
793 # Any args are passed to the eventscript.
794
795 # Eventscript tracing can be done by setting:
796 #   EVENTSCRIPTS_TESTS_TRACE="sh -x"
797
798 # or similar.  This will almost certainly make a test fail but is
799 # useful for debugging.
800 simple_test ()
801 {
802     [ -n "$event" ] || die 'simple_test: $event not set'
803
804     _extra_header=$(_extra_header)
805
806     echo "Running eventscript \"$script $event${1:+ }$*\""
807     _trace=""
808     if $TEST_COMMAND_TRACE ; then
809         _trace="sh -x"
810     fi
811     _out=$($_trace "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
812
813     result_check "$_extra_header"
814 }
815
816 simple_test_event ()
817 {
818     # If something has previously failed then don't continue.
819     : ${_passed:=true}
820     $_passed || return 1
821
822     event="$1" ; shift
823     echo "=================================================="
824     simple_test "$@"
825 }
826
827 simple_test_command ()
828 {
829     # If something has previously failed then don't continue.
830     : ${_passed:=true}
831     $_passed || return 1
832
833     echo "=================================================="
834     echo "Running command \"$*\""
835     _out=$("$@" 2>&1)
836
837     result_check
838 }
839
840 check_ctdb_logfile ()
841 {
842     # If something has previously failed then don't continue.
843     : ${_passed:=true}
844     $_passed || return 1
845
846     echo "=================================================="
847     echo "Checking CTDB_LOGFILE=\"${CTDB_LOGFILE}\""
848     _out=$(cat "$CTDB_LOGFILE" 2>&1)
849
850     result_check
851 }
852
853 # Run an eventscript iteratively.
854 # - 1st argument is the number of iterations.
855 # - 2nd argument is something to eval to do setup for every iteration.
856 #   The easiest thing to do here is to define a function and pass it
857 #   here.
858 # - Subsequent arguments come in pairs: an iteration number and
859 #   something to eval for that iteration.  Each time an iteration
860 #   number is matched the associated argument is given to eval after
861 #   the default setup is done.  The iteration numbers need to be given
862 #   in ascending order.
863 #
864 # Some optional args can be given *before* these, surrounded by extra
865 # "--" args.  These args are passed to the eventscript.  Quoting is
866 # lost.
867 #
868 # One use of the 2nd and further arguments is to call
869 # required_result() to change what is expected of a particular
870 # iteration.
871 iterate_test ()
872 {
873     [ -n "$event" ] || die 'simple_test: $event not set'
874
875     args=""
876     if [ "$1" = "--" ] ; then
877         shift
878         while [ "$1" != "--" ] ; do
879             args="${args}${args:+ }$1"
880             shift
881         done
882         shift
883     fi
884
885     _repeats="$1"
886     _setup_default="$2"
887     shift 2
888
889     echo "Running $_repeats iterations of \"$script $event\" $args"
890
891     _result=true
892
893     for iteration in $(seq 1 $_repeats) ; do
894         # This is inefficient because the iteration-specific setup
895         # might completely replace the default one.  However, running
896         # the default is good because it allows you to revert to a
897         # particular result without needing to specify it explicitly.
898         eval $_setup_default
899         if [ $iteration = "$1" ] ; then
900             eval $2
901             shift 2
902         fi
903
904         _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" $args 2>&1)
905         _rc=$?
906
907     if [ -n "$OUT_FILTER" ] ; then
908         _fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
909     else
910         _fout="$_out"
911     fi
912
913         if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
914             _passed=true
915         else
916             _passed=false
917             _result=false
918         fi
919
920         result_print "$_passed" "$_out" "$_rc" "Iteration $iteration"
921     done
922
923     result_footer "$_result" "$(_extra_header)"
924 }