1#!/bin/bash
2#
3# XTF test utilities.
4#
5# Environment variables:
6#   BOOT_MSG: Expected boot message
7#   FW_PREFIX: Firmware images path including '/' at the end
8#   PASSED: XTF test printout in case of a pass
9#   QEMU_PREFIX: QEMU path including '/' at the end
10#   TEST_LOG: Output log file
11#   UBOOT_CMD: U-Boot command line
12#   WORKDIR: Test working directory
13#   XEN_BINARY: Xen binary location
14#   XEN_CONSOLE: Xen console device name
15#   XTF_SRC_CONFIG: XTF config file
16#   XTF_SRC_BRANCH: XTF branch
17#   XTF_SRC_URI: XTF source code URI
18
19# Output log file
20TEST_LOG="${TEST_LOG:-${XEN_ROOT}/smoke.serial}"
21# XTF test printout in case of a pass
22PASSED="${PASSED:-Test result: SUCCESS}"
23# Expected boot message
24BOOT_MSG="${BOOT_MSG:-Latest ChangeSet: }"
25# Test working directory
26WORKDIR="${WORKDIR:-${XEN_ROOT}/binaries}"
27# XTF source code
28XTF_SRC_CONFIG="${XTF_SRC_CONFIG:-include/configs/xtf-${ARCH}-config}"
29
30function die()
31{
32    set +x
33    echo "FATAL: $*" >&2
34    exit 1
35}
36
37# Build an XTF test binary.
38# $1 Test variant.
39# $2 Test name.
40function xtf_build_binary()
41{
42    local xtf_variant=$1
43    local xtf_name=$2
44    local xtf_dir="xtf-${ARCH}"
45
46    # Crude check for local testing
47    if [ ! -d ${xtf_dir} ]; then
48        git clone ${XTF_SRC_URI} ${xtf_dir} -b ${XTF_SRC_BRANCH}
49    fi
50
51    make \
52        -C ${xtf_dir} \
53        -j$(nproc) \
54        $(tr '\n' ' ' < ${XTF_SRC_CONFIG}) \
55        TESTS=tests/${xtf_name}
56
57    export XTF_NAME="${xtf_name}"
58    export XTF_VARIANT="${xtf_variant}"
59    export XTF_WORKDIR="$(readlink -f ${xtf_dir})"
60    export XTF_BINARY="${XTF_WORKDIR}/tests/${xtf_name}/test-${xtf_variant}-${xtf_name}"
61}
62
63# Build Xen command line for running an XTF test.
64# $1 Test variant.
65# $2 Test name.
66function xtf_build_cmdline()
67{
68    local xtf_variant=$1
69    local xtf_name=$2
70    declare -a cmdline=()
71    declare -A per_test_args=(
72        [argo]="argo=1 mac-permissive=1"
73    )
74
75    cmdline+=("${XEN_CMDLINE}")
76
77    # NB: OK to have hvm64, which is x86-only variant
78    if [[ $xtf_variant == "hvm64" ]]; then
79        cmdline+=("dom0-iommu=none dom0=pvh")
80    fi
81
82    if [[ -v per_test_args[${xtf_name}] ]]; then
83        cmdline+=("${per_test_args[${xtf_name}]}")
84    fi
85
86    export XEN_CMDLINE="${cmdline[@]}"
87}
88
89# Build an XTF test environment.
90# $1 Test variant.
91# $2 Test name.
92function xtf_build_test()
93{
94    local v=$1
95    local xtf_name=$2
96    local xtf_variant=""
97
98    for x in ${XTF_SRC_VARIANTS}; do
99        if [[ "${x}" == "${v}" ]]; then
100            xtf_variant=${v}
101            break
102        fi
103    done
104    if [[ -z $xtf_variant ]]; then
105        die "unsupported test variant '$1', supported variants: ${XTF_SRC_VARIANTS}"
106    fi
107
108    xtf_build_binary ${xtf_variant} ${xtf_name}
109    xtf_build_cmdline ${xtf_variant} ${xtf_name}
110}
111
112# Execute an XTF test.
113function xtf_run_test()
114{
115    rm -f ${TEST_LOG}
116    export BOOT_MSG PASSED TEST_CMD TEST_LOG UBOOT_CMD
117    ./console.exp | sed 's/\r\+$//'
118}
119
120# Setup environment and run an XTF test.
121# $1 Test variant.
122# $2 Test name.
123function xtf_test()
124{
125    # Out: FW_*, QEMU_*, XEN_{BINARY,CONSOLE}, XTF_SRC_*
126    xtf_arch_prepare
127
128    # In: XTF_SRC_*
129    # OUt: XTF_{BINARY,NAME,VARIANT,WORKDIR} and XEN_CMDLINE
130    xtf_build_test $@
131
132    # In: FW_*, QEMU_*, XTF_*, XEN_*
133    # Out: BOOT_MSG, PASSED, TEST_{CMD,LOG}, UBOOT_CMD
134    xtf_arch_setup
135
136    # In: BOOT_MSG, PASSED, TEST_{CMD,LOG}, UBOOT_CMD
137    xtf_run_test
138}
139