ctdb-tests: Use $CTDB_SCRIPTS_TOOLS_BIN_DIR
[vlendec/samba-autobuild/.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.
21 if ! $CTDB_TESTS_ARE_INSTALLED ; then
22         if [ ! -f "${CTDB_TEST_DIR}/run_tests.sh" ] ; then
23                 die "Tests not installed but can't find run_tests.sh"
24         fi
25
26         ctdb_dir=$(dirname "$CTDB_TEST_DIR")
27
28         top_dir=$(cd -P "$ctdb_dir" && echo "$PWD") # real path
29         if [ ! -d "${top_dir}/bin" ] ; then
30                 top_dir=$(dirname "$top_dir")
31         fi
32 fi
33
34 . "${TEST_SCRIPTS_DIR}/script_install_paths.sh"
35
36 if [ -d "$CTDB_SCRIPTS_TOOLS_BIN_DIR" ] ; then
37         PATH="${CTDB_SCRIPTS_TOOLS_BIN_DIR}:${PATH}"
38 fi
39
40 if [ -d "$CTDB_SCRIPTS_TESTS_BINDIR" ] ; then
41         PATH="${CTDB_SCRIPTS_TESTS_BINDIR}:${PATH}"
42 fi
43
44 # Wait until either timeout expires or command succeeds.  The command
45 # will be tried once per second, unless timeout has format T/I, where
46 # I is the recheck interval.
47 wait_until ()
48 {
49     local timeout="$1" ; shift # "$@" is the command...
50
51     local interval=1
52     case "$timeout" in
53         */*)
54             interval="${timeout#*/}"
55             timeout="${timeout%/*}"
56     esac
57
58     local negate=false
59     if [ "$1" = "!" ] ; then
60         negate=true
61         shift
62     fi
63
64     echo -n "<${timeout}|"
65     local t=$timeout
66     while [ $t -gt 0 ] ; do
67         local rc=0
68         "$@" || rc=$?
69         if { ! $negate && [ $rc -eq 0 ] ; } || \
70             { $negate && [ $rc -ne 0 ] ; } ; then
71             echo "|$(($timeout - $t))|"
72             echo "OK"
73             return 0
74         fi
75         local i
76         for i in $(seq 1 $interval) ; do
77             echo -n .
78         done
79         t=$(($t - $interval))
80         sleep $interval
81     done
82
83     echo "*TIMEOUT*"
84
85     return 1
86 }
87
88 # setup_ctdb_base <parent> <subdir> [item-to-copy]...
89 setup_ctdb_base ()
90 {
91         [ $# -ge 2 ] || die "usage: setup_ctdb_base <parent> <subdir> [item]..."
92         # If empty arguments are passed then we attempt to remove /
93         # (i.e. the root directory) below
94         [ -n "$1" -a -n "$2" ] || \
95                 die "usage: setup_ctdb_base <parent> <subdir> [item]..."
96
97         _parent="$1"
98         _subdir="$2"
99
100         # Other arguments are files/directories to copy
101         shift 2
102
103         export CTDB_BASE="${_parent}/${_subdir}"
104         if [ -d "$CTDB_BASE" ] ; then
105                 rm -r "$CTDB_BASE"
106         fi
107         mkdir -p "$CTDB_BASE" || die "Failed to create CTDB_BASE=$CTDB_BASE"
108         mkdir -p "${CTDB_BASE}/run" || die "Failed to create ${CTDB_BASE}/run"
109         mkdir -p "${CTDB_BASE}/var" || die "Failed to create ${CTDB_BASE}/var"
110
111         for _i ; do
112                 cp -pr "${CTDB_SCRIPTS_BASE}/${_i}" "${CTDB_BASE}/"
113         done
114
115         for _i in "${TEST_SUBDIR}/etc-ctdb/"* ; do
116                 # No/empty etc-ctdb directory
117                 [ -e "$_i" ] || break
118
119                 cp -pr "$_i" "${CTDB_BASE}/"
120         done
121 }