xref: /kvm-unit-tests/scripts/runtime.bash (revision 863e0b90fb888a5b8a76ff97e13df1f56f037cb2)
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 -5 | 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
19
20    log="$(eval "$(get_cmdline _NO_FILE_4Uhere_)" 2>&1)"
21
22    echo "$log" | grep "_NO_FILE_4Uhere_" |
23        grep -q -e "[Cc]ould not \(load\|open\) kernel" \
24                -e "error loading" \
25                -e "failed to load" &&
26        return 1
27
28    RUNTIME_log_stderr <<< "$log"
29
30    echo "$log"
31    return 0
32}
33
34get_cmdline()
35{
36    local kernel=$1
37    echo "TESTNAME=$testname TIMEOUT=$timeout MACHINE=$machine ACCEL=$accel $RUNTIME_arch_run $kernel -smp $smp $opts"
38}
39
40skip_nodefault()
41{
42    [ "$run_all_tests" = "yes" ] && return 1
43    [ "$KUT_STANDALONE" != "yes" ] && return 0
44
45    while true; do
46        read -r -p "Test marked not to be run by default, are you sure (y/N)? " yn
47        case $yn in
48            "Y" | "y" | "Yes" | "yes")
49                return 1
50                ;;
51            "" | "N" | "n" | "No" | "no" | "q" | "quit" | "exit")
52                return 0
53                ;;
54        esac
55    done
56}
57
58function print_result()
59{
60    local status="$1"
61    local testname="$2"
62    local summary="$3"
63    local reason="$4"
64
65    if [ -z "$reason" ]; then
66        echo "$($status) $testname $summary"
67    else
68        echo "$($status) $testname ($reason)"
69    fi
70}
71
72function find_word()
73{
74    grep -Fq " $1 " <<< " $2 "
75}
76
77function run()
78{
79    local testname="$1"
80    local groups="$2"
81    local smp="$3"
82    local kernel="$4"
83    local opts="$5"
84    local arch="$6"
85    local machine="$7"
86    local check="${CHECK:-$8}"
87    local accel="$9"
88    local timeout="${10:-$TIMEOUT}" # unittests.cfg overrides the default
89
90    if [ "${CONFIG_EFI}" == "y" ]; then
91        kernel=${kernel/%.flat/.efi}
92    fi
93
94    if [ -z "$testname" ]; then
95        return
96    fi
97
98    if [ -n "$only_tests" ] && ! find_word "$testname" "$only_tests"; then
99        return
100    fi
101
102    if [ -n "$only_group" ] && ! find_word "$only_group" "$groups"; then
103        return
104    fi
105
106    if [ -z "$GEN_SE_HEADER" ] && find_word "pv-host" "$groups"; then
107        print_result "SKIP" $testname "" "no gen-se-header available for pv-host test"
108        return
109    fi
110
111    if [ -z "$only_group" ] && find_word nodefault "$groups" &&
112            skip_nodefault; then
113        print_result "SKIP" $testname "" "test marked as manual run only"
114        return;
115    fi
116
117    if [ -n "$arch" ] && [ "$arch" != "$ARCH" ]; then
118        print_result "SKIP" $testname "" "$arch only"
119        return 2
120    fi
121
122    if [ -n "$machine" ] && [ -n "$MACHINE" ] && [ "$machine" != "$MACHINE" ]; then
123        print_result "SKIP" $testname "" "$machine only"
124        return 2
125    elif [ -n "$MACHINE" ]; then
126        machine="$MACHINE"
127    fi
128
129    if [ -n "$accel" ] && [ -n "$ACCEL" ] && [[ ! "$ACCEL" =~ ^$accel(,|$) ]]; then
130        print_result "SKIP" $testname "" "$accel only, but ACCEL=$ACCEL"
131        return 2
132    elif [ -n "$ACCEL" ]; then
133        accel="$ACCEL"
134    fi
135
136    # check a file for a particular value before running a test
137    # the check line can contain multiple files to check separated by a space
138    # but each check parameter needs to be of the form <path>=<value>
139    if [ "$check" ]; then
140        # There is no globbing or whitespace allowed in check parameters.
141        # shellcheck disable=SC2206
142        check=($check)
143        for check_param in "${check[@]}"; do
144            path=${check_param%%=*}
145            value=${check_param#*=}
146            if ! [ -f "$path" ] || [ "$(cat $path)" != "$value" ]; then
147                print_result "SKIP" $testname "" "$path not equal to $value"
148                return 2
149            fi
150        done
151    fi
152
153    if [ ! -f "$kernel" ]; then
154        print_result "SKIP" $testname "" "Test file '$kernel' not found";
155        return 2;
156    fi
157
158    log=$(premature_failure) && {
159        skip=true
160        if [ "${CONFIG_EFI}" == "y" ]; then
161            if [ "$ARCH" == "x86_64" ] &&
162               [[ "$(tail -1 <<<"$log")" =~ "Dummy Hello World!" ]]; then
163                   skip=false
164            elif [ "$ARCH" == "arm64" ] &&
165               [[ "$(tail -2 <<<"$log" | head -1)" =~ "Dummy Hello World!" ]]; then
166                   skip=false
167            fi
168        fi
169
170        if [ ${skip} == true ]; then
171            print_result "SKIP" $testname "" "$(tail -1 <<<"$log")"
172            return 77
173        fi
174    }
175
176    cmdline=$(get_cmdline $kernel)
177    if find_word "migration" "$groups"; then
178        cmdline="MIGRATION=yes $cmdline"
179    fi
180    if find_word "panic" "$groups"; then
181        cmdline="PANIC=yes $cmdline"
182    fi
183    if [ "$verbose" = "yes" ]; then
184        echo $cmdline
185    fi
186
187    # extra_params in the config file may contain backticks that need to be
188    # expanded, so use eval to start qemu.  Use "> >(foo)" instead of a pipe to
189    # preserve the exit status.
190    summary=$(eval "$cmdline" 2> >(RUNTIME_log_stderr $testname) \
191                             > >(tee >(RUNTIME_log_stdout $testname $kernel) | extract_summary))
192    ret=$?
193    [ "$KUT_STANDALONE" != "yes" ] && echo > >(RUNTIME_log_stdout $testname $kernel)
194
195    if [ $ret -eq 0 ]; then
196        print_result "PASS" $testname "$summary"
197    elif [ $ret -eq 77 ]; then
198        print_result "SKIP" $testname "$summary"
199    elif [ $ret -eq 124 ]; then
200        print_result "FAIL" $testname "" "timeout; duration=$timeout"
201        if [ "$tap_output" = "yes" ]; then
202            echo "not ok TEST_NUMBER - ${testname}: timeout; duration=$timeout" >&3
203        fi
204    elif [ $ret -gt 127 ]; then
205        signame="SIG"$(kill -l $(($ret - 128)))
206        print_result "FAIL" $testname "" "terminated on $signame"
207        if [ "$tap_output" = "yes" ]; then
208            echo "not ok TEST_NUMBER - ${testname}: terminated on $signame" >&3
209        fi
210    elif [ $ret -eq 127 ] && [ "$tap_output" = "yes" ]; then
211        echo "not ok TEST_NUMBER - ${testname}: aborted" >&3
212    else
213        print_result "FAIL" $testname "$summary"
214    fi
215
216    return $ret
217}
218
219#
220# Probe for MAX_SMP, in case it's less than the number of host cpus.
221#
222function probe_maxsmp()
223{
224	local smp
225
226	if smp=$($RUNTIME_arch_run _NO_FILE_4Uhere_ -smp $MAX_SMP |& grep 'SMP CPUs'); then
227		smp=${smp##* }
228		smp=${smp/\(}
229		smp=${smp/\)}
230		echo "Restricting MAX_SMP from ($MAX_SMP) to the max supported ($smp)" >&2
231		MAX_SMP=$smp
232	fi
233}
234