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