xref: /kvm-unit-tests/scripts/runtime.bash (revision 33a6576c6e1bfd98fd35402c9f64dfd7537dd2ff)
1: "${RUNTIME_arch_run?}"
2: ${MAX_SMP:=$(getconf _NPROCESSORS_ONLN)}
3: ${TIMEOUT:=90s}
4
5PASS() { echo -ne "\e[32mPASS\e[0m"; }
6SKIP() { echo -ne "\e[33mSKIP\e[0m"; }
7FAIL() { echo -ne "\e[31mFAIL\e[0m"; }
8
9extract_summary()
10{
11    local cr=$'\r'
12    tail -3 | grep '^SUMMARY: ' | sed 's/^SUMMARY: /(/;s/'"$cr"'\{0,1\}$/)/'
13}
14
15# We assume that QEMU is going to work if it tried to load the kernel
16premature_failure()
17{
18    local log="$(eval $(get_cmdline _NO_FILE_4Uhere_) 2>&1)"
19
20    echo "$log" | grep "_NO_FILE_4Uhere_" |
21        grep -q -e "could not \(load\|open\) kernel" -e "error loading" &&
22        return 1
23
24    RUNTIME_log_stderr <<< "$log"
25
26    echo "$log"
27    return 0
28}
29
30get_cmdline()
31{
32    local kernel=$1
33    echo "TESTNAME=$testname TIMEOUT=$timeout ACCEL=$accel $RUNTIME_arch_run $kernel -smp $smp $opts"
34}
35
36skip_nodefault()
37{
38    [ "$run_all_tests" = "yes" ] && return 1
39    [ "$STANDALONE" != "yes" ] && return 0
40
41    while true; do
42        read -r -p "Test marked not to be run by default, are you sure (y/N)? " yn
43        case $yn in
44            "Y" | "y" | "Yes" | "yes")
45                return 1
46                ;;
47            "" | "N" | "n" | "No" | "no" | "q" | "quit" | "exit")
48                return 0
49                ;;
50        esac
51    done
52}
53
54function print_result()
55{
56    # output test results in a TAP format
57    # https://testanything.org/tap-version-13-specification.html
58
59    local status="$1"
60    local testname="$2"
61    local summary="$3"
62    local reason="$4"
63
64    if [ -z "$reason" ]; then
65        echo "`$status` $testname $summary"
66    else
67        echo "`$status` $testname ($reason)"
68    fi
69}
70
71function run()
72{
73    local testname="$1"
74    local groups="$2"
75    local smp="$3"
76    local kernel="$4"
77    local opts="$5"
78    local arch="$6"
79    local check="${CHECK:-$7}"
80    local accel="$8"
81    local timeout="${9:-$TIMEOUT}" # unittests.cfg overrides the default
82
83    if [ -z "$testname" ]; then
84        return
85    fi
86
87    if [ -n "$only_tests" ] && ! grep -qw "$testname" <<<$only_tests; then
88        return
89    fi
90
91    if [ -n "$only_group" ] && ! grep -qw "$only_group" <<<$groups; then
92        return
93    fi
94
95    if [ -z "$only_group" ] && grep -qw "nodefault" <<<$groups &&
96            skip_nodefault; then
97        print_result "SKIP" $testname "" "test marked as manual run only"
98        return;
99    fi
100
101    if [ -n "$arch" ] && [ "$arch" != "$ARCH" ]; then
102        print_result "SKIP" $testname "" "$arch only"
103        return 2
104    fi
105
106    if [ -n "$accel" ] && [ -n "$ACCEL" ] && [ "$accel" != "$ACCEL" ]; then
107        print_result "SKIP" $testname "" "$accel only, but ACCEL=$ACCEL"
108        return 2
109    elif [ -n "$ACCEL" ]; then
110        accel="$ACCEL"
111    fi
112
113    # check a file for a particular value before running a test
114    # the check line can contain multiple files to check separated by a space
115    # but each check parameter needs to be of the form <path>=<value>
116    for check_param in "${check[@]}"; do
117        path=${check_param%%=*}
118        value=${check_param#*=}
119        if [ "$path" ] && [ "$(cat $path)" != "$value" ]; then
120            print_result "SKIP" $testname "" "$path not equal to $value"
121            return 2
122        fi
123    done
124
125    last_line=$(premature_failure > >(tail -1)) && {
126        print_result "SKIP" $testname "" "$last_line"
127        return 77
128    }
129
130    cmdline=$(get_cmdline $kernel)
131    if grep -qw "migration" <<<$groups ; then
132        cmdline="MIGRATION=yes $cmdline"
133    fi
134    if [ "$verbose" = "yes" ]; then
135        echo $cmdline
136    fi
137
138    # extra_params in the config file may contain backticks that need to be
139    # expanded, so use eval to start qemu.  Use "> >(foo)" instead of a pipe to
140    # preserve the exit status.
141    summary=$(eval $cmdline 2> >(RUNTIME_log_stderr) \
142                             > >(tee >(RUNTIME_log_stdout $kernel) | extract_summary))
143    ret=$?
144    [ "$STANDALONE" != "yes" ] && echo > >(RUNTIME_log_stdout $kernel)
145
146    if [ $ret -eq 0 ]; then
147        print_result "PASS" $testname "$summary"
148    elif [ $ret -eq 77 ]; then
149        print_result "SKIP" $testname "$summary"
150    elif [ $ret -eq 124 ]; then
151        print_result "FAIL" $testname "" "timeout; duration=$timeout"
152    elif [ $ret -gt 127 ]; then
153        print_result "FAIL" $testname "" "terminated on SIG$(kill -l $(($ret - 128)))"
154    else
155        print_result "FAIL" $testname "$summary"
156    fi
157
158    return $ret
159}
160
161#
162# Probe for MAX_SMP, in case it's less than the number of host cpus.
163#
164# This probing currently only works for ARM, as x86 bails on another
165# error first. Also, this probing isn't necessary for any ARM hosts
166# running kernels later than v4.3, i.e. those including ef748917b52
167# "arm/arm64: KVM: Remove 'config KVM_ARM_MAX_VCPUS'". So, at some
168# point when maintaining the while loop gets too tiresome, we can
169# just remove it...
170while $RUNTIME_arch_run _NO_FILE_4Uhere_ -smp $MAX_SMP \
171		|& grep -qi 'exceeds max CPUs'; do
172	MAX_SMP=$((MAX_SMP >> 1))
173done
174