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