101769bf9e2bcba852da883e84773cd77fdcc89c
[metze/samba/wip.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 # Default is to use script name with ".options" appended.  With
75 # arguments, this can specify an alternate script name (and
76 # component).
77 setup_script_options ()
78 {
79         if [ $# -eq 2 ] ; then
80                 _script="$2"
81         elif [ $# -eq 0  ] ; then
82                 _script=""
83         else
84                 die "usage: setup_script_options [ component script ]"
85         fi
86
87         if [ -n "$_script" ] ; then
88                 _options="${CTDB_BASE}/events.d/${_script}.options"
89         else
90                 _options="${script_dir}/${script%.script}.options"
91         fi
92
93         cat >>"$_options"
94
95         # Source the options so that tests can use the variables
96         . "$_options"
97 }
98
99 setup_dbdir ()
100 {
101         export CTDB_DBDIR_BASE="${EVENTSCRIPTS_TESTS_VAR_DIR}/db"
102         export CTDB_DBDIR="${CTDB_DBDIR_BASE}/volatile"
103         export CTDB_DBDIR_PERSISTENT="${CTDB_DBDIR_BASE}/persistent"
104         export CTDB_DBDIR_STATE="${CTDB_DBDIR_BASE}/state"
105         mkdir -p "$CTDB_DBDIR"
106         mkdir -p "$CTDB_DBDIR_PERSISTENT"
107         mkdir -p "$CTDB_DBDIR_STATE"
108 }
109
110 setup_date ()
111 {
112         export FAKE_DATE_OUTPUT="$1"
113 }
114
115 setup_tcp_listen ()
116 {
117         export FAKE_TCP_LISTEN="$*"
118 }
119
120 tcp_port_listening ()
121 {
122         for _i ; do
123                    FAKE_TCP_LISTEN="${FAKE_TCP_LISTEN} ${_i}"
124         done
125 }
126
127 tcp_port_down ()
128 {
129         _port="$1"
130         debug "Marking TCP port \"${_port}\" as not listening"
131
132         _t=""
133         for _i in $FAKE_TCP_LISTEN ; do
134                 if [ "$_i" = "$_port" ] ; then
135                         continue
136                 fi
137                 _t="${_t} ${_i}"
138         done
139
140         FAKE_TCP_LISTEN="$_t"
141 }
142
143 setup_unix_listen ()
144 {
145         export FAKE_NETSTAT_UNIX_LISTEN="$*"
146 }
147
148 unix_socket_listening ()
149 {
150         _s="$1"
151
152         FAKE_NETSTAT_UNIX_LISTEN="${FAKE_NETSTAT_UNIX_LISTEN} ${_s}"
153 }
154
155 setup_shares ()
156 {
157         debug "Setting up shares (3 existing shares)"
158         # Create 3 fake shares/exports.
159         export FAKE_SHARES=""
160         for i in $(seq 1 3) ; do
161                 _s="${EVENTSCRIPTS_TESTS_VAR_DIR}/shares/share${i}"
162                 mkdir -p "$_s"
163                 FAKE_SHARES="${FAKE_SHARES}${FAKE_SHARES:+ }${_s}"
164         done
165 }
166
167 shares_missing ()
168 {
169         # Mark some shares as non-existent
170         _fmt="$1" ; shift
171
172         _out=""
173         _nl="
174 "
175
176         _n=1
177         for _i in $FAKE_SHARES ; do
178                 for _j ; do
179                         if [ $_n -ne "$_j" ] ; then
180                                 continue
181                         fi
182
183                         debug "Mark share $_n as missing share \"$_i\""
184                         rmdir "$_i"
185                         _t=$(printf "$_fmt" "${_i}")
186                         _out="${_out}${_out:+${_nl}}${_t}"
187                 done
188                 _n=$(($_n + 1))
189         done
190
191         echo "$_out"
192 }
193
194 _ethtool_setup ()
195 {
196         FAKE_ETHTOOL_LINK_DOWN="${FAKE_NETWORK_STATE}/ethtool-link-down"
197         export FAKE_ETHTOOL_LINK_DOWN
198         mkdir -p "$FAKE_ETHTOOL_LINK_DOWN"
199 }
200
201 ethtool_interfaces_down ()
202 {
203         _ethtool_setup
204
205         for _i ; do
206                 echo "Marking interface $_i DOWN for ethtool"
207                 touch "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
208         done
209 }
210
211 ethtool_interfaces_up ()
212 {
213         _ethtool_setup
214
215         for _i ; do
216                 echo "Marking interface $_i UP for ethtool"
217                 rm -f "${FAKE_ETHTOOL_LINK_DOWN}/${_i}"
218         done
219 }
220
221 dump_routes ()
222 {
223     echo "# ip rule show"
224     ip rule show
225
226     ip rule show |
227     while read _p _x _i _x _t ; do
228         # Remove trailing colon after priority/preference.
229         _p="${_p%:}"
230         # Only remove rules that match our priority/preference.
231         [ "$CTDB_PER_IP_ROUTING_RULE_PREF" = "$_p" ] || continue
232
233         echo "# ip route show table $_t"
234         ip route show table "$_t"
235     done
236 }
237
238 # Copied from 13.per_ip_routing for now... so this is lazy testing  :-(
239 ipv4_host_addr_to_net ()
240 {
241     _host="$1"
242     _maskbits="$2"
243
244     # Convert the host address to an unsigned long by splitting out
245     # the octets and doing the math.
246     _host_ul=0
247     for _o in $(export IFS="." ; echo $_host) ; do
248         _host_ul=$(( ($_host_ul << 8) + $_o)) # work around Emacs color bug
249     done
250
251     # Calculate the mask and apply it.
252     _mask_ul=$(( 0xffffffff << (32 - $_maskbits) ))
253     _net_ul=$(( $_host_ul & $_mask_ul ))
254
255     # Now convert to a network address one byte at a time.
256     _net=""
257     for _o in $(seq 1 4) ; do
258         _net="$(($_net_ul & 255))${_net:+.}${_net}"
259         _net_ul=$(($_net_ul >> 8))
260     done
261
262     echo "${_net}/${_maskbits}"
263 }
264
265 ######################################################################
266
267 # CTDB fakery
268
269 setup_numnodes ()
270 {
271         export FAKE_CTDB_NUMNODES="${1:-3}"
272         echo "Setting up CTDB with ${FAKE_CTDB_NUMNODES} fake nodes"
273 }
274
275 # For now this creates the same public addresses each time.  However,
276 # it could be made more flexible.
277 setup_public_addresses ()
278 {
279         _f="${CTDB_BASE}/public_addresses"
280
281         echo "Setting up public addresses in ${_f}"
282         cat >"$_f" <<EOF
283 10.0.0.1/24 dev123
284 10.0.0.2/24 dev123
285 10.0.0.3/24 dev123
286 10.0.0.4/24 dev123
287 10.0.0.5/24 dev123
288 10.0.0.6/24 dev123
289 10.0.1.1/24 dev456
290 10.0.1.2/24 dev456
291 10.0.1.3/24 dev456
292 EOF
293
294     # Needed for IP allocation
295     setup_numnodes
296 }
297
298 # Need to cope with ctdb_get_pnn().  If a test changes PNN then it
299 # needs to be using a different state directory, otherwise the wrong
300 # PNN can already be cached in the state directory.
301 ctdb_set_pnn ()
302 {
303     export FAKE_CTDB_PNN="$1"
304     echo "Setting up PNN ${FAKE_CTDB_PNN}"
305
306     CTDB_SCRIPT_VARDIR="${EVENTSCRIPTS_TESTS_VAR_DIR}/scripts/${FAKE_CTDB_PNN}"
307     export CTDB_SCRIPT_VARDIR
308     mkdir -p "$CTDB_SCRIPT_VARDIR"
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 # Check the routes against those that are expected.  $1 is the number
360 # of assigned IPs to use (<num>, all), defaulting to 1.  If $2 is
361 # "default" then expect default routes to have been added.
362 check_routes ()
363 {
364     _num_ips="${1:-1}"
365     _should_add_default="$2"
366
367     _policy_rules=""
368     _policy_routes=""
369
370     ctdb_get_my_public_addresses |
371     if [ "$_num_ips" = "all" ] ; then
372         cat
373     else
374         { head -n "$_num_ips" ; cat >/dev/null ; }
375     fi | {
376         while read _dev _ip _bits ; do
377             _net=$(ipv4_host_addr_to_net "$_ip" "$_bits")
378             _gw="${_net%.*}.254" # a dumb, calculated default
379
380             _policy_rules="${_policy_rules}
381 ${CTDB_PER_IP_ROUTING_RULE_PREF}:       from $_ip lookup ctdb.$_ip "
382             _policy_routes="${_policy_routes}
383 # ip route show table ctdb.$_ip
384 $_net dev $_dev  scope link "
385
386             if [ "$_should_add_default" = "default" ] ; then
387                 _policy_routes="${_policy_routes}
388 default via $_gw dev $_dev "
389             fi
390         done
391
392         ok <<EOF
393 # ip rule show
394 0:      from all lookup local ${_policy_rules}
395 32766:  from all lookup main 
396 32767:  from all lookup default ${_policy_routes}
397 EOF
398
399         simple_test_command dump_routes
400     } || test_fail
401 }
402
403 ######################################################################
404
405
406 nfs_load_config ()
407 {
408     _etc="$CTDB_SYS_ETCDIR" # shortcut for readability
409     for _c in "$_etc/sysconfig/nfs" "$_etc/default/nfs" "$_etc/ctdb/sysconfig/nfs" ; do
410         if [ -r "$_c" ] ; then
411             . "$_c"
412             break
413         fi
414     done
415 }
416
417 program_stack_trace ()
418 {
419         _prog="$1"
420         _pid="$2"
421
422         cat <<EOF
423 Stack trace for ${_prog}[${_pid}]:
424 [<ffffffff87654321>] fake_stack_trace_for_pid_${_pid}/stack+0x0/0xff
425 EOF
426 }
427
428 ######################################################################
429
430 # Result and test functions
431
432
433 ############################################################
434
435 setup ()
436 {
437         die "setup() is not defined"
438 }
439
440 # Set some globals and print the summary.
441 define_test ()
442 {
443     desc="$1"
444
445     _f=$(basename "$0" ".sh")
446
447     # Remaining format should be NN.service.event.NNN or NN.service.NNN:
448     _num="${_f##*.}"
449     _f="${_f%.*}"
450
451     case "$_f" in
452         [0-9][0-9].*.*)
453             script="${_f%.*}"
454             event="${_f##*.}"
455             script_dir="${CTDB_BASE}/events.d"
456             ;;
457         [0-9][0-9].*)
458             script="$_f"
459             unset event
460             script_dir="${CTDB_BASE}/events.d"
461             ;;
462         *.*)
463             script="${_f%.*}"
464             event="${_f##*.}"
465             script_dir="${CTDB_BASE}"
466             ;;
467         *)
468             script="${_f%.*}"
469             unset event
470             script_dir="${CTDB_BASE}"
471     esac
472
473     [ -r "${script_dir}/${script}" ] || \
474         die "Internal error - unable to find script \"${script_dir}/${script}\""
475
476     printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
477
478     _f="${TEST_SUBDIR}/scripts/${script}.sh"
479     if [ -r "$_f" ] ; then
480             . "$_f"
481     fi
482
483     ctdb_set_pnn 0
484 }
485
486 # Run an eventscript once.  The test passes if the return code and
487 # output match those required.
488
489 # Any args are passed to the eventscript.
490
491 simple_test ()
492 {
493     [ -n "$event" ] || die 'simple_test: $event not set'
494
495     args="$@"
496
497     test_header ()
498     {
499         echo "Running script \"$script $event${args:+ }$args\""
500     }
501
502     extra_header ()
503     {
504         cat <<EOF
505
506 ##################################################
507 CTDB_BASE="$CTDB_BASE"
508 CTDB_SYS_ETCDIR="$CTDB_SYS_ETCDIR"
509 ctdb client is "$(which ctdb)"
510 ip command is "$(which ip)"
511 EOF
512     }
513
514     script_test "${script_dir}/${script}" "$event" "$@"
515
516     reset_test_header
517     reset_extra_header
518 }
519
520 simple_test_event ()
521 {
522     # If something has previously failed then don't continue.
523     : ${_passed:=true}
524     $_passed || return 1
525
526     event="$1" ; shift
527     echo "=================================================="
528     simple_test "$@"
529 }
530
531 simple_test_command ()
532 {
533     unit_test "$@"
534 }