tests/eventscripts: setup_ctdb() should always set $CTDB_PUBLIC_ADDRESSES
[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     export CTDB_PUBLIC_ADDRESSES="${CTDB_BASE}/public_addresses"
279     if [ -n "$3" ] ; then
280         echo "Setting up CTDB_PUBLIC_ADDRESSES: $3"
281         CTDB_PUBLIC_ADDRESSES=$(mktemp)
282         for _i in $3 ; do
283             _ip="${_i%@*}"
284             _ifaces="${_i#*@}"
285             echo "${_ip} ${_ifaces}" >>"$CTDB_PUBLIC_ADDRESSES"
286         done
287         eventscripts_test_add_cleanup "rm -f $CTDB_PUBLIC_ADDRESSES"
288     fi
289
290     export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
291
292     export FAKE_CTDB_IFACES_DOWN="$FAKE_CTDB_STATE/ifaces-down"
293     mkdir -p "$FAKE_CTDB_IFACES_DOWN"
294     rm -f "$FAKE_CTDB_IFACES_DOWN"/*
295
296     export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus"
297     mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
298     rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
299 }
300
301 ctdb_get_interfaces ()
302 {
303     # The echo/subshell forces all the output onto 1 line.
304     echo $(ctdb ifaces -Y | awk -F: 'FNR > 1 {print $2}')
305 }
306
307 ctdb_get_1_interface ()
308 {
309     _t=$(ctdb_get_interfaces)
310     echo ${_t%% *}
311 }
312
313 # Print all public addresses as: interface IP maskbits
314 # Each line is suitable for passing to takeip/releaseip
315 ctdb_get_all_public_addresses ()
316 {
317     _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
318     while IFS="/$IFS" read _ip _maskbits _ifaces ; do
319         echo "$_ifaces $_ip $_maskbits"
320     done <"$_f"
321 }
322
323 # Print public addresses on this node as: interface IP maskbits
324 # Each line is suitable for passing to takeip/releaseip
325 ctdb_get_my_public_addresses ()
326 {
327     ctdb ip -v -Y | {
328         read _x # skip header line
329
330         while IFS=":" read _x _ip _x _iface _x ; do
331             [ -n "$_iface" ] || continue
332             while IFS="/$IFS" read _i _maskbits _x ; do
333                 if [ "$_ip" = "$_i" ] ; then
334                     echo $_iface $_ip $_maskbits
335                     break
336                 fi
337             done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
338         done
339     }
340 }
341
342 # Prints the 1st public address as: interface IP maskbits
343 # This is suitable for passing to takeip/releaseip
344 ctdb_get_1_public_address ()
345 {
346     ctdb_get_my_public_addresses | head -n 1
347 }
348
349 ctdb_not_implemented ()
350 {
351     export CTDB_NOT_IMPLEMENTED="$1"
352     ctdb_not_implemented="\
353 DEBUG: ctdb: command \"$1\" not implemented in stub"
354 }
355
356 ctdb_fake_scriptstatus ()
357 {
358     _code="$1"
359     _status="$2"
360     _err_out="$3"
361
362     _d1=$(date '+%s.%N')
363     _d2=$(date '+%s.%N')
364
365     echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
366 }
367
368 ######################################################################
369
370 setup_ctdb_policy_routing ()
371 {
372     service_name="per_ip_routing"
373
374     export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing"
375     export CTDB_PER_IP_ROUTING_RULE_PREF=100
376     export CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000
377     export CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000
378
379     # Tests need to create and populate this file
380     rm -f "$CTDB_PER_IP_ROUTING_CONF"
381 }
382
383 # Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF.
384 # $1 is the number of assigned IPs to use (<num>, all), defaulting to
385 # 1.  If $2 is "default" then a default route is also added.
386 create_policy_routing_config ()
387 {
388     _num_ips="${1:-1}"
389     _should_add_default="$2"
390
391     ctdb_get_my_public_addresses |
392     if [ "$_num_ips" = "all" ] ; then
393         cat
394     else
395         head -n "$_num_ips"
396     fi |
397     while read _dev _ip _bits ; do
398         _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
399         _gw="${_net%.*}.1" # a dumb, calculated default
400
401         echo "$_ip $_net"
402
403         if [ "$_should_add_default" = "default" ] ; then
404             echo "$_ip 0.0.0.0/0 $_gw"
405         fi
406     done >"$CTDB_PER_IP_ROUTING_CONF"
407 }
408
409 # Check the routes against those that are expected.  $1 is the number
410 # of assigned IPs to use (<num>, all), defaulting to 1.  If $2 is
411 # "default" then expect default routes to have been added.
412 check_routes ()
413 {
414     _num_ips="${1:-1}"
415     _should_add_default="$2"
416
417     _policy_rules=""
418     _policy_routes=""
419
420     ctdb_get_my_public_addresses |
421     if [ "$_num_ips" = "all" ] ; then
422         cat
423     else
424         head -n "$_num_ips"
425     fi | {
426         while read _dev _ip _bits ; do
427             _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
428             _gw="${_net%.*}.1" # a dumb, calculated default
429
430             _policy_rules="${_policy_rules}
431 ${CTDB_PER_IP_ROUTING_RULE_PREF}:       from $_ip lookup ctdb.$_ip "
432             _policy_routes="${_policy_routes}
433 # ip route show table ctdb.$_ip
434 $_net dev $_dev  scope link "
435
436             if [ "$_should_add_default" = "default" ] ; then
437                 _policy_routes="${_policy_routes}
438 default via $_gw dev $_dev "
439             fi
440         done
441
442         ok <<EOF
443 # ip rule show
444 0:      from all lookup local ${_policy_rules}
445 32766:  from all lookup main 
446 32767:  from all lookup default ${_policy_routes}
447 EOF
448
449         simple_test_command dump_routes
450     }
451 }
452
453 ######################################################################
454
455 # Samba/winbind fakery
456
457 setup_samba ()
458 {
459     setup_ctdb
460
461     service_name="samba"
462
463     if [ "$1" != "down" ] ; then
464
465         debug "Marking Samba services as up, listening and managed by CTDB"
466         # Get into known state.
467         eventscript_call ctdb_service_managed
468
469         # All possible service names for all known distros.
470         for i in "smb" "nmb" "samba" ; do
471             service "$i" force-started
472         done
473
474         export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
475         export CTDB_MANAGED_SERVICES="foo samba bar"
476
477         export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
478         export FAKE_WBINFO_FAIL="no"
479
480         # Some things in 50.samba are backgrounded and waited for.  If
481         # we don't sleep at all then timeouts can happen.  This avoids
482         # that...  :-)
483         export FAKE_SLEEP_FORCE=0.1
484     else
485         debug "Marking Samba services as down, not listening and not managed by CTDB"
486         # Get into known state.
487         eventscript_call ctdb_service_unmanaged
488
489         # All possible service names for all known distros.
490         for i in "smb" "nmb" "samba" ; do
491             service "$i" force-stopped
492         done
493
494         export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
495         export CTDB_MANAGED_SERVICES="foo bar"
496         unset CTDB_MANAGES_SAMBA
497
498         export FAKE_TCP_LISTEN=""
499         export FAKE_WBINFO_FAIL="yes"
500     fi
501
502     # This is ugly but if this file isn't removed before each test
503     # then configuration changes between tests don't stick.
504     rm -f "$CTDB_VARDIR/state/samba/smb.conf.cache"
505 }
506
507 setup_winbind ()
508 {
509     setup_ctdb
510
511     service_name="winbind"
512
513     if [ "$1" != "down" ] ; then
514
515         debug "Marking Winbind service as up and managed by CTDB"
516         # Get into known state.
517         eventscript_call ctdb_service_managed
518
519         service "winbind" force-started
520
521         export CTDB_MANAGED_SERVICES="foo winbind bar"
522
523         export FAKE_WBINFO_FAIL="no"
524
525     else
526         debug "Marking Winbind service as down and not managed by CTDB"
527         # Get into known state.
528         eventscript_call ctdb_service_unmanaged
529
530         service "winbind" force-stopped
531
532         export CTDB_MANAGED_SERVICES="foo bar"
533         unset CTDB_MANAGES_WINBIND
534
535         export FAKE_WBINFO_FAIL="yes"
536     fi
537 }
538
539 wbinfo_down ()
540 {
541     debug "Making wbinfo commands fail"
542     FAKE_WBINFO_FAIL="yes"
543 }
544
545 ######################################################################
546
547 # NFS fakery
548
549 setup_nfs ()
550 {
551     setup_ctdb
552
553     service_name="nfs"
554
555     export FAKE_RPCINFO_SERVICES=""
556
557     export CTDB_NFS_SKIP_SHARE_CHECK="no"
558
559     export CTDB_MONITOR_NFS_THREAD_COUNT RPCNFSDCOUNT FAKE_NFSD_THREAD_PIDS
560     export CTDB_NFS_DUMP_STUCK_THREADS
561
562     # Reset the failcounts for nfs services.
563     eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
564
565     if [ "$1" != "down" ] ; then
566         debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
567
568         eventscript_call ctdb_service_managed
569         service "nfs" force-started  # might not be enough
570
571         export CTDB_MANAGED_SERVICES="foo nfs bar"
572
573         rpc_services_up "nfs" "mountd" "rquotad" "nlockmgr" "status"
574     else
575         debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
576
577         eventscript_call ctdb_service_unmanaged
578         service "nfs" force-stopped  # might not be enough
579         eventscript_call startstop_nfs stop
580
581         export CTDB_MANAGED_SERVICES="foo bar"
582         unset CTDB_MANAGES_NFS
583     fi
584 }
585
586 rpc_services_down ()
587 {
588     for _i ; do
589         debug "Marking RPC service \"${_i}\" as unavailable"
590         FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
591     done
592 }
593
594 rpc_services_up ()
595 {
596     for _i ; do
597         debug "Marking RPC service \"${_i}\" as available"
598         case "$_i" in
599             nfs)      _t="2:3" ;;
600             mountd)   _t="1:3" ;;
601             rquotad)  _t="1:2" ;;
602             nlockmgr) _t="3:4" ;;
603             status)   _t="1:1" ;;
604             *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
605         esac
606
607         FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
608     done
609 }
610
611 # Set the required result for a particular RPC program having failed
612 # for a certain number of iterations.  This is probably still a work
613 # in progress.  Note that we could hook aggressively
614 # nfs_check_rpc_service() to try to implement this but we're better
615 # off testing nfs_check_rpc_service() using independent code...  even
616 # if it is incomplete and hacky.  So, if the 60.nfs eventscript
617 # changes and the tests start to fail then it may be due to this
618 # function being incomplete.
619 rpc_set_service_failure_response ()
620 {
621     _progname="$1"
622     # The number of failures defaults to the iteration number.  This
623     # will be true when we fail from the 1st iteration... but we need
624     # the flexibility to set the number of failures.
625     _numfails="${2:-${iteration}}"
626
627     _etc="$CTDB_ETCDIR" # shortcut for readability
628     for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
629         if [ -r "$_c" ] ; then
630             . "$_c"
631             break
632         fi
633     done
634
635     # A handy newline.  :-)
636     _nl="
637 "
638
639     # Default
640     ok_null
641
642     _file=$(ls "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9]."${_progname}.check")
643     [ -r "$_file" ] || die "RPC check file \"$_file\" does not exist or is not unique"
644
645     while read _op _li _actions ; do
646         # Skip comments
647         case "$_op" in
648             \#*) continue ;;
649         esac
650
651         if [ "$_numfails" "$_op" "$_li" ] ; then
652             _out=""
653             _rc=0
654             for _action in $_actions ; do
655                 case "$_action" in
656                     verbose)
657                         _ver=1
658                         _pn="$_progname"
659                         case "$_progname" in
660                             nfsd) _ver=3 ; _pn="nfs" ;;
661                             lockd) _ver=4 ; _pn="nlockmgr" ;;
662                             statd) _pn="status" ;;
663                         esac
664                         _out="\
665 ERROR: $_pn failed RPC check:
666 rpcinfo: RPC: Program not registered
667 program $_pn version $_ver is not available"
668                         ;;
669                     restart|restart:*)
670                         _opts=""
671                         _p="rpc.${_progname}"
672                         case "$_progname" in
673                             statd)
674                                 _opts="${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
675                                 ;;
676                         esac
677                         case "${_progname}${_action#restart}" in
678                             nfsd)
679                                 _t="\
680 Trying to restart NFS service"
681
682                                 if [ -n "$CTDB_NFS_DUMP_STUCK_THREADS" ] ; then
683                                     for _pid in $FAKE_NFSD_THREAD_PIDS ; do
684                                         _t="\
685 $_t
686 Stack trace for stuck nfsd thread [${_pid}]:
687 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff"
688                                     done
689                                 fi
690
691                                 _t="\
692 ${_t}
693 Starting nfslock: OK
694 Starting nfs: OK"
695                                 ;;
696                             nfsd:bs)
697                                 _t="Trying to restart NFS service"
698                                 ;;
699                             lockd|lockd:b)
700                                 _t="\
701 Trying to restart lock manager service
702 Stopping nfslock: OK
703 Starting nfslock: OK"
704                                 ;;
705                             lockd:*)
706                                 _t="Trying to restart lock manager service"
707                                 ;;
708                             *)
709                                 _t="Trying to restart $_progname [${_p}${_opts}]"
710                         esac
711                         _out="${_out}${_out:+${_nl}}${_t}"
712                         ;;
713                     unhealthy)
714                         _rc=1
715                 esac
716             done
717             required_result $_rc "$_out"
718             return
719         fi
720     done <"$_file"
721 }
722
723 ######################################################################
724
725 # VSFTPD fakery
726
727 setup_vsftpd ()
728 {
729     service_name="vsftpd"
730
731     if [ "$1" != "down" ] ; then
732         die "setup_vsftpd up not implemented!!!"
733     else
734         debug "Setting up VSFTPD environment: service down, not managed by CTDB"
735
736         eventscript_call ctdb_service_unmanaged
737         service vsftpd force-stopped
738
739         export CTDB_MANAGED_SERVICES="foo"
740         unset CTDB_MANAGES_VSFTPD
741     fi
742 }
743
744 ######################################################################
745
746 # HTTPD fakery
747
748 setup_httpd ()
749 {
750     if [ "$1" != "down" ] ; then
751         die "setup_httpd up not implemented!!!"
752     else
753         debug "Setting up HTTPD environment: service down, not managed by CTDB"
754
755         for service_name in "apache2" "httpd" ; do
756             eventscript_call ctdb_service_unmanaged
757             service "$service_name" force-stopped
758         done
759
760         export CTDB_MANAGED_SERVICES="foo"
761         unset CTDB_MANAGES_HTTPD
762     fi
763 }
764
765 ######################################################################
766
767 # Result and test functions
768
769 # Set some globals and print the summary.
770 define_test ()
771 {
772     desc="$1"
773
774     _f=$(basename "$0" ".sh")
775
776     # Remaining format should be NN.service.event.NNN or NN.service.NNN:
777     _num="${_f##*.}"
778     _f="${_f%.*}"
779     case "$_f" in
780         *.*.*)
781             script="${_f%.*}"
782             event="${_f##*.}"
783             ;;
784         *.*)
785             script="$_f"
786             unset event
787             ;;
788         *)
789             die "Internal error - unknown testcase filename format"
790     esac
791
792     printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
793 }
794
795 _extra_header ()
796 {
797     cat <<EOF
798 CTDB_BASE="$CTDB_BASE"
799 CTDB_ETCDIR="$CTDB_ETCDIR"
800 ctdb client is "$(which ctdb)"
801 EOF
802 }
803
804 # Run an eventscript once.  The test passes if the return code and
805 # output match those required.
806
807 # Any args are passed to the eventscript.
808
809 simple_test ()
810 {
811     [ -n "$event" ] || die 'simple_test: $event not set'
812
813     _extra_header=$(_extra_header)
814
815     echo "Running eventscript \"$script $event${1:+ }$*\""
816     _trace=""
817     if $TEST_COMMAND_TRACE ; then
818         _trace="sh -x"
819     fi
820     _out=$($_trace "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
821
822     result_check "$_extra_header"
823 }
824
825 simple_test_event ()
826 {
827     # If something has previously failed then don't continue.
828     : ${_passed:=true}
829     $_passed || return 1
830
831     event="$1" ; shift
832     echo "=================================================="
833     simple_test "$@"
834 }
835
836 simple_test_command ()
837 {
838     # If something has previously failed then don't continue.
839     : ${_passed:=true}
840     $_passed || return 1
841
842     echo "=================================================="
843     echo "Running command \"$*\""
844     _out=$("$@" 2>&1)
845
846     result_check
847 }
848
849 check_ctdb_logfile ()
850 {
851     # If something has previously failed then don't continue.
852     : ${_passed:=true}
853     $_passed || return 1
854
855     echo "=================================================="
856     echo "Checking CTDB_LOGFILE=\"${CTDB_LOGFILE}\""
857     _out=$(cat "$CTDB_LOGFILE" 2>&1)
858
859     result_check
860 }
861
862 # Run an eventscript iteratively.
863 # - 1st argument is the number of iterations.
864 # - 2nd argument is something to eval to do setup for every iteration.
865 #   The easiest thing to do here is to define a function and pass it
866 #   here.
867 # - Subsequent arguments come in pairs: an iteration number and
868 #   something to eval for that iteration.  Each time an iteration
869 #   number is matched the associated argument is given to eval after
870 #   the default setup is done.  The iteration numbers need to be given
871 #   in ascending order.
872 #
873 # Some optional args can be given *before* these, surrounded by extra
874 # "--" args.  These args are passed to the eventscript.  Quoting is
875 # lost.
876 #
877 # One use of the 2nd and further arguments is to call
878 # required_result() to change what is expected of a particular
879 # iteration.
880 iterate_test ()
881 {
882     [ -n "$event" ] || die 'simple_test: $event not set'
883
884     args=""
885     if [ "$1" = "--" ] ; then
886         shift
887         while [ "$1" != "--" ] ; do
888             args="${args}${args:+ }$1"
889             shift
890         done
891         shift
892     fi
893
894     _repeats="$1"
895     _setup_default="$2"
896     shift 2
897
898     echo "Running $_repeats iterations of \"$script $event\" $args"
899
900     _result=true
901
902     for iteration in $(seq 1 $_repeats) ; do
903         # This is inefficient because the iteration-specific setup
904         # might completely replace the default one.  However, running
905         # the default is good because it allows you to revert to a
906         # particular result without needing to specify it explicitly.
907         eval $_setup_default
908         if [ $iteration = "$1" ] ; then
909             eval $2
910             shift 2
911         fi
912
913         _trace=""
914         if $TEST_COMMAND_TRACE ; then
915             _trace="sh -x"
916         fi
917         _out=$($_trace "${CTDB_BASE}/events.d/$script" "$event" $args 2>&1)
918         _rc=$?
919
920     if [ -n "$OUT_FILTER" ] ; then
921         _fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
922     else
923         _fout="$_out"
924     fi
925
926         if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
927             _passed=true
928         else
929             _passed=false
930             _result=false
931         fi
932
933         result_print "$_passed" "$_out" "$_rc" "Iteration $iteration"
934     done
935
936     result_footer "$_result" "$(_extra_header)"
937 }