xref: /cloud-hypervisor/scripts/test-util.sh (revision 19d36c765fdf00be749d95b3e61028bc302d6d73)
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
45# Not actively used by CI
46build_custom_linux() {
47    ARCH=$(uname -m)
48    SRCDIR=$PWD
49    LINUX_CUSTOM_DIR="$WORKLOADS_DIR/linux-custom"
50    LINUX_CUSTOM_BRANCH="ch-6.2"
51    LINUX_CUSTOM_URL="https://github.com/cloud-hypervisor/linux.git"
52
53    checkout_repo "$LINUX_CUSTOM_DIR" "$LINUX_CUSTOM_URL" "$LINUX_CUSTOM_BRANCH"
54
55    cp "$SRCDIR"/resources/linux-config-"${ARCH}" "$LINUX_CUSTOM_DIR"/.config
56
57    pushd "$LINUX_CUSTOM_DIR" || exit
58    make -j "$(nproc)"
59    if [ "${ARCH}" == "x86_64" ]; then
60        cp vmlinux "$WORKLOADS_DIR/" || exit 1
61        cp arch/x86/boot/bzImage "$WORKLOADS_DIR/" || exit 1
62    elif [ "${ARCH}" == "aarch64" ]; then
63        cp arch/arm64/boot/Image "$WORKLOADS_DIR/" || exit 1
64        cp arch/arm64/boot/Image.gz "$WORKLOADS_DIR/" || exit 1
65    fi
66    popd || exit
67}
68
69cmd_help() {
70    echo ""
71    echo "Cloud Hypervisor $(basename "$0")"
72    echo "Usage: $(basename "$0") [<args>]"
73    echo ""
74    echo "Available arguments:"
75    echo ""
76    echo "    --hypervisor  Underlying hypervisor. Options kvm, mshv"
77    echo "    --test-filter Tests to run"
78    echo ""
79    echo "    --help        Display this help message."
80    echo ""
81}
82
83process_common_args() {
84    while [ $# -gt 0 ]; do
85        case "$1" in
86        "-h" | "--help") {
87            cmd_help
88            exit 1
89        } ;;
90        "--hypervisor")
91            shift
92            hypervisor="$1"
93            ;;
94        "--test-filter")
95            shift
96            # shellcheck disable=SC2034
97            test_filter="$1"
98            ;;
99        "--") {
100            shift
101            break
102        } ;;
103        *)
104            echo "Unknown test scripts argument: $1. Please use '-- --help' for help."
105            exit
106            ;;
107        esac
108        shift
109    done
110    if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then
111        die "Hypervisor value must be kvm or mshv"
112    fi
113    # shellcheck disable=SC2034
114    test_binary_args=("$@")
115}
116
117download_hypervisor_fw() {
118    if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then
119        echo "Using authenticated download from GitHub"
120        FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/latest \
121            --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \
122            --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" | grep -o 'https://.*[^ "]')
123    else
124        echo "Using anonymous download from GitHub"
125        FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/latest | grep "browser_download_url" | grep -o 'https://.*[^ "]')
126    fi
127    FW="$WORKLOADS_DIR/hypervisor-fw"
128    pushd "$WORKLOADS_DIR" || exit
129    rm -f "$FW"
130    time wget --quiet "$FW_URL" || exit 1
131    popd || exit
132}
133
134download_linux() {
135    if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then
136        echo "Using authenticated download from GitHub"
137        KERNEL_URLS=$(curl --silent https://api.github.com/repos/cloud-hypervisor/linux/releases/latest \
138            --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \
139            --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" | grep -o 'https://.*[^ "]')
140    else
141        echo "Using anonymous download from GitHub"
142        KERNEL_URLS=$(curl --silent https://api.github.com/repos/cloud-hypervisor/linux/releases/latest | grep "browser_download_url" | grep -o 'https://.*[^ "]')
143    fi
144    pushd "$WORKLOADS_DIR" || exit
145    for url in $KERNEL_URLS; do
146        wget -N --quiet "$url" || exit 1
147    done
148
149    popd || exit
150}
151
152download_ovmf() {
153    OVMF_FW_TAG="ch-6624aa331f"
154    OVMF_FW_URL="https://github.com/cloud-hypervisor/edk2/releases/download/$OVMF_FW_TAG/CLOUDHV.fd"
155    OVMF_FW="$WORKLOADS_DIR/CLOUDHV.fd"
156    pushd "$WORKLOADS_DIR" || exit
157    rm -f "$OVMF_FW"
158    time wget --quiet $OVMF_FW_URL || exit 1
159    popd || exit
160}
161