summaryrefslogtreecommitdiff
path: root/ctdb/tests/run_tests.sh
blob: f1268a5f4046b8c1e2151fd94b09ea8dc8b70de5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash

usage() {
    cat <<EOF
Usage: $0 [OPTIONS] [TESTS]

Options:
  -A		Use "cat -A" to print test output (only some tests)
  -c		Run integration tests on a cluster
  -C		Clean up - kill daemons and remove TEST_VAR_DIR when done
  -d		Print descriptions of tests instead of filenames (dodgy!)
  -D		Show diff between failed/expected test output (some tests only)
  -e		Exit on the first test failure
  -H		No headers - for running single test with other wrapper
  -N		Don't print summary of tests results after running all tests
  -q		Quiet - don't show tests being run (hint: use with -s)
  -S <lib>      Use socket wrapper library <lib> for local integration tests
  -v		Verbose - print test output for non-failures (only some tests)
  -V <dir>	Use <dir> as TEST_VAR_DIR
  -x		Trace this script with the -x option
  -X		Trace certain scripts run by tests using -x (only some tests)
EOF
    exit 1
}

# Print a message and exit.
die ()
{
    echo "$1" >&2 ; exit ${2:-1}
}

######################################################################

with_summary=true
with_desc=false
quiet=false
exit_on_fail=false
no_header=false

export TEST_VERBOSE=false
export TEST_COMMAND_TRACE=false
export TEST_CAT_RESULTS_OPTS=""
export TEST_DIFF_RESULTS=false
export TEST_LOCAL_DAEMONS
[ -n "$TEST_LOCAL_DAEMONS" ] || TEST_LOCAL_DAEMONS=3
export TEST_VAR_DIR=""
export TEST_CLEANUP=false
export TEST_TIMEOUT=3600
export TEST_SOCKET_WRAPPER_SO_PATH=""

temp=$(getopt -n "$prog" -o "AcCdDehHNqS:T:vV:xX" -l help -- "$@")

[ $? != 0 ] && usage

eval set -- "$temp"

while true ; do
    case "$1" in
	-A) TEST_CAT_RESULTS_OPTS="-A" ; shift ;;
	-c) TEST_LOCAL_DAEMONS="" ; shift ;;
	-C) TEST_CLEANUP=true ; shift ;;
	-d) with_desc=true ; shift ;;  # 4th line of output is description
	-D) TEST_DIFF_RESULTS=true ; shift ;;
	-e) exit_on_fail=true ; shift ;;
	-H) no_header=true ; shift ;;
	-N) with_summary=false ; shift ;;
	-q) quiet=true ; shift ;;
	-S) TEST_SOCKET_WRAPPER_SO_PATH="$2" ; shift 2 ;;
	-T) TEST_TIMEOUT="$2" ; shift 2 ;;
	-v) TEST_VERBOSE=true ; shift ;;
	-V) TEST_VAR_DIR="$2" ; shift 2 ;;
	-x) set -x; shift ;;
	-X) TEST_COMMAND_TRACE=true ; shift ;;
	--) shift ; break ;;
	*) usage ;;
    esac
done

case $(basename "$0") in
    *run_cluster_tests*)
	# Running on a cluster...  same as -c
	TEST_LOCAL_DAEMONS=""
	;;
esac

if $quiet ; then
    show_progress() { cat >/dev/null ; }
else
    show_progress() { cat ; }
fi

######################################################################

ctdb_test_begin ()
{
    local name="$1"

    teststarttime=$(date '+%s')
    testduration=0

    echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
    echo "Running test $name ($(date '+%T'))"
    echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
}

ctdb_test_end ()
{
    local name="$1" ; shift
    local status="$1" ; shift
    # "$@" is command-line

    local interp="SKIPPED"
    local statstr=" (reason $*)"
    if [ -n "$status" ] ; then
	if [ $status -eq 0 ] ; then
	    interp="PASSED"
	    statstr=""
	    echo "ALL OK: $*"
	elif [ $status -eq 124 ] ; then
	    interp="TIMEOUT"
	    statstr=" (status $status)"
	else
	    interp="FAILED"
	    statstr=" (status $status)"
	fi
    fi

    testduration=$(($(date +%s)-$teststarttime))

    echo "=========================================================================="
    echo "TEST ${interp}: ${name}${statstr} (duration: ${testduration}s)"
    echo "=========================================================================="

}

ctdb_test_run ()
{
    local name="$1" ; shift

    [ -n "$1" ] || set -- "$name"

    $no_header || ctdb_test_begin "$name"

    local status=0
    if [ -x "$1" ] ; then
	    timeout $TEST_TIMEOUT "$@" || status=$?
    else
	    echo "TEST IS NOT EXECUTABLE"
	    status=1
    fi

    $no_header || ctdb_test_end "$name" "$status" "$*"

    return $status
}

######################################################################

tests_total=0
tests_passed=0
tests_failed=0
summary=""

