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