blob: b41481e3e90dc511abc66c206dc5dcd901e124b3 [file] [log] [blame]
#!/usr/bin/env bash
# Copyright 2020 The Android KVM Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
source "$(dirname "${BASH_SOURCE[0]}")/../common.inc"
default_var TIMEOUT 180s
default_var VHE 0
default_var VERBOSE 0
default_var QUIET 0
default_var GDB 0
default_var KVM_PROTECTED 1
default_var KERNEL ""
default_var TEST_PATH ""
default_var DISPLAY_NAME ""
default_var OUTPUT ""
# QEMU CPUs to use for VHE and nVHE runs
CPU_VHE="max"
CPU_NVHE="cortex-a53"
RESULT_ALIGN=40
function usage() {
cat <<EOF
Usage: $0 [-h] [-v] [-q] [-G] [-V]
[-k KERNEL] [-d DISPLAY_NAME] [-o OUTPUT]
PATH [-- RUN_QEMU_ARGS]
PATH Path to test binary. Basenames are resolved in the default output folder.
-h Output this help text
-v Print invoked command
-q Print only the result of the test
-k Kernel image
-d Override test name displayed in result
-o Redirect stdout/stderr output to given file (implies -q)
-V Enable VHE configuration
-N Disable protected KVM configuration
-G Enable debugging of emulated system with GDB
EOF
}
while getopts ":k:d:o:vVNGqh" OPT; do
case "${OPT}" in
k) KERNEL="${OPTARG}" ;;
d) DISPLAY_NAME="${OPTARG}" ;;
v) VERBOSE=1 ;;
V) VHE=1 ;;
N) KVM_PROTECTED=0 ;;
q) QUIET=1 ;;
G) GDB=1 ;;
o)
OUTPUT="${OPTARG}"
QUIET=1
;;
h)
usage
exit 0
;;
\?)
echo "Invalid option: -${OPTARG}" 1>&2
usage 1>&2
exit 1
;;
:)
echo "Invalid option: -${OPTARG} requires an argument" 1>&2
usage 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
# Parse test name from a positional argument.
if [ $# -eq 0 ]; then
usage 1>&2
exit 1
fi
TEST_PATH="$1"
shift 1
# Default display name is the basename from TEST_PATH.
if [ -z "${DISPLAY_NAME}" ]; then
DISPLAY_NAME="$(basename "${TEST_PATH}")"
fi
# If TEST_PATH is just a basename, search for the test in
# the kvm-unit-tests' default output folder.
if [ "$(basename "${TEST_PATH}")" = "${TEST_PATH}" ]; then
TEST_PATH="${KUT_TEST_DIR}/${TEST_PATH}"
fi
CMD=("${SCRIPT_RUN_QEMU}" -R "${TEST_PATH}" -t "${TIMEOUT}")
if [ "${VHE}" -eq 1 ]; then
CMD+=(-c "${CPU_VHE}")
else
CMD+=(-c "${CPU_NVHE}")
fi
if [ "${KVM_PROTECTED}" -ne 1 ]; then
CMD+=(-N)
fi
if [ -n "${KERNEL}" ]; then
CMD+=(-k "${KERNEL}")
fi
if [ "${GDB}" -eq 1 ]; then
CMD+=(-G)
fi
# If there are arguments after "--", pass them to the underlying run_qemu.sh.
if [ $# -gt 0 ]; then
if [ $1 = "--" ]; then
shift 1
# Note: Due to a bug in older versions of Bash, use'${array[@]+"${array[@]}"}'
# to expand potentially empty arrays. '${array[@]}' is treated as undefined.
CMD+=(${@+"$@"})
else
echo "Unrecognized options: $@" 1>&2
usage 1>&2
exit 1
fi
fi
# Disable exiting on failure.
set +eo pipefail
if [ "${VERBOSE}" -eq 1 ]; then
echo "+ ${CMD[@]}" 1>&2
fi
if [ "${QUIET}" -eq 0 ]; then
exec "${CMD[@]}"
else
if [ -z "${OUTPUT}" ]; then
OUTPUT="$(mktemp)"
fi
mkdir -p "$(dirname "${OUTPUT}")"
"${CMD[@]}" &> "${OUTPUT}"
EXIT_STATUS=$?
RESULT=$(grep -E -h --color=never "[^ ](PASS|FAIL|SKIP)[^:]" "${OUTPUT}")
if [ -z "${RESULT}" ]; then
RESULT="\e[31;1mTIMEOUT/CRASH\e[0m"
EXIT_STATUS=1
fi
printf "%-${RESULT_ALIGN}s " "${DISPLAY_NAME}"
echo -e "${RESULT}"
exit ${EXIT_STATUS}
fi