xref: /kvm-unit-tests/run_tests.sh (revision d74708246bd9a593e03ecca476a5f1ed36e47288)
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
37# require enhanced getopt
38getopt -T > /dev/null
39if [ $? -ne 4 ]; then
40    echo "Enhanced getopt is not available, add it to your PATH?"
41    exit 1
42fi
43
44only_tests=""
45args=`getopt -u -o ag:htj:v -l all,group:,help,tap13,parallel:,verbose -- $*`
46[ $? -ne 0 ] && exit 2;
47set -- $args;
48while [ $# -gt 0 ]; do
49    case "$1" in
50        -a | --all)
51            run_all_tests="yes"
52            export ERRATA_FORCE=y
53            ;;
54        -g | --group)
55            shift
56            only_group=$1
57            ;;
58        -h | --help)
59            usage
60            exit
61            ;;
62        -j | --parallel)
63            shift
64            unittest_run_queues=$1
65            if (( $unittest_run_queues <= 0 )); then
66                echo "Invalid -j option: $unittest_run_queues"
67                exit 2
68            fi
69            ;;
70        -v | --verbose)
71            verbose="yes"
72            ;;
73        -t | --tap13)
74            tap_output="yes"
75            ;;
76        --)
77            ;;
78        *)
79            only_tests="$only_tests $1"
80            ;;
81    esac
82    shift
83done
84
85# RUNTIME_log_file will be configured later
86if [[ $tap_output == "no" ]]; then
87    process_test_output() { cat >> $RUNTIME_log_file; }
88    postprocess_suite_output() { cat; }
89else
90    process_test_output() {
91        local testname="$1"
92        CR=$'\r'
93        while read -r line; do
94            line="${line%$CR}"
95            case "${line:0:4}" in
96                PASS)
97                    echo "ok TEST_NUMBER - ${testname}: ${line#??????}" >&3
98                    ;;
99                FAIL)
100                    echo "not ok TEST_NUMBER - ${testname}: ${line#??????}" >&3
101                    ;;
102                SKIP)
103                    echo "ok TEST_NUMBER - ${testname}: ${line#??????} # skip" >&3
104                    ;;
105                *)
106                    ;;
107            esac
108            echo "${line}"
109        done >> $RUNTIME_log_file
110    }
111    postprocess_suite_output() {
112        test_number=0
113        while read -r line; do
114            case "${line}" in
115                ok*|"not ok"*)
116                    (( test_number++ ))
117                    echo "${line/TEST_NUMBER/${test_number}}" ;;
118                *) echo "${line}" ;;
119            esac
120        done
121        echo "1..$test_number"
122    }
123fi
124
125RUNTIME_log_stderr () { process_test_output "$1"; }
126RUNTIME_log_stdout () {
127    local testname="$1"
128    if [ "$PRETTY_PRINT_STACKS" = "yes" ]; then
129        local kernel="$2"
130        ./scripts/pretty_print_stacks.py "$kernel" | process_test_output "$testname"
131    else
132        process_test_output "$testname"
133    fi
134}
135
136function run_task()
137{
138	local testname="$1"
139
140	while (( $(jobs | wc -l) == $unittest_run_queues )); do
141		# wait for any background test to finish
142		wait -n 2>/dev/null
143	done
144
145	RUNTIME_log_file="${unittest_log_dir}/${testname}.log"
146	if [ $unittest_run_queues = 1 ]; then
147		run "$@"
148	else
149		run "$@" &
150	fi
151}
152
153: ${unittest_log_dir:=logs}
154: ${unittest_run_queues:=1}
155config=$TEST_DIR/unittests.cfg
156
157rm -rf $unittest_log_dir.old
158[ -d $unittest_log_dir ] && mv $unittest_log_dir $unittest_log_dir.old
159mkdir $unittest_log_dir || exit 2
160
161echo "BUILD_HEAD=$(cat build-head)" > $unittest_log_dir/SUMMARY
162
163if [[ $tap_output == "yes" ]]; then
164    echo "TAP version 13"
165fi
166
167trap "wait; exit 130" SIGINT
168
169(
170   # preserve stdout so that process_test_output output can write TAP to it
171   exec 3>&1
172   test "$tap_output" == "yes" && exec > /dev/null
173   for_each_unittest $config run_task
174) | postprocess_suite_output
175
176# wait until all tasks finish
177wait
178