if ! which mktemp >/dev/null 2>&1 ; then
    # Not perfect, but it will do...
    mktemp ()
    {
	local dir=false
	if [ "$1" = "-d" ] ; then
	    dir=true
	fi
	local t="${TMPDIR:-/tmp}/tmp.$$.$RANDOM"
	(
	    umask 077
	    if $dir ; then
		mkdir "$t"
	    else
		>"$t"
	    fi
	)
	echo "$t"
    }
fi

tf=$(mktemp) || die "mktemp failed for tf - is TMPDIR missing?"
sf=$(mktemp) || die "mktemp failed for sf - is TMPDIR missing?"

set -o pipefail

run_one_test ()
{
    local f="$1"

    tests_total=$(($tests_total + 1))

    ctdb_test_run "$f" | tee "$tf" | show_progress
    status=$?
    if [ $status -eq 0 ] ; then
	tests_passed=$(($tests_passed + 1))
    else
	tests_failed=$(($tests_failed + 1))
    fi
    if $with_summary ; then
	local t
	if [ $status -eq 0 ] ; then
	    t=" PASSED "
	else
	    t="*FAILED*"
	fi
	if $with_desc ; then
	    desc=$(tail -n +4 $tf | head -n 1)
	    f="$desc"
	fi
	echo "$t $f" >>"$sf"
    fi
}

find_and_run_one_test ()
{
    local t="$1"
    local dir="$2"

    local f="${dir}${dir:+/}${t}"

    if [ -d "$f" ] ; then
	local i
	for i in $(ls "${f%/}/"*".sh" 2>/dev/null) ; do
	    run_one_test "$i"
	    if $exit_on_fail && [ $status -ne 0 ] ; then
		break
	    fi
	done
	# No tests found?  Not a tests directory!  Not found...
	[ -n "$status" ] || status=127
    elif [ -f "$f" ] ; then
	run_one_test "$f"
    else
	status=127
    fi
}

export CTDB_TEST_MODE="yes"

# Following 2 lines may be modified by installation script
export CTDB_TESTS_ARE_INSTALLED=false
export CTDB_TEST_DIR=$(dirname "$0")

if [ -z "$TEST_VAR_DIR" ] ; then
    if $CTDB_TESTS_ARE_INSTALLED ; then
	TEST_VAR_DIR=$(mktemp -d)
    else
	TEST_VAR_DIR="${CTDB_TEST_DIR}/var"
    fi
fi
mkdir -p "$TEST_VAR_DIR"

# Must be absolute
TEST_VAR_DIR=$(cd "$TEST_VAR_DIR"; echo "$PWD")
echo "TEST_VAR_DIR=$TEST_VAR_DIR"

export TEST_SCRIPTS_DIR="${CTDB_TEST_DIR}/scripts"

unit_tests="
	cunit
	eventd
	eventscripts
	onnode
	shellcheck
	takeover
	takeover_helper
	tool
"

# If no tests specified then run some defaults
if [ -z "$1" ] ; then
	if [ -n "$TEST_LOCAL_DAEMONS" ] ; then
		set -- UNIT simple
	else
		set -- simple complex
    fi
fi

do_cleanup ()
{
    if $TEST_CLEANUP ; then
	echo "Removing TEST_VAR_DIR=$TEST_VAR_DIR"
	rm -rf "$TEST_VAR_DIR"
    else
	echo "Not cleaning up TEST_VAR_DIR=$TEST_VAR_DIR"
    fi
}

cleanup_handler ()
{
    if $TEST_CLEANUP ; then
	if [ -n "$TEST_LOCAL_DAEMONS" -a "$f" = "simple" ] ; then
	    echo "***** shutting down daemons *****"
	    find_and_run_one_test simple/99_daemons_shutdown.sh "$tests_dir"
	fi
    fi
    do_cleanup
}

trap cleanup_handler SIGINT SIGTERM

declare -a tests
i=0
for f ; do
	if [ "$f" = "UNIT" ] ; then
		for t in $unit_tests ; do
			tests[i++]="$t"
		done
	else
		tests[i++]="$f"
	fi
done

for f in "${tests[@]}" ; do
    find_and_run_one_test "$f"

    if [ $status -eq 127 ] ; then
	# Find the the top-level tests directory
	tests_dir=$(dirname $(cd $TEST_SCRIPTS_DIR; echo $PWD))
	# Strip off current directory from beginning, if there, just
	# to make paths more friendly.
	tests_dir=${tests_dir#$PWD/}
	find_and_run_one_test "$f" "$tests_dir"
    fi

    if [ $status -eq 127 ] ; then
	    die "test \"$f\" is not recognised"
    fi

    if $exit_on_fail && [ $status -ne 0 ] ; then
	    break
    fi
done

rm -f "$tf"

if $with_summary ; then
    echo
    cat "$sf"
    echo
    echo "${tests_passed}/${tests_total} tests passed"
fi

rm -f "$sf"

echo

do_cleanup

if $no_header || $exit_on_fail ; then
    exit $status
elif [ $tests_failed -gt 0 ] ; then
    exit 1
else
    exit 0
fi