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