From 26a1cb6f3cc62961734f242f118d3b5e7f33595a Mon Sep 17 00:00:00 2001 From: "Jorge E. Moreira" Date: Thu, 29 Feb 2024 15:24:52 -0800 Subject: [PATCH] Run e2e tests as kokoro presubmit --- .kokoro/prepare_host.sh | 109 ++++++++++++++++++++++++++++++++++++++++ .kokoro/presubmit.cfg | 8 +-- .kokoro/presubmit.sh | 11 ++++ .kokoro/public_test.sh | 2 +- .kokoro/runtests.sh | 31 ++++++++++++ 5 files changed, 157 insertions(+), 4 deletions(-) create mode 100755 .kokoro/prepare_host.sh create mode 100755 .kokoro/presubmit.sh create mode 100755 .kokoro/runtests.sh diff --git a/.kokoro/prepare_host.sh b/.kokoro/prepare_host.sh new file mode 100755 index 0000000000..05d9334b78 --- /dev/null +++ b/.kokoro/prepare_host.sh @@ -0,0 +1,109 @@ +#!/bin/bash + +set -e -x + +function install_debuild_dependencies() { + echo "Installing debuild dependencies" + sudo apt-get update + sudo DEBIAN_FRONTEND=noninteractive apt-get -y \ + -o Dpkg::Options::="--force-confold" \ + -o Dpkg::Options::="--force-confdef" \ + upgrade + sudo apt-get install -y devscripts config-package-dev debhelper-compat equivs +} + +function install_bazel() { + # From https://bazel.build/install/ubuntu + echo "Installing bazel" + sudo apt install apt-transport-https curl gnupg -y + curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor >bazel-archive-keyring.gpg + sudo mv bazel-archive-keyring.gpg /usr/share/keyrings + echo "deb [arch=amd64 signed-by=/usr/share/keyrings/bazel-archive-keyring.gpg] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list + # bazel needs the zip command to gather test outputs but doesn't depend on it + sudo apt-get update && sudo apt-get install -y bazel zip unzip +} + +function build_and_install_pkgs() { + local pkgdir="$1" + shift + pushd "${pkgdir}" + echo "Installing dependencies for: $@" + sudo mk-build-deps -i -t 'apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y' + echo "Building packages: $@" + debuild -i -uc -us -b + for pkg in "$@"; do + echo "Installing package: ${pkg}" + sudo apt-get install -y ../"${pkg}"_*_*64.deb + done + popd +} + +function check_service_started() { + local service="$1" + echo "Checking service ${service} status" + systemctl is-active "${service}" +} + +function load_kernel_modules() { + echo "Loading kernel modules" + sudo modprobe "$@" +} + +function grant_device_access() { + for d in "$@"; do + ls -l /dev/"${d}" + sudo chmod a+rw /dev/"${d}" + done +} + +function create_test_user() { + local username=$1 + local groups="kvm,cvdnetwork" + if [[ "$2" != "" ]]; then + groups="${groups},$2" + fi + echo "Creating user: ${username}" + sudo useradd -G "${groups}" -m "${username}" + sudo chmod a+rx "/home/${username}" +} + + +REPO_DIR="$(realpath "$(dirname "$0")/..")" +TEST_USER="" +EXTRA_GROUPS="" +while getopts "d:u:g:" opt; do + case "${opt}" in + u) + TEST_USER="${OPTARG}" + ;; + g) + EXTRA_GROUPS="${OPTARG}" + ;; + *) + echo "Invalid option: -${opt}" + echo "Usage: $0 [-u TEST_USER [-g EXTRA_GROUPS]]" + exit 1 + ;; + esac +done + +if [[ "${REPO_DIR}" == "" ]] || ! [[ -d "${REPO_DIR}" ]]; then + echo "Invalid repo directory: ${REPO_DIR}" + exit 1 +fi + +install_debuild_dependencies + +install_bazel + +build_and_install_pkgs "${REPO_DIR}/base" cuttlefish-base +check_service_started cuttlefish-host-resources +load_kernel_modules kvm vhost-vsock vhost-net bridge +grant_device_access vhost-vsock vhost-net kvm + +build_and_install_pkgs "${REPO_DIR}/frontend" cuttlefish-user +check_service_started cuttlefish-operator + +if [[ "${TEST_USER}" != "" ]]; then + create_test_user "${TEST_USER}" "${EXTRA_GROUPS}" +fi diff --git a/.kokoro/presubmit.cfg b/.kokoro/presubmit.cfg index 795cc99912..7f371b1f12 100644 --- a/.kokoro/presubmit.cfg +++ b/.kokoro/presubmit.cfg @@ -1,8 +1,10 @@ -build_file: "android-cuttlefish/.kokoro/public_test.sh" +build_file: "android-cuttlefish/.kokoro/presubmit.sh" action { define_artifacts { - regex: "**/*sponge_log.xml" # Actually a glob, not a regex - regex: "**/*sponge_log.log" + regex: "*/*sponge_log.xml" # Actually a glob, not a regex + regex: "*/*sponge_log.log" + regex: "*/device_logs/*" + regex: "github/android-cuttlefish/*.deb" } } diff --git a/.kokoro/presubmit.sh b/.kokoro/presubmit.sh new file mode 100755 index 0000000000..a6e1a2cc6a --- /dev/null +++ b/.kokoro/presubmit.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -e -x + +TOOL_DIR="$(realpath "$(dirname "$0")")" + +# Add test user to the kokoro group so it has access to the source dir +"${TOOL_DIR}/prepare_host.sh" -u testrunner -g kokoro + +# Run as different user without sudo privileges +sudo -u testrunner "${TOOL_DIR}/runtests.sh" ... diff --git a/.kokoro/public_test.sh b/.kokoro/public_test.sh index 710cd27084..29b311ee19 100755 --- a/.kokoro/public_test.sh +++ b/.kokoro/public_test.sh @@ -1,7 +1,7 @@ #!/bin/bash # Fail on errors -set -e +set -e -x echo "Cuttlefish debian package build and testing placeholder script for kokoro" diff --git a/.kokoro/runtests.sh b/.kokoro/runtests.sh new file mode 100755 index 0000000000..749390227b --- /dev/null +++ b/.kokoro/runtests.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +set -x -e + +REPO_DIR="$(realpath "$(dirname "$0")/..")" +OUTPUT_DIR="$(pwd)" + +function gather_test_results() { + # Don't immediately exit on error anymore + set +e + for d in ./bazel-testlogs/*; do + dir="${OUTPUT_DIR}/$(basename "$d")" + mkdir -p "${dir}" + cp "${d}/test.log" "${dir}/sponge_log.log" + cp "${d}/test.xml" "${dir}/sponge_log.xml" + if [[ -f "${d}/test.outputs/outputs.zip" ]]; then + mkdir -p "${dir}" + unzip "${d}/test.outputs/outputs.zip" -d "${dir}/device_logs" + fi + # Make sure everyone has access to the output files + chmod -R a+rw "${dir}" + done +} + +cd "${REPO_DIR}/e2etests" + +# Gather test results regardless of status, but still return the exit code from +# those tests +trap gather_test_results EXIT +bazel test "$@" +