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