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