281a2e23262c38a923ef3d29c04a6d136fd77ae2
[sfrench/samba-autobuild/.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=2
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         $VALGRIND fake_ctdbd -s "$ctdbd_socket" -p "$ctdbd_pidfile"
50         test_cleanup cleanup_ctdbd
51 }
52
53 ctdbd_getpid ()
54 {
55         cat "$ctdbd_pidfile"
56 }
57
58 setup_natgw ()
59 {
60         debug "Setting up NAT gateway"
61
62         # Use in-tree binaries if running against local daemons.
63         # Otherwise CTDB need to be installed on all nodes.
64         if [ -n "$ctdb_dir" -a -d "${ctdb_dir}/bin" ] ; then
65                 if [ -z "$CTDB_NATGW_HELPER" ] ; then
66                         export CTDB_NATGW_HELPER="${ctdb_dir}/tools/ctdb_natgw"
67                 fi
68                 # Only want to find functions file, so this is OK
69                 export CTDB_BASE="${ctdb_dir}/config"
70         fi
71
72         natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
73         mkdir -p "$natgw_config_dir"
74
75         export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
76         test_cleanup "rm -f $CTDB_NATGW_NODES"
77
78         cat >"$CTDB_NATGW_NODES"
79 }
80
81 setup_lvs ()
82 {
83         debug "Setting up LVS"
84
85         # Use in-tree binaries if running against local daemons.
86         # Otherwise CTDB need to be installed on all nodes.
87         if [ -n "$ctdb_dir" -a -d "${ctdb_dir}/bin" ] ; then
88                 if [ -z "$CTDB_LVS_HELPER" ] ; then
89                         export CTDB_LVS_HELPER="${ctdb_dir}/tools/ctdb_lvs"
90                 fi
91                 # Only want to find functions file, so this is OK
92                 export CTDB_BASE="${ctdb_dir}/config"
93         fi
94
95         lvs_config_dir="${TEST_VAR_DIR}/lvs_config"
96         mkdir -p "$lvs_config_dir"
97
98         export CTDB_LVS_NODES=$(mktemp --tmpdir="$lvs_config_dir")
99         test_cleanup "rm -f ${CTDB_LVS_NODES}"
100
101         cat >"$CTDB_LVS_NODES"
102 }
103
104 setup_nodes ()
105 {
106     _pnn="$1"
107
108     _v="CTDB_NODES${_pnn:+_}${_pnn}"
109     debug "Setting up ${_v}"
110
111     eval export "${_v}"=$(mktemp --tmpdir="$TEST_VAR_DIR")
112
113     eval _f="\${${_v}}"
114     test_cleanup "rm -f ${_f}"
115     cat >"$_f"
116
117     # You can't be too careful about what might be in the
118     # environment...  so clean up when setting the default variable.
119     if [ -z "$_pnn" ] ; then
120         _n=$(wc -l "$CTDB_NODES" | awk '{ print $1 }')
121         for _i in $(seq 0 $_n) ; do
122             eval unset "CTDB_NODES_${_i}"
123         done
124     fi
125 }
126
127 simple_test_other ()
128 {
129         (unit_test $CTDB -d $CTDB_DEBUGLEVEL "$@")
130         status=$?
131         [ $status -eq 0 ] || exit $status
132 }
133
134 simple_test ()
135 {
136         simple_test_other $test_args "$@"
137 }