tests: Fix a typo in daemons_setup()
[samba.git] / ctdb / tests / scripts / integration.bash
1 # Hey Emacs, this is a -*- shell-script -*- !!!  :-)
2
3 . "${TEST_SCRIPTS_DIR}/common.sh"
4
5 # If we're not running on a real cluster then we need a local copy of
6 # ctdb (and other stuff) in $PATH and we will use local daemons.
7 if [ -n "$TEST_LOCAL_DAEMONS" ] ; then
8     export CTDB_NODES_SOCKETS=""
9     for i in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
10         CTDB_NODES_SOCKETS="${CTDB_NODES_SOCKETS}${CTDB_NODES_SOCKETS:+ }${TEST_VAR_DIR}/sock.${i}"
11     done
12
13     # Use in-tree binaries if running against local daemons.
14     # Otherwise CTDB need to be installed on all nodes.
15     if [ -n "$ctdb_dir" -a -d "${ctdb_dir}/bin" ] ; then
16         PATH="${ctdb_dir}/bin:${PATH}"
17     fi
18
19     export CTDB_NODES="${TEST_VAR_DIR}/nodes.txt"
20 fi
21
22 ######################################################################
23
24 export CTDB_TIMEOUT=60
25
26 if [ -n "$CTDB_TEST_REMOTE_DIR" ] ; then
27     CTDB_TEST_WRAPPER="${CTDB_TEST_REMOTE_DIR}/test_wrap"
28 else
29     _d=$(cd ${TEST_SCRIPTS_DIR}; echo $PWD)
30     CTDB_TEST_WRAPPER="$_d/test_wrap"
31 fi
32 export CTDB_TEST_WRAPPER
33
34 # If $VALGRIND is set then use it whenever ctdb is called, but only if
35 # $CTDB is not already set.
36 [ -n "$CTDB" ] || export CTDB="${VALGRIND}${VALGRIND:+ }ctdb"
37
38 # why???
39 PATH="${TEST_SCRIPTS_DIR}:${PATH}"
40
41 ######################################################################
42
43 ctdb_check_time_logs ()
44 {
45     local threshold=20
46
47     local jump=false
48     local prev=""
49     local ds_prev=""
50     local node=""
51
52     out=$(onnode all tail -n 20 "${TEST_VAR_DIR}/ctdb.test.time.log" 2>&1)
53
54     if [ $? -eq 0 ] ; then
55         local line
56         while read line ; do
57             case "$line" in
58                 \>\>\ NODE:\ *\ \<\<)
59                     node="${line#>> NODE: }"
60                     node=${node% <<*}
61                     ds_prev=""
62                     ;;
63                 *\ *)
64                     set -- $line
65                     ds_curr="$1${2:0:1}"
66                     if [ -n "$ds_prev" ] && \
67                         [ $(($ds_curr - $ds_prev)) -ge $threshold ] ; then
68                         echo "Node $node had time jump of $(($ds_curr - $ds_prev))ds between $(date +'%T' -d @${ds_prev%?}) and $(date +'%T' -d @${ds_curr%?})"
69                         jump=true
70                     fi
71                     prev="$line"
72                     ds_prev="$ds_curr"
73                     ;;
74             esac
75         done <<<"$out"
76     else
77         echo Error getting time logs
78     fi
79     if $jump ; then
80         echo "Check time sync (test client first):"
81         date
82         onnode -p all date
83         echo "Information from test client:"
84         hostname
85         top -b -n 1
86         echo "Information from cluster nodes:"
87         onnode all "top -b -n 1 ; echo '/proc/slabinfo' ; cat /proc/slabinfo"
88     fi
89 }
90
91 ctdb_test_exit ()
92 {
93     local status=$?
94
95     trap - 0
96
97     [ $(($testfailures+0)) -eq 0 -a $status -ne 0 ] && testfailures=$status
98     status=$(($testfailures+0))
99
100     # Avoid making a test fail from this point onwards.  The test is
101     # now complete.
102     set +e
103
104     echo "*** TEST COMPLETED (RC=$status) AT $(date '+%F %T'), CLEANING UP..."
105
106     if [ -z "$TEST_LOCAL_DAEMONS" -a -n "$CTDB_TEST_TIME_LOGGING" -a \
107         $status -ne 0 ] ; then
108         ctdb_check_time_logs
109     fi
110
111     eval "$ctdb_test_exit_hook" || true
112     unset ctdb_test_exit_hook
113
114     if $ctdb_test_restart_scheduled || ! cluster_is_healthy ; then
115
116         restart_ctdb
117     else
118         # This could be made unconditional but then we might get
119         # duplication from the recovery in restart_ctdb.  We want to
120         # leave the recovery in restart_ctdb so that future tests that
121         # might do a manual restart mid-test will benefit.
122         echo "Forcing a recovery..."
123         onnode 0 $CTDB recover
124     fi
125
126     exit $status
127 }
128
129 ctdb_test_exit_hook_add ()
130 {
131     ctdb_test_exit_hook="${ctdb_test_exit_hook}${ctdb_test_exit_hook:+ ; }$*"
132 }
133
134 ctdb_test_init ()
135 {
136     scriptname=$(basename "$0")
137     testfailures=0
138     ctdb_test_restart_scheduled=false
139
140     trap "ctdb_test_exit" 0
141 }
142
143 ########################################
144
145 # Sets: $out
146 try_command_on_node ()
147 {
148     local nodespec="$1" ; shift
149
150     local verbose=false
151     local onnode_opts=""
152
153     while [ "${nodespec#-}" != "$nodespec" ] ; do
154         if [ "$nodespec" = "-v" ] ; then
155             verbose=true
156         else
157             onnode_opts="$nodespec"
158         fi
159         nodespec="$1" ; shift
160     done
161
162     local cmd="$*"
163
164     out=$(onnode -q $onnode_opts "$nodespec" "$cmd" 2>&1) || {
165
166         echo "Failed to execute \"$cmd\" on node(s) \"$nodespec\""
167         echo "$out"
168         return 1
169     }
170
171     if $verbose ; then
172         echo "Output of \"$cmd\":"
173         echo "$out"
174     fi
175 }
176
177 sanity_check_output ()
178 {
179     local min_lines="$1"
180     local regexp="$2" # Should be anchored as necessary.
181     local output="$3"
182
183     local ret=0
184
185     local num_lines=$(echo "$output" | wc -l)
186     echo "There are $num_lines lines of output"
187     if [ $num_lines -lt $min_lines ] ; then
188         echo "BAD: that's less than the required number (${min_lines})"
189         ret=1
190     fi
191
192     local status=0
193     local unexpected # local doesn't pass through status of command on RHS.
194     unexpected=$(echo "$output" | egrep -v "$regexp") || status=$?
195
196     # Note that this is reversed.
197     if [ $status -eq 0 ] ; then
198         echo "BAD: unexpected lines in output:"
199         echo "$unexpected" | cat -A
200         ret=1
201     else
202         echo "Output lines look OK"
203     fi
204
205     return $ret
206 }
207
208 sanity_check_ips ()
209 {
210     local ips="$1" # list of "ip node" lines
211
212     echo "Sanity checking IPs..."
213
214     local x ipp prev
215     prev=""
216     while read x ipp ; do
217         [ "$ipp" = "-1" ] && break
218         if [ -n "$prev" -a "$ipp" != "$prev" ] ; then
219             echo "OK"
220             return 0
221         fi
222         prev="$ipp"
223     done <<<"$ips"
224
225     echo "BAD: a node was -1 or IPs are only assigned to one node"
226     echo "Are you running an old version of CTDB?"
227     return 1
228 }
229
230 # This returns a list of "ip node" lines in $out
231 all_ips_on_node()
232 {
233     local node=$@
234     try_command_on_node $node "$CTDB ip -Y -n all | cut -d ':' -f1-3 | sed -e '1d' -e 's@^:@@' -e 's@:@ @g'"
235 }
236
237 select_test_node_and_ips ()
238 {
239     all_ips_on_node 0
240
241     # When selecting test_node we just want a node that has public
242     # IPs.  This will work and is economically semi-random.  :-)
243     local x
244     read x test_node <<<"$out"
245
246     test_node_ips=""
247     local ip pnn
248     while read ip pnn ; do
249         if [ "$pnn" = "$test_node" ] ; then
250             test_node_ips="${test_node_ips}${test_node_ips:+ }${ip}"
251         fi
252     done <<<"$out" # bashism to avoid problem setting variable in pipeline.
253
254     echo "Selected node ${test_node} with IPs: ${test_node_ips}."
255     test_ip="${test_node_ips%% *}"
256 }
257
258 #######################################
259
260 # Wait until either timeout expires or command succeeds.  The command
261 # will be tried once per second.
262 wait_until ()
263 {
264     local timeout="$1" ; shift # "$@" is the command...
265
266     local negate=false
267     if [ "$1" = "!" ] ; then
268         negate=true
269         shift
270     fi
271
272     echo -n "<${timeout}|"
273     local t=$timeout
274     while [ $t -gt 0 ] ; do
275         local rc=0
276         "$@" || rc=$?
277         if { ! $negate && [ $rc -eq 0 ] ; } || \
278             { $negate && [ $rc -ne 0 ] ; } ; then
279             echo "|$(($timeout - $t))|"
280             echo "OK"
281             return 0
282         fi
283         echo -n .
284         t=$(($t - 1))
285         sleep 1
286     done
287
288     echo "*TIMEOUT*"
289
290     return 1
291 }
292
293 sleep_for ()
294 {
295     echo -n "=${1}|"
296     for i in $(seq 1 $1) ; do
297         echo -n '.'
298         sleep 1
299     done
300     echo '|'
301 }
302
303 _cluster_is_healthy ()
304 {
305     local out x count line
306
307     out=$($CTDB -Y status 2>/dev/null) || return 1
308
309     {
310         read x
311         count=0
312         while read line ; do
313             # We need to see valid lines if we're going to be healthy.
314             [ "${line#:[0-9]}" != "$line" ] && count=$(($count + 1))
315             # A line indicating a node is unhealthy causes failure.
316             [ "${line##:*:*:*1:}" != "$line" ] && return 1
317         done
318         [ $count -gt 0 ] && return $?
319     } <<<"$out" # Yay bash!
320 }
321
322 cluster_is_healthy ()
323 {
324     if onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
325         echo "Cluster is HEALTHY"
326         return 0
327     else
328         echo "Cluster is UNHEALTHY"
329         if ! ${ctdb_test_restart_scheduled:-false} ; then
330             echo "DEBUG AT $(date '+%F %T'):"
331             local i
332             for i in "onnode -q 0 $CTDB status" "onnode -q 0 onnode all $CTDB scriptstatus" ; do
333                 echo "$i"
334                 $i || true
335             done
336         fi
337         return 1
338     fi
339 }
340
341 wait_until_healthy ()
342 {
343     local timeout="${1:-120}"
344
345     echo "Waiting for cluster to become healthy..."
346
347     wait_until 120 _cluster_is_healthy
348 }
349
350 # This function is becoming nicely overloaded.  Soon it will collapse!  :-)
351 node_has_status ()
352 {
353     local pnn="$1"
354     local status="$2"
355
356     local bits fpat mpat
357     case "$status" in
358         (unhealthy)    bits="?:?:?:1:*" ;;
359         (healthy)      bits="?:?:?:0:*" ;;
360         (disconnected) bits="1:*" ;;
361         (connected)    bits="0:*" ;;
362         (banned)       bits="?:1:*" ;;
363         (unbanned)     bits="?:0:*" ;;
364         (disabled)     bits="?:?:1:*" ;;
365         (enabled)      bits="?:?:0:*" ;;
366         (stopped)      bits="?:?:?:?:1:*" ;;
367         (notstopped)   bits="?:?:?:?:0:*" ;;
368         (frozen)       fpat='^[[:space:]]+frozen[[:space:]]+1$' ;;
369         (unfrozen)     fpat='^[[:space:]]+frozen[[:space:]]+0$' ;;
370         (monon)        mpat='^Monitoring mode:ACTIVE \(0\)$' ;;
371         (monoff)       mpat='^Monitoring mode:DISABLED \(1\)$' ;;
372         *)
373             echo "node_has_status: unknown status \"$status\""
374             return 1
375     esac
376
377     if [ -n "$bits" ] ; then
378         local out x line
379
380         out=$($CTDB -Y status 2>&1) || return 1
381
382         {
383             read x
384             while read line ; do
385                 # This needs to be done in 2 steps to avoid false matches.
386                 local line_bits="${line#:${pnn}:*:}"
387                 [ "$line_bits" = "$line" ] && continue
388                 [ "${line_bits#${bits}}" != "$line_bits" ] && return 0
389             done
390             return 1
391         } <<<"$out" # Yay bash!
392     elif [ -n "$fpat" ] ; then
393         $CTDB statistics -n "$pnn" | egrep -q "$fpat"
394     elif [ -n "$mpat" ] ; then
395         $CTDB getmonmode -n "$pnn" | egrep -q "$mpat"
396     else
397         echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
398         return 1
399     fi
400 }
401
402 wait_until_node_has_status ()
403 {
404     local pnn="$1"
405     local status="$2"
406     local timeout="${3:-30}"
407     local proxy_pnn="${4:-any}"
408
409     echo "Waiting until node $pnn has status \"$status\"..."
410
411     if ! wait_until $timeout onnode $proxy_pnn $CTDB_TEST_WRAPPER node_has_status "$pnn" "$status" ; then
412         for i in "onnode -q any $CTDB status" "onnode -q any onnode all $CTDB scriptstatus" ; do
413             echo "$i"
414             $i || true
415         done
416
417         return 1
418     fi
419
420 }
421
422 # Useful for superficially testing IP failover.
423 # IPs must be on nodes matching nodeglob.
424 ips_are_on_nodeglob ()
425 {
426     local nodeglob="$1" ; shift
427     local ips="$*"
428
429     local out
430
431     all_ips_on_node 1
432
433     while read ip pnn ; do
434         for check in $ips ; do
435             if [ "$check" = "$ip" ] ; then
436                 case "$pnn" in
437                     ($nodeglob) : ;;
438                     (*) return 1  ;;
439                 esac
440                 ips="${ips/${ip}}" # Remove from list
441             fi
442         done
443     done <<<"$out" # bashism to avoid problem setting variable in pipeline.
444
445     ips="${ips// }" # Remove any spaces.
446     [ -z "$ips" ]
447 }
448
449 wait_until_ips_are_on_nodeglob ()
450 {
451     echo "Waiting for IPs to fail over..."
452
453     wait_until 60 ips_are_on_nodeglob "$@"
454 }
455
456 node_has_some_ips ()
457 {
458     local node="$1"
459
460     local out
461
462     all_ips_on_node 1
463
464     while read ip pnn ; do
465         if [ "$node" = "$pnn" ] ; then
466             return 0
467         fi
468     done <<<"$out" # bashism to avoid problem setting variable in pipeline.
469
470     return 1
471 }
472
473 wait_until_node_has_some_ips ()
474 {
475     echo "Waiting for node to have some IPs..."
476
477     wait_until 60 node_has_some_ips "$@"
478 }
479
480 #######################################
481
482 daemons_stop ()
483 {
484     echo "Attempting to politely shutdown daemons..."
485     onnode 1 $CTDB shutdown -n all || true
486
487     echo "Sleeping for a while..."
488     sleep_for 1
489
490     local pat="ctdbd --socket=.* --nlist .* --nopublicipcheck"
491     if pgrep -f "$pat" >/dev/null ; then
492         echo "Killing remaining daemons..."
493         pkill -f "$pat"
494
495         if pgrep -f "$pat" >/dev/null ; then
496             echo "Once more with feeling.."
497             pkill -9 -f "$pat"
498         fi
499     fi
500
501     rm -rf "${TEST_VAR_DIR}/test.db"
502 }
503
504 daemons_setup ()
505 {
506     mkdir -p "${TEST_VAR_DIR}/test.db/persistent"
507
508     local public_addresses_all="${TEST_VAR_DIR}/public_addresses_all"
509     local no_public_addresses="${TEST_VAR_DIR}/no_public_addresses.txt"
510     rm -f $CTDB_NODES $public_addresses_all $no_public_addresses
511
512     # If there are (strictly) greater than 2 nodes then we'll randomly
513     # choose a node to have no public addresses.
514     local no_public_ips=-1
515     [ $TEST_LOCAL_DAEMONS -gt 2 ] && no_public_ips=$(($RANDOM % $TEST_LOCAL_DAEMONS))
516     echo "$no_public_ips" >$no_public_addresses
517
518     # When running certain tests we add and remove eventscripts, so we
519     # need to be able to modify the events.d/ directory.  Therefore,
520     # we use a temporary events.d/ directory under $TEST_VAR_DIR.  We
521     # copy the actual test eventscript(s) in there from the original
522     # events.d/ directory that sits alongside $TEST_SCRIPT_DIR.
523     local top=$(dirname "$TEST_SCRIPTS_DIR")
524     local events_d="${top}/events.d"
525     mkdir -p "${TEST_VAR_DIR}/events.d"
526     cp -p "${events_d}/"* "${TEST_VAR_DIR}/events.d/"
527
528     local i
529     for i in $(seq 1 $TEST_LOCAL_DAEMONS) ; do
530         if [ "${CTDB_USE_IPV6}x" != "x" ]; then
531             echo ::$i >>"$CTDB_NODES"
532             ip addr add ::$i/128 dev lo
533         else
534             echo 127.0.0.$i >>"$CTDB_NODES"
535             # 2 public addresses on most nodes, just to make things interesting.
536             if [ $(($i - 1)) -ne $no_public_ips ] ; then
537                 echo "192.0.2.$i/24 lo" >>"$public_addresses_all"
538                 echo "192.0.2.$(($i + $TEST_LOCAL_DAEMONS))/24 lo" >>"$public_addresses_all"
539             fi
540         fi
541     done
542 }
543
544 daemons_start_1 ()
545 {
546     local pnn="$1"
547     shift # "$@" gets passed to ctdbd
548
549     local public_addresses_all="${TEST_VAR_DIR}/public_addresses_all"
550     local public_addresses_mine="${TEST_VAR_DIR}/public_addresses.${pnn}"
551     local no_public_addresses="${TEST_VAR_DIR}/no_public_addresses.txt"
552
553     local no_public_ips=-1
554     [ -r $no_public_addresses ] && read no_public_ips <$no_public_addresses
555
556     if  [ "$no_public_ips" = $pnn ] ; then
557         echo "Node $no_public_ips will have no public IPs."
558     fi
559
560     local node_ip=$(sed -n -e "$(($pnn + 1))p" "$CTDB_NODES")
561     local ctdb_options="--reclock=${TEST_VAR_DIR}/rec.lock --nlist $CTDB_NODES --nopublicipcheck --node-ip=${node_ip} --event-script-dir=${TEST_VAR_DIR}/events.d --logfile=${TEST_VAR_DIR}/daemons.log -d 3 --dbdir=${TEST_VAR_DIR}/test.db --dbdir-persistent=${TEST_VAR_DIR}/test.db/persistent --dbdir-state=${TEST_VAR_DIR}/test.db/state"
562
563     if [ -z "$CTDB_TEST_REAL_CLUSTER" ]; then
564         ctdb_options="$ctdb_options --public-interface=lo"
565     fi
566
567     if [ $pnn -eq $no_public_ips ] ; then
568         ctdb_options="$ctdb_options --public-addresses=/dev/null"
569     else
570         cp "$public_addresses_all" "$public_addresses_mine"
571         ctdb_options="$ctdb_options --public-addresses=$public_addresses_mine"
572     fi
573
574     # We'll use "pkill -f" to kill the daemons with
575     # "--socket=.* --nlist .* --nopublicipcheck" as context.
576     $VALGRIND ctdbd --socket="${TEST_VAR_DIR}/sock.$pnn" $ctdb_options "$@" ||return 1
577 }
578
579 daemons_start ()
580 {
581     # "$@" gets passed to ctdbd
582
583     echo "Starting $TEST_LOCAL_DAEMONS ctdb daemons..."
584
585     for i in $(seq 0 $(($TEST_LOCAL_DAEMONS - 1))) ; do
586         daemons_start_1 $i "$@"
587     done
588
589     if [ -L /tmp/ctdb.socket -o ! -S /tmp/ctdb.socket ] ; then 
590         ln -sf "${TEST_VAR_DIR}/sock.0" /tmp/ctdb.socket || return 1
591     fi
592 }
593
594 #######################################
595
596 _ctdb_hack_options ()
597 {
598     local ctdb_options="$*"
599
600     # We really just want to pass CTDB_OPTIONS but on RH
601     # /etc/sysconfig/ctdb can, and frequently does, set that variable.
602     # So instead, we hack badly.  We'll add these as we use them.
603     # Note that these may still be overridden by the above file... but
604     # we tend to use the exotic options here... so that is unlikely.
605
606     case "$ctdb_options" in
607         *--start-as-stopped*)
608             export CTDB_START_AS_STOPPED="yes"
609     esac
610 }
611
612 _restart_ctdb ()
613 {
614     _ctdb_hack_options "$@"
615
616     if [ -e /etc/redhat-release ] ; then
617         service ctdb restart
618     else
619         /etc/init.d/ctdb restart
620     fi
621 }
622
623 _ctdb_start ()
624 {
625     _ctdb_hack_options "$@"
626
627     /etc/init.d/ctdb start
628 }
629
630 setup_ctdb ()
631 {
632     if [ -n "$CTDB_NODES_SOCKETS" ] ; then
633         daemons_setup
634     fi
635 }
636
637 # Common things to do after starting one or more nodes.
638 _ctdb_start_post ()
639 {
640     onnode -q 1  $CTDB_TEST_WRAPPER wait_until_healthy || return 1
641
642     echo "Setting RerecoveryTimeout to 1"
643     onnode -pq all "$CTDB setvar RerecoveryTimeout 1"
644
645     # In recent versions of CTDB, forcing a recovery like this blocks
646     # until the recovery is complete.  Hopefully this will help the
647     # cluster to stabilise before a subsequent test.
648     echo "Forcing a recovery..."
649     onnode -q 0 $CTDB recover
650     sleep_for 1
651     echo "Forcing a recovery..."
652     onnode -q 0 $CTDB recover
653
654     echo "ctdb is ready"
655 }
656
657 # This assumes that ctdbd is not running on the given node.
658 ctdb_start_1 ()
659 {
660     local pnn="$1"
661     shift # "$@" is passed to ctdbd start.
662
663     echo -n "Starting CTDB on node ${pnn}..."
664
665     if [ -n "$CTDB_NODES_SOCKETS" ] ; then
666         daemons_start_1 $pnn "$@"
667     else
668         onnode $pnn $CTDB_TEST_WRAPPER _ctdb_start "$@"
669     fi
670
671     # If we're starting only 1 node then we're doing something weird.
672     ctdb_restart_when_done
673 }
674
675 restart_ctdb ()
676 {
677     # "$@" is passed to ctdbd start.
678
679     echo -n "Restarting CTDB"
680     if $ctdb_test_restart_scheduled ; then
681         echo -n " (scheduled)"
682     fi
683     echo "..."
684
685     local i
686     for i in $(seq 1 5) ; do
687         if [ -n "$CTDB_NODES_SOCKETS" ] ; then
688             daemons_stop
689             daemons_start "$@"
690         else
691             onnode -p all $CTDB_TEST_WRAPPER _restart_ctdb "$@"
692         fi || {
693             echo "Restart failed.  Trying again in a few seconds..."
694             sleep_for 5
695             continue
696         }
697
698         onnode -q 1  $CTDB_TEST_WRAPPER wait_until_healthy || {
699             echo "Cluster didn't become healthy.  Restarting..."
700             continue
701         }
702
703         local debug_out=$(onnode -p all ctdb status -Y 2>&1; onnode -p all ctdb scriptstatus 2>&1)
704
705         echo "Setting RerecoveryTimeout to 1"
706         onnode -pq all "$CTDB setvar RerecoveryTimeout 1"
707
708         # In recent versions of CTDB, forcing a recovery like this
709         # blocks until the recovery is complete.  Hopefully this will
710         # help the cluster to stabilise before a subsequent test.
711         echo "Forcing a recovery..."
712         onnode -q 0 $CTDB recover
713         sleep_for 1
714         echo "Forcing a recovery..."
715         onnode -q 0 $CTDB recover
716
717         # Cluster is still healthy.  Good, we're done!
718         if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
719             echo "Cluster become UNHEALTHY again.  Restarting..."
720             continue
721         fi
722
723         echo "Doing a sync..."
724         onnode -q 0 $CTDB sync
725
726         echo "ctdb is ready"
727         return 0
728     done
729
730     echo "Cluster UNHEALTHY...  too many attempts..."
731     echo "$debug_out"
732     # Try to make the calling test fail
733     status=1
734     return 1
735 }
736
737 ctdb_restart_when_done ()
738 {
739     ctdb_test_restart_scheduled=true
740 }
741
742 get_ctdbd_command_line_option ()
743 {
744     local pnn="$1"
745     local option="$2"
746
747     try_command_on_node "$pnn" "$CTDB getpid" || \
748         die "Unable to get PID of ctdbd on node $pnn"
749
750     local pid="${out#*:}"
751     try_command_on_node "$pnn" "ps -p $pid -o args hww" || \
752         die "Unable to get command-line of PID $pid"
753
754     # Strip everything up to and including --option
755     local t="${out#*--${option}}"
756     # Strip leading '=' or space if present
757     t="${t#=}"
758     t="${t# }"
759     # Strip any following options and print
760     echo "${t%% -*}"
761 }
762
763 #######################################
764
765 install_eventscript ()
766 {
767     local script_name="$1"
768     local script_contents="$2"
769
770     if [ -z "$TEST_LOCAL_DAEMONS" ] ; then
771         # The quoting here is *very* fragile.  However, we do
772         # experience the joy of installing a short script using
773         # onnode, and without needing to know the IP addresses of the
774         # nodes.
775         onnode all "f=\"\${CTDB_BASE:-/etc/ctdb}/events.d/${script_name}\" ; echo \"Installing \$f\" ; echo '${script_contents}' > \"\$f\" ; chmod 755 \"\$f\""
776     else
777         f="${TEST_VAR_DIR}/events.d/${script_name}"
778         echo "$script_contents" >"$f"
779         chmod 755 "$f"
780     fi
781 }
782
783 uninstall_eventscript ()
784 {
785     local script_name="$1"
786
787     if [ -z "$TEST_LOCAL_DAEMONS" ] ; then
788         onnode all "rm -vf \"\${CTDB_BASE:-/etc/ctdb}/events.d/${script_name}\""
789     else
790         rm -vf "${TEST_VAR_DIR}/events.d/${script_name}"
791     fi
792 }
793
794 #######################################
795
796 # This section deals with the 99.ctdb_test eventscript.
797
798 # Metafunctions: Handle a ctdb-test file on a node.
799 # given event.
800 ctdb_test_eventscript_file_create ()
801 {
802     local pnn="$1"
803     local type="$2"
804
805     try_command_on_node $pnn touch "/tmp/ctdb-test-${type}.${pnn}"
806 }
807
808 ctdb_test_eventscript_file_remove ()
809 {
810     local pnn="$1"
811     local type="$2"
812
813     try_command_on_node $pnn rm -f "/tmp/ctdb-test-${type}.${pnn}"
814 }
815
816 ctdb_test_eventscript_file_exists ()
817 {
818     local pnn="$1"
819     local type="$2"
820
821     try_command_on_node $pnn test -f "/tmp/ctdb-test-${type}.${pnn}" >/dev/null 2>&1
822 }
823
824
825 # Handle a flag file on a node that is removed by 99.ctdb_test on the
826 # given event.
827 ctdb_test_eventscript_flag ()
828 {
829     local cmd="$1"
830     local pnn="$2"
831     local event="$3"
832
833     ctdb_test_eventscript_file_${cmd} "$pnn" "flag-${event}"
834 }
835
836
837 # Handle a trigger that causes 99.ctdb_test to fail it's monitor
838 # event.
839 ctdb_test_eventscript_unhealthy_trigger ()
840 {
841     local cmd="$1"
842     local pnn="$2"
843
844     ctdb_test_eventscript_file_${cmd} "$pnn" "unhealthy-trigger"
845 }
846
847 # Handle the file that 99.ctdb_test created to show that it has marked
848 # a node unhealthy because it detected the above trigger.
849 ctdb_test_eventscript_unhealthy_detected ()
850 {
851     local cmd="$1"
852     local pnn="$2"
853
854     ctdb_test_eventscript_file_${cmd} "$pnn" "unhealthy-detected"
855 }
856
857 # Handle a trigger that causes 99.ctdb_test to timeout it's monitor
858 # event.  This should cause the node to be banned.
859 ctdb_test_eventscript_timeout_trigger ()
860 {
861     local cmd="$1"
862     local pnn="$2"
863     local event="$3"
864
865     ctdb_test_eventscript_file_${cmd} "$pnn" "${event}-timeout"
866 }
867
868 # Note that the eventscript can't use the above functions!
869 ctdb_test_eventscript_install ()
870 {
871
872     local script='#!/bin/sh
873 out=$(ctdb pnn)
874 pnn="${out#PNN:}"
875
876 rm -vf "/tmp/ctdb-test-flag-${1}.${pnn}"
877
878 trigger="/tmp/ctdb-test-unhealthy-trigger.${pnn}"
879 detected="/tmp/ctdb-test-unhealthy-detected.${pnn}"
880 timeout_trigger="/tmp/ctdb-test-${1}-timeout.${pnn}"
881 case "$1" in
882     monitor)
883         if [ -e "$trigger" ] ; then
884             echo "${0}: Unhealthy because \"$trigger\" detected"
885             touch "$detected"
886             exit 1
887         elif [ -e "$detected" -a ! -e "$trigger" ] ; then
888             echo "${0}: Healthy again, \"$trigger\" no longer detected"
889             rm "$detected"
890         fi
891
892         ;;
893     *)
894         if [ -e "$timeout_trigger" ] ; then
895             echo "${0}: Sleeping for a long time because \"$timeout_trigger\" detected"
896             sleep 9999
897         fi
898         ;;
899         *)
900
901 esac
902
903 exit 0
904 '
905     install_eventscript "99.ctdb_test" "$script"
906 }
907
908 ctdb_test_eventscript_uninstall ()
909 {
910     uninstall_eventscript "99.ctdb_test"
911 }
912
913 # Note that this only works if you know all other monitor events will
914 # succeed.  You also need to install the eventscript before using it.
915 wait_for_monitor_event ()
916 {
917     local pnn="$1"
918
919     echo "Waiting for a monitor event on node ${pnn}..."
920     ctdb_test_eventscript_flag create $pnn "monitor"
921
922     wait_until 120 ! ctdb_test_eventscript_flag exists $pnn "monitor"
923
924 }
925
926 # Make sure that $CTDB is set.
927 : ${CTDB:=ctdb}
928
929 local="${TEST_SUBDIR}/scripts/local.bash"
930 if [ -r "$local" ] ; then
931     . "$local"
932 fi