ctdb-tests: Only use socket-wrapper for simple, local daemon tests
[samba.git] / ctdb / tests / run_tests.sh
1 #!/bin/bash
2
3 usage() {
4     cat <<EOF
5 Usage: $0 [OPTIONS] [TESTS]
6
7 Options:
8   -A            Use "cat -A" to print test output (only some tests)
9   -c            Run integration tests on a cluster
10   -C            Clean up - kill daemons and remove TEST_VAR_DIR when done
11   -d            Print descriptions of tests instead of filenames (dodgy!)
12   -D            Show diff between failed/expected test output (some tests only)
13   -e            Exit on the first test failure
14   -H            No headers - for running single test with other wrapper
15   -N            Don't print summary of tests results after running all tests
16   -q            Quiet - don't show tests being run (hint: use with -s)
17   -S <lib>      Use socket wrapper library <lib> for local integration tests
18   -v            Verbose - print test output for non-failures (only some tests)
19   -V <dir>      Use <dir> as TEST_VAR_DIR
20   -x            Trace this script with the -x option
21   -X            Trace certain scripts run by tests using -x (only some tests)
22 EOF
23     exit 1
24 }
25
26 # Print a message and exit.
27 die ()
28 {
29     echo "$1" >&2 ; exit ${2:-1}
30 }
31
32 ######################################################################
33
34 with_summary=true
35 with_desc=false
36 quiet=false
37 exit_on_fail=false
38 no_header=false
39
40 export TEST_VERBOSE=false
41 export TEST_COMMAND_TRACE=false
42 export TEST_CAT_RESULTS_OPTS=""
43 export TEST_DIFF_RESULTS=false
44 export TEST_LOCAL_DAEMONS
45 [ -n "$TEST_LOCAL_DAEMONS" ] || TEST_LOCAL_DAEMONS=3
46 export TEST_VAR_DIR=""
47 export TEST_CLEANUP=false
48 export TEST_TIMEOUT=600
49 export TEST_SOCKET_WRAPPER_SO_PATH=""
50
51 temp=$(getopt -n "$prog" -o "AcCdDehHNqS:T:vV:xX" -l help -- "$@")
52
53 [ $? != 0 ] && usage
54
55 eval set -- "$temp"
56
57 while true ; do
58     case "$1" in
59         -A) TEST_CAT_RESULTS_OPTS="-A" ; shift ;;
60         -c) TEST_LOCAL_DAEMONS="" ; shift ;;
61         -C) TEST_CLEANUP=true ; shift ;;
62         -d) with_desc=true ; shift ;;  # 4th line of output is description
63         -D) TEST_DIFF_RESULTS=true ; shift ;;
64         -e) exit_on_fail=true ; shift ;;
65         -H) no_header=true ; shift ;;
66         -N) with_summary=false ; shift ;;
67         -q) quiet=true ; shift ;;
68         -S) TEST_SOCKET_WRAPPER_SO_PATH="$2" ; shift 2 ;;
69         -T) TEST_TIMEOUT="$2" ; shift 2 ;;
70         -v) TEST_VERBOSE=true ; shift ;;
71         -V) TEST_VAR_DIR="$2" ; shift 2 ;;
72         -x) set -x; shift ;;
73         -X) TEST_COMMAND_TRACE=true ; shift ;;
74         --) shift ; break ;;
75         *) usage ;;
76     esac
77 done
78
79 case $(basename "$0") in
80     *run_cluster_tests*)
81         # Running on a cluster...  same as -c
82         TEST_LOCAL_DAEMONS=""
83         ;;
84 esac
85
86 if $quiet ; then
87     show_progress() { cat >/dev/null ; }
88 else
89     show_progress() { cat ; }
90 fi
91
92 ######################################################################
93
94 ctdb_test_begin ()
95 {
96     local name="$1"
97
98     teststarttime=$(date '+%s')
99     testduration=0
100
101     echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
102     echo "Running test $name ($(date '+%T'))"
103     echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
104 }
105
106 ctdb_test_end ()
107 {
108     local name="$1" ; shift
109     local status="$1" ; shift
110     # "$@" is command-line
111
112     local interp="SKIPPED"
113     local statstr=" (reason $*)"
114     if [ -n "$status" ] ; then
115         if [ $status -eq 0 ] ; then
116             interp="PASSED"
117             statstr=""
118             echo "ALL OK: $*"
119         elif [ $status -eq 124 ] ; then
120             interp="TIMEOUT"
121             statstr=" (status $status)"
122         else
123             interp="FAILED"
124             statstr=" (status $status)"
125         fi
126     fi
127
128     testduration=$(($(date +%s)-$teststarttime))
129
130     echo "=========================================================================="
131     echo "TEST ${interp}: ${name}${statstr} (duration: ${testduration}s)"
132     echo "=========================================================================="
133
134 }
135
136 ctdb_test_run ()
137 {
138     local name="$1" ; shift
139
140     [ -n "$1" ] || set -- "$name"
141
142     $no_header || ctdb_test_begin "$name"
143
144     local status=0
145     timeout $TEST_TIMEOUT "$@" || status=$?
146
147     $no_header || ctdb_test_end "$name" "$status" "$*"
148
149     return $status
150 }
151
152 ######################################################################
153
154 tests_total=0
155 tests_passed=0
156 tests_failed=0
157 summary=""
158
159 if ! which mktemp >/dev/null 2>&1 ; then
160     # Not perfect, but it will do...
161     mktemp ()
162     {
163         local dir=false
164         if [ "$1" = "-d" ] ; then
165             dir=true
166         fi
167         local t="${TMPDIR:-/tmp}/tmp.$$.$RANDOM"
168         (
169             umask 077
170             if $dir ; then
171                 mkdir "$t"
172             else
173                 >"$t"
174             fi
175         )
176         echo "$t"
177     }
178 fi
179
180 tf=$(mktemp) || die "mktemp failed for tf - is TMPDIR missing?"
181 sf=$(mktemp) || die "mktemp failed for sf - is TMPDIR missing?"
182
183 set -o pipefail
184
185 run_one_test ()
186 {
187     local f="$1"
188
189     [ -x "$f" ] || die "test \"$f\" is not executable"
190     tests_total=$(($tests_total + 1))
191
192     ctdb_test_run "$f" | tee "$tf" | show_progress
193     status=$?
194     if [ $status -eq 0 ] ; then
195         tests_passed=$(($tests_passed + 1))
196     else
197         tests_failed=$(($tests_failed + 1))
198     fi
199     if $with_summary ; then
200         local t
201         if [ $status -eq 0 ] ; then
202             t=" PASSED "
203         else
204             t="*FAILED*"
205         fi
206         if $with_desc ; then
207             desc=$(tail -n +4 $tf | head -n 1)
208             f="$desc"
209         fi
210         echo "$t $f" >>"$sf"
211     fi
212 }
213
214 find_and_run_one_test ()
215 {
216     local t="$1"
217     local dir="$2"
218
219     local f="${dir}${dir:+/}${t}"
220
221     if [ -d "$f" ] ; then
222         local i
223         for i in $(ls "${f%/}/"*".sh" 2>/dev/null) ; do
224             run_one_test "$i"
225             if $exit_on_fail && [ $status -ne 0 ] ; then
226                 break
227             fi
228         done
229         # No tests found?  Not a tests directory!  Not found...
230         [ -n "$status" ] || status=127
231     elif [ -f "$f" ] ; then
232         run_one_test "$f"
233     else
234         status=127
235     fi
236 }
237
238 # Following 2 lines may be modified by installation script
239 export CTDB_TESTS_ARE_INSTALLED=false
240 export CTDB_TEST_DIR=$(dirname "$0")
241
242 if [ -z "$TEST_VAR_DIR" ] ; then
243     if $CTDB_TESTS_ARE_INSTALLED ; then
244         TEST_VAR_DIR=$(mktemp -d)
245     else
246         TEST_VAR_DIR="${CTDB_TEST_DIR}/var"
247     fi
248 fi
249 mkdir -p "$TEST_VAR_DIR"
250
251 # Must be absolute
252 TEST_VAR_DIR=$(cd "$TEST_VAR_DIR"; echo "$PWD")
253 echo "TEST_VAR_DIR=$TEST_VAR_DIR"
254
255 export TEST_SCRIPTS_DIR="${CTDB_TEST_DIR}/scripts"
256
257 # If no tests specified then run some defaults
258 if [ -z "$1" ] ; then
259     if [ -n "$TEST_LOCAL_DAEMONS" ] ; then
260         set -- onnode takeover takeover_helper tool eventscripts \
261             cunit eventd shellcheck simple
262     else
263         set -- simple complex
264     fi
265 fi
266
267 do_cleanup ()
268 {
269     if $TEST_CLEANUP ; then
270         echo "Removing TEST_VAR_DIR=$TEST_VAR_DIR"
271         rm -rf "$TEST_VAR_DIR"
272     else
273         echo "Not cleaning up TEST_VAR_DIR=$TEST_VAR_DIR"
274     fi
275 }
276
277 cleanup_handler ()
278 {
279     if $TEST_CLEANUP ; then
280         if [ -n "$TEST_LOCAL_DAEMONS" -a "$f" = "simple" ] ; then
281             echo "***** shutting down daemons *****"
282             find_and_run_one_test simple/99_daemons_shutdown.sh "$tests_dir"
283         fi
284     fi
285     do_cleanup
286 }
287
288 trap cleanup_handler SIGINT SIGTERM
289
290 for f ; do
291     find_and_run_one_test "$f"
292
293     if [ $status -eq 127 ] ; then
294         # Find the the top-level tests directory
295         tests_dir=$(dirname $(cd $TEST_SCRIPTS_DIR; echo $PWD))
296         # Strip off current directory from beginning, if there, just
297         # to make paths more friendly.
298         tests_dir=${tests_dir#$PWD/}
299         find_and_run_one_test "$f" "$tests_dir"
300     fi
301
302     if [ $status -eq 127 ] ; then
303             die "test \"$f\" is not recognised"
304     fi
305
306     if $exit_on_fail && [ $status -ne 0 ] ; then
307             break
308     fi
309 done
310
311 rm -f "$tf"
312
313 if $with_summary ; then
314     echo
315     cat "$sf"
316     echo
317     echo "${tests_passed}/${tests_total} tests passed"
318 fi
319
320 rm -f "$sf"
321
322 echo
323
324 do_cleanup
325
326 if $no_header || $exit_on_fail ; then
327     exit $status
328 elif [ $tests_failed -gt 0 ] ; then
329     exit 1
330 else
331     exit 0
332 fi