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