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