| #!/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. |
| |
| |
| # Variables to represent VHE and nVHE emulation parameter |
| VHE=1 |
| NVHE=0 |
| |
| exit_status=0 |
| script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
| test_dir="" |
| log_dir=${LOG_DIR:-out/test/kvm-unit-tests} |
| exclude_regex="" |
| j=1 |
| linux="" |
| rootfs="" |
| verbose=${VERBOSE:-0} |
| qemu=${QEMU:-qemu-system-aarch64} |
| |
| function get_outcome() |
| { |
| grep -E -h --color=never "[^ ](PASS|FAIL|SKIP)[^:]" |
| } |
| |
| function usage() |
| { |
| cat <<EOF |
| |
| Usage: $0 [-h] [-t TEST-FILES] [-j NUM-TASKS] [-l KERNEL] [-e QEMU] [-r ROOT] |
| |
| -h Output this help text |
| -t kvm-unit-tests standalone directory |
| -x tests to exclude (regex), can be specified more than once |
| -j Execute tests in parallel |
| -l Linux kernel image to use |
| -e QEMU emulator binary |
| -r Root file system image to use |
| |
| EOF |
| } |
| |
| function usage_abort() |
| { |
| usage |
| exit 1 |
| } |
| |
| function wait_free_job_slots() |
| { |
| while (( $(jobs | wc -l) == j )); do |
| # Wait for enough jobs to finish |
| wait -n || exit_status=1 |
| done |
| } |
| |
| function wait_background_jobs() |
| { |
| while (( $(jobs | wc -l) > 0 )); do |
| # Wait for background jobs |
| wait -n || exit_status=1 |
| done |
| } |
| |
| function run_job() |
| { |
| wait_free_job_slots |
| |
| if [ "$j" = 1 ]; then |
| bash -c "$@" || exit_status=1 |
| else |
| bash -c "$@" & |
| fi |
| } |
| |
| while getopts ":t:x:j:l:r:e:hv" opt; do |
| case "${opt}" in |
| t) |
| test_dir=${OPTARG} |
| ;; |
| x) |
| # OR with the previous regex if specified |
| if [ -n "${exclude_regex}" ]; then |
| exclude_regex+="|" |
| fi |
| exclude_regex+="(^${OPTARG}$)" |
| ;; |
| j) |
| j=${OPTARG} |
| ;; |
| l) |
| linux=${OPTARG} |
| ;; |
| r) |
| rootfs=${OPTARG} |
| ;; |
| e) |
| qemu=${OPTARG} |
| ;; |
| h) |
| usage |
| exit 0 |
| ;; |
| v) |
| verbose=1 |
| ;; |
| *) |
| usage_abort |
| ;; |
| esac |
| done |
| if ((OPTIND == 1)); then |
| usage_abort |
| fi |
| shift $((OPTIND-1)) |
| |
| if [[ ! -d ${test_dir} ]]; then |
| echo "No test directory specified." |
| usage_abort |
| fi |
| |
| if [[ ! -f ${linux} ]]; then |
| echo "Linux kernel image file not found." |
| usage_abort |
| fi |
| |
| if [[ ! -f ${rootfs} ]]; then |
| echo "Root filesystem image file not found." |
| usage_abort |
| fi |
| |
| if [[ ! -x ${qemu} ]]; then |
| echo "QEMU executable not found." |
| usage_abort |
| fi |
| |
| rm -rf "$log_dir"/logs.old |
| if [ -d "$log_dir"/logs ]; then |
| mv "$log_dir"/logs "$log_dir"/logs.old |
| fi |
| mkdir -p "$log_dir"/logs/vhe || exit 2 |
| mkdir -p "$log_dir"/logs/nvhe || exit 2 |
| |
| for test in "${test_dir}"/*; do |
| test_name=$(basename "${test}") |
| |
| if [[ -n "${exclude_regex}" && "${test_name}" =~ ${exclude_regex} ]]; then |
| if [ "$verbose" != "0" ]; then |
| echo "Excluding ${test_name}" |
| fi |
| continue |
| fi |
| |
| if [ "$verbose" != "0" ]; then |
| echo "Running ${test_name}" |
| fi |
| |
| # Redirect output instead of pipe to preserve exit status. |
| run_job "${script_dir}/run_emu.sh ${test} ${linux} ${rootfs} ${qemu} ${VHE} ${log_dir}/logs/vhe/${test_name}.log \"${test_name} (vhe)\" &> >(tail -1)" |
| run_job "${script_dir}/run_emu.sh ${test} ${linux} ${rootfs} ${qemu} ${NVHE} ${log_dir}/logs/nvhe/${test_name}.log \"${test_name} (nvhe)\" &> >(tail -1)" |
| done |
| |
| wait_background_jobs |
| |
| exit $exit_status |