ctdb-tests: Make the fake log timestamp string easy to modify
[vlendec/samba-autobuild/.git] / ctdb / tests / scripts / unit.sh
1 # Hey Emacs, this is a -*- shell-script -*- !!!  :-)
2
3 . "${TEST_SCRIPTS_DIR}/common.sh"
4
5 # Common variables and functions for CTDB unit tests.
6
7 trap -- '' PIPE
8
9 # Set the required result for a test.
10 # - Argument 1 is exit code.
11 # - Argument 2, if present is the required test output but "--"
12 #   indicates empty output.
13 # If argument 2 is not present or null then read required test output
14 # from stdin.
15 required_result ()
16 {
17     required_rc="${1:-0}"
18     if [ -n "$2" ] ; then
19         if [ "$2" = "--" ] ; then
20             required_output=""
21         else
22             required_output="$2"
23         fi
24     else
25         if ! tty -s ; then
26             required_output=$(cat)
27         else
28             required_output=""
29         fi
30     fi
31 }
32
33 ok ()
34 {
35     required_result 0 "$@"
36 }
37
38 ok_null ()
39 {
40     ok --
41 }
42
43 result_print ()
44 {
45     _passed="$1"
46     _out="$2"
47     _rc="$3"
48     _extra_header="$4"
49
50     if "$TEST_VERBOSE" || ! $_passed ; then
51         if [ -n "$_extra_header" ] ; then
52             cat <<EOF
53
54 ##################################################
55 $_extra_header
56 EOF
57         fi
58
59 cat <<EOF
60 --------------------------------------------------
61 Output (Exit status: ${_rc}):
62 --------------------------------------------------
63 EOF
64         echo "$_out" | cat $TEST_CAT_RESULTS_OPTS
65     fi
66
67     if ! $_passed ; then
68         cat <<EOF
69 --------------------------------------------------
70 Required output (Exit status: ${required_rc}):
71 --------------------------------------------------
72 EOF
73         echo "$required_output" | cat $TEST_CAT_RESULTS_OPTS
74
75         if $TEST_DIFF_RESULTS ; then
76             _outr=$(mktemp)
77             echo "$required_output" >"$_outr"
78
79             _outf=$(mktemp)
80             echo "$_fout" >"$_outf"
81
82             cat <<EOF
83 --------------------------------------------------
84 Diff:
85 --------------------------------------------------
86 EOF
87             diff -u "$_outr" "$_outf" | cat -A
88             rm "$_outr" "$_outf"
89         fi
90     fi
91 }
92
93 result_footer ()
94 {
95     _passed="$1"
96     _extra_footer="$2"
97
98     if "$TEST_VERBOSE" || ! $_passed ; then
99         if [ -n "$_extra_footer" ] ; then
100             cat <<EOF
101 --------------------------------------------------
102 $_extra_footer
103 --------------------------------------------------
104 EOF
105         fi
106     fi
107
108     if $_passed ; then
109         echo "PASSED"
110         return 0
111     else
112         echo
113         echo "FAILED"
114         return 1
115     fi
116 }
117
118 # Result filtering is (usually) used to replace the date/time/PID
119 # prefix on some CTDB tool/client log messages with the literal string
120 # "DATE TIME [PID]".  This allows tests to loosely match this output,
121 # since it can't otherwise be matched.
122 result_filter_default ()
123 {
124     _date_time_pid='[0-9/][0-9/]*\ [0-9:\.][0-9:\.]*\ \[[\ 0-9][\ 0-9]*\]'
125     sed -e "s@^${_date_time_pid}:@DATE\ TIME\ \[PID\]:@"
126 }
127 TEST_DATE_STAMP="DATE TIME [PID]: "
128
129 # Override this function to customise output filtering.
130 result_filter ()
131 {
132     result_filter_default
133 }
134
135 result_check ()
136 {
137     _rc=$?
138
139     _extra_header="$1"
140
141     _fout=$(echo "$_out" | result_filter)
142
143     if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
144         _passed=true
145     else
146         _passed=false
147     fi
148
149     result_print "$_passed" "$_out" "$_rc" "$_extra_header"
150     result_footer "$_passed"
151 }
152
153 local="${TEST_SUBDIR}/scripts/local.sh"
154 if [ -r "$local" ] ; then
155     . "$local"
156 fi