ctdb/tests/simple: Move the local daemons code to its own file
[samba.git] / ctdb / tests / scripts / integration.bash
1 # Hey Emacs, this is a -*- shell-script -*- !!!  :-)
2
3 . "${TEST_SCRIPTS_DIR}/common.sh"
4
5 ######################################################################
6
7 export CTDB_TIMEOUT=60
8
9 if [ -n "$CTDB_TEST_REMOTE_DIR" ] ; then
10     CTDB_TEST_WRAPPER="${CTDB_TEST_REMOTE_DIR}/test_wrap"
11 else
12     _d=$(cd ${TEST_SCRIPTS_DIR}; echo $PWD)
13     CTDB_TEST_WRAPPER="$_d/test_wrap"
14 fi
15 export CTDB_TEST_WRAPPER
16
17 # If $VALGRIND is set then use it whenever ctdb is called, but only if
18 # $CTDB is not already set.
19 [ -n "$CTDB" ] || export CTDB="${VALGRIND}${VALGRIND:+ }ctdb"
20
21 # why???
22 PATH="${TEST_SCRIPTS_DIR}:${PATH}"
23
24 ######################################################################
25
26 ctdb_check_time_logs ()
27 {
28     local threshold=20
29
30     local jump=false
31     local prev=""
32     local ds_prev=""
33     local node=""
34
35     out=$(onnode all tail -n 20 "${TEST_VAR_DIR}/ctdb.test.time.log" 2>&1)
36
37     if [ $? -eq 0 ] ; then
38         local line
39         while read line ; do
40             case "$line" in
41                 \>\>\ NODE:\ *\ \<\<)
42                     node="${line#>> NODE: }"
43                     node=${node% <<*}
44                     ds_prev=""
45                     ;;
46                 *\ *)
47                     set -- $line
48                     ds_curr="$1${2:0:1}"
49                     if [ -n "$ds_prev" ] && \
50                         [ $(($ds_curr - $ds_prev)) -ge $threshold ] ; then
51                         echo "Node $node had time jump of $(($ds_curr - $ds_prev))ds between $(date +'%T' -d @${ds_prev%?}) and $(date +'%T' -d @${ds_curr%?})"
52                         jump=true
53                     fi
54                     prev="$line"
55                     ds_prev="$ds_curr"
56                     ;;
57             esac
58         done <<<"$out"
59     else
60         echo Error getting time logs
61     fi
62     if $jump ; then
63         echo "Check time sync (test client first):"
64         date
65         onnode -p all date
66         echo "Information from test client:"
67         hostname
68         top -b -n 1
69         echo "Information from cluster nodes:"
70         onnode all "top -b -n 1 ; echo '/proc/slabinfo' ; cat /proc/slabinfo"
71     fi
72 }
73
74 ctdb_test_exit ()
75 {
76     local status=$?
77
78     trap - 0
79
80     [ $(($testfailures+0)) -eq 0 -a $status -ne 0 ] && testfailures=$status
81     status=$(($testfailures+0))
82
83     # Avoid making a test fail from this point onwards.  The test is
84     # now complete.
85     set +e
86
87     echo "*** TEST COMPLETED (RC=$status) AT $(date '+%F %T'), CLEANING UP..."
88
89     if [ -z "$TEST_LOCAL_DAEMONS" -a -n "$CTDB_TEST_TIME_LOGGING" -a \
90         $status -ne 0 ] ; then
91         ctdb_check_time_logs
92     fi
93
94     eval "$ctdb_test_exit_hook" || true
95     unset ctdb_test_exit_hook
96
97     if $ctdb_test_restart_scheduled || ! cluster_is_healthy ; then
98
99         restart_ctdb
100     else
101         # This could be made unconditional but then we might get
102         # duplication from the recovery in restart_ctdb.  We want to
103         # leave the recovery in restart_ctdb so that future tests that
104         # might do a manual restart mid-test will benefit.
105         echo "Forcing a recovery..."
106         onnode 0 $CTDB recover
107     fi
108
109     exit $status
110 }
111
112 ctdb_test_exit_hook_add ()
113 {
114     ctdb_test_exit_hook="${ctdb_test_exit_hook}${ctdb_test_exit_hook:+ ; }$*"
115 }
116
117 ctdb_test_init ()
118 {
119     scriptname=$(basename "$0")
120     testfailures=0
121     ctdb_test_restart_scheduled=false
122
123     trap "ctdb_test_exit" 0
124 }
125
126 ########################################
127
128 # Sets: $out
129 try_command_on_node ()
130 {
131     local nodespec="$1" ; shift
132
133     local verbose=false
134     local onnode_opts=""
135
136     while [ "${nodespec#-}" != "$nodespec" ] ; do
137         if [ "$nodespec" = "-v" ] ; then
138             verbose=true
139         else
140             onnode_opts="${onnode_opts}${onnode_opts:+ }${nodespec}"
141         fi
142         nodespec="$1" ; shift
143     done
144
145     local cmd="$*"
146
147     out=$(onnode -q $onnode_opts "$nodespec" "$cmd" 2>&1) || {
148
149         echo "Failed to execute \"$cmd\" on node(s) \"$nodespec\""
150         echo "$out"
151         return 1
152     }
153
154     if $verbose ; then
155         echo "Output of \"$cmd\":"
156         echo "$out"
157     fi
158 }
159
160 sanity_check_output ()
161 {
162     local min_lines="$1"
163     local regexp="$2" # Should be anchored as necessary.
164     local output="$3"
165
166     local ret=0
167
168     local num_lines=$(echo "$output" | wc -l)
169     echo "There are $num_lines lines of output"
170     if [ $num_lines -lt $min_lines ] ; then
171         echo "BAD: that's less than the required number (${min_lines})"
172         ret=1
173     fi
174
175     local status=0
176     local unexpected # local doesn't pass through status of command on RHS.
177     unexpected=$(echo "$output" | egrep -v "$regexp") || status=$?
178
179     # Note that this is reversed.
180     if [ $status -eq 0 ] ; then
181         echo "BAD: unexpected lines in output:"
182         echo "$unexpected" | cat -A
183         ret=1
184     else
185         echo "Output lines look OK"
186     fi
187
188     return $ret
189 }
190
191 sanity_check_ips ()
192 {
193     local ips="$1" # list of "ip node" lines
194
195     echo "Sanity checking IPs..."
196
197     local x ipp prev
198     prev=""
199     while read x ipp ; do
200         [ "$ipp" = "-1" ] && break
201         if [ -n "$prev" -a "$ipp" != "$prev" ] ; then
202             echo "OK"
203             return 0
204         fi
205         prev="$ipp"
206     done <<<"$ips"
207
208     echo "BAD: a node was -1 or IPs are only assigned to one node"
209     echo "Are you running an old version of CTDB?"
210     return 1
211 }
212
213 # This returns a list of "ip node" lines in $out
214 all_ips_on_node()
215 {
216     local node=$@
217     try_command_on_node $node "$CTDB ip -Y -n all | cut -d ':' -f1-3 | sed -e '1d' -e 's@^:@@' -e 's@:@ @g'"
218 }
219
220 _select_test_node_and_ips ()
221 {
222     all_ips_on_node 0
223
224     test_node=""  # this matches no PNN
225     test_node_ips=""
226     local ip pnn
227     while read ip pnn ; do
228         if [ -z "$test_node" -a "$pnn" != "-1" ] ; then
229             test_node="$pnn"
230         fi
231         if [ "$pnn" = "$test_node" ] ; then
232             test_node_ips="${test_node_ips}${test_node_ips:+ }${ip}"
233         fi
234     done <<<"$out" # bashism to avoid problem setting variable in pipeline.
235
236     echo "Selected node ${test_node} with IPs: ${test_node_ips}."
237     test_ip="${test_node_ips%% *}"
238
239     [ -n "$test_node" ] || return 1
240 }
241
242 select_test_node_and_ips ()
243 {
244     local timeout=10
245     while ! _select_test_node_and_ips ; do
246         echo "Unable to find a test node with IPs assigned"
247         if [ $timeout -le 0 ] ; then
248             echo "BAD: Too many attempts"
249             return 1
250         fi
251         sleep_for 1
252         timeout=$(($timeout - 1))
253     done
254
255     return 0
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     $CTDB nodestatus all >/dev/null && \
306         node_has_status 0 recovered
307 }
308
309 cluster_is_healthy ()
310 {
311     if onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
312         echo "Cluster is HEALTHY"
313         return 0
314     else
315         echo "Cluster is UNHEALTHY"
316         if ! ${ctdb_test_restart_scheduled:-false} ; then
317             echo "DEBUG AT $(date '+%F %T'):"
318             local i
319             for i in "onnode -q 0 $CTDB status" "onnode -q 0 onnode all $CTDB scriptstatus" ; do
320                 echo "$i"
321                 $i || true
322             done
323         fi
324         return 1
325     fi
326 }
327
328 wait_until_healthy ()
329 {
330     local timeout="${1:-120}"
331
332     echo "Waiting for cluster to become healthy..."
333
334     wait_until 120 _cluster_is_healthy
335 }
336
337 # This function is becoming nicely overloaded.  Soon it will collapse!  :-)
338 node_has_status ()
339 {
340     local pnn="$1"
341     local status="$2"
342
343     local bits fpat mpat rpat
344     case "$status" in
345         (unhealthy)    bits="?:?:?:1:*" ;;
346         (healthy)      bits="?:?:?:0:*" ;;
347         (disconnected) bits="1:*" ;;
348         (connected)    bits="0:*" ;;
349         (banned)       bits="?:1:*" ;;
350         (unbanned)     bits="?:0:*" ;;
351         (disabled)     bits="?:?:1:*" ;;
352         (enabled)      bits="?:?:0:*" ;;
353         (stopped)      bits="?:?:?:?:1:*" ;;
354         (notstopped)   bits="?:?:?:?:0:*" ;;
355         (frozen)       fpat='^[[:space:]]+frozen[[:space:]]+1$' ;;
356         (unfrozen)     fpat='^[[:space:]]+frozen[[:space:]]+0$' ;;
357         (monon)        mpat='^Monitoring mode:ACTIVE \(0\)$' ;;
358         (monoff)       mpat='^Monitoring mode:DISABLED \(1\)$' ;;
359         (recovered)    rpat='^Recovery mode:NORMAL \(0\)$' ;;
360         *)
361             echo "node_has_status: unknown status \"$status\""
362             return 1
363     esac
364
365     if [ -n "$bits" ] ; then
366         local out x line
367
368         out=$($CTDB -Y status 2>&1) || return 1
369
370         {
371             read x
372             while read line ; do
373                 # This needs to be done in 2 steps to avoid false matches.
374                 local line_bits="${line#:${pnn}:*:}"
375                 [ "$line_bits" = "$line" ] && continue
376                 [ "${line_bits#${bits}}" != "$line_bits" ] && return 0
377             done
378             return 1
379         } <<<"$out" # Yay bash!
380     elif [ -n "$fpat" ] ; then
381         $CTDB statistics -n "$pnn" | egrep -q "$fpat"
382     elif [ -n "$mpat" ] ; then
383         $CTDB getmonmode -n "$pnn" | egrep -q "$mpat"
384     elif [ -n "$rpat" ] ; then
385         $CTDB status -n "$pnn" | egrep -q "$rpat"
386     else
387         echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
388         return 1
389     fi
390 }
391
392 wait_until_node_has_status ()
393 {
394     local pnn="$1"
395     local status="$2"
396     local timeout="${3:-30}"
397     local proxy_pnn="${4:-any}"
398
399     echo "Waiting until node $pnn has status \"$status\"..."
400
401     if ! wait_until $timeout onnode $proxy_pnn $CTDB_TEST_WRAPPER node_has_status "$pnn" "$status" ; then
402         for i in "onnode -q any $CTDB status" "onnode -q any onnode all $CTDB scriptstatus" ; do
403             echo "$i"
404             $i || true
405         done
406
407         return 1
408     fi
409
410 }
411
412 # Useful for superficially testing IP failover.
413 # IPs must be on nodes matching nodeglob.
414 # If the first argument is '!' then the IPs must not be on nodes
415 # matching nodeglob.
416 ips_are_on_nodeglob ()
417 {
418     local negating=false
419     if [ "$1" = "!" ] ; then
420         negating=true ; shift
421     fi
422     local nodeglob="$1" ; shift
423     local ips="$*"
424
425     local out
426
427     all_ips_on_node 1
428
429     for check in $ips ; do
430         while read ip pnn ; do
431             if [ "$check" = "$ip" ] ; then
432                 case "$pnn" in
433                     ($nodeglob) if $negating ; then return 1 ; fi ;;
434                     (*) if ! $negating ; then return 1 ; fi  ;;
435                 esac
436                 ips="${ips/${ip}}" # Remove from list
437                 break
438             fi
439             # If we're negating and we didn't see the address then it
440             # isn't hosted by anyone!
441             if $negating ; then
442                 ips="${ips/${check}}"
443             fi
444         done <<<"$out" # bashism to avoid problem setting variable in pipeline.
445     done
446
447     ips="${ips// }" # Remove any spaces.
448     [ -z "$ips" ]
449 }
450
451 wait_until_ips_are_on_nodeglob ()
452 {
453     echo "Waiting for IPs to fail over..."
454
455     wait_until 60 ips_are_on_nodeglob "$@"
456 }
457
458 node_has_some_ips ()
459 {
460     local node="$1"
461
462     local out
463
464     all_ips_on_node 1
465
466     while read ip pnn ; do
467         if [ "$node" = "$pnn" ] ; then
468             return 0
469         fi
470     done <<<"$out" # bashism to avoid problem setting variable in pipeline.
471
472     return 1
473 }
474
475 wait_until_node_has_some_ips ()
476 {
477     echo "Waiting for node to have some IPs..."
478
479     wait_until 60 node_has_some_ips "$@"
480 }
481
482 #######################################
483
484 _ctdb_hack_options ()
485 {
486     local ctdb_options="$*"
487
488     case "$ctdb_options" in
489         *--start-as-stopped*)
490             export CTDB_START_AS_STOPPED="yes"
491     esac
492 }
493
494 _restart_ctdb ()
495 {
496     _ctdb_hack_options "$@"
497
498     if [ -e /etc/redhat-release ] ; then
499         service ctdb restart
500     else
501         /etc/init.d/ctdb restart
502     fi
503 }
504
505 _ctdb_start ()
506 {
507     _ctdb_hack_options "$@"
508
509     /etc/init.d/ctdb start
510 }
511
512 setup_ctdb ()
513 {
514     if [ -n "$CTDB_NODES_SOCKETS" ] ; then
515         daemons_setup
516     fi
517 }
518
519 # Common things to do after starting one or more nodes.
520 _ctdb_start_post ()
521 {
522     onnode -q 1  $CTDB_TEST_WRAPPER wait_until_healthy || return 1
523
524     echo "Setting RerecoveryTimeout to 1"
525     onnode -pq all "$CTDB setvar RerecoveryTimeout 1"
526
527     # In recent versions of CTDB, forcing a recovery like this blocks
528     # until the recovery is complete.  Hopefully this will help the
529     # cluster to stabilise before a subsequent test.
530     echo "Forcing a recovery..."
531     onnode -q 0 $CTDB recover
532     sleep_for 1
533
534     echo "ctdb is ready"
535 }
536
537 # This assumes that ctdbd is not running on the given node.
538 ctdb_start_1 ()
539 {
540     local pnn="$1"
541     shift # "$@" is passed to ctdbd start.
542
543     echo -n "Starting CTDB on node ${pnn}..."
544
545     if [ -n "$CTDB_NODES_SOCKETS" ] ; then
546         daemons_start_1 $pnn "$@"
547     else
548         onnode $pnn $CTDB_TEST_WRAPPER _ctdb_start "$@"
549     fi
550
551     # If we're starting only 1 node then we're doing something weird.
552     ctdb_restart_when_done
553 }
554
555 restart_ctdb ()
556 {
557     # "$@" is passed to ctdbd start.
558
559     echo -n "Restarting CTDB"
560     if $ctdb_test_restart_scheduled ; then
561         echo -n " (scheduled)"
562     fi
563     echo "..."
564
565     local i
566     for i in $(seq 1 5) ; do
567         if [ -n "$CTDB_NODES_SOCKETS" ] ; then
568             daemons_stop
569             daemons_start "$@"
570         else
571             onnode -p all $CTDB_TEST_WRAPPER _restart_ctdb "$@"
572         fi || {
573             echo "Restart failed.  Trying again in a few seconds..."
574             sleep_for 5
575             continue
576         }
577
578         onnode -q 1  $CTDB_TEST_WRAPPER wait_until_healthy || {
579             echo "Cluster didn't become healthy.  Restarting..."
580             continue
581         }
582
583         echo "Setting RerecoveryTimeout to 1"
584         onnode -pq all "$CTDB setvar RerecoveryTimeout 1"
585
586         # In recent versions of CTDB, forcing a recovery like this
587         # blocks until the recovery is complete.  Hopefully this will
588         # help the cluster to stabilise before a subsequent test.
589         echo "Forcing a recovery..."
590         onnode -q 0 $CTDB recover
591         sleep_for 1
592
593         # Cluster is still healthy.  Good, we're done!
594         if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
595             echo "Cluster became UNHEALTHY again [$(date)]"
596             onnode -p all ctdb status -Y 2>&1
597             onnode -p all ctdb scriptstatus 2>&1
598             echo "Restarting..."
599             continue
600         fi
601
602         echo "Doing a sync..."
603         onnode -q 0 $CTDB sync
604
605         echo "ctdb is ready"
606         return 0
607     done
608
609     echo "Cluster UNHEALTHY...  too many attempts..."
610     onnode -p all ctdb status -Y 2>&1
611     onnode -p all ctdb scriptstatus 2>&1
612
613     # Try to make the calling test fail
614     status=1
615     return 1
616 }
617
618 ctdb_restart_when_done ()
619 {
620     ctdb_test_restart_scheduled=true
621 }
622
623 get_ctdbd_command_line_option ()
624 {
625     local pnn="$1"
626     local option="$2"
627
628     try_command_on_node "$pnn" "$CTDB getpid" || \
629         die "Unable to get PID of ctdbd on node $pnn"
630
631     local pid="${out#*:}"
632     try_command_on_node "$pnn" "ps -p $pid -o args hww" || \
633         die "Unable to get command-line of PID $pid"
634
635     # Strip everything up to and including --option
636     local t="${out#*--${option}}"
637     # Strip leading '=' or space if present
638     t="${t#=}"
639     t="${t# }"
640     # Strip any following options and print
641     echo "${t%% -*}"
642 }
643
644 #######################################
645
646 wait_for_monitor_event ()
647 {
648     local pnn="$1"
649     local timeout=120
650
651     echo "Waiting for a monitor event on node ${pnn}..."
652
653     try_command_on_node "$pnn" $CTDB scriptstatus || {
654         echo "Unable to get scriptstatus from node $pnn"
655         return 1
656     }
657
658     local ctdb_scriptstatus_original="$out"
659     wait_until 120 _ctdb_scriptstatus_changed
660 }
661
662 _ctdb_scriptstatus_changed ()
663 {
664     try_command_on_node "$pnn" $CTDB scriptstatus || {
665         echo "Unable to get scriptstatus from node $pnn"
666         return 1
667     }
668
669     [ "$out" != "$ctdb_scriptstatus_original" ]
670 }
671
672 #######################################
673
674 nfs_test_setup ()
675 {
676     select_test_node_and_ips
677
678     nfs_first_export=$(showmount -e $test_ip | sed -n -e '2s/ .*//p')
679
680     echo "Creating test subdirectory..."
681     try_command_on_node $test_node "mktemp -d --tmpdir=$nfs_first_export"
682     nfs_test_dir="$out"
683     try_command_on_node $test_node "chmod 777 $nfs_test_dir"
684
685     nfs_mnt_d=$(mktemp -d)
686     nfs_local_file="${nfs_mnt_d}/${nfs_test_dir##*/}/TEST_FILE"
687     nfs_remote_file="${nfs_test_dir}/TEST_FILE"
688
689     ctdb_test_exit_hook_add nfs_test_cleanup
690
691     echo "Mounting ${test_ip}:${nfs_first_export} on ${nfs_mnt_d} ..."
692     mount -o timeo=1,hard,intr,vers=3 \
693         ${test_ip}:${nfs_first_export} ${nfs_mnt_d}
694 }
695
696 nfs_test_cleanup ()
697 {
698     rm -f "$nfs_local_file"
699     umount -f "$nfs_mnt_d"
700     rmdir "$nfs_mnt_d"
701     onnode -q $test_node rmdir "$nfs_test_dir"
702 }
703
704 #######################################
705
706 # $1: pnn, $2: DB name
707 db_get_path ()
708 {
709     try_command_on_node -v $1 $CTDB getdbstatus "$2" |
710     sed -n -e "s@^path: @@p"
711 }
712
713 # $1: pnn, $2: DB name
714 db_ctdb_cattdb_count_records ()
715 {
716     try_command_on_node -v $1 $CTDB cattdb "$2" |
717     grep '^key' | grep -v '__db_sequence_number__' |
718     wc -l
719 }
720
721 # $1: pnn, $2: DB name, $3: key string, $4: value string, $5: RSN (default 7)
722 db_ctdb_tstore ()
723 {
724     _tdb=$(db_get_path $1 "$2")
725     _rsn="${5:-7}"
726     try_command_on_node $1 $CTDB tstore "$_tdb" "$3" "$4" "$_rsn"
727 }
728
729 # $1: pnn, $2: DB name, $3: dbseqnum (must be < 255!!!!!)
730 db_ctdb_tstore_dbseqnum ()
731 {
732     # "__db_sequence_number__" + trailing 0x00
733     _key='0x5f5f64625f73657175656e63655f6e756d6265725f5f00'
734
735     # Construct 8 byte (unit64_t) database sequence number.  This
736     # probably breaks if $3 > 255
737     _value=$(printf "0x%02x%014x" $3 0)
738
739     db_ctdb_tstore $1 "$2" "$_key" "$_value"
740 }
741
742 #######################################
743
744 # Make sure that $CTDB is set.
745 : ${CTDB:=ctdb}
746
747 local="${TEST_SUBDIR}/scripts/local.bash"
748 if [ -r "$local" ] ; then
749     . "$local"
750 fi