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