xref: /cloud-hypervisor/scripts/test-util.sh (revision 261bfac4d47e4da0a8554b0968706ce30c6cc70c)
1#!/usr/bin/env bash
2hypervisor="kvm"
3test_filter=""
4build_kernel=false
5
6# Checkout source code of a GIT repo with specified branch and commit
7# Args:
8#   $1: Target directory
9#   $2: GIT URL of the repo
10#   $3: Required branch
11#   $4: Required commit (optional)
12checkout_repo() {
13    SRC_DIR="$1"
14    GIT_URL="$2"
15    GIT_BRANCH="$3"
16    GIT_COMMIT="$4"
17
18    # Check whether the local HEAD commit same as the requested commit or not.
19    # If commit is not specified, compare local HEAD and remote HEAD.
20    # Remove the folder if there is difference.
21    if [ -d "$SRC_DIR" ]; then
22        pushd "$SRC_DIR" || exit
23        git fetch
24        SRC_LOCAL_COMMIT=$(git rev-parse HEAD)
25        if [ -z "$GIT_COMMIT" ]; then
26            GIT_COMMIT=$(git rev-parse remotes/origin/"$GIT_BRANCH")
27        fi
28        popd || exit
29        if [ "$SRC_LOCAL_COMMIT" != "$GIT_COMMIT" ]; then
30            rm -rf "$SRC_DIR"
31        fi
32    fi
33
34    # Checkout the specified branch and commit (if required)
35    if [ ! -d "$SRC_DIR" ]; then
36        git clone --depth 1 "$GIT_URL" -b "$GIT_BRANCH" "$SRC_DIR"
37        if [ "$GIT_COMMIT" ]; then
38            pushd "$SRC_DIR" || exit
39            git fetch --depth 1 origin "$GIT_COMMIT"
40            git reset --hard FETCH_HEAD
41            popd || exit
42        fi
43    fi
44}
45
46# Not actively used by CI
47build_custom_linux() {
48    ARCH=$(uname -m)
49    SRCDIR=$PWD
50    LINUX_CUSTOM_DIR="$WORKLOADS_DIR/linux-custom"
51    LINUX_CUSTOM_BRANCH="ch-6.2"
52    LINUX_CUSTOM_URL="https://github.com/cloud-hypervisor/linux.git"
53
54    checkout_repo "$LINUX_CUSTOM_DIR" "$LINUX_CUSTOM_URL" "$LINUX_CUSTOM_BRANCH"
55
56    cp "$SRCDIR"/resources/linux-config-"${ARCH}" "$LINUX_CUSTOM_DIR"/.config
57
58    pushd "$LINUX_CUSTOM_DIR" || exit
59    make -j "$(nproc)"
60    if [ "${ARCH}" == "x86_64" ]; then
61        cp vmlinux "$WORKLOADS_DIR/" || exit 1
62        cp arch/x86/boot/bzImage "$WORKLOADS_DIR/" || exit 1
63    elif [ "${ARCH}" == "aarch64" ]; then
64        cp arch/arm64/boot/Image "$WORKLOADS_DIR/" || exit 1
65        cp arch/arm64/boot/Image.gz "$WORKLOADS_DIR/" || 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    if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then
144        echo "Using authenticated download from GitHub"
145        KERNEL_URLS=$(curl --silent https://api.github.com/repos/cloud-hypervisor/linux/releases/latest \
146            --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \
147            --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" | grep -o 'https://.*[^ "]')
148    else
149        echo "Using anonymous download from GitHub"
150        KERNEL_URLS=$(curl --silent https://api.github.com/repos/cloud-hypervisor/linux/releases/latest | grep "browser_download_url" | grep -o 'https://.*[^ "]')
151    fi
152    pushd "$WORKLOADS_DIR" || exit
153    for url in $KERNEL_URLS; do
154        wget -N --quiet "$url" || exit 1
155    done
156
157    popd || exit
158}
159
160prepare_linux() {
161    if [ "$build_kernel" = true ]; then
162        echo "Building kernel from source"
163        build_custom_linux
164        echo "Using kernel built from source"
165    else
166        echo "Downloading pre-built kernel from GitHub"
167        download_linux
168        echo "Using kernel downloaded from GitHub"
169    fi
170}
171
172download_ovmf() {
173    OVMF_FW_TAG="ch-6624aa331f"
174    OVMF_FW_URL="https://github.com/cloud-hypervisor/edk2/releases/download/$OVMF_FW_TAG/CLOUDHV.fd"
175    OVMF_FW="$WORKLOADS_DIR/CLOUDHV.fd"
176    pushd "$WORKLOADS_DIR" || exit
177    rm -f "$OVMF_FW"
178    time wget --quiet $OVMF_FW_URL || exit 1
179    popd || exit
180}
181