ctdb-tests: Start daemons in ctdb_test_init(), stop them in ctdb_test_exit()
[vlendec/samba-autobuild/.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_test_exit ()
27 {
28     local status=$?
29
30     trap - 0
31
32     # run_tests.sh pipes stdout into tee.  If the tee process is
33     # killed then any attempt to write to stdout (e.g. echo) will
34     # result in SIGPIPE, terminating the caller.  Ignore SIGPIPE to
35     # ensure that all clean-up is run.
36     trap '' PIPE
37
38     # Avoid making a test fail from this point onwards.  The test is
39     # now complete.
40     set +e
41
42     echo "*** TEST COMPLETED (RC=$status) AT $(date '+%F %T'), CLEANING UP..."
43
44     eval "$ctdb_test_exit_hook" || true
45     unset ctdb_test_exit_hook
46
47     echo "Stopping cluster..."
48     ctdb_stop_all
49
50     exit $status
51 }
52
53 ctdb_test_exit_hook_add ()
54 {
55     ctdb_test_exit_hook="${ctdb_test_exit_hook}${ctdb_test_exit_hook:+ ; }$*"
56 }
57
58 ctdb_test_init ()
59 {
60         ctdb_test_restart_scheduled=false
61
62         trap "ctdb_test_exit" 0
63
64         ctdb_stop_all >/dev/null 2>&1 || true
65
66         echo "Configuring cluster..."
67         setup_ctdb || exit 1
68
69         echo "Starting cluster..."
70         ctdb_init || exit 1
71
72         echo  "*** SETUP COMPLETE AT $(date '+%F %T'), RUNNING TEST..."
73 }
74
75 ########################################
76
77 # Sets: $out
78 try_command_on_node ()
79 {
80     local nodespec="$1" ; shift
81
82     local verbose=false
83     local onnode_opts=""
84
85     while [ "${nodespec#-}" != "$nodespec" ] ; do
86         if [ "$nodespec" = "-v" ] ; then
87             verbose=true
88         else
89             onnode_opts="${onnode_opts}${onnode_opts:+ }${nodespec}"
90         fi
91         nodespec="$1" ; shift
92     done
93
94     local cmd="$*"
95
96     out=$(onnode -q $onnode_opts "$nodespec" "$cmd" 2>&1) || {
97
98         echo "Failed to execute \"$cmd\" on node(s) \"$nodespec\""
99         echo "$out"
100         return 1
101     }
102
103     if $verbose ; then
104         echo "Output of \"$cmd\":"
105         echo "$out"
106     fi
107 }
108
109 sanity_check_output ()
110 {
111     local min_lines="$1"
112     local regexp="$2" # Should be anchored as necessary.
113     local output="$3"
114
115     local ret=0
116
117     local num_lines=$(echo "$output" | wc -l)
118     echo "There are $num_lines lines of output"
119     if [ $num_lines -lt $min_lines ] ; then
120         echo "BAD: that's less than the required number (${min_lines})"
121         ret=1
122     fi
123
124     local status=0
125     local unexpected # local doesn't pass through status of command on RHS.
126     unexpected=$(echo "$output" | egrep -v "$regexp") || status=$?
127
128     # Note that this is reversed.
129     if [ $status -eq 0 ] ; then
130         echo "BAD: unexpected lines in output:"
131         echo "$unexpected" | cat -A
132         ret=1
133     else
134         echo "Output lines look OK"
135     fi
136
137     return $ret
138 }
139
140 sanity_check_ips ()
141 {
142     local ips="$1" # list of "ip node" lines
143
144     echo "Sanity checking IPs..."
145
146     local x ipp prev
147     prev=""
148     while read x ipp ; do
149         [ "$ipp" = "-1" ] && break
150         if [ -n "$prev" -a "$ipp" != "$prev" ] ; then
151             echo "OK"
152             return 0
153         fi
154         prev="$ipp"
155     done <<<"$ips"
156
157     echo "BAD: a node was -1 or IPs are only assigned to one node:"
158     echo "$ips"
159     echo "Are you running an old version of CTDB?"
160     return 1
161 }
162
163 # This returns a list of "ip node" lines in $out
164 all_ips_on_node()
165 {
166     local node="$1"
167     try_command_on_node $node \
168         "$CTDB ip -X | awk -F'|' 'NR > 1 { print \$2, \$3 }'"
169 }
170
171 _select_test_node_and_ips ()
172 {
173     try_command_on_node any \
174         "$CTDB ip -X all | awk -F'|' 'NR > 1 { print \$2, \$3 }'"
175
176     test_node=""  # this matches no PNN
177     test_node_ips=""
178     local ip pnn
179     while read ip pnn ; do
180         if [ -z "$test_node" -a "$pnn" != "-1" ] ; then
181             test_node="$pnn"
182         fi
183         if [ "$pnn" = "$test_node" ] ; then
184             test_node_ips="${test_node_ips}${test_node_ips:+ }${ip}"
185         fi
186     done <<<"$out" # bashism to avoid problem setting variable in pipeline.
187
188     echo "Selected node ${test_node} with IPs: ${test_node_ips}."
189     test_ip="${test_node_ips%% *}"
190
191     case "$test_ip" in
192         *:*) test_prefix="${test_ip}/128" ;;
193         *)   test_prefix="${test_ip}/32"  ;;
194     esac
195
196     [ -n "$test_node" ] || return 1
197 }
198
199 select_test_node_and_ips ()
200 {
201     local timeout=10
202     while ! _select_test_node_and_ips ; do
203         echo "Unable to find a test node with IPs assigned"
204         if [ $timeout -le 0 ] ; then
205             echo "BAD: Too many attempts"
206             return 1
207         fi
208         sleep_for 1
209         timeout=$(($timeout - 1))
210     done
211
212     return 0
213 }
214
215 # Sets: mask, iface
216 get_test_ip_mask_and_iface ()
217 {
218     # Find the interface
219     try_command_on_node $test_node "$CTDB ip -v -X | awk -F'|' -v ip=$test_ip '\$2 == ip { print \$4 }'"
220     iface="$out"
221
222     if [ -z "$TEST_LOCAL_DAEMONS" ] ; then
223         # Find the netmask
224         try_command_on_node $test_node ip addr show to $test_ip
225         mask="${out##*/}"
226         mask="${mask%% *}"
227     else
228         mask="24"
229     fi
230
231     echo "$test_ip/$mask is on $iface"
232 }
233
234 ctdb_get_all_pnns ()
235 {
236     try_command_on_node -q all "$CTDB pnn"
237     all_pnns="$out"
238 }
239
240 # The subtlety is that "ctdb delip" will fail if the IP address isn't
241 # configured on a node...
242 delete_ip_from_all_nodes ()
243 {
244     _ip="$1"
245
246     ctdb_get_all_pnns
247
248     _nodes=""
249
250     for _pnn in $all_pnns ; do
251         all_ips_on_node $_pnn
252         while read _i _n ; do
253             if [ "$_ip" = "$_i" ] ; then
254                 _nodes="${_nodes}${_nodes:+,}${_pnn}"
255             fi
256         done <<<"$out" # bashism
257     done
258
259     try_command_on_node -pq "$_nodes" "$CTDB delip $_ip"
260 }
261
262 #######################################
263
264 sleep_for ()
265 {
266     echo -n "=${1}|"
267     for i in $(seq 1 $1) ; do
268         echo -n '.'
269         sleep 1
270     done
271     echo '|'
272 }
273
274 _cluster_is_healthy ()
275 {
276     $CTDB nodestatus all >/dev/null
277 }
278
279 _cluster_is_recovered ()
280 {
281     node_has_status 0 recovered
282 }
283
284 _cluster_is_ready ()
285 {
286     _cluster_is_healthy && _cluster_is_recovered
287 }
288
289 cluster_is_healthy ()
290 {
291     if onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
292         echo "Cluster is HEALTHY"
293         if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_recovered ; then
294           echo "WARNING: cluster in recovery mode!"
295         fi
296         return 0
297     else
298         echo "Cluster is UNHEALTHY"
299         if ! ${ctdb_test_restart_scheduled:-false} ; then
300             echo "DEBUG AT $(date '+%F %T'):"
301             local i
302             for i in "onnode -q 0 $CTDB status" "onnode -q 0 onnode all $CTDB scriptstatus" ; do
303                 echo "$i"
304                 $i || true
305             done
306         fi
307         return 1
308     fi
309 }
310
311 wait_until_ready ()
312 {
313     local timeout="${1:-120}"
314
315     echo "Waiting for cluster to become ready..."
316
317     wait_until $timeout onnode -q any $CTDB_TEST_WRAPPER _cluster_is_ready
318 }
319
320 # This function is becoming nicely overloaded.  Soon it will collapse!  :-)
321 node_has_status ()
322 {
323     local pnn="$1"
324     local status="$2"
325
326     local bits fpat mpat rpat
327     case "$status" in
328         (unhealthy)    bits="?|?|?|1|*" ;;
329         (healthy)      bits="?|?|?|0|*" ;;
330         (disconnected) bits="1|*" ;;
331         (connected)    bits="0|*" ;;
332         (banned)       bits="?|1|*" ;;
333         (unbanned)     bits="?|0|*" ;;
334         (disabled)     bits="?|?|1|*" ;;
335         (enabled)      bits="?|?|0|*" ;;
336         (stopped)      bits="?|?|?|?|1|*" ;;
337         (notstopped)   bits="?|?|?|?|0|*" ;;
338         (frozen)       fpat='^[[:space:]]+frozen[[:space:]]+1$' ;;
339         (unfrozen)     fpat='^[[:space:]]+frozen[[:space:]]+0$' ;;
340         (recovered)    rpat='^Recovery mode:RECOVERY \(1\)$' ;;
341         (notlmaster)   rpat="^hash:.* lmaster:${pnn}\$" ;;
342         *)
343             echo "node_has_status: unknown status \"$status\""
344             return 1
345     esac
346
347     if [ -n "$bits" ] ; then
348         local out x line
349
350         out=$($CTDB -X status 2>&1) || return 1
351
352         {
353             read x
354             while read line ; do
355                 # This needs to be done in 2 steps to avoid false matches.
356                 local line_bits="${line#|${pnn}|*|}"
357                 [ "$line_bits" = "$line" ] && continue
358                 [ "${line_bits#${bits}}" != "$line_bits" ] && return 0
359             done
360             return 1
361         } <<<"$out" # Yay bash!
362     elif [ -n "$fpat" ] ; then
363         $CTDB statistics -n "$pnn" | egrep -q "$fpat"
364     elif [ -n "$rpat" ] ; then
365         ! $CTDB status -n "$pnn" | egrep -q "$rpat"
366     else
367         echo 'node_has_status: unknown mode, neither $bits nor $fpat is set'
368         return 1
369     fi
370 }
371
372 wait_until_node_has_status ()
373 {
374     local pnn="$1"
375     local status="$2"
376     local timeout="${3:-30}"
377     local proxy_pnn="${4:-any}"
378
379     echo "Waiting until node $pnn has status \"$status\"..."
380
381     if ! wait_until $timeout onnode $proxy_pnn $CTDB_TEST_WRAPPER node_has_status "$pnn" "$status" ; then
382         for i in "onnode -q any $CTDB status" "onnode -q any onnode all $CTDB scriptstatus" ; do
383             echo "$i"
384             $i || true
385         done
386
387         return 1
388     fi
389
390 }
391
392 # Useful for superficially testing IP failover.
393 # IPs must be on the given node.
394 # If the first argument is '!' then the IPs must not be on the given node.
395 ips_are_on_node ()
396 {
397     local negating=false
398     if [ "$1" = "!" ] ; then
399         negating=true ; shift
400     fi
401     local node="$1" ; shift
402     local ips="$*"
403
404     local out
405
406     all_ips_on_node $node
407
408     local check
409     for check in $ips ; do
410         local ip pnn
411         while read ip pnn ; do
412             if [ "$check" = "$ip" ] ; then
413                 if [ "$pnn" = "$node" ] ; then
414                     if $negating ; then return 1 ; fi
415                 else
416                     if ! $negating ; then return 1 ; fi
417                 fi
418                 ips="${ips/${ip}}" # Remove from list
419                 break
420             fi
421             # If we're negating and we didn't see the address then it
422             # isn't hosted by anyone!
423             if $negating ; then
424                 ips="${ips/${check}}"
425             fi
426         done <<<"$out" # bashism to avoid problem setting variable in pipeline.
427     done
428
429     ips="${ips// }" # Remove any spaces.
430     [ -z "$ips" ]
431 }
432
433 wait_until_ips_are_on_node ()
434 {
435     # Go to some trouble to print a use description of what is happening
436     local not=""
437     if [ "$1" == "!" ] ; then
438         not="no longer "
439     fi
440     local node=""
441     local ips=""
442     local i
443     for i ; do
444         [ "$i" != "!" ] || continue
445         if [ -z "$node" ] ; then
446             node="$i"
447             continue
448         fi
449         ips="${ips}${ips:+, }${i}"
450     done
451     echo "Waiting for ${ips} to ${not}be assigned to node ${node}"
452
453     wait_until 60 ips_are_on_node "$@"
454 }
455
456 node_has_some_ips ()
457 {
458     local node="$1"
459
460     local out
461
462     all_ips_on_node $node
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 some IPs to be assigned to node ${test_node}"
476
477     wait_until 60 node_has_some_ips "$@"
478 }
479
480 wait_until_node_has_no_ips ()
481 {
482     echo "Waiting until no IPs are assigned to node ${test_node}"
483
484     wait_until 60 ! node_has_some_ips "$@"
485 }
486
487 #######################################
488
489 _service_ctdb ()
490 {
491     cmd="$1"
492
493     if [ -e /etc/redhat-release ] ; then
494         service ctdb "$cmd"
495     else
496         /etc/init.d/ctdb "$cmd"
497     fi
498 }
499
500 # Stop/start CTDB on all nodes.  Override for local daemons.
501 ctdb_stop_all ()
502 {
503         onnode -p all $CTDB_TEST_WRAPPER _service_ctdb stop
504 }
505 ctdb_start_all ()
506 {
507         onnode -p all $CTDB_TEST_WRAPPER _service_ctdb start
508 }
509
510 # Nothing needed for a cluster.  Override for local daemons.
511 setup_ctdb ()
512 {
513     :
514 }
515
516 start_ctdb_1 ()
517 {
518     onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb start
519 }
520
521 stop_ctdb_1 ()
522 {
523     onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb stop
524 }
525
526 restart_ctdb_1 ()
527 {
528     onnode "$1" $CTDB_TEST_WRAPPER _service_ctdb restart
529 }
530
531 ctdb_init ()
532 {
533     local i
534     for i in $(seq 1 5) ; do
535         ctdb_start_all || {
536             echo "Start failed.  Trying again in a few seconds..."
537             sleep_for 5
538             continue
539         }
540
541         wait_until_ready || {
542             echo "Cluster didn't become ready.  Restarting..."
543             continue
544         }
545
546         echo "Setting RerecoveryTimeout to 1"
547         onnode -pq all "$CTDB setvar RerecoveryTimeout 1"
548
549         # In recent versions of CTDB, forcing a recovery like this
550         # blocks until the recovery is complete.  Hopefully this will
551         # help the cluster to stabilise before a subsequent test.
552         echo "Forcing a recovery..."
553         onnode -q 0 $CTDB recover
554         sleep_for 2
555
556         if ! onnode -q any $CTDB_TEST_WRAPPER _cluster_is_recovered ; then
557             echo "Cluster has gone into recovery again, waiting..."
558             wait_until 30/2 onnode -q any $CTDB_TEST_WRAPPER _cluster_is_recovered
559         fi
560
561
562         # Cluster is still healthy.  Good, we're done!
563         if ! onnode 0 $CTDB_TEST_WRAPPER _cluster_is_healthy ; then
564             echo "Cluster became UNHEALTHY again [$(date)]"
565             onnode -p all ctdb status -X 2>&1
566             onnode -p all ctdb scriptstatus 2>&1
567             echo "Restarting..."
568             continue
569         fi
570
571         echo "Doing a sync..."
572         onnode -q 0 $CTDB sync
573
574         echo "ctdb is ready"
575         return 0
576     done
577
578     echo "Cluster UNHEALTHY...  too many attempts..."
579     onnode -p all ctdb status -X 2>&1
580     onnode -p all ctdb scriptstatus 2>&1
581
582     # Try to make the calling test fail
583     status=1
584     return 1
585 }
586
587 ctdb_restart_when_done ()
588 {
589     ctdb_test_restart_scheduled=true
590 }
591
592 ctdb_base_show ()
593 {
594         echo "${CTDB_BASE:-${CTDB_SCRIPTS_BASE}}"
595 }
596
597 #######################################
598
599 wait_for_monitor_event ()
600 {
601     local pnn="$1"
602     local timeout=120
603
604     echo "Waiting for a monitor event on node ${pnn}..."
605
606     try_command_on_node "$pnn" $CTDB scriptstatus || {
607         echo "Unable to get scriptstatus from node $pnn"
608         return 1
609     }
610
611     local ctdb_scriptstatus_original="$out"
612     wait_until 120 _ctdb_scriptstatus_changed
613 }
614
615 _ctdb_scriptstatus_changed ()
616 {
617     try_command_on_node "$pnn" $CTDB scriptstatus || {
618         echo "Unable to get scriptstatus from node $pnn"
619         return 1
620     }
621
622     [ "$out" != "$ctdb_scriptstatus_original" ]
623 }
624
625 #######################################
626
627 nfs_test_setup ()
628 {
629     select_test_node_and_ips
630
631     nfs_first_export=$(showmount -e $test_ip | sed -n -e '2s/ .*//p')
632
633     echo "Creating test subdirectory..."
634     try_command_on_node $test_node "TMPDIR=$nfs_first_export mktemp -d"
635     nfs_test_dir="$out"
636     try_command_on_node $test_node "chmod 777 $nfs_test_dir"
637
638     nfs_mnt_d=$(mktemp -d)
639     nfs_local_file="${nfs_mnt_d}/${nfs_test_dir##*/}/TEST_FILE"
640     nfs_remote_file="${nfs_test_dir}/TEST_FILE"
641
642     ctdb_test_exit_hook_add nfs_test_cleanup
643
644     echo "Mounting ${test_ip}:${nfs_first_export} on ${nfs_mnt_d} ..."
645     mount -o timeo=1,hard,intr,vers=3 \
646         "[${test_ip}]:${nfs_first_export}" ${nfs_mnt_d}
647 }
648
649 nfs_test_cleanup ()
650 {
651     rm -f "$nfs_local_file"
652     umount -f "$nfs_mnt_d"
653     rmdir "$nfs_mnt_d"
654     onnode -q $test_node rmdir "$nfs_test_dir"
655 }
656
657 #######################################
658
659 # If the given IP is hosted then print 2 items: maskbits and iface
660 ip_maskbits_iface ()
661 {
662     _addr="$1"
663
664     case "$_addr" in
665         *:*) _family="inet6" ; _bits=128 ;;
666         *)   _family="inet"  ; _bits=32  ;;
667     esac
668
669     ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
670         awk -v family="${_family}" \
671             'NR == 1 { iface = $2; sub(":$", "", iface) } \
672              $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
673                            print mask, iface, family }'
674 }
675
676 drop_ip ()
677 {
678     _addr="${1%/*}"  # Remove optional maskbits
679
680     set -- $(ip_maskbits_iface $_addr)
681     if [ -n "$1" ] ; then
682         _maskbits="$1"
683         _iface="$2"
684         echo "Removing public address $_addr/$_maskbits from device $_iface"
685         ip addr del "$_ip/$_maskbits" dev "$_iface" >/dev/null 2>&1 || true
686     fi
687 }
688
689 drop_ips ()
690 {
691     for _ip ; do
692         drop_ip "$_ip"
693     done
694 }
695
696 #######################################
697
698 # $1: pnn, $2: DB name
699 db_get_path ()
700 {
701     try_command_on_node -v $1 $CTDB getdbstatus "$2" |
702     sed -n -e "s@^path: @@p"
703 }
704
705 # $1: pnn, $2: DB name
706 db_ctdb_cattdb_count_records ()
707 {
708         # Count the number of keys, excluding any that begin with '_'.
709         # This excludes at least the sequence number record in
710         # persistent/replicated databases.  The trailing "|| :" forces
711         # the command to succeed when no records are matched.
712         try_command_on_node $1 \
713                 "$CTDB cattdb $2 | grep -c '^key([0-9][0-9]*) = \"[^_]' || :"
714         echo "$out"
715 }
716
717 # $1: pnn, $2: DB name, $3: key string, $4: value string, $5: RSN (default 7)
718 db_ctdb_tstore ()
719 {
720     _tdb=$(db_get_path $1 "$2")
721     _rsn="${5:-7}"
722     try_command_on_node $1 $CTDB tstore "$_tdb" "$3" "$4" "$_rsn"
723 }
724
725 # $1: pnn, $2: DB name, $3: dbseqnum (must be < 255!!!!!)
726 db_ctdb_tstore_dbseqnum ()
727 {
728     # "__db_sequence_number__" + trailing 0x00
729     _key='0x5f5f64625f73657175656e63655f6e756d6265725f5f00'
730
731     # Construct 8 byte (unit64_t) database sequence number.  This
732     # probably breaks if $3 > 255
733     _value=$(printf "0x%02x%014x" $3 0)
734
735     db_ctdb_tstore $1 "$2" "$_key" "$_value"
736 }
737
738 #######################################
739
740 # Enables all of the event scripts used in cluster tests, except for
741 # the mandatory scripts
742 ctdb_enable_cluster_test_event_scripts ()
743 {
744         local scripts="
745                        06.nfs
746                        10.interface
747                        49.winbind
748                        50.samba
749                        60.nfs
750                       "
751
752         local s
753         for s in $scripts ; do
754                 try_command_on_node all ctdb event script enable legacy "$s"
755         done
756 }
757
758 ########################################
759
760 # Make sure that $CTDB is set.
761 : ${CTDB:=ctdb}
762
763 local="${TEST_SUBDIR}/scripts/local.bash"
764 if [ -r "$local" ] ; then
765     . "$local"
766 fi