summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/tool/common.sh
blob: 75ca6ba207604bf02d156afe5b366b8553e358da (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
# Hey Emacs, this is a -*- shell-script -*- !!!  :-)

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

test_bin="$(dirname ${TESTS_SUBDIR})/bin"

define_test ()
{
    _f="$0"
    _f="${_f#./}"  # strip leading ./
    _f="${_f#testcases/}"  # strip leading testcases/
    _f="${_f%.sh}" # strip off .sh suffix if any

    case "$_f" in
	func.*)
	    _func="${_f#func.}"
	    _func="${_func%.*}" # Strip test number
	    test_prog="${test_bin}/ctdb_tool_libctdb ${_func}"
	    ;;
	stubby.*)
	    _cmd="${_f#stubby.}"
	    _cmd="${_cmd%.*}" # Strip test number
	    test_prog="${test_bin}/ctdb_tool_stubby ${_cmd}"
	    ;;
	*)
	    die "Unknown pattern for testcase \"$_f\""
    esac

    printf "%-28s - %s\n" "$_f" "$1"
}

required_result ()
{
    required_rc="${1:-0}"
    required_output=$(cat)
}

simple_test ()
{
    _out=$($test_prog "$@" 2>&1)
    _rc=$?

    # Most of the tests when the tool fails will have a date/time/pid
    # prefix.  Strip that because it isn't possible to match it.
    if [ $required_rc -ne 0 ]  ; then
	OUT_FILTER='s@^[0-9/]+ [0-9:\.]+ \[[0-9]+\]:@DATE TIME \[PID\]:@'
    fi

    if [ -n "$OUT_FILTER" ] ; then
	_fout=$(echo "$_out" | sed -r "$OUT_FILTER")
    else
	_fout="$_out"
    fi

    if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
	echo "PASSED"
    else
	cat -A <<EOF
##################################################
Required output (Exit status: ${required_rc}):
##################################################
$required_output
##################################################
Actual output (Exit status: ${_rc}):
##################################################
$_out
EOF
	return 1
    fi
}