Merge branch 'eventscript_tests' into ronnie_target
[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_NETSTAT_TCP_LISTEN=$(echo "$FAKE_NETSTAT_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_NETSTAT_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
307         export FAKE_WBINFO_FAIL="no"
308     else
309         debug "Marking Samba services as down, not listening and not managed by CTDB"
310         # Get into known state.
311         for i in "samba" "winbind" ; do
312             eventscript_call ctdb_service_unmanaged "$i"
313         done
314         # All possible service names for all known distros.
315         for i in "smb" "nmb" "winbind" "samba" ; do
316             service "$i" force-stopped
317         done
318
319         export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
320         export CTDB_MANAGED_SERVICES="foo bar"
321         unset CTDB_MANAGES_SAMBA
322         unset CTDB_MANAGES_WINBIND
323
324         export FAKE_NETSTAT_TCP_LISTEN=""
325         export FAKE_WBINFO_FAIL="yes"
326     fi
327
328     # This is ugly but if this file isn't removed before each test
329     # then configuration changes between tests don't stick.
330     rm -f "$CTDB_VARDIR/state/samba/smb.conf.cache"
331 }
332
333 wbinfo_down ()
334 {
335     debug "Making wbinfo commands fail"
336     FAKE_WBINFO_FAIL="yes"
337 }
338
339 ######################################################################
340
341 # NFS fakery
342
343 setup_nfs ()
344 {
345     setup_ctdb
346
347     export FAKE_RPCINFO_SERVICES=""
348
349     export CTDB_NFS_SKIP_SHARE_CHECK="no"
350     export CTDB_NFS_SKIP_KNFSD_ALIVE_CHECK="no"
351
352     # Reset the failcounts for nfs services.
353     eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
354
355     rpc_fail_limits_file="${EVENTSCRIPTS_TESTS_VAR_DIR}/rpc_fail_limits"
356
357     # Force this file to exist so tests can be individually run.
358     if [ ! -f "$rpc_fail_limits_file" ] ; then
359         # This is gross... but is needed to fake through the nfs monitor event.
360         eventscript_call ctdb_service_managed "nfs"
361         service "nfs" force-started  # might not be enough
362         CTDB_RC_LOCAL="$CTDB_BASE/rc.local.nfs.monitor.get-limits" \
363             CTDB_MANAGES_NFS="yes" \
364             "${CTDB_BASE}/events.d/60.nfs" "monitor" >"$rpc_fail_limits_file"
365     fi
366     
367     if [ "$1" != "down" ] ; then
368         debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
369
370         eventscript_call ctdb_service_managed "nfs"
371         service "nfs" force-started  # might not be enough
372
373         export CTDB_MANAGED_SERVICES="foo nfs bar"
374
375         rpc_services_up "nfs" "mountd" "rquotad" "nlockmgr" "status"
376     else
377         debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
378
379         eventscript_call ctdb_service_unmanaged "nfs"
380         service "nfs" force-stopped  # might not be enough
381         eventscript_call startstop_nfs stop
382
383         export CTDB_MANAGED_SERVICES="foo bar"
384         unset CTDB_MANAGES_NFS
385     fi
386 }
387
388 rpc_services_down ()
389 {
390     for _i ; do
391         debug "Marking RPC service \"${_i}\" as unavailable"
392         FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
393     done
394 }
395
396 rpc_services_up ()
397 {
398     for _i ; do
399         debug "Marking RPC service \"${_i}\" as available"
400         case "$_i" in
401             nfs)      _t="2:3" ;;
402             mountd)   _t="1:3" ;;
403             rquotad)  _t="1:2" ;;
404             nlockmgr) _t="3:4" ;;
405             status)   _t="1:1" ;;
406             *)
407                 echo "Internal error - unsupported RPC service \"${_i}\""
408                 exit 1
409         esac
410
411         FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
412     done
413 }
414
415 # Set the required result for a particular RPC program having failed
416 # for a certain number of iterations.  This is probably still a work
417 # in progress.  Note that we could hook aggressively
418 # nfs_check_rpc_service() to try to implement this but we're better
419 # off testing nfs_check_rpc_service() using independent code...  even
420 # if it is incomplete and hacky.  So, if the 60.nfs eventscript
421 # changes and the tests start to fail then it may be due to this
422 # function being incomplete.
423 rpc_set_service_failure_response ()
424 {
425     _progname="$1"
426     # The number of failures defaults to the iteration number.  This
427     # will be true when we fail from the 1st iteration... but we need
428     # the flexibility to set the number of failures.
429     _numfails="${2:-${iteration}}"
430
431     _c="${CTDB_ETCDIR}/sysconfig/nfs"
432     if [ -r "$_c" ] ; then
433         . "$_c"
434     fi
435
436     # A handy newline.  :-)
437     _nl="
438 "
439
440     # Default
441     ok_null
442
443     _ts=$(sed -n -e "s@^${_progname} @@p" "$rpc_fail_limits_file")
444
445     while [ -n "$_ts" ] ; do
446         # Get the triple: operator, fail limit and actions.
447         _op="${_ts%% *}" ; _ts="${_ts#* }"
448         _li="${_ts%% *}" ; _ts="${_ts#* }"
449         # We've lost some of the quoting but we can simulate
450         # because we know an operator is always the first in a
451         # triple.
452         _actions=""
453         while [ -n "$_ts" ] ; do
454             # If this is an operator then we've got all of the
455             # actions.
456             case "$_ts" in
457                 -*) break ;;
458             esac
459
460             _actions="${_actions}${_actions:+ }${_ts%% *}"
461             # Special case for end of list.
462             if [ "$_ts" != "${_ts#* }" ] ; then
463                 _ts="${_ts#* }"
464             else
465                 _ts=""
466             fi
467         done
468
469         if [ "$_numfails" "$_op" "$_li" ] ; then
470             _out=""
471             _rc=0
472             for _action in $_actions ; do
473                 case "$_action" in
474                     verbose)
475                         _ver=1
476                         _pn="$_progname"
477                         case "$_progname" in
478                             knfsd) _ver=3 ; _pn="nfs" ;;
479                             lockd) _ver=4 ; _pn="nlockmgr" ;;
480                             statd) _pn="status" ;;
481                         esac
482                         _out="\
483 ERROR: $_pn failed RPC check:
484 rpcinfo: RPC: Program not registered
485 program $_pn version $_ver is not available"
486                         ;;
487                     restart|restart:*)
488                         _opts=""
489                         _p="rpc.${_progname}"
490                         case "$_progname" in
491                             statd)
492                                 _opts="${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
493                                 ;;
494                         esac
495                         case "${_progname}${_action#restart}" in
496                             knfsd)
497                                 _t="\
498 Trying to restart NFS service
499 Starting nfslock: OK
500 Starting nfs: OK"
501                                 ;;
502                             knfsd:bs)
503                                 _t="Trying to restart NFS service"
504                                 ;;
505                             lockd)
506                                 _t="\
507 Trying to restart lock manager service
508 Stopping nfslock: OK
509 Starting nfslock: OK"
510                                 ;;
511                             lockd:*)
512                                 _t="Trying to restart lock manager service"
513                                 ;;
514                             *)
515                                 _t="Trying to restart $_progname [${_p}${_opts}]"
516                         esac
517                         _out="${_out}${_out:+${_nl}}${_t}"
518                         ;;
519                     unhealthy)
520                         _rc=1
521                 esac
522             done
523             required_result $_rc "$_out"
524             return
525         fi
526     done
527 }
528
529 ######################################################################
530
531 # VSFTPD fakery
532
533 setup_vsftpd ()
534 {
535     if [ "$1" != "down" ] ; then
536         echo "setup_vsftpd up not implemented!!!"
537         exit 1
538     else
539         debug "Setting up VSFTPD environment: service down, not managed by CTDB"
540
541         eventscript_call ctdb_service_unmanaged vsftpd
542         service vsftpd force-stopped
543
544         export CTDB_MANAGED_SERVICES="foo"
545         unset CTDB_MANAGES_VSFTPD
546     fi
547 }
548
549 ######################################################################
550
551 # HTTPD fakery
552
553 setup_httpd ()
554 {
555     if [ "$1" != "down" ] ; then
556         echo "setup_httpd up not implemented!!!"
557         exit 1
558     else
559         debug "Setting up HTTPD environment: service down, not managed by CTDB"
560
561         for i in "apache2" "httpd" ; do
562             eventscript_call ctdb_service_unmanaged "$i"
563             service "$i" force-stopped
564         done
565
566         export CTDB_MANAGED_SERVICES="foo"
567         unset CTDB_MANAGES_HTTPD
568     fi
569 }
570
571 ######################################################################
572
573 # Result and test functions
574
575 # Set some globals and print the summary.
576 define_test ()
577 {
578     desc="$1"
579
580     _f="$0"
581     _f="${_f#./}"      # strip leading ./
582     _f="${_f#simple/}" # strip leading simple/
583     _f="${_f%%/*}"     # if subdir, strip off file
584     _f="${_f%.sh}"     # strip off .sh suffix if any
585
586     # Remaining format should be NN.service.event.NNN:
587     _num="${_f##*.}"
588     _f="${_f%.*}"
589     event="${_f##*.}"
590     script="${_f%.*}"
591
592     printf "%-14s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
593 }
594
595 # Set the required result for a test.
596 # - Argument 1 is exit code.
597 # - Argument 2, if present is the required test output but "--"
598 #   indicates empty output.
599 # If argument 2 is not present or null then read required test output
600 # from stdin.
601 required_result ()
602 {
603     required_rc="${1:-0}"
604     if [ -n "$2" ] ; then
605         if [ "$2" = "--" ] ; then
606             required_output=""
607         else
608             required_output="$2"
609         fi
610     else
611         if ! tty -s ; then
612             required_output=$(cat)
613         else
614             required_output=""
615         fi
616     fi
617 }
618
619 ok ()
620 {
621     required_result 0 "$@"
622 }
623
624 ok_null ()
625 {
626     ok --
627 }
628
629 result_print ()
630 {
631     _passed="$1"
632     _out="$2"
633     _rc="$3"
634     _iteration="$4"
635
636     if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
637         if [ -n "$_iteration" ] ; then
638             cat <<EOF
639
640 ##################################################
641 ##################################################
642 Iteration $_iteration
643 EOF
644         fi
645
646 cat <<EOF
647 ##################################################
648 Output (Exit status: ${_rc}):
649 ##################################################
650 $_out
651 EOF
652     fi
653
654     if ! $_passed ; then
655         cat <<EOF
656 ##################################################
657 Required output (Exit status: ${required_rc}):
658 ##################################################
659 $required_output
660 EOF
661     fi
662 }
663
664 result_footer ()
665 {
666     _passed="$1"
667
668     if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
669
670         cat <<EOF
671 ##################################################
672 CTDB_BASE="$CTDB_BASE"
673 CTDB_ETCDIR="$CTDB_ETCDIR"
674 ctdb client is "$(which ctdb)"
675 ##################################################
676 EOF
677     fi
678
679     echo
680
681     if $_passed ; then
682         echo "PASSED"
683         return 0
684     else
685         echo "FAILED"
686         return 1
687     fi
688 }
689
690 # Run an eventscript once.  The test passes if the return code and
691 # output match those required.
692
693 # Any args are passed to the eventscript.
694
695 # Eventscript tracing can be done by setting:
696 #   EVENTSCRIPTS_TESTS_TRACE="sh -x"
697
698 # or similar.  This will almost certainly make a test fail but is
699 # useful for debugging.
700 simple_test ()
701 {
702     echo "Running \"${CTDB_BASE}/events.d/$script $event\""
703     _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
704     _rc=$?
705
706     if [ "$_out" = "$required_output" -a $_rc = $required_rc ] ; then
707         _passed=true
708     else
709         _passed=false
710     fi
711
712     result_print "$_passed" "$_out" "$_rc"
713     result_footer "$_passed"
714 }
715
716 # Run an eventscript iteratively.
717 # - 1st argument is the number of iterations.
718 # - 2nd argument is something to eval to do setup for every iteration.
719 #   The easiest thing to do here is to define a function and pass it
720 #   here.
721 # - Subsequent arguments come in pairs: an iteration number and
722 #   something to eval for that iteration.  Each time an iteration
723 #   number is matched the associated argument is given to eval after
724 #   the default setup is done.  The iteration numbers need to be given
725 #   in ascending order.
726 #
727 # Some optional args can be given *before* these, surrounded by extra
728 # "--" args.  These args are passed to the eventscript.  Quoting is
729 # lost.
730 #
731 # One use of the 2nd and further arguments is to call
732 # required_result() to change what is expected of a particular
733 # iteration.
734 iterate_test ()
735 {
736     args=""
737     if [ "$1" = "--" ] ; then
738         shift
739         while [ "$1" != "--" ] ; do
740             args="${args}${args:+ }$1"
741             shift
742         done
743         shift
744     fi
745
746     _repeats="$1"
747     _setup_default="$2"
748     shift 2
749
750     echo "Running $_repeats iterations of \"${CTDB_BASE}/events.d/$script $event\" $args"
751
752     _result=true
753
754     for iteration in $(seq 1 $_repeats) ; do
755         # This is inefficient because the iteration-specific setup
756         # might completely replace the default one.  However, running
757         # the default is good because it allows you to revert to a
758         # particular result without needing to specify it explicitly.
759         eval $_setup_default
760         if [ $iteration = "$1" ] ; then
761             eval $2
762             shift 2
763         fi
764
765         _out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" $args 2>&1)
766         _rc=$?
767
768         if [ "$_out" = "$required_output" -a $_rc = $required_rc ] ; then
769             _passed=true
770         else
771             _passed=false
772             _result=false
773         fi
774
775         result_print "$_passed" "$_out" "$_rc" "$iteration"
776     done
777
778     result_footer "$_result"
779 }