xref: /cloud-hypervisor/scripts/test-util.sh (revision 3f8cd52ffd74627242cb7e8ea1c2bdedadf6741a)
1#!/usr/bin/env bash
2hypervisor="kvm"
3test_filter=""
4
5# Checkout source code of a GIT repo with specified branch and commit
6# Args:
7#   $1: Target directory
8#   $2: GIT URL of the repo
9#   $3: Required branch
10#   $4: Required commit (optional)
11checkout_repo() {
12    SRC_DIR="$1"
13    GIT_URL="$2"
14    GIT_BRANCH="$3"
15    GIT_COMMIT="$4"
16
17    # Check whether the local HEAD commit same as the requested commit or not.
18    # If commit is not specified, compare local HEAD and remote HEAD.
19    # Remove the folder if there is difference.
20    if [ -d "$SRC_DIR" ]; then
21        pushd "$SRC_DIR" || exit
22        git fetch
23        SRC_LOCAL_COMMIT=$(git rev-parse HEAD)
24        if [ -z "$GIT_COMMIT" ]; then
25            GIT_COMMIT=$(git rev-parse remotes/origin/"$GIT_BRANCH")
26        fi
27        popd || exit
28        if [ "$SRC_LOCAL_COMMIT" != "$GIT_COMMIT" ]; then
29            rm -rf "$SRC_DIR"
30        fi
31    fi
32
33    # Checkout the specified branch and commit (if required)
34    if [ ! -d "$SRC_DIR" ]; then
35        git clone --depth 1 "$GIT_URL" -b "$GIT_BRANCH" "$SRC_DIR"
36        if [ "$GIT_COMMIT" ]; then
37            pushd "$SRC_DIR" || exit
38            git fetch --depth 1 origin "$GIT_COMMIT"
39            git reset --hard FETCH_HEAD
40            popd || exit
41        fi
42    fi
43}
44
45build_custom_linux() {
46    ARCH=$(uname -m)
47    SRCDIR=$PWD
48    LINUX_CUSTOM_DIR="$WORKLOADS_DIR/linux-custom"
49    LINUX_CUSTOM_BRANCH="ch-6.2"
50    LINUX_CUSTOM_URL="https://github.com/cloud-hypervisor/linux.git"
51
52    checkout_repo "$LINUX_CUSTOM_DIR" "$LINUX_CUSTOM_URL" "$LINUX_CUSTOM_BRANCH"
53
54    cp "$SRCDIR"/resources/linux-config-"${ARCH}" "$LINUX_CUSTOM_DIR"/.config
55
56    pushd "$LINUX_CUSTOM_DIR" || exit
57    make -j "$(nproc)"
58    if [ "${ARCH}" == "x86_64" ]; then
59        cp vmlinux "$WORKLOADS_DIR/" || exit 1
60        cp arch/x86/boot/bzImage "$WORKLOADS_DIR/" || exit 1
61    elif [ "${ARCH}" == "aarch64" ]; then
62        cp arch/arm64/boot/Image "$WORKLOADS_DIR/" || exit 1
63        cp arch/arm64/boot/Image.gz "$WORKLOADS_DIR/" || exit 1
64    fi
65    popd || exit
66}
67
68cmd_help() {
69    echo ""
70    echo "Cloud Hypervisor $(basename "$0")"
71    echo "Usage: $(basename "$0") [<args>]"
72    echo ""
73    echo "Available arguments:"
74    echo ""
75    echo "    --hypervisor  Underlying hypervisor. Options kvm, mshv"
76    echo "    --test-filter Tests to run"
77    echo ""
78    echo "    --help        Display this help message."
79    echo ""
80}
81
82process_common_args() {
83    while [ $# -gt 0 ]; do
84        case "$1" in
85        "-h" | "--help") {
86            cmd_help
87            exit 1
88        } ;;
89        "--hypervisor")
90            shift
91            hypervisor="$1"
92            ;;
93        "--test-filter")
94            shift
95            # shellcheck disable=SC2034
96            test_filter="$1"
97            ;;
98        "--") {
99            shift
100            break
101        } ;;
102        *)
103            echo "Unknown test scripts argument: $1. Please use '-- --help' for help."
104            exit
105            ;;
106        esac
107        shift
108    done
109    if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then
110        die "Hypervisor value must be kvm or mshv"
111    fi
112    # shellcheck disable=SC2034
113    test_binary_args=("$@")
114}
115
116download_hypervisor_fw() {
117    if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then
118        echo "Using authenticated download from GitHub"
119        FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/latest \
120            --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \
121            --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" | grep -o 'https://.*[^ "]')
122    else
123        echo "Using anonymous download from GitHub"
124        FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/latest | grep "browser_download_url" | grep -o 'https://.*[^ "]')
125    fi
126    FW="$WORKLOADS_DIR/hypervisor-fw"
127    pushd "$WORKLOADS_DIR" || exit
128    rm -f "$FW"
129    time wget --quiet "$FW_URL" || exit 1
130    popd || exit
131}
132
133download_ovmf() {
134    OVMF_FW_TAG="ch-6624aa331f"
135    OVMF_FW_URL="https://github.com/cloud-hypervisor/edk2/releases/download/$OVMF_FW_TAG/CLOUDHV.fd"
136    OVMF_FW="$WORKLOADS_DIR/CLOUDHV.fd"
137    pushd "$WORKLOADS_DIR" || exit
138    rm -f "$OVMF_FW"
139    time wget --quiet $OVMF_FW_URL || exit 1
140    popd || exit
141}
142