xref: /kvm-unit-tests/scripts/runtime.bash (revision 08db0f5cfbca16b36f200b7bc54a78fa4941bcce)
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    log=$(premature_failure) && {
154        skip=true
155        if [ "${CONFIG_EFI}" == "y" ]; then
156            if [ "$ARCH" == "x86_64" ] &&
157               [[ "$(tail -1 <<<"$log")" =~ "Dummy Hello World!" ]]; then
158                   skip=false
159            elif [ "$ARCH" == "arm64" ] &&
160               [[ "$(tail -2 <<<"$log" | head -1)" =~ "Dummy Hello World!" ]]; then
161                   skip=false
162            fi
163        fi
164
165        if [ ${skip} == true ]; then
166            print_result "SKIP" $testname "" "$(tail -1 <<<"$log")"
167            return 77
168        fi
169    }
170
171    cmdline=$(get_cmdline $kernel)
172    if find_word "migration" "$groups"; then
173        cmdline="MIGRATION=yes $cmdline"
174    fi
175    if find_word "panic" "$groups"; then
176        cmdline="PANIC=yes $cmdline"
177    fi
178    if [ "$verbose" = "yes" ]; then
179        echo $cmdline
180    fi
181
182    # extra_params in the config file may contain backticks that need to be
183    # expanded, so use eval to start qemu.  Use "> >(foo)" instead of a pipe to
184    # preserve the exit status.
185    summary=$(eval "$cmdline" 2> >(RUNTIME_log_stderr $testname) \
186                             > >(tee >(RUNTIME_log_stdout $testname $kernel) | extract_summary))
187    ret=$?
188    [ "$KUT_STANDALONE" != "yes" ] && echo > >(RUNTIME_log_stdout $testname $kernel)
189
190    if [ $ret -eq 0 ]; then
191        print_result "PASS" $testname "$summary"
192    elif [ $ret -eq 77 ]; then
193        print_result "SKIP" $testname "$summary"
194    elif [ $ret -eq 124 ]; then
195        print_result "FAIL" $testname "" "timeout; duration=$timeout"
196        if [ "$tap_output" = "yes" ]; then
197            echo "not ok TEST_NUMBER - ${testname}: timeout; duration=$timeout" >&3
198        fi
199    elif [ $ret -gt 127 ]; then
200        signame="SIG"$(kill -l $(($ret - 128)))
201        print_result "FAIL" $testname "" "terminated on $signame"
202        if [ "$tap_output" = "yes" ]; then
203            echo "not ok TEST_NUMBER - ${testname}: terminated on $signame" >&3
204        fi
205    elif [ $ret -eq 127 ] && [ "$tap_output" = "yes" ]; then
206        echo "not ok TEST_NUMBER - ${testname}: aborted" >&3
207    else
208        print_result "FAIL" $testname "$summary"
209    fi
210
211    return $ret
212}
213
214#
215# Probe for MAX_SMP, in case it's less than the number of host cpus.
216#
217function probe_maxsmp()
218{
219	local smp
220
221	if smp=$($RUNTIME_arch_run _NO_FILE_4Uhere_ -smp $MAX_SMP |& grep 'SMP CPUs'); then
222		smp=${smp##* }
223		smp=${smp/\(}
224		smp=${smp/\)}
225		echo "Restricting MAX_SMP from ($MAX_SMP) to the max supported ($smp)" >&2
226		MAX_SMP=$smp
227	fi
228}
229