xref: /cloud-hypervisor/scripts/test-util.sh (revision 3ce0fef7fd546467398c914dbc74d8542e45cf6f)
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
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
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"
38            git fetch --depth 1 origin "$GIT_COMMIT"
39            git reset --hard FETCH_HEAD
40            popd
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
57    make -j `nproc`
58    if [ ${ARCH} == "x86_64" ]; then
59       cp vmlinux "$WORKLOADS_DIR/" || exit 1
60    elif [ ${ARCH} == "aarch64" ]; then
61       cp arch/arm64/boot/Image "$WORKLOADS_DIR/" || exit 1
62       cp arch/arm64/boot/Image.gz "$WORKLOADS_DIR/" || exit 1
63    fi
64    popd
65}
66
67cmd_help() {
68    echo ""
69    echo "Cloud Hypervisor $(basename $0)"
70    echo "Usage: $(basename $0) [<args>]"
71    echo ""
72    echo "Available arguments:"
73    echo ""
74    echo "    --hypervisor  Underlying hypervisor. Options kvm, mshv"
75    echo "    --test-filter Tests to run"
76    echo ""
77    echo "    --help        Display this help message."
78    echo ""
79}
80
81process_common_args() {
82    while [ $# -gt 0 ]; do
83	case "$1" in
84            "-h"|"--help")  { cmd_help; exit 1; } ;;
85            "--hypervisor")
86                shift
87                hypervisor="$1"
88                ;;
89            "--test-filter")
90                shift
91                test_filter="$1"
92                ;;
93            "--") {
94                shift
95                break
96            } ;;
97            *)
98                echo "Unknown test scripts argument: $1. Please use '-- --help' for help."
99                exit
100                ;;
101	esac
102	shift
103    done
104    if [[ ! ("$hypervisor" = "kvm" ||  "$hypervisor" = "mshv") ]]; then
105        die "Hypervisor value must be kvm or mshv"
106    fi
107
108    test_binary_args=($@)
109}
110
111download_hypervisor_fw() {
112    if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then
113        echo "Using authenticated download from GitHub"
114        FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/latest \
115                 --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \
116                 --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" | grep -o 'https://.*[^ "]')
117    else
118        echo "Using anonymous download from GitHub"
119        FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/latest | grep "browser_download_url" | grep -o 'https://.*[^ "]')
120    fi
121    FW="$WORKLOADS_DIR/hypervisor-fw"
122    pushd $WORKLOADS_DIR
123    rm -f $FW
124    time wget --quiet $FW_URL || exit 1
125    popd
126}
127
128download_ovmf() {
129    OVMF_FW_TAG="ch-6624aa331f"
130    OVMF_FW_URL="https://github.com/cloud-hypervisor/edk2/releases/download/$OVMF_FW_TAG/CLOUDHV.fd"
131    OVMF_FW="$WORKLOADS_DIR/CLOUDHV.fd"
132    pushd $WORKLOADS_DIR
133    rm -f $OVMF_FW
134    time wget --quiet $OVMF_FW_URL || exit 1
135    popd
136}
137