ctdb-tests: Create statd-callout.sh
[samba.git] / ctdb / tests / eventscripts / scripts / local.sh
1 # Hey Emacs, this is a -*- shell-script -*- !!!  :-)
2
3 #
4 # Augment PATH with relevant stubs/ directories.
5 #
6
7 stubs_dir="${TEST_SUBDIR}/stubs"
8 [ -d "${stubs_dir}" ] || die "Failed to locate stubs/ subdirectory"
9
10 # Make the path absolute for tests that change directory
11 case "$stubs_dir" in
12 /*) : ;;
13 *) stubs_dir="${PWD}/${stubs_dir}" ;;
14 esac
15
16 # Use stubs as helpers
17 export CTDB_HELPER_BINDIR="$stubs_dir"
18
19 PATH="${stubs_dir}:${PATH}"
20
21
22 export CTDB="ctdb"
23
24 [ -n "$TEST_VAR_DIR" ] || die "TEST_VAR_DIR unset"
25 export EVENTSCRIPTS_TESTS_VAR_DIR="${TEST_VAR_DIR}/unit_eventscripts"
26 if [ -d "$EVENTSCRIPTS_TESTS_VAR_DIR" ] ; then
27         rm -r "$EVENTSCRIPTS_TESTS_VAR_DIR"
28 fi
29 mkdir -p "$EVENTSCRIPTS_TESTS_VAR_DIR"
30
31 export CTDB_LOGGING="file:${EVENTSCRIPTS_TESTS_VAR_DIR}/log.ctdb"
32 touch "${CTDB_LOGGING#file:}" || \
33     die "Unable to setup logging for \"$CTDB_LOGGING\""
34
35 if [ -d "${TEST_SUBDIR}/etc" ] ; then
36     cp -a "${TEST_SUBDIR}/etc" "$EVENTSCRIPTS_TESTS_VAR_DIR"
37     export CTDB_SYS_ETCDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/etc"
38 else
39     die "Unable to setup \$CTDB_SYS_ETCDIR"
40 fi
41
42 setup_ctdb_base "$EVENTSCRIPTS_TESTS_VAR_DIR" "etc-ctdb" \
43                 events.d \
44                 functions \
45                 nfs-checks.d \
46                 nfs-linux-kernel-callout \
47                 statd-callout
48
49 export FAKE_CTDB_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb"
50 mkdir -p "$FAKE_CTDB_STATE"
51
52 export FAKE_NETWORK_STATE="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-network-state"
53 mkdir -p "$FAKE_NETWORK_STATE"
54
55 ######################################################################
56
57 if "$TEST_VERBOSE" ; then
58         debug ()
59         {
60                 if [ -n "$1" ] ; then
61                         echo "$@" >&2
62                 else
63                         cat >&2
64                 fi
65         }
66 else
67         debug () { : ; }
68 fi
69
70 ######################################################################
71
72 # General setup fakery
73
74 setup_dbdir ()
75 {
76         export CTDB_DBDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/db"
77         export CTDB_DBDIR_PERSISTENT="${CTDB_DBDIR}/persistent"
78         export CTDB_DBDIR_STATE="${CTDB_DBDIR}/state"
79         mkdir -p "$CTDB_DBDIR_PERSISTENT"
80         mkdir -p "$CTDB_DBDIR_STATE"
81 }
82
83 setup_date ()
84 {
85         export FAKE_DATE_OUTPUT="$1"
86 }
87
88 setup_generic ()
89 {
90     setup_shares
91
92     export FAKE_PROC_NET_BONDING="$EVENTSCRIPTS_TESTS_VAR_DIR/proc-net-bonding"
93     mkdir -p "$FAKE_PROC_NET_BONDING"
94     rm -f "$FAKE_PROC_NET_BONDING"/*
95
96     export FAKE_TDBTOOL_SUPPORTS_CHECK="yes"
97     export FAKE_TDB_IS_OK
98
99     setup_dbdir
100     setup_date
101
102     export FAKE_TCP_LISTEN FAKE_NETSTAT_UNIX_LISTEN
103 }
104
105 tcp_port_down ()
106 {
107     for _i ; do
108         debug "Marking TCP port \"${_i}\" as not listening"
109         FAKE_TCP_LISTEN=$(echo "$FAKE_TCP_LISTEN" | sed -r -e "s@[[:space:]]*[\.0-9]+:${_i}@@g")
110     done
111 }
112
113 unix_socket_listening ()
114 {
115         _s="$1"
116
117         FAKE_NETSTAT_UNIX_LISTEN="${FAKE_NETSTAT_UNIX_LISTEN} ${_s}"
118 }
119
120 setup_shares ()
121 {
122         debug "Setting up shares (3 existing shares)"
123         # Create 3 fake shares/exports.
124         export FAKE_SHARES=""
125         for i in $(seq 1 3) ; do
126                 _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/share${i}"
127                 mkdir -p "$_s"
128                 FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
129         done
130 }
131
132 shares_missing ()
133 {
134         # Mark some shares as non-existent
135         _fmt="$1" ; shift
136
137         _out=""
138         _nl="
139 "
140
141         _n=1
142         for _i in $FAKE_SHARES ; do
143                 for _j ; do
144                         if [ $_n -ne "$_j" ] ; then
145                                 continue
146                         fi
147
148                         debug "Mark share $_n as missing share \"$_i\""
149                         rmdir "$_i"
150                         _t=$(printf "$_fmt" "${_i}")
151                         _out="${_out}${_out:+${_nl}}${_t}"
152                 done
153                 _n=$(($_n + 1))
154         done
155
156         echo "$_out"
157 }
158
159 _ethtool_setup ()
160 {
161         FAKE_ETHTOOL_LINK_DOWN="${FAKE_NETWORK_STATE}/ethtool-link-down"
162         export FAKE_ETHTOOL_LINK_DOWN
163         mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
164 }
165
166 ethtool_interfaces_down ()
167 {
168         _ethtool_setup
169
170         for _i ; do
171                 echo "Marking interface $_i DOWN for ethtool"
172                 touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
173         done
174 }
175
176 ethtool_interfaces_up ()
177 {
178         _ethtool_setup
179
180         for _i ; do
181                 echo "Marking interface $_i UP for ethtool"
182                 rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
183         done
184 }
185
186 dump_routes ()
187 {
188     echo "# ip rule show"
189     ip rule show
190
191     ip rule show |
192     while read _p _x _i _x _t ; do
193         # Remove trailing colon after priority/preference.
194         _p="${_p%:}"
195         # Only remove rules that match our priority/preference.
196         [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
197
198         echo "# ip route show table $_t"
199         ip route show table "$_t"
200     done
201 }
202
203 # Copied from 13.per_ip_routing for now... so this is lazy testing  :-(
204 ipv4_host_addr_to_net ()
205 {
206     _host="$1"
207     _maskbits="$2"
208
209     # Convert the host address to an unsigned long by splitting out
210     # the octets and doing the math.
211     _host_ul=0
212     for _o in $(export IFS="." ; echo $_host) ; do
213         _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
214     done
215
216     # Calculate the mask and apply it.
217     _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
218     _net_ul=$(( $_host_ul & $_mask_ul ))
219
220     # Now convert to a network address one byte at a time.
221     _net=""
222     for _o in $(seq 1 4) ; do
223         _net="$(($_net_ul & 255))${_net:+.}${_net}"
224         _net_ul=$(($_net_ul >> 8))
225     done
226
227     echo "${_net}/${_maskbits}"
228 }
229
230 ######################################################################
231
232 # CTDB fakery
233
234 # Evaluate an expression that probably calls functions or uses
235 # variables from the CTDB functions file.  This is used for test
236 # initialisation.
237 eventscript_call ()
238 {
239     (
240         . "$CTDB_BASE/functions"
241         "$@"
242     )
243 }
244
245 setup_numnodes ()
246 {
247         export FAKE_CTDB_NUMNODES="${1:-3}"
248         echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
249 }
250
251 # For now this creates the same public addresses each time.  However,
252 # it could be made more flexible.
253 setup_public_addresses ()
254 {
255         _f="${CTDB_BASE}/public_addresses"
256
257         echo "Setting up public addresses in ${_f}"
258         cat >"$_f" <<EOF
259 10.0.0.1/24 dev123
260 10.0.0.2/24 dev123
261 10.0.0.3/24 dev123
262 10.0.0.4/24 dev123
263 10.0.0.5/24 dev123
264 10.0.0.6/24 dev123
265 10.0.1.1/24 dev456
266 10.0.1.2/24 dev456
267 10.0.1.3/24 dev456
268 EOF
269
270     # Needed for IP allocation
271     setup_numnodes
272 }
273
274 # Need to cope with ctdb_get_pnn().  If a test changes PNN then it
275 # needs to be using a different state directory, otherwise the wrong
276 # PNN can already be cached in the state directory.
277 ctdb_set_pnn ()
278 {
279     export FAKE_CTDB_PNN="$1"
280     echo "Setting up PNN ${FAKE_CTDB_PNN}"
281
282     CTDB_SCRIPT_VARDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/scripts/${FAKE_CTDB_PNN}"
283     export CTDB_SCRIPT_VARDIR
284     mkdir -p "$CTDB_SCRIPT_VARDIR"
285 }
286
287 setup_ctdb ()
288 {
289     setup_generic
290
291     setup_public_addresses
292
293     export FAKE_CTDB_EXTRA_CONFIG="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-config.sh"
294     rm -f "$FAKE_CTDB_EXTRA_CONFIG"
295
296     export CTDB_PARTIALLY_ONLINE_INTERFACES
297
298     export FAKE_CTDB_TUNABLES_OK="MonitorInterval TDBMutexEnabled DatabaseHashSize"
299     export FAKE_CTDB_TUNABLES_OBSOLETE="EventScriptUnhealthyOnTimeout"
300 }
301
302 validate_percentage ()
303 {
304     case "$1" in
305         [0-9]|[0-9][0-9]|100) return 0 ;;
306         *) echo "WARNING: ${1} is an invalid percentage${2:+\" in }${2}${2:+\"}"
307            return 1
308     esac
309 }
310
311 ctdb_get_interfaces ()
312 {
313     # The echo/subshell forces all the output onto 1 line.
314     echo $(ctdb ifaces -X | awk -F'|' 'FNR > 1 {print $2}')
315 }
316
317 ctdb_get_1_interface ()
318 {
319     _t=$(ctdb_get_interfaces)
320     echo ${_t%% *}
321 }
322
323 # Print all public addresses as: interface IP maskbits
324 # Each line is suitable for passing to takeip/releaseip
325 ctdb_get_all_public_addresses ()
326 {
327     _f="${CTDB_BASE}/public_addresses"
328     while IFS="/$IFS" read _ip _maskbits _ifaces ; do
329         echo "$_ifaces $_ip $_maskbits"
330     done <"$_f"
331 }
332
333 # Print public addresses on this node as: interface IP maskbits
334 # Each line is suitable for passing to takeip/releaseip
335 ctdb_get_my_public_addresses ()
336 {
337     ctdb ip -v -X | {
338         read _x # skip header line
339
340         while IFS="|" read _x _ip _x _iface _x ; do
341             [ -n "$_iface" ] || continue
342             while IFS="/$IFS" read _i _maskbits _x ; do
343                 if [ "$_ip" = "$_i" ] ; then
344                     echo $_iface $_ip $_maskbits
345                     break
346                 fi
347             done <"${CTDB_BASE}/public_addresses"
348         done
349     }
350 }
351
352 # Prints the 1st public address as: interface IP maskbits
353 # This is suitable for passing to takeip/releaseip
354 ctdb_get_1_public_address ()
355 {
356     ctdb_get_my_public_addresses | { head -n 1 ; cat >/dev/null ; }
357 }
358
359 ctdb_not_implemented ()
360 {
361     export CTDB_NOT_IMPLEMENTED="$1"
362     ctdb_not_implemented="\
363 DEBUG: ctdb: command \"$1\" not implemented in stub"
364 }
365
366 # Check the routes against those that are expected.  $1 is the number
367 # of assigned IPs to use (<num>, all), defaulting to 1.  If $2 is
368 # "default" then expect default routes to have been added.
369 check_routes ()
370 {
371     _num_ips="${1:-1}"
372     _should_add_default="$2"
373
374     _policy_rules=""
375     _policy_routes=""
376
377     ctdb_get_my_public_addresses |
378     if [ "$_num_ips" = "all" ] ; then
379         cat
380     else
381         { head -n "$_num_ips" ; cat >/dev/null ; }
382     fi | {
383         while read _dev _ip _bits ; do
384             _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
385             _gw="${_net%.*}.254" # a dumb, calculated default
386
387             _policy_rules="${_policy_rules}
388 ${CTDB_PER_IP_ROUTING_RULE_PREF}:       from $_ip lookup ctdb.$_ip "
389             _policy_routes="${_policy_routes}
390 # ip route show table ctdb.$_ip
391 $_net dev $_dev  scope link "
392
393             if [ "$_should_add_default" = "default" ] ; then
394                 _policy_routes="${_policy_routes}
395 default via $_gw dev $_dev "
396             fi
397         done
398
399         ok <<EOF
400 # ip rule show
401 0:      from all lookup local ${_policy_rules}
402 32766:  from all lookup main 
403 32767:  from all lookup default ${_policy_routes}
404 EOF
405
406         simple_test_command dump_routes
407     } || test_fail
408 }
409
410 ######################################################################
411
412 setup_nfs_ganesha ()
413 {
414     setup_nfs "$@"
415     export CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-ganesha-callout"
416     if [ "$1" != "down" ] ; then
417         export CTDB_MANAGES_NFS="yes"
418     fi
419
420     export CTDB_NFS_SKIP_SHARE_CHECK="yes"
421 }
422
423
424 nfs_load_config ()
425 {
426     _etc="$CTDB_SYS_ETCDIR" # shortcut for readability
427     for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
428         if [ -r "$_c" ] ; then
429             . "$_c"
430             break
431         fi
432     done
433 }
434
435 program_stack_trace ()
436 {
437         _prog="$1"
438         _pid="$2"
439
440         cat <<EOF
441 Stack trace for ${_prog}[${_pid}]:
442 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff
443 EOF
444 }
445
446 ######################################################################
447
448 # Result and test functions
449
450 # Set some globals and print the summary.
451 define_test ()
452 {
453     desc="$1"
454
455     _f=$(basename "$0" ".sh")
456
457     # Remaining format should be NN.service.event.NNN or NN.service.NNN:
458     _num="${_f##*.}"
459     _f="${_f%.*}"
460
461     case "$_f" in
462         [0-9][0-9].*.*)
463             script="${_f%.*}"
464             event="${_f##*.}"
465             script_dir="${CTDB_BASE}/events.d"
466             ;;
467         [0-9][0-9].*)
468             script="$_f"
469             unset event
470             script_dir="${CTDB_BASE}/events.d"
471             ;;
472         *.*)
473             script="${_f%.*}"
474             event="${_f##*.}"
475             script_dir="${CTDB_BASE}"
476             ;;
477         *)
478             script="${_f%.*}"
479             unset event
480             script_dir="${CTDB_BASE}"
481     esac
482
483     [ -r "${script_dir}/${script}" ] || \
484         die "Internal error - unable to find script \"${script_dir}/${script}\""
485
486     printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
487
488     _f="${TEST_SUBDIR}/scripts/${script}.sh"
489     if [ -r "$_f" ] ; then
490             . "$_f"
491     fi
492
493     ctdb_set_pnn 0
494 }
495
496 # Run an eventscript once.  The test passes if the return code and
497 # output match those required.
498
499 # Any args are passed to the eventscript.
500
501 simple_test ()
502 {
503     [ -n "$event" ] || die 'simple_test: $event not set'
504
505     args="$@"
506
507     test_header ()
508     {
509         echo "Running script \"$script $event${args:+ }$args\""
510     }
511
512     extra_header ()
513     {
514         cat <<EOF
515
516 ##################################################
517 CTDB_BASE="$CTDB_BASE"
518 CTDB_SYS_ETCDIR="$CTDB_SYS_ETCDIR"
519 ctdb client is "$(which ctdb)"
520 ip command is "$(which ip)"
521 EOF
522     }
523
524     script_test "${script_dir}/${script}" "$event" "$@"
525
526     reset_test_header
527     reset_extra_header
528 }
529
530 simple_test_event ()
531 {
532     # If something has previously failed then don't continue.
533     : ${_passed:=true}
534     $_passed || return 1
535
536     event="$1" ; shift
537     echo "=================================================="
538     simple_test "$@"
539 }
540
541 simple_test_command ()
542 {
543     unit_test "$@"
544 }