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