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