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