winbind: Fix a memleak
[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
6 # Commands on different platforms may quote or sort things differently
7 # without this
8 export LANG=C
9
10 # Print a message and exit.
11 die ()
12 {
13         echo "$1" >&2 ; exit "${2:-1}"
14 }
15
16 . "${TEST_SCRIPTS_DIR}/script_install_paths.sh"
17
18 if [ -d "$CTDB_SCRIPTS_TOOLS_BIN_DIR" ] ; then
19         PATH="${CTDB_SCRIPTS_TOOLS_BIN_DIR}:${PATH}"
20 fi
21
22 if [ -d "$CTDB_SCRIPTS_TESTS_LIBEXEC_DIR" ] ; then
23         PATH="${CTDB_SCRIPTS_TESTS_LIBEXEC_DIR}:${PATH}"
24 fi
25
26 ctdb_test_error ()
27 {
28         if [ $# -gt 0 ] ; then
29                 echo "$*"
30         fi
31         exit 99
32 }
33
34 ctdb_test_fail ()
35 {
36         if [ $# -gt 0 ] ; then
37                 echo "$*"
38         fi
39         exit 1
40 }
41
42 ctdb_test_skip ()
43 {
44         if [ $# -gt 0 ] ; then
45                 echo "$*"
46         fi
47         exit 77
48 }
49
50 # "$@" is supported OSes
51 ctdb_test_check_supported_OS ()
52 {
53         _os=$(uname -s)
54         for _i ; do
55                 if [ "$_os" = "$_i" ] ; then
56                         return
57                 fi
58         done
59
60         ctdb_test_skip "This test is not supported on ${_os}"
61 }
62
63 # Wait until either timeout expires or command succeeds.  The command
64 # will be tried once per second, unless timeout has format T/I, where
65 # I is the recheck interval.
66 wait_until ()
67 {
68         _timeout="$1" ; shift # "$@" is the command...
69
70         _interval=1
71         case "$_timeout" in
72         */*)
73                 _interval="${_timeout#*/}"
74                 _timeout="${_timeout%/*}"
75         esac
76
77         _negate=false
78         if [ "$1" = "!" ] ; then
79                 _negate=true
80                 shift
81         fi
82
83         printf '<%d|' "$_timeout"
84         _t="$_timeout"
85         while [ "$_t" -gt 0 ] ; do
86                 _rc=0
87                 "$@" || _rc=$?
88                 if { ! $_negate && [ $_rc -eq 0 ] ; } || \
89                            { $_negate && [ $_rc -ne 0 ] ; } ; then
90                         echo "|$((_timeout - _t))|"
91                         echo "OK"
92                         return 0
93                 fi
94                 for _i in $(seq 1 "$_interval") ; do
95                         printf '.'
96                 done
97                 _t=$((_t - _interval))
98                 sleep "$_interval"
99         done
100
101         echo "*TIMEOUT*"
102
103         return 1
104 }
105
106 # setup_ctdb_base <parent> <subdir> [item-to-copy]...
107 setup_ctdb_base ()
108 {
109         [ $# -ge 2 ] || die "usage: setup_ctdb_base <parent> <subdir> [item]..."
110         # If empty arguments are passed then we attempt to remove /
111         # (i.e. the root directory) below
112         if [ -z "$1" ] || [ -z "$2" ] ; then
113                 die "usage: setup_ctdb_base <parent> <subdir> [item]..."
114         fi
115
116         _parent="$1"
117         _subdir="$2"
118
119         # Other arguments are files/directories to copy
120         shift 2
121
122         export CTDB_BASE="${_parent}/${_subdir}"
123         if [ -d "$CTDB_BASE" ] ; then
124                 rm -r "$CTDB_BASE"
125         fi
126         mkdir -p "$CTDB_BASE" || die "Failed to create CTDB_BASE=$CTDB_BASE"
127         mkdir -p "${CTDB_BASE}/run" || die "Failed to create ${CTDB_BASE}/run"
128         mkdir -p "${CTDB_BASE}/var" || die "Failed to create ${CTDB_BASE}/var"
129
130         for _i ; do
131                 cp -pr "${CTDB_SCRIPTS_BASE}/${_i}" "${CTDB_BASE}/"
132         done
133
134         mkdir -p "${CTDB_BASE}/events/legacy"
135
136         if [ -z "$CTDB_TEST_SUITE_DIR" ] ; then
137                 return
138         fi
139
140         for _i in "${CTDB_TEST_SUITE_DIR}/etc-ctdb/"* ; do
141                 # No/empty etc-ctdb directory
142                 [ -e "$_i" ] || break
143
144                 cp -pr "$_i" "${CTDB_BASE}/"
145         done
146 }