xref: /cloud-hypervisor/scripts/test-util.sh (revision 2b05753716936506ed440863fe6a29dfc7a427e2)
1#!/usr/bin/env bash
2set -x
3
4hypervisor="kvm"
5test_filter=""
6build_kernel=false
7
8# Checkout source code of a GIT repo with specified branch and commit
9# Args:
10#   $1: Target directory
11#   $2: GIT URL of the repo
12#   $3: Required branch
13#   $4: Required commit (optional)
14checkout_repo() {
15    SRC_DIR="$1"
16    GIT_URL="$2"
17    GIT_BRANCH="$3"
18    GIT_COMMIT="$4"
19
20    # Check whether the local HEAD commit same as the requested commit or not.
21    # If commit is not specified, compare local HEAD and remote HEAD.
22    # Remove the folder if there is difference.
23    if [ -d "$SRC_DIR" ]; then
24        pushd "$SRC_DIR" || exit
25        git fetch
26        SRC_LOCAL_COMMIT=$(git rev-parse HEAD)
27        if [ -z "$GIT_COMMIT" ]; then
28            GIT_COMMIT=$(git rev-parse remotes/origin/"$GIT_BRANCH")
29        fi
30        popd || exit
31        if [ "$SRC_LOCAL_COMMIT" != "$GIT_COMMIT" ]; then
32            rm -rf "$SRC_DIR"
33        fi
34    fi
35
36    # Checkout the specified branch and commit (if required)
37    if [ ! -d "$SRC_DIR" ]; then
38        git clone --depth 1 "$GIT_URL" -b "$GIT_BRANCH" "$SRC_DIR"
39        if [ "$GIT_COMMIT" ]; then
40            pushd "$SRC_DIR" || exit
41            git fetch --depth 1 origin "$GIT_COMMIT"
42            git reset --hard FETCH_HEAD
43            popd || exit
44        fi
45    fi
46}
47
48# Not actively used by CI
49build_custom_linux() {
50    ARCH=$(uname -m)
51    LINUX_CUSTOM_DIR="$WORKLOADS_DIR/linux-custom"
52    LINUX_CUSTOM_BRANCH="ch-6.12.8"
53    LINUX_CUSTOM_URL="https://github.com/cloud-hypervisor/linux.git"
54
55    checkout_repo "$LINUX_CUSTOM_DIR" "$LINUX_CUSTOM_URL" "$LINUX_CUSTOM_BRANCH"
56
57    pushd "$LINUX_CUSTOM_DIR" || exit
58    make ch_defconfig
59    make -j "$(nproc)"
60    if [ "${ARCH}" == "x86_64" ]; then
61        cp vmlinux "$WORKLOADS_DIR/vmlinux-x86_64" || exit 1
62        cp arch/x86/boot/bzImage "$WORKLOADS_DIR/bzImage-x86_64" || exit 1
63    elif [ "${ARCH}" == "aarch64" ]; then
64        cp arch/arm64/boot/Image "$WORKLOADS_DIR/Image-arm64" || exit 1
65        cp arch/arm64/boot/Image.gz "$WORKLOADS_DIR/Image-arm64.gz" || exit 1
66    fi
67    popd || exit
68}
69
70cmd_help() {
71    echo ""
72    echo "Cloud Hypervisor $(basename "$0")"
73    echo "Usage: $(basename "$0") [<args>]"
74    echo ""
75    echo "Available arguments:"
76    echo ""
77    echo "    --hypervisor  Underlying hypervisor. Options kvm, mshv"
78    echo "    --test-filter Tests to run"
79    echo "    --build-guest-kernel Build guest kernel from source instead of downloading pre-built"
80    echo ""
81    echo "    --help        Display this help message."
82    echo ""
83}
84
85# shellcheck disable=SC2034
86process_common_args() {
87    while [ $# -gt 0 ]; do
88        case "$1" in
89        "-h" | "--help") {
90            cmd_help
91            exit 1
92        } ;;
93        "--hypervisor")
94            shift
95            hypervisor="$1"
96            ;;
97        "--test-filter")
98            shift
99            test_filter="$1"
100            ;;
101        "--build-guest-kernel")
102            build_kernel=true
103            ;;
104        "--") {
105            shift
106            break
107        } ;;
108        *)
109            echo "Unknown test scripts argument: $1. Please use '-- --help' for help."
110            exit
111            ;;
112        esac
113        shift
114    done
115    if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then
116        die "Hypervisor value must be kvm or mshv"
117    fi
118    # shellcheck disable=SC2034
119    test_binary_args=("$@")
120}
121
122download_hypervisor_fw() {
123    FW_TAG="0.5.0"
124    if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then
125        echo "Using authenticated download from GitHub"
126        FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/tags/${FW_TAG} \
127            --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \
128            --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" |
129            grep -oP '"https://[^"]*hypervisor-fw"' | sed -e 's/^"//' -e 's/"$//')
130    else
131        echo "Using anonymous download from GitHub"
132        FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/tags/${FW_TAG} |
133            grep "browser_download_url" | grep -oP '"https://[^"]*hypervisor-fw"' | sed -e 's/^"//' -e 's/"$//')
134    fi
135    FW="$WORKLOADS_DIR/hypervisor-fw"
136    pushd "$WORKLOADS_DIR" || exit
137    rm -f "$FW"
138    time wget --quiet "$FW_URL" || exit 1
139    popd || exit
140}
141
142download_linux() {
143    KERNEL_TAG="ch-release-v6.12.8-20250613"
144    if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then
145        echo "Using authenticated download from GitHub"
146        KERNEL_URLS=$(curl --silent https://api.github.com/repos/cloud-hypervisor/linux/releases/tags/${KERNEL_TAG} \
147            --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \
148            --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" | grep -o 'https://.*[^ "]')
149    else
150        echo "Using anonymous download from GitHub"
151        KERNEL_URLS=$(curl --silent https://api.github.com/repos/cloud-hypervisor/linux/releases/tags/${KERNEL_TAG} | grep "browser_download_url" | grep -o 'https://.*[^ "]')
152    fi
153    pushd "$WORKLOADS_DIR" || exit
154    for url in $KERNEL_URLS; do
155        wget -N --quiet "$url" || exit 1
156    done
157
158    popd || exit
159}
160
161prepare_linux() {
162    if [ "$build_kernel" = true ]; then
163        echo "Building kernel from source"
164        build_custom_linux
165        echo "Using kernel built from source"
166    else
167        echo "Downloading pre-built kernel from GitHub"
168        download_linux
169        echo "Using kernel downloaded from GitHub"
170    fi
171}
172
173download_ovmf() {
174    OVMF_FW_TAG="ch-a54f262b09"
175    OVMF_FW_URL="https://github.com/cloud-hypervisor/edk2/releases/download/$OVMF_FW_TAG/CLOUDHV.fd"
176    OVMF_FW="$WORKLOADS_DIR/CLOUDHV.fd"
177    pushd "$WORKLOADS_DIR" || exit
178    rm -f "$OVMF_FW"
179    time wget --quiet $OVMF_FW_URL || exit 1
180    popd || exit
181}
182