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