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