tests - add -v option to set TEST_VERBOSE=true
[samba.git] / ctdb / tests / scripts / run_tests
1 #!/bin/bash
2
3 # The ability of ctdb_test_env to take tests on the command-line is
4 # nice, but here we need to hack around it with that colon to reset
5 # the arguments that it sees.
6 . $(dirname $0)/ctdb_test_env :
7
8 . ctdb_test_functions.bash
9
10 usage() {
11     cat <<EOF
12 Usage: run_tests [OPTIONS] [TESTS]
13
14 EOF
15     exit 1
16 }
17
18 ######################################################################
19
20 with_summary=false
21 with_desc=false
22 quiet=false
23
24 export TEST_VERBOSE=false
25
26 temp=$(getopt -n "$prog" -o "xdhqsv" -l help -- "$@")
27
28 [ $? != 0 ] && usage
29
30 eval set -- "$temp"
31
32 while true ; do
33     case "$1" in
34         -x) set -x; shift ;;
35         -d) with_desc=true ; shift ;;  # 4th line of output is description
36         -q) quiet=true ; shift ;;
37         -s) with_summary=true ; shift ;;
38         -v) TEST_VERBOSE=true ; shift ;;
39         --) shift ; break ;;
40         *) usage ;;
41     esac
42 done
43
44 if $quiet ; then
45     show_progress() { cat >/dev/null ; }
46 else
47     show_progress() { cat ; }
48 fi
49
50 ######################################################################
51
52 ctdb_test_begin ()
53 {
54     local name="$1"
55
56     teststarttime=$(date '+%s')
57     testduration=0
58
59     echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
60     echo "Running test $name ($(date '+%T'))"
61     echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
62 }
63
64 ctdb_test_end ()
65 {
66     local name="$1" ; shift
67     local status="$1" ; shift
68     # "$@" is command-line
69
70     local interp="SKIPPED"
71     local statstr=" (reason $*)"
72     if [ -n "$status" ] ; then
73         if [ $status -eq 0 ] ; then
74             interp="PASSED"
75             statstr=""
76             echo "ALL OK: $*"
77         else
78             interp="FAILED"
79             statstr=" (status $status)"
80         fi
81     fi
82
83     testduration=$(($(date +%s)-$teststarttime))
84
85     echo "=========================================================================="
86     echo "TEST ${interp}: ${name}${statstr} (duration: ${testduration}s)"
87     echo "=========================================================================="
88
89 }
90
91 ctdb_test_run ()
92 {
93     local name="$1" ; shift
94
95     [ -n "$1" ] || set -- "$name"
96
97     ctdb_test_begin "$name"
98
99     local status=0
100     "$@" || status=$?
101
102     ctdb_test_end "$name" "$status" "$*"
103
104     return $status
105 }
106
107 ######################################################################
108
109 tests_total=0
110 tests_passed=0
111 tests_failed=0
112 summary=""
113
114 rows=$(if tty -s ; then stty size ; else echo x 80 ; fi | sed -e 's@.* @@' -e 's@^0$@80@')
115 ww=$((rows - 7))
116
117 tf=$(mktemp)
118 sf=$(mktemp)
119
120 set -o pipefail
121
122 for f; do
123     [ -x $f ] || fail "test \"$f\" is not executable"
124     tests_total=$(($tests_total + 1))
125     ctdb_test_run "$f" | tee "$tf" | show_progress
126     status=$?
127     if $with_summary ; then
128         if [ $status -eq 0 ] ; then
129             tests_passed=$(($tests_passed + 1))
130             t=" PASSED "
131         else
132             t="*FAILED*"
133             tests_failed=$(($tests_failed + 1))
134         fi
135         if $with_desc ; then
136             desc=$(tail -n +4 $tf | head -n 1)
137             f="$desc"
138         fi
139         echo "$t $f" >>"$sf"
140     fi
141 done
142
143 rm -f "$tf"
144
145 if $with_summary ; then
146     echo
147     cat "$sf"
148     echo
149     echo "${tests_passed}/${tests_total} tests passed"
150 fi
151
152 rm -f "$sf"
153
154 if [ $tests_failed -gt 0 ] ; then
155     exit 1
156 fi