xref: /kvm-unit-tests/scripts/runtime.bash (revision dfc1fec2fbde04ad607e1aed560cf7059350c70f)
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    [ "$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 opts="$5"
80    local arch="$6"
81    local check="${CHECK:-$7}"
82    local accel="$8"
83    local timeout="${9:-$TIMEOUT}" # unittests.cfg overrides the default
84
85    if [ "${CONFIG_EFI}" == "y" ]; then
86        kernel=${kernel/%.flat/.efi}
87    fi
88
89    if [ -z "$testname" ]; then
90        return
91    fi
92
93    if [ -n "$only_tests" ] && ! find_word "$testname" "$only_tests"; then
94        return
95    fi
96
97    if [ -n "$only_group" ] && ! find_word "$only_group" "$groups"; then
98        return
99    fi
100
101    if [ -z "$GEN_SE_HEADER" ] && find_word "pv-host" "$groups"; then
102        print_result "SKIP" $testname "" "no gen-se-header available for pv-host test"
103        return
104    fi
105
106    if [ -z "$only_group" ] && find_word nodefault "$groups" &&
107            skip_nodefault; then
108        print_result "SKIP" $testname "" "test marked as manual run only"
109        return;
110    fi
111
112    if [ -n "$arch" ] && [ "$arch" != "$ARCH" ]; then
113        print_result "SKIP" $testname "" "$arch only"
114        return 2
115    fi
116
117    if [ -n "$accel" ] && [ -n "$ACCEL" ] && [ "$accel" != "$ACCEL" ]; then
118        print_result "SKIP" $testname "" "$accel only, but ACCEL=$ACCEL"
119        return 2
120    elif [ -n "$ACCEL" ]; then
121        accel="$ACCEL"
122    fi
123
124    # check a file for a particular value before running a test
125    # the check line can contain multiple files to check separated by a space
126    # but each check parameter needs to be of the form <path>=<value>
127    if [ "$check" ]; then
128        check=($check)
129        for check_param in "${check[@]}"; do
130            path=${check_param%%=*}
131            value=${check_param#*=}
132            if ! [ -f "$path" ] || [ "$(cat $path)" != "$value" ]; then
133                print_result "SKIP" $testname "" "$path not equal to $value"
134                return 2
135            fi
136        done
137    fi
138
139    log=$(premature_failure) && {
140        skip=true
141        if [ "${CONFIG_EFI}" == "y" ]; then
142            if [ "$ARCH" == "x86_64" ] &&
143               [[ "$(tail -1 <<<"$log")" =~ "Dummy Hello World!" ]]; then
144                   skip=false
145            elif [ "$ARCH" == "arm64" ] &&
146               [[ "$(tail -2 <<<"$log" | head -1)" =~ "Dummy Hello World!" ]]; then
147                   skip=false
148            fi
149        fi
150
151        if [ ${skip} == true ]; then
152            print_result "SKIP" $testname "" "$last_line"
153            return 77
154        fi
155    }
156
157    cmdline=$(get_cmdline $kernel)
158    if find_word "migration" "$groups"; then
159        cmdline="MIGRATION=yes $cmdline"
160    fi
161    if find_word "panic" "$groups"; then
162        cmdline="PANIC=yes $cmdline"
163    fi
164    if [ "$verbose" = "yes" ]; then
165        echo $cmdline
166    fi
167
168    # extra_params in the config file may contain backticks that need to be
169    # expanded, so use eval to start qemu.  Use "> >(foo)" instead of a pipe to
170    # preserve the exit status.
171    summary=$(eval "$cmdline" 2> >(RUNTIME_log_stderr $testname) \
172                             > >(tee >(RUNTIME_log_stdout $testname $kernel) | extract_summary))
173    ret=$?
174    [ "$KUT_STANDALONE" != "yes" ] && echo > >(RUNTIME_log_stdout $testname $kernel)
175
176    if [ $ret -eq 0 ]; then
177        print_result "PASS" $testname "$summary"
178    elif [ $ret -eq 77 ]; then
179        print_result "SKIP" $testname "$summary"
180    elif [ $ret -eq 124 ]; then
181        print_result "FAIL" $testname "" "timeout; duration=$timeout"
182        if [ "$tap_output" = "yes" ]; then
183            echo "not ok TEST_NUMBER - ${testname}: timeout; duration=$timeout" >&3
184        fi
185    elif [ $ret -gt 127 ]; then
186        signame="SIG"$(kill -l $(($ret - 128)))
187        print_result "FAIL" $testname "" "terminated on $signame"
188        if [ "$tap_output" = "yes" ]; then
189            echo "not ok TEST_NUMBER - ${testname}: terminated on $signame" >&3
190        fi
191    elif [ $ret -eq 127 ] && [ "$tap_output" = "yes" ]; then
192        echo "not ok TEST_NUMBER - ${testname}: aborted" >&3
193    else
194        print_result "FAIL" $testname "$summary"
195    fi
196
197    return $ret
198}
199
200#
201# Probe for MAX_SMP, in case it's less than the number of host cpus.
202#
203# This probing currently only works for ARM, as x86 bails on another
204# error first, so this check is only run for ARM and ARM64. The
205# parameter expansion takes the last number from the QEMU error
206# message, which gives the allowable MAX_SMP.
207if [[ $ARCH == 'arm' || $ARCH == 'arm64' ]] &&
208   smp=$($RUNTIME_arch_run _NO_FILE_4Uhere_ -smp $MAX_SMP |& grep 'exceeds max CPUs'); then
209	smp=${smp##*(}
210	MAX_SMP=${smp:0:-1}
211fi
212