ctdb-tests: Correctly cascade test failures from the end of pipes
[vlendec/samba-autobuild/.git] / ctdb / tests / eventscripts / scripts / local.sh
1 # Hey Emacs, this is a -*- shell-script -*- !!!  :-)
2
3 # Augment PATH with relevant stubs/ directories.  We do this by actually
4 # setting PATH, and also by setting $EVENTSCRIPTS_PATH and then
5 # prepending that to $PATH in rc.local to avoid the PATH reset in
6 # functions.
7
8 EVENTSCRIPTS_PATH=""
9
10 if [ -d "${TEST_SUBDIR}/stubs" ] ; then
11     EVENTSCRIPTS_PATH="${TEST_SUBDIR}/stubs"
12     case "$EVENTSCRIPTS_PATH" in
13         /*) : ;;
14         *) EVENTSCRIPTS_PATH="${PWD}/${EVENTSCRIPTS_PATH}" ;;
15     esac
16 fi
17
18 export EVENTSCRIPTS_PATH
19
20 PATH="${EVENTSCRIPTS_PATH}:${PATH}"
21
22 export EVENTSCRIPTS_TESTS_VAR_DIR="${TEST_VAR_DIR}/unit_eventscripts"
23 if [ -d "$EVENTSCRIPTS_TESTS_VAR_DIR" -a \
24     "$EVENTSCRIPTS_TESTS_VAR_DIR" != "/unit_eventscripts" ] ; then
25     rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR"
26 fi
27 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
28 export CTDB_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb"
29
30 export CTDB_LOGGING="file:${EVENTSCRIPTS_TESTS_VAR_DIR}/log.ctdb"
31 touch "${CTDB_LOGGING#file:}" || \
32     die "Unable to setup logging for \"$CTDB_LOGGING\""
33
34 if [ -d "${TEST_SUBDIR}/etc" ] ; then    
35     cp -a "${TEST_SUBDIR}/etc" "$EVENTSCRIPTS_TESTS_VAR_DIR"
36     export CTDB_ETCDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc"
37 else
38     die "Unable to setup \$CTDB_ETCDIR"
39 fi
40
41 if [ -d "${TEST_SUBDIR}/etc-ctdb" ] ; then
42     cp -prL "${TEST_SUBDIR}/etc-ctdb" "$EVENTSCRIPTS_TESTS_VAR_DIR"
43     export CTDB_BASE="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc-ctdb"
44 else
45     die "Unable to set \$CTDB_BASE"
46 fi
47 export CTDB_BASE
48
49 if [ ! -d "${CTDB_BASE}/events.d" ] ; then
50     cat <<EOF
51 ERROR: Directory ${CTDB_BASE}/events.d does not exist.
52
53 That means that no eventscripts can be tested.
54
55 One possible explanation:
56
57   You have CTDB installed via RPMs (or similar), so the regular
58   CTDB_BASE directory is in /etc/ctdb/
59
60   BUT
61
62   You have done a regular "configure" and "make install" so the tests
63   are installed under /usr/local/.
64
65 If so, one possible hack to fix this is to create a symlink:
66
67   ln -s /etc/ctdb /usr/local/etc/ctdb
68
69 This is nasty but it works...  :-)
70 EOF
71     exit 1
72 fi
73
74 ######################################################################
75
76 if "$TEST_VERBOSE" ; then
77     debug () { echo "$@" ; }
78 else
79     debug () { : ; }
80 fi
81
82 eventscripts_tests_cleanup_hooks=""
83
84 # This loses quoting!
85 eventscripts_test_add_cleanup ()
86 {
87     eventscripts_tests_cleanup_hooks="${eventscripts_tests_cleanup_hooks}${eventscripts_tests_cleanup_hooks:+ ; }$*"
88 }
89
90 trap 'eval $eventscripts_tests_cleanup_hooks' 0
91
92
93 ######################################################################
94
95 # General setup fakery
96
97 setup_generic ()
98 {
99     debug "Setting up shares (3 existing shares)"
100     # Create 3 fake shares/exports.
101     export FAKE_SHARES=""
102     for i in $(seq 1 3) ; do
103         _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/${i}_existing"
104         mkdir -p "$_s"
105         FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
106     done
107
108     export FAKE_PROC_NET_BONDING="$EVENTSCRIPTS_TESTS_VAR_DIR/proc-net-bonding"
109     mkdir -p "$FAKE_PROC_NET_BONDING"
110     rm -f "$FAKE_PROC_NET_BONDING"/*
111
112     export FAKE_ETHTOOL_LINK_DOWN="$EVENTSCRIPTS_TESTS_VAR_DIR/ethtool-link-down"
113     mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
114     rm -f "$FAKE_ETHTOOL_LINK_DOWN"/*
115
116     # This can only have 2 levels.  We don't want to resort to usings
117     # something dangerous like "rm -r" setup time.
118     export FAKE_IP_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ip-state"
119     mkdir -p "$FAKE_IP_STATE"
120     rm -f "$FAKE_IP_STATE"/*/*
121     rm -f "$FAKE_IP_STATE"/* 2>/dev/null || true
122     rmdir "$FAKE_IP_STATE"/* 2>/dev/null || true
123
124
125     export CTDB_DBDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/db"
126     mkdir -p "${CTDB_DBDIR}/persistent"
127
128     export FAKE_TDBTOOL_SUPPORTS_CHECK="yes"
129     export FAKE_TDB_IS_OK
130     export FAKE_DATE_OUTPUT
131
132     export FAKE_NETSTAT_TCP_ESTABLISHED FAKE_TCP_LISTEN FAKE_NETSTAT_UNIX_LISTEN
133     export FAKE_NETSTAT_TCP_ESTABLISHED_FILE=$(mktemp --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR")
134 }
135
136 tcp_port_down ()
137 {
138     for _i ; do
139         debug "Marking TCP port \"${_i}\" as not listening"
140         FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
141     done
142 }
143
144 shares_missing ()
145 {
146     _fmt="$1" ; shift
147
148     # Replace some shares with non-existent ones.
149     _t=""
150     _n=1
151     _nl="
152 "
153     export MISSING_SHARES_TEXT=""
154     for _i in $FAKE_SHARES ; do
155         if [ $_n = "$1" ] ; then
156             shift
157             _i="${_i%_existing}_missing"
158             debug "Replacing share $_n with missing share \"$_i\""
159             rmdir "$_i" 2>/dev/null || true
160             MISSING_SHARES_TEXT="${MISSING_SHARES_TEXT}${MISSING_SHARES_TEXT:+${_nl}}"$(printf "$_fmt" "${_i}")
161         fi
162         _t="${_t}${_t:+ }${_i}"
163         _n=$(($_n + 1))
164     done
165     FAKE_SHARES="$_t"
166 }
167
168 # Setup some fake /proc/net/bonding files with just enough info for
169 # the eventscripts.
170
171 # arg1 is interface name, arg2 is currently active slave (use "None"
172 # if none), arg3 is MII status ("up" or "down").
173 setup_bond ()
174 {
175     _iface="$1"
176     _slave="${2:-${_iface}_sl_0}"
177     _mii_s="${3:-up}"
178     _mii_subs="${4:-${_mii_s:-up}}"
179     echo "Setting $_iface to be a bond with active slave $_slave and MII status $_mii_s"
180     cat >"${FAKE_PROC_NET_BONDING}/$_iface" <<EOF
181 Bonding Mode: IEEE 802.3ad Dynamic link aggregation
182 Currently Active Slave: $_slave
183 # Status of the bond
184 MII Status: $_mii_s
185 # Status of 1st pretend adapter
186 MII Status: $_mii_subs
187 # Status of 2nd pretend adapter
188 MII Status: $_mii_subs
189 EOF
190 }
191
192 ethtool_interfaces_down ()
193 {
194     for _i ; do
195         echo "Marking interface $_i DOWN for ethtool"
196         touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
197     done
198 }
199
200 ethtool_interfaces_up ()
201 {
202     for _i ; do
203         echo "Marking interface $_i UP for ethtool"
204         rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
205     done
206 }
207
208 dump_routes ()
209 {
210     echo "# ip rule show"
211     ip rule show
212
213     ip rule show |
214     while read _p _x _i _x _t ; do
215         # Remove trailing colon after priority/preference.
216         _p="${_p%:}"
217         # Only remove rules that match our priority/preference.
218         [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
219
220         echo "# ip route show table $_t"
221         ip route show table "$_t"
222     done
223 }
224
225 # Copied from 13.per_ip_routing for now... so this is lazy testing  :-(
226 ipv4_host_addr_to_net ()
227 {
228     _host="$1"
229     _maskbits="$2"
230
231     # Convert the host address to an unsigned long by splitting out
232     # the octets and doing the math.
233     _host_ul=0
234     for _o in $(export IFS="." ; echo $_host) ; do
235         _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
236     done
237
238     # Calculate the mask and apply it.
239     _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
240     _net_ul=$(( $_host_ul & $_mask_ul ))
241
242     # Now convert to a network address one byte at a time.
243     _net=""
244     for _o in $(seq 1 4) ; do
245         _net="$(($_net_ul & 255))${_net:+.}${_net}"
246         _net_ul=$(($_net_ul >> 8))
247     done
248
249     echo "${_net}/${_maskbits}"
250 }
251
252 ######################################################################
253
254 # CTDB fakery
255
256 # Evaluate an expression that probably calls functions or uses
257 # variables from the CTDB functions file.  This is used for test
258 # initialisation.
259 eventscript_call ()
260 {
261     (
262         . "$CTDB_BASE/functions"
263         "$@"
264     )
265 }
266
267 # Set output for ctdb command.  Option 1st argument is return code.
268 ctdb_set_output ()
269 {
270     _out="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.out"
271     cat >"$_out"
272
273     _rc="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb.rc"
274     echo "${1:-0}" >"$_rc"
275
276     eventscripts_test_add_cleanup "rm -f $_out $_rc"
277 }
278
279 # For now this creates the same public addresses each time.  However,
280 # it could be made more flexible.
281 setup_public_addresses ()
282 {
283     if [ -f "$CTDB_PUBLIC_ADDRESSES" -a \
284             "${CTDB_PUBLIC_ADDRESSES%/*}" = "$EVENTSCRIPTS_TESTS_VAR_DIR" ] ; then
285         rm "$CTDB_PUBLIC_ADDRESSES"
286     fi
287
288     export CTDB_PUBLIC_ADDRESSES=$(mktemp \
289                                        --tmpdir="$EVENTSCRIPTS_TESTS_VAR_DIR" \
290                                        "public-addresses-XXXXXXXX")
291
292     echo "Setting up CTDB_PUBLIC_ADDRESSES=${CTDB_PUBLIC_ADDRESSES}"
293     cat >"$CTDB_PUBLIC_ADDRESSES" <<EOF
294 10.0.0.1/24 dev123
295 10.0.0.2/24 dev123
296 10.0.0.3/24 dev123
297 10.0.0.4/24 dev123
298 10.0.0.5/24 dev123
299 10.0.0.6/24 dev123
300 10.0.1.1/24 dev456
301 10.0.1.2/24 dev456
302 10.0.1.3/24 dev456
303 EOF
304 }
305
306 setup_ctdb ()
307 {
308     setup_generic
309
310     export FAKE_CTDB_NUMNODES="${1:-3}"
311     echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
312
313     export FAKE_CTDB_PNN="${2:-0}"
314     echo "Setting up CTDB with PNN ${FAKE_CTDB_PNN}"
315
316     setup_public_addresses
317
318     export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
319
320     export FAKE_CTDB_EXTRA_CONFIG="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-config.sh"
321     rm -f "$FAKE_CTDB_EXTRA_CONFIG"
322
323     export FAKE_CTDB_IFACES_DOWN="$FAKE_CTDB_STATE/ifaces-down"
324     mkdir -p "$FAKE_CTDB_IFACES_DOWN"
325     rm -f "$FAKE_CTDB_IFACES_DOWN"/*
326
327     export FAKE_CTDB_SCRIPTSTATUS="$FAKE_CTDB_STATE/scriptstatus"
328     mkdir -p "$FAKE_CTDB_SCRIPTSTATUS"
329     rm -f "$FAKE_CTDB_SCRIPTSTATUS"/*
330
331     export CTDB_PARTIALLY_ONLINE_INTERFACES
332
333     export FAKE_CTDB_TUNABLES_OK="MonitorInterval TDBMutexEnabled DatabaseHashSize"
334     export FAKE_CTDB_TUNABLES_OBSOLETE="EventScriptUnhealthyOnTimeout"
335 }
336
337 setup_config ()
338 {
339     cat >"$FAKE_CTDB_EXTRA_CONFIG"
340 }
341
342 setup_memcheck ()
343 {
344     setup_ctdb
345
346     _swap_total="5857276"
347
348     if [ "$1" = "bad" ] ; then
349         _swap_free="   4352"
350         _mem_cached=" 112"
351         _mem_free=" 468"
352     else
353         _swap_free="$_swap_total"
354         _mem_cached="1112"
355         _mem_free="1468"
356     fi
357
358     export FAKE_PROC_MEMINFO="\
359 MemTotal:        3940712 kB
360 MemFree:          225268 kB
361 Buffers:          146120 kB
362 Cached:          1139348 kB
363 SwapCached:        56016 kB
364 Active:          2422104 kB
365 Inactive:        1019928 kB
366 Active(anon):    1917580 kB
367 Inactive(anon):   523080 kB
368 Active(file):     504524 kB
369 Inactive(file):   496848 kB
370 Unevictable:        4844 kB
371 Mlocked:            4844 kB
372 SwapTotal:       ${_swap_total} kB
373 SwapFree:        ${_swap_free} kB
374 ..."
375
376     export FAKE_FREE_M="\
377              total       used       free     shared    buffers     cached
378 Mem:          3848       3634        213          0        142       ${_mem_cached}
379 -/+ buffers/cache:       2379       ${_mem_free}
380 Swap:         5719        246       5473"
381
382     export CTDB_MONITOR_FREE_MEMORY
383     export CTDB_MONITOR_FREE_MEMORY_WARN
384     export CTDB_CHECK_SWAP_IS_NOT_USED
385 }
386
387 ctdb_get_interfaces ()
388 {
389     # The echo/subshell forces all the output onto 1 line.
390     echo $(ctdb ifaces -X | awk -F'|' 'FNR > 1 {print $2}')
391 }
392
393 ctdb_get_1_interface ()
394 {
395     _t=$(ctdb_get_interfaces)
396     echo ${_t%% *}
397 }
398
399 # Print all public addresses as: interface IP maskbits
400 # Each line is suitable for passing to takeip/releaseip
401 ctdb_get_all_public_addresses ()
402 {
403     _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
404     while IFS="/$IFS" read _ip _maskbits _ifaces ; do
405         echo "$_ifaces $_ip $_maskbits"
406     done <"$_f"
407 }
408
409 # Print public addresses on this node as: interface IP maskbits
410 # Each line is suitable for passing to takeip/releaseip
411 ctdb_get_my_public_addresses ()
412 {
413     ctdb ip -v -X | {
414         read _x # skip header line
415
416         while IFS="|" read _x _ip _x _iface _x ; do
417             [ -n "$_iface" ] || continue
418             while IFS="/$IFS" read _i _maskbits _x ; do
419                 if [ "$_ip" = "$_i" ] ; then
420                     echo $_iface $_ip $_maskbits
421                     break
422                 fi
423             done <"${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
424         done
425     }
426 }
427
428 # Prints the 1st public address as: interface IP maskbits
429 # This is suitable for passing to takeip/releaseip
430 ctdb_get_1_public_address ()
431 {
432     ctdb_get_my_public_addresses | { head -n 1 ; cat >/dev/null ; }
433 }
434
435 ctdb_not_implemented ()
436 {
437     export CTDB_NOT_IMPLEMENTED="$1"
438     ctdb_not_implemented="\
439 DEBUG: ctdb: command \"$1\" not implemented in stub"
440 }
441
442 ctdb_fake_scriptstatus ()
443 {
444     _code="$1"
445     _status="$2"
446     _err_out="$3"
447
448     _d1=$(date '+%s.%N')
449     _d2=$(date '+%s.%N')
450
451     echo "$_code $_status $_err_out" >"$FAKE_CTDB_SCRIPTSTATUS/$script"
452 }
453
454 ######################################################################
455
456 setup_ctdb_policy_routing ()
457 {
458     service_name="per_ip_routing"
459
460     export CTDB_PER_IP_ROUTING_CONF="$CTDB_BASE/policy_routing"
461     export CTDB_PER_IP_ROUTING_RULE_PREF=100
462     export CTDB_PER_IP_ROUTING_TABLE_ID_LOW=1000
463     export CTDB_PER_IP_ROUTING_TABLE_ID_HIGH=2000
464
465     # Tests need to create and populate this file
466     rm -f "$CTDB_PER_IP_ROUTING_CONF"
467 }
468
469 # Create policy routing configuration in $CTDB_PER_IP_ROUTING_CONF.
470 # $1 is the number of assigned IPs to use (<num>, all), defaulting to
471 # 1.  If $2 is "default" then a default route is also added.
472 create_policy_routing_config ()
473 {
474     _num_ips="${1:-1}"
475     _should_add_default="$2"
476
477     ctdb_get_my_public_addresses |
478     if [ "$_num_ips" = "all" ] ; then
479         cat
480     else
481         { head -n "$_num_ips" ; cat >/dev/null ; }
482     fi |
483     while read _dev _ip _bits ; do
484         _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
485         _gw="${_net%.*}.1" # a dumb, calculated default
486
487         echo "$_ip $_net"
488
489         if [ "$_should_add_default" = "default" ] ; then
490             echo "$_ip 0.0.0.0/0 $_gw"
491         fi
492     done >"$CTDB_PER_IP_ROUTING_CONF"
493 }
494
495 # Check the routes against those that are expected.  $1 is the number
496 # of assigned IPs to use (<num>, all), defaulting to 1.  If $2 is
497 # "default" then expect default routes to have been added.
498 check_routes ()
499 {
500     _num_ips="${1:-1}"
501     _should_add_default="$2"
502
503     _policy_rules=""
504     _policy_routes=""
505
506     ctdb_get_my_public_addresses |
507     if [ "$_num_ips" = "all" ] ; then
508         cat
509     else
510         { head -n "$_num_ips" ; cat >/dev/null ; }
511     fi | {
512         while read _dev _ip _bits ; do
513             _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
514             _gw="${_net%.*}.1" # a dumb, calculated default
515
516             _policy_rules="${_policy_rules}
517 ${CTDB_PER_IP_ROUTING_RULE_PREF}:       from $_ip lookup ctdb.$_ip "
518             _policy_routes="${_policy_routes}
519 # ip route show table ctdb.$_ip
520 $_net dev $_dev  scope link "
521
522             if [ "$_should_add_default" = "default" ] ; then
523                 _policy_routes="${_policy_routes}
524 default via $_gw dev $_dev "
525             fi
526         done
527
528         ok <<EOF
529 # ip rule show
530 0:      from all lookup local ${_policy_rules}
531 32766:  from all lookup main 
532 32767:  from all lookup default ${_policy_routes}
533 EOF
534
535         simple_test_command dump_routes
536     } || test_fail
537 }
538
539 ######################################################################
540
541 ctdb_catdb_format_pairs ()
542 {
543     _count=0
544
545     while read _k _v ; do
546         _kn=$(echo -n "$_k" | wc -c)
547         _vn=$(echo -n "$_v" | wc -c)
548         cat <<EOF
549 key(${_kn}) = "${_k}"
550 dmaster: 0
551 rsn: 1
552 data(${_vn}) = "${_v}"
553
554 EOF
555         _count=$(($_count + 1))
556     done
557
558     echo "Dumped ${_count} records"
559 }
560
561 check_ctdb_tdb_statd_state ()
562 {
563     ctdb_get_my_public_addresses |
564     while read _x _sip _x ; do
565         for _cip ; do
566             echo "statd-state@${_sip}@${_cip}" "$FAKE_DATE_OUTPUT"
567         done
568     done |
569     ctdb_catdb_format_pairs | {
570         ok
571         simple_test_command ctdb catdb ctdb.tdb
572     } || test_fail
573 }
574
575 check_statd_callout_smnotify ()
576 {
577     _state_even=$(( $(date '+%s') / 2 * 2))
578     _state_odd=$(($_state_even + 1))
579
580     nfs_load_config
581
582     ctdb_get_my_public_addresses |
583     while read _x _sip _x ; do
584         for _cip ; do
585             cat <<EOF
586 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_even}
587 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_even}
588 --client=${_cip} --ip=${_sip} --server=${_sip} --stateval=${_state_odd}
589 --client=${_cip} --ip=${_sip} --server=${NFS_HOSTNAME} --stateval=${_state_odd}
590 EOF
591         done
592     done | {
593         ok
594         simple_test_event "notify"
595     } || test_fail
596 }
597
598 ######################################################################
599
600 setup_ctdb_natgw ()
601 {
602     debug "Setting up NAT gateway"
603
604     natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
605     mkdir -p "$natgw_config_dir"
606
607     # These will accumulate, 1 per test... but will be cleaned up at
608     # the end.
609     export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
610
611     # Read from stdin
612     while read _ip _master _dev ; do
613         echo "$_ip"
614         if [ "$_master" = "master" ] ; then
615             export FAKE_CTDB_NATGW_MASTER="$_ip"
616         fi
617     done >"$CTDB_NATGW_NODES"
618
619     # Assume all of the nodes are on a /24 network and have IPv4
620     # addresses:
621     read _ip <"$CTDB_NATGW_NODES"
622     export CTDB_NATGW_PRIVATE_NETWORK="${_ip%.*}.0/24"
623
624     # These are fixed.  Probably don't use the same network for the
625     # private node IPs.  To unset the default gateway just set it to
626     # "".  :-)
627     export CTDB_NATGW_PUBLIC_IP="10.1.1.121/24"
628     export CTDB_NATGW_PUBLIC_IFACE="eth1"
629     export CTDB_NATGW_DEFAULT_GATEWAY="10.1.1.254"
630     export CTDB_NATGW_SLAVE_ONLY=""
631 }
632
633 ok_natgw_master_ip_addr_show ()
634 {
635     _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
636
637     # This is based on CTDB_NATGW_PUBLIC_IP
638     _brd="10.1.1.255"
639
640 ok <<EOF
641 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
642     link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
643     inet ${CTDB_NATGW_PUBLIC_IP} brd ${_brd} scope global ${CTDB_NATGW_PUBLIC_IFACE}
644        valid_lft forever preferred_lft forever
645 EOF
646 }
647
648 ok_natgw_slave_ip_addr_show ()
649 {
650     _mac=$(echo "$CTDB_NATGW_PUBLIC_IFACE" | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
651 ok <<EOF
652 1: ${CTDB_NATGW_PUBLIC_IFACE}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
653     link/ether ${_mac} brd ff:ff:ff:ff:ff:ff
654 EOF
655 }
656
657 ok_natgw_master_static_routes ()
658 {
659     _nl="
660 "
661     _t=""
662     for _i in $CTDB_NATGW_STATIC_ROUTES ; do
663         # This is intentionally different to the code in 11.natgw ;-)
664         case "$_i" in
665             *@*)
666                 _net=$(echo "$_i" | sed -e 's|@.*||')
667                 _gw=$(echo "$_i" | sed -e 's|.*@||')
668                 ;;
669             *)
670                 _net="$_i"
671                 _gw="$CTDB_NATGW_DEFAULT_GATEWAY"
672         esac
673
674         [ -n "$_gw" ] || continue
675         _t="${_t}${_t:+${_nl}}"
676         _t="${_t}${_net} via ${_gw} dev ethXXX  metric 10 "
677     done
678     ok "$_t"
679 }
680
681 ok_natgw_slave_static_routes ()
682 {
683     _nl="
684 "
685     _t=""
686     for _i in $CTDB_NATGW_STATIC_ROUTES ; do
687         # This is intentionally different to the code in 11.natgw ;-)
688         _net=$(echo "$_i" | sed -e 's|@.*||')
689
690         # The interface for the private network isn't specified as
691         # part of the NATGW configuration and isn't part of the
692         # command to add the route.  It is implicitly added by "ip
693         # route" but our stub doesn't do this and adds "ethXXX".
694         _t="${_t}${_t:+${_nl}}"
695         _t="${_t}${_net} via ${FAKE_CTDB_NATGW_MASTER} dev ethXXX  metric 10 "
696     done
697     ok "$_t"
698 }
699
700 ######################################################################
701
702 # Samba/winbind fakery
703
704 setup_samba ()
705 {
706     setup_ctdb
707
708     service_name="samba"
709
710     if [ "$1" != "down" ] ; then
711
712         debug "Marking Samba services as up, listening and managed by CTDB"
713         # Get into known state.
714         eventscript_call ctdb_service_managed
715
716         # All possible service names for all known distros.
717         for i in "smb" "nmb" "samba" ; do
718             service "$i" force-started
719         done
720
721         export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
722         export CTDB_MANAGED_SERVICES="foo samba bar"
723
724         export FAKE_TCP_LISTEN="0.0.0.0:445 0.0.0.0:139"
725         export FAKE_WBINFO_FAIL="no"
726
727         # Some things in 50.samba are backgrounded and waited for.  If
728         # we don't sleep at all then timeouts can happen.  This avoids
729         # that...  :-)
730         export FAKE_SLEEP_FORCE=0.1
731     else
732         debug "Marking Samba services as down, not listening and not managed by CTDB"
733         # Get into known state.
734         eventscript_call ctdb_service_unmanaged
735
736         # All possible service names for all known distros.
737         for i in "smb" "nmb" "samba" ; do
738             service "$i" force-stopped
739         done
740
741         export CTDB_SAMBA_SKIP_SHARE_CHECK="no"
742         export CTDB_MANAGED_SERVICES="foo bar"
743         unset CTDB_MANAGES_SAMBA
744
745         export FAKE_TCP_LISTEN=""
746         export FAKE_WBINFO_FAIL="yes"
747     fi
748
749     # This is ugly but if this file isn't removed before each test
750     # then configuration changes between tests don't stick.
751     rm -f "$CTDB_VARDIR/state/samba/smb.conf.cache"
752 }
753
754 setup_winbind ()
755 {
756     setup_ctdb
757
758     service_name="winbind"
759
760     if [ "$1" != "down" ] ; then
761
762         debug "Marking Winbind service as up and managed by CTDB"
763         # Get into known state.
764         eventscript_call ctdb_service_managed
765
766         service "winbind" force-started
767
768         export CTDB_MANAGED_SERVICES="foo winbind bar"
769
770         export FAKE_WBINFO_FAIL="no"
771
772     else
773         debug "Marking Winbind service as down and not managed by CTDB"
774         # Get into known state.
775         eventscript_call ctdb_service_unmanaged
776
777         service "winbind" force-stopped
778
779         export CTDB_MANAGED_SERVICES="foo bar"
780         unset CTDB_MANAGES_WINBIND
781
782         export FAKE_WBINFO_FAIL="yes"
783     fi
784 }
785
786 wbinfo_down ()
787 {
788     debug "Making wbinfo commands fail"
789     FAKE_WBINFO_FAIL="yes"
790 }
791
792 ######################################################################
793
794 # NFS fakery
795
796 setup_nfs ()
797 {
798     setup_ctdb
799
800     service_name="nfs"
801
802     export FAKE_RPCINFO_SERVICES=""
803
804     export CTDB_NFS_SKIP_SHARE_CHECK="no"
805
806     export CTDB_MONITOR_NFS_THREAD_COUNT RPCNFSDCOUNT FAKE_NFSD_THREAD_PIDS
807     export CTDB_NFS_DUMP_STUCK_THREADS FAKE_RPC_THREAD_PIDS
808
809     # Reset the failcounts for nfs services.
810     eventscript_call eval rm -f '$ctdb_fail_dir/nfs_*'
811
812     if [ "$1" != "down" ] ; then
813         debug "Setting up NFS environment: all RPC services up, NFS managed by CTDB"
814
815         eventscript_call ctdb_service_managed
816         service "nfs" force-started  # might not be enough
817
818         export CTDB_MANAGED_SERVICES="foo nfs bar"
819
820         rpc_services_up "nfs" "mountd" "rquotad" "nlockmgr" "status"
821     else
822         debug "Setting up NFS environment: all RPC services down, NFS not managed by CTDB"
823
824         eventscript_call ctdb_service_unmanaged
825         service "nfs" force-stopped  # might not be enough
826         eventscript_call startstop_nfs stop
827
828         export CTDB_MANAGED_SERVICES="foo bar"
829         unset CTDB_MANAGES_NFS
830     fi
831 }
832
833 setup_nfs_ganesha ()
834 {
835     setup_nfs "$@"
836     export CTDB_NFS_SERVER_MODE="ganesha"
837     if [ "$1" != "down" ] ; then
838         export CTDB_MANAGES_NFS="yes"
839     fi
840
841     # We do not support testing the Ganesha-nfsd-specific part of the
842     # eventscript.
843     export CTDB_SKIP_GANESHA_NFSD_CHECK="yes"
844     export CTDB_NFS_SKIP_SHARE_CHECK="yes"
845 }
846
847 rpc_services_down ()
848 {
849     for _i ; do
850         debug "Marking RPC service \"${_i}\" as unavailable"
851         FAKE_RPCINFO_SERVICES=$(echo "$FAKE_RPCINFO_SERVICES" | sed -r -e "s@[[:space:]]*${_i}:[0-9]+:[0-9]+@@g")
852     done
853 }
854
855 rpc_services_up ()
856 {
857     for _i ; do
858         debug "Marking RPC service \"${_i}\" as available"
859         case "$_i" in
860             nfs)      _t="2:3" ;;
861             mountd)   _t="1:3" ;;
862             rquotad)  _t="1:2" ;;
863             nlockmgr) _t="3:4" ;;
864             status)   _t="1:1" ;;
865             *) die "Internal error - unsupported RPC service \"${_i}\"" ;;
866         esac
867
868         FAKE_RPCINFO_SERVICES="${FAKE_RPCINFO_SERVICES}${FAKE_RPCINFO_SERVICES:+ }${_i}:${_t}"
869     done
870 }
871
872
873 nfs_load_config ()
874 {
875     _etc="$CTDB_ETCDIR" # shortcut for readability
876     for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
877         if [ -r "$_c" ] ; then
878             . "$_c"
879             break
880         fi
881     done
882 }
883
884
885 # Set the required result for a particular RPC program having failed
886 # for a certain number of iterations.  This is probably still a work
887 # in progress.  Note that we could hook aggressively
888 # nfs_check_rpc_service() to try to implement this but we're better
889 # off testing nfs_check_rpc_service() using independent code...  even
890 # if it is incomplete and hacky.  So, if the 60.nfs eventscript
891 # changes and the tests start to fail then it may be due to this
892 # function being incomplete.
893 rpc_set_service_failure_response ()
894 {
895     _progname="$1"
896     # The number of failures defaults to the iteration number.  This
897     # will be true when we fail from the 1st iteration... but we need
898     # the flexibility to set the number of failures.
899     _numfails="${2:-${iteration}}"
900
901     nfs_load_config
902
903     # A handy newline.  :-)
904     _nl="
905 "
906
907     # Default
908     ok_null
909
910     _file=$(ls "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9]."${_progname}.check")
911     [ -r "$_file" ] || die "RPC check file \"$_file\" does not exist or is not unique"
912
913     while read _op _li _actions ; do
914         # Skip comments
915         case "$_op" in
916             \#*) continue ;;
917         esac
918
919         _hit=false
920         if [ "$_op" != "%" ] ; then
921             if [ $_numfails $_op $_li ] ; then
922                 _hit=true
923             fi
924         else
925             if [ $(($_numfails $_op $_li)) -eq 0 ] ; then
926                 _hit=true
927             fi
928         fi
929         if $_hit ; then
930             _out=""
931             _rc=0
932             for _action in $_actions ; do
933                 case "$_action" in
934                     verbose)
935                         _ver=1
936                         _pn="$_progname"
937                         case "$_progname" in
938                             nfsd) _ver=3 ; _pn="nfs" ;;
939                             lockd) _ver=4 ; _pn="nlockmgr" ;;
940                             statd) _pn="status" ;;
941                         esac
942                         _out="\
943 ERROR: $_pn failed RPC check:
944 rpcinfo: RPC: Program not registered
945 program $_pn version $_ver is not available"
946                         ;;
947                     restart*)
948                         _p="rpc.${_progname}"
949                         case "$_action" in
950                             *:b) _bg="&" ;;
951                             *)   _bg=""  ;;
952                         esac
953                         case "$_progname" in
954                             nfsd)
955                                 _t="\
956 Trying to restart NFS service"
957
958                                 if [ -n "$CTDB_NFS_DUMP_STUCK_THREADS" ] ; then
959                                     for _pid in $FAKE_NFSD_THREAD_PIDS ; do
960                                         _t="\
961 $_t
962 ${_bg}Stack trace for nfsd[${_pid}]:
963 ${_bg}[<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff"
964                                     done
965                                 fi
966
967                                 _t="\
968 ${_t}
969 ${_bg}Starting nfslock: OK
970 ${_bg}Starting nfs: OK"
971                                 ;;
972                             lockd)
973                                 _t="\
974 Trying to restart lock manager service
975 ${_bg}Starting nfslock: OK"
976                                 ;;
977                             *)
978                                 _t="Trying to restart $_progname [${_p}]"
979                                 if [ -n "$CTDB_NFS_DUMP_STUCK_THREADS" ] ; then
980                                     for _pid in $FAKE_RPC_THREAD_PIDS ; do
981                                         _t="\
982 $_t
983 Stack trace for ${_p}[${_pid}]:
984 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff"
985                                     done
986                                 fi
987                         esac
988                         _out="${_out}${_out:+${_nl}}${_t}"
989                         ;;
990                     unhealthy)
991                         _rc=1
992                 esac
993             done
994             required_result $_rc "$_out"
995             return
996         fi
997     done <"$_file"
998 }
999
1000 ######################################################################
1001
1002 # VSFTPD fakery
1003
1004 setup_vsftpd ()
1005 {
1006     service_name="vsftpd"
1007
1008     if [ "$1" != "down" ] ; then
1009         die "setup_vsftpd up not implemented!!!"
1010     else
1011         debug "Setting up VSFTPD environment: service down, not managed by CTDB"
1012
1013         eventscript_call ctdb_service_unmanaged
1014         service vsftpd force-stopped
1015
1016         export CTDB_MANAGED_SERVICES="foo"
1017         unset CTDB_MANAGES_VSFTPD
1018     fi
1019 }
1020
1021 ######################################################################
1022
1023 # HTTPD fakery
1024
1025 setup_httpd ()
1026 {
1027     if [ "$1" != "down" ] ; then
1028         die "setup_httpd up not implemented!!!"
1029     else
1030         debug "Setting up HTTPD environment: service down, not managed by CTDB"
1031
1032         for service_name in "apache2" "httpd" ; do
1033             eventscript_call ctdb_service_unmanaged
1034             service "$service_name" force-stopped
1035         done
1036
1037         export CTDB_MANAGED_SERVICES="foo"
1038         unset CTDB_MANAGES_HTTPD
1039     fi
1040 }
1041
1042 ######################################################################
1043
1044 # multipathd fakery
1045
1046 setup_multipathd ()
1047 {
1048     for i ; do
1049         case "$i" in
1050             \!*)
1051                 _t="${i#!}"
1052                 echo "Marking ${_t} as having no active paths"
1053                 FAKE_MULTIPATH_FAILURES="${FAKE_MULTIPATH_FAILURES}${FAKE_MULTIPATH+FAILURES:+ }${_t}"
1054                 ;;
1055             *)
1056                 _t="$i"         
1057         esac
1058         CTDB_MONITOR_MPDEVICES="${CTDB_MONITOR_MPDEVICES}${CTDB_MONITOR_MPDEVICES:+ }${_t}"
1059     done
1060
1061     export CTDB_MONITOR_MPDEVICES FAKE_MULTIPATH_FAILURES
1062     export FAKE_SLEEP_FORCE=0.1
1063 }
1064
1065 ######################################################################
1066
1067 # Result and test functions
1068
1069 # Set some globals and print the summary.
1070 define_test ()
1071 {
1072     desc="$1"
1073
1074     _f=$(basename "$0" ".sh")
1075
1076     # Remaining format should be NN.service.event.NNN or NN.service.NNN:
1077     _num="${_f##*.}"
1078     _f="${_f%.*}"
1079
1080     case "$_f" in
1081         [0-9][0-9].*.*)
1082             script="${_f%.*}"
1083             event="${_f##*.}"
1084             script_dir="${CTDB_BASE}/events.d"
1085             ;;
1086         [0-9][0-9].*)
1087             script="$_f"
1088             unset event
1089             script_dir="${CTDB_BASE}/events.d"
1090             ;;
1091         *.*)
1092             script="${_f%.*}"
1093             event="${_f##*.}"
1094             script_dir="${CTDB_BASE}"
1095             ;;
1096         *)
1097             script="${_f%.*}"
1098             unset event
1099             script_dir="${CTDB_BASE}"
1100     esac
1101
1102     [ -x "${script_dir}/${script}" ] || \
1103         die "Internal error - unable to find script \"${script_dir}/${script}\""
1104
1105     printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
1106 }
1107
1108 _extra_header ()
1109 {
1110     cat <<EOF
1111 CTDB_BASE="$CTDB_BASE"
1112 CTDB_ETCDIR="$CTDB_ETCDIR"
1113 ctdb client is "$(which ctdb)"
1114 ip command is "$(which ip)"
1115 EOF
1116 }
1117
1118 # Run an eventscript once.  The test passes if the return code and
1119 # output match those required.
1120
1121 # Any args are passed to the eventscript.
1122
1123 simple_test ()
1124 {
1125     [ -n "$event" ] || die 'simple_test: $event not set'
1126
1127     _extra_header=$(_extra_header)
1128
1129     echo "Running script \"$script $event${1:+ }$*\""
1130     _shell=""
1131     if $TEST_COMMAND_TRACE ; then
1132         _shell="sh -x"
1133     else
1134         _shell="sh"
1135     fi
1136     _out=$($_shell "${script_dir}/${script}" "$event" "$@" 2>&1)
1137
1138     result_check "$_extra_header"
1139 }
1140
1141 simple_test_event ()
1142 {
1143     # If something has previously failed then don't continue.
1144     : ${_passed:=true}
1145     $_passed || return 1
1146
1147     event="$1" ; shift
1148     echo "=================================================="
1149     simple_test "$@"
1150 }
1151
1152 simple_test_command ()
1153 {
1154     # If something has previously failed then don't continue.
1155     : ${_passed:=true}
1156     $_passed || return 1
1157
1158     echo "=================================================="
1159     echo "Running command \"$*\""
1160     _out=$("$@" 2>&1)
1161
1162     result_check
1163 }
1164
1165 # Run an eventscript iteratively.
1166 # - 1st argument is the number of iterations.
1167 # - 2nd argument is something to eval to do setup for every iteration.
1168 #   The easiest thing to do here is to define a function and pass it
1169 #   here.
1170 # - Subsequent arguments come in pairs: an iteration number and
1171 #   something to eval for that iteration.  Each time an iteration
1172 #   number is matched the associated argument is given to eval after
1173 #   the default setup is done.  The iteration numbers need to be given
1174 #   in ascending order.
1175 #
1176 # Some optional args can be given *before* these, surrounded by extra
1177 # "--" args.  These args are passed to the eventscript.  Quoting is
1178 # lost.
1179 #
1180 # One use of the 2nd and further arguments is to call
1181 # required_result() to change what is expected of a particular
1182 # iteration.
1183 iterate_test ()
1184 {
1185     [ -n "$event" ] || die 'simple_test: $event not set'
1186
1187     args=""
1188     if [ "$1" = "--" ] ; then
1189         shift
1190         while [ "$1" != "--" ] ; do
1191             args="${args}${args:+ }$1"
1192             shift
1193         done
1194         shift
1195     fi
1196
1197     _repeats="$1"
1198     _setup_default="$2"
1199     shift 2
1200
1201     echo "Running $_repeats iterations of \"$script $event\" $args"
1202
1203     _result=true
1204
1205     for iteration in $(seq 1 $_repeats) ; do
1206         # This is inefficient because the iteration-specific setup
1207         # might completely replace the default one.  However, running
1208         # the default is good because it allows you to revert to a
1209         # particular result without needing to specify it explicitly.
1210         eval $_setup_default
1211         if [ $iteration = "$1" ] ; then
1212             eval $2
1213             shift 2
1214         fi
1215
1216         _shell=""
1217         if $TEST_COMMAND_TRACE ; then
1218             _shell="sh -x"
1219         else
1220             _shell="sh"
1221         fi
1222         _out=$($_shell "${script_dir}/${script}" "$event" $args 2>&1)
1223         _rc=$?
1224
1225         _fout=$(echo "$_out" | result_filter)
1226
1227         if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
1228             _passed=true
1229         else
1230             _passed=false
1231             _result=false
1232         fi
1233
1234         result_print "$_passed" "$_out" "$_rc" "Iteration $iteration"
1235     done
1236
1237     result_footer "$_result" "$(_extra_header)"
1238 }