ctdb-tests: If bin/ isn't in ctdb/ then look one level higher
[samba.git] / ctdb / tests / scripts / common.sh
1 # Hey Emacs, this is a -*- shell-script -*- !!!  :-)
2
3 # Common variables and functions for all CTDB tests.
4
5 # Print a message and exit.
6 die ()
7 {
8         echo "$1" >&2 ; exit ${2:-1}
9 }
10
11 # This expands the most probable problem cases like "." and "..".
12 TEST_SUBDIR=$(dirname "$0")
13 if [ $(dirname "$TEST_SUBDIR") = "." ] ; then
14         TEST_SUBDIR=$(cd "$TEST_SUBDIR" ; pwd)
15 fi
16
17 # If we are running from within the source tree then, depending on the
18 # tests that we're running, we may need to add the top level bin/ and
19 # tools/ subdirectories to $PATH.  In this case, sanity check that
20 # run_tests.sh is in the expected place.  If the tests are installed
21 # then sanity check that TEST_BIN_DIR is set.
22 if $CTDB_TESTS_ARE_INSTALLED ; then
23         if [ -z "$TEST_BIN_DIR" ] ; then
24                 die "CTDB_TESTS_ARE_INSTALLED but TEST_BIN_DIR not set"
25         fi
26
27         _test_bin_dir="$TEST_BIN_DIR"
28 else
29         if [ ! -f "${CTDB_TEST_DIR}/run_tests.sh" ] ; then
30                 die "Tests not installed but can't find run_tests.sh"
31         fi
32
33         ctdb_dir=$(dirname "$CTDB_TEST_DIR")
34
35         _tools_dir="${ctdb_dir}/tools"
36         if [ -d "$_tools_dir" ] ; then
37                 PATH="${_tools_dir}:$PATH"
38         fi
39
40         top_dir=$(cd -P "$ctdb_dir" && echo "$PWD") # real path
41
42         _test_bin_dir="${top_dir}/bin"
43         if [ ! -d "$_test_bin_dir" ] ; then
44                 top_dir=$(dirname "$top_dir")
45                 _test_bin_dir="${top_dir}/bin"
46         fi
47 fi
48
49 if [ -d "$_test_bin_dir" ] ; then
50         PATH="${_test_bin_dir}:$PATH"
51 fi
52
53 . "${TEST_SCRIPTS_DIR}/script_install_paths.sh"
54
55 # Wait until either timeout expires or command succeeds.  The command
56 # will be tried once per second, unless timeout has format T/I, where
57 # I is the recheck interval.
58 wait_until ()
59 {
60     local timeout="$1" ; shift # "$@" is the command...
61
62     local interval=1
63     case "$timeout" in
64         */*)
65             interval="${timeout#*/}"
66             timeout="${timeout%/*}"
67     esac
68
69     local negate=false
70     if [ "$1" = "!" ] ; then
71         negate=true
72         shift
73     fi
74
75     echo -n "<${timeout}|"
76     local t=$timeout
77     while [ $t -gt 0 ] ; do
78         local rc=0
79         "$@" || rc=$?
80         if { ! $negate && [ $rc -eq 0 ] ; } || \
81             { $negate && [ $rc -ne 0 ] ; } ; then
82             echo "|$(($timeout - $t))|"
83             echo "OK"
84             return 0
85         fi
86         local i
87         for i in $(seq 1 $interval) ; do
88             echo -n .
89         done
90         t=$(($t - $interval))
91         sleep $interval
92     done
93
94     echo "*TIMEOUT*"
95
96     return 1
97 }
98
99 # setup_ctdb_base <parent> <subdir> [item-to-copy]...
100 setup_ctdb_base ()
101 {
102         [ $# -ge 2 ] || die "usage: setup_ctdb_base <parent> <subdir> [item]..."
103         # If empty arguments are passed then we attempt to remove /
104         # (i.e. the root directory) below
105         [ -n "$1" -a -n "$2" ] || \
106                 die "usage: setup_ctdb_base <parent> <subdir> [item]..."
107
108         _parent="$1"
109         _subdir="$2"
110
111         # Other arguments are files/directories to copy
112         shift 2
113
114         export CTDB_BASE="${_parent}/${_subdir}"
115         if [ -d "$CTDB_BASE" ] ; then
116                 rm -r "$CTDB_BASE"
117         fi
118         mkdir -p "$CTDB_BASE" || die "Failed to create CTDB_BASE=$CTDB_BASE"
119         mkdir -p "${CTDB_BASE}/run" || die "Failed to create ${CTDB_BASE}/run"
120         mkdir -p "${CTDB_BASE}/var" || die "Failed to create ${CTDB_BASE}/var"
121
122         for _i ; do
123                 cp -pr "${CTDB_SCRIPTS_BASE}/${_i}" "${CTDB_BASE}/"
124         done
125
126         for _i in "${TEST_SUBDIR}/etc-ctdb/"* ; do
127                 # No/empty etc-ctdb directory
128                 [ -e "$_i" ] || break
129
130                 cp -pr "$_i" "${CTDB_BASE}/"
131         done
132 }