From 13c2577004df7edb8392c36d0d76566c0a1e6081 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 25 Mar 2022 20:00:30 +1100 Subject: [PATCH] runtime: Move go testing script locally The go unit tests for the runtime are invoked by the helper script ci/go-test.sh. Which calls the run_go_test() function in ci/lib.sh. Which calls into .ci/go-test.sh from the tests repository. But.. the runtime is the only user of this script, and generally stuff for unit tests (rather than functional or integration tests) lives in the main repository, not the tests repository. So, just move the actual script into src/runtime. A change to remove it from the tests repo will follow. Signed-off-by: David Gibson --- ci/go-test.sh | 12 -- ci/lib.sh | 6 - src/runtime/Makefile | 2 +- src/runtime/go-test.sh | 258 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 259 insertions(+), 19 deletions(-) delete mode 100755 ci/go-test.sh create mode 100755 src/runtime/go-test.sh diff --git a/ci/go-test.sh b/ci/go-test.sh deleted file mode 100755 index ca1ad0dfdb..0000000000 --- a/ci/go-test.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash -# -# Copyright (c) 2020 Intel Corporation -# -# SPDX-License-Identifier: Apache-2.0 - -set -e - -cidir=$(dirname "$0") -source "${cidir}/lib.sh" - -run_go_test diff --git a/ci/lib.sh b/ci/lib.sh index 51f99bb4df..f7f1eeeb75 100644 --- a/ci/lib.sh +++ b/ci/lib.sh @@ -39,12 +39,6 @@ run_static_checks() bash "$tests_repo_dir/.ci/static-checks.sh" "$@" } -run_go_test() -{ - clone_tests_repo - bash "$tests_repo_dir/.ci/go-test.sh" -} - run_docs_url_alive_check() { clone_tests_repo diff --git a/src/runtime/Makefile b/src/runtime/Makefile index 8f18568218..774f491f59 100644 --- a/src/runtime/Makefile +++ b/src/runtime/Makefile @@ -596,7 +596,7 @@ hook: go-test: $(GENERATED_FILES) go clean -testcache - $(QUIET_TEST)../../ci/go-test.sh + $(QUIET_TEST)./go-test.sh fast-test: $(GENERATED_FILES) go clean -testcache diff --git a/src/runtime/go-test.sh b/src/runtime/go-test.sh new file mode 100755 index 0000000000..949c4f2752 --- /dev/null +++ b/src/runtime/go-test.sh @@ -0,0 +1,258 @@ +#!/bin/bash +# +# Copyright (c) 2017-2018 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 +# + +set -e + +script_name=${0##*/} +typeset -A long_options + +long_options=( + [help]="Show usage" + [list]="List available packages" + [package:]="Specify test package to run" +) + +# Set default test run timeout value. +# +# KATA_GO_TEST_TIMEOUT can be set to any value accepted by +# "go test -timeout X" +timeout_value=${KATA_GO_TEST_TIMEOUT:-30s} + +# -race flag is not supported on s390x +[ "$(go env GOARCH)" != "s390x" ] && race="-race" + +# Notes: +# +# - The vendor filtering is required for versions of go older than 1.9. +# - The test package filtering is required since those packages need special setup. +all_test_packages=$(go list ./... 2>/dev/null |\ + grep -v "/vendor/" |\ + grep -v "github.com/kata-containers/agent/protocols/grpc" |\ + grep -v "github.com/kata-containers/tests/functional" |\ + grep -v "github.com/kata-containers/tests/integration/docker" \ + || true) + +# The "master" coverage file that contains the coverage results for +# all packages run under all scenarios. +test_coverage_file="coverage.txt" + +# Temporary coverage file created for a single package. The results in this +# file will be added to the master coverage file. +tmp_coverage_file="${test_coverage_file}.tmp" + +# Permissions to create coverage files with +coverage_file_mode=0644 + +# Name of HTML format coverage file +html_report_file="coverage.html" + +warn() +{ + local msg="$*" + echo >&2 "WARNING: $msg" +} + +usage() +{ + cat <> "$test_coverage_file" + run_as_user "$user" rm -f "$tmp_coverage_file" + fi +} + +# Run all tests and generate a test coverage file. +test_coverage() +{ + echo "mode: atomic" > "$test_coverage_file" + + users="current" + + if [ "$(id -u)" -eq 0 ]; then + warn "Already running as root so will not re-run tests as non-root user." + warn "As a result, only a subset of tests will be run" + warn "(run this script as a non-privileged to ensure all tests are run)." + else + if [ "$CI" = true ] && [ -n "$KATA_DEV_MODE" ]; then + warn "Dangerous to set CI and KATA_DEV_MODE together." + warn "NOT running tests as root." + else + # Run the unit-tests *twice* (since some must run as root and + # others must run as non-root), combining the resulting test + # coverage files. + users+=" root" + fi + fi + + echo "INFO: Currently running as user '$(id -un)'" + for pkg in $test_packages; do + for user in $users; do + test_go_package "$pkg" "$user" + done + done +} + +# Run the tests locally +test_local() +{ + for pkg in $test_packages; do + eval go test "$go_test_flags" "$pkg" + done +} + +main() +{ + local long_option_names="${!long_options[@]}" + + local args=$(getopt \ + -n "$script_name" \ + -a \ + --options="h" \ + --longoptions="$long_option_names" \ + -- "$@") + + local package + + eval set -- "$args" + [ $? -ne 0 ] && { usage >&2; exit 1; } + + while [ $# -gt 1 ] + do + case "$1" in + -h|--help) usage; exit 0 ;; + --list) list_packages; exit 0 ;; + --package) package="$2"; shift 2;; + --) shift; break ;; + esac + + shift + done + + if [ -n "$package" ]; then + test_packages="$package" + else + test_packages="$all_test_packages" + fi + + # Consume getopt cruft + [ "$1" = "--" ] && shift + + [ "$1" = "help" ] && usage && exit 0 + + run_coverage=no + if [ "$CI" = true ] || [ -n "$KATA_DEV_MODE" ]; then + run_coverage=yes + fi + + [ -z "$test_packages" ] && echo "INFO: no golang code to test" && exit 0 + + local go_ldflags + [ "$(go env GOARCH)" = s390x ] && go_ldflags="-extldflags -Wl,--s390-pgste" + + # KATA_GO_TEST_FLAGS can be set to change the flags passed to "go test". + go_test_flags=${KATA_GO_TEST_FLAGS:-"-v $race -timeout $timeout_value -ldflags '$go_ldflags'"} + + if [ "$1" = "html-coverage" ]; then + test_html_coverage + elif [ "$run_coverage" = yes ]; then + test_coverage + else + test_local + fi +} + +main "$@"