3033c73a68b2c4bba16535e4271ff08e8e7cb201
[samba.git] / ctdb / tests / tool / scripts / local.sh
1 # Hey Emacs, this is a -*- shell-script -*- !!!  :-)
2
3 # Augment PATH with stubs/ directory.
4
5 if "$TEST_VERBOSE" ; then
6     debug () { echo "$@" ; }
7 else
8     debug () { : ; }
9 fi
10
11 ctdbd_socket="${TEST_VAR_DIR}/ctdbd.socket.$$"
12 ctdbd_pidfile="${TEST_VAR_DIR}/ctdbd.pid.$$"
13
14 define_test ()
15 {
16     _f=$(basename "$0" ".sh")
17
18     case "$_f" in
19         ctdb.*)
20             _cmd="${_f#ctdb.}"
21             _cmd="${_cmd%.*}" # Strip test number
22             export CTDB="ctdb --socket $ctdbd_socket"
23             export CTDB_DEBUGLEVEL=3
24             test_args="$_cmd"
25             ;;
26         *)
27             die "Unknown pattern for testcase \"$_f\""
28     esac
29
30     printf "%-28s - %s\n" "$_f" "$1"
31 }
32
33 cleanup_ctdbd ()
34 {
35         debug "Cleaning up fake ctdbd"
36
37         pid=$(cat "$ctdbd_pidfile" 2>/dev/null || echo)
38         if [ -n "$pid" ] ; then
39                 kill $pid || true
40                 rm -f "$ctdbd_pidfile"
41         fi
42         rm -f "$ctdbd_socket"
43 }
44
45 setup_ctdbd ()
46 {
47         debug "Setting up fake ctdbd"
48
49         cat | fake_ctdbd -s "$ctdbd_socket" -p "$ctdbd_pidfile"
50         #test_cleanup cleanup_ctdbd
51 }
52
53 setup_natgw ()
54 {
55         debug "Setting up NAT gateway"
56
57         # Use in-tree binaries if running against local daemons.
58         # Otherwise CTDB need to be installed on all nodes.
59         if [ -n "$ctdb_dir" -a -d "${ctdb_dir}/bin" ] ; then
60                 if [ -z "$CTDB_NATGW_HELPER" ] ; then
61                         export CTDB_NATGW_HELPER="${ctdb_dir}/tools/ctdb_natgw"
62                 fi
63                 # Only want to find functions file, so this is OK
64                 export CTDB_BASE="${ctdb_dir}/config"
65         fi
66
67         natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
68         mkdir -p "$natgw_config_dir"
69
70         # These will accumulate, 1 per test... but will be cleaned up
71         # at the end.
72         export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
73
74         cat >"$CTDB_NATGW_NODES"
75 }
76
77 setup_lvs ()
78 {
79         debug "Setting up LVS"
80
81         # Use in-tree binaries if running against local daemons.
82         # Otherwise CTDB need to be installed on all nodes.
83         if [ -n "$ctdb_dir" -a -d "${ctdb_dir}/bin" ] ; then
84                 if [ -z "$CTDB_LVS_HELPER" ] ; then
85                         export CTDB_LVS_HELPER="${ctdb_dir}/tools/ctdb_lvs"
86                 fi
87                 # Only want to find functions file, so this is OK
88                 export CTDB_BASE="${ctdb_dir}/config"
89         fi
90
91         lvs_config_dir="${TEST_VAR_DIR}/lvs_config"
92         mkdir -p "$lvs_config_dir"
93
94         # These will accumulate, 1 per test... but will be cleaned up
95         # at the end.
96         export CTDB_LVS_NODES=$(mktemp --tmpdir="$lvs_config_dir")
97
98         cat >"$CTDB_LVS_NODES"
99 }
100
101 setup_nodes ()
102 {
103     _pnn="$1"
104
105     _v="CTDB_NODES${_pnn:+_}${_pnn}"
106     debug "Setting up ${_v}"
107
108     # These will accumulate, 1 per test... but will be cleaned up at
109     # the end.
110     eval export "${_v}"=$(mktemp --tmpdir="$TEST_VAR_DIR")
111
112     eval _f="\${${_v}}"
113     cat >"$_f"
114
115     # You can't be too careful about what might be in the
116     # environment...  so clean up when setting the default variable.
117     if [ -z "$_pnn" ] ; then
118         _n=$(wc -l "$CTDB_NODES" | awk '{ print $1 }')
119         for _i in $(seq 0 $_n) ; do
120             eval unset "CTDB_NODES_${_i}"
121         done
122     fi
123 }
124
125 simple_test ()
126 {
127     setup_ctdbd
128     (unit_test $CTDB -d $CTDB_DEBUGLEVEL $test_args "$@")
129     status=$?
130     cleanup_ctdbd
131     [ $status -eq 0 ] || exit $status
132 }