xref: /kvm-unit-tests/run_tests.sh (revision bfed1760b7c05d810a4a049dfdc4360621f84847)
1#!/usr/bin/env bash
2
3verbose="no"
4tap_output="no"
5run_all_tests="no" # don't run nodefault tests
6
7if [ ! -f config.mak ]; then
8    echo "run ./configure && make first. See ./configure -h"
9    exit 1
10fi
11source config.mak
12source scripts/common.bash
13
14function usage()
15{
16cat <<EOF
17
18Usage: $0 [-h] [-v] [-a] [-g group] [-j NUM-TASKS] [-t]
19
20    -h, --help      Output this help text
21    -v, --verbose   Enables verbose mode
22    -a, --all       Run all tests, including those flagged as 'nodefault'
23                    and those guarded by errata.
24    -g, --group     Only execute tests in the given group
25    -j, --parallel  Execute tests in parallel
26    -t, --tap13     Output test results in TAP format
27
28Set the environment variable QEMU=/path/to/qemu-system-ARCH to
29specify the appropriate qemu binary for ARCH-run.
30
31EOF
32}
33
34RUNTIME_arch_run="./$TEST_DIR/run"
35source scripts/runtime.bash
36
37only_tests=""
38args=`getopt -u -o ag:htj:v -l all,group:,help,tap13,parallel:,verbose -- $*`
39[ $? -ne 0 ] && exit 2;
40set -- $args;
41while [ $# -gt 0 ]; do
42    case "$1" in
43        -a | --all)
44            run_all_tests="yes"
45            export ERRATA_FORCE=y
46            ;;
47        -g | --group)
48            shift
49            only_group=$1
50            ;;
51        -h | --help)
52            usage
53            exit
54            ;;
55        -j | --parallel)
56            shift
57            unittest_run_queues=$1
58            if (( $unittest_run_queues <= 0 )); then
59                echo "Invalid -j option: $unittest_run_queues"
60                exit 2
61            fi
62            ;;
63        -v | --verbose)
64            verbose="yes"
65            ;;
66        -t | --tap13)
67            tap_output="yes"
68            ;;
69        --)
70            ;;
71        *)
72            only_tests="$only_tests $1"
73            ;;
74    esac
75    shift
76done
77
78# RUNTIME_log_file will be configured later
79if [[ $tap_output == "no" ]]; then
80    process_test_output() { cat >> $RUNTIME_log_file; }
81    postprocess_suite_output() { cat; }
82else
83    process_test_output() {
84        CR=$'\r'
85        while read -r line; do
86            line="${line%$CR}"
87            case "${line:0:4}" in
88                PASS)
89                    echo "ok TEST_NUMBER - ${line#??????}" >&3
90                    ;;
91                FAIL)
92                    echo "not ok TEST_NUMBER - ${line#??????}" >&3
93                    ;;
94                SKIP)
95                    echo "ok TEST_NUMBER - ${line#??????} # skip" >&3
96                    ;;
97                *)
98                    ;;
99            esac
100            echo "${line}"
101        done >> $RUNTIME_log_file
102    }
103    postprocess_suite_output() {
104        test_number=0
105        while read -r line; do
106            case "${line}" in
107                ok*|"not ok"*)
108                    (( test_number++ ))
109                    echo "${line/TEST_NUMBER/${test_number}}" ;;
110                *) echo "${line}" ;;
111            esac
112        done
113        echo "1..$test_number"
114    }
115fi
116
117RUNTIME_log_stderr () { process_test_output; }
118RUNTIME_log_stdout () {
119    if [ "$PRETTY_PRINT_STACKS" = "yes" ]; then
120        ./scripts/pretty_print_stacks.py $1 | process_test_output
121    else
122        process_test_output
123    fi
124}
125
126function run_task()
127{
128	local testname="$1"
129	if [ -z "$testname" ]; then
130		return
131	fi
132
133	while (( $(jobs | wc -l) == $unittest_run_queues )); do
134		# wait for any background test to finish
135		wait -n 2>/dev/null
136	done
137
138	RUNTIME_log_file="${unittest_log_dir}/${testname}.log"
139	if [ $unittest_run_queues = 1 ]; then
140		run "$@"
141	else
142		run "$@" &
143	fi
144}
145
146: ${unittest_log_dir:=logs}
147: ${unittest_run_queues:=1}
148config=$TEST_DIR/unittests.cfg
149
150rm -rf $unittest_log_dir.old
151[ -d $unittest_log_dir ] && mv $unittest_log_dir $unittest_log_dir.old
152mkdir $unittest_log_dir || exit 2
153
154echo "BUILD_HEAD=$(cat build-head)" > $unittest_log_dir/SUMMARY
155
156if [[ $tap_output == "yes" ]]; then
157    echo "TAP version 13"
158fi
159
160trap "wait; exit 130" SIGINT
161
162(
163   # preserve stdout so that process_test_output output can write TAP to it
164   exec 3>&1
165   test "$tap_output" == "yes" && exec > /dev/null
166   for_each_unittest $config run_task
167) | postprocess_suite_output
168
169# wait until all tasks finish
170wait
171