Skip to content

Commit

Permalink
runtime: Move go testing script locally
Browse files Browse the repository at this point in the history
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 <david@gibson.dropbear.id.au>
  • Loading branch information
dgibson committed May 13, 2022
1 parent 97425a7 commit 13c2577
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 19 deletions.
12 changes: 0 additions & 12 deletions ci/go-test.sh

This file was deleted.

6 changes: 0 additions & 6 deletions ci/lib.sh
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Makefile
Expand Up @@ -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
Expand Down
258 changes: 258 additions & 0 deletions 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 <<EOF
Usage: $script_name help
$script_name [options] [cmd]
Options:
EOF

local option
local description

local long_option_names="${!long_options[@]}"

# Sort space-separated list by converting to newline separated list
# and back again.
long_option_names=$(echo "$long_option_names"|tr ' ' '\n'|sort|tr '\n' ' ')

# Display long options
for option in ${long_option_names}
do
description=${long_options[$option]}

# Remove any trailing colon which is for getopt(1) alone.
option=$(echo "$option"|sed 's/:$//g')

printf " --%-10.10s # %s\n" "$option" "$description"
done

cat <<EOF
Commands:
help # Show usage.
html-coverage # Run tests and create HTML coverage report.
EOF
}

list_packages()
{
echo "$all_test_packages"
}

# Run a command as either root or the current user (which might still be root).
#
# If the first argument is "root", run using sudo, else run as the current
# user. All arguments after the first will be treated as the command to run.
run_as_user()
{
local user="$1"

shift

local cmd=$*

if [ "$user" = root ]; then
# use a shell to ensure PATH is correct.
sudo -E PATH="$PATH" sh -c "$cmd"
else
eval "$cmd"
fi
}

# Run the tests and generate an HTML report of the results
test_html_coverage()
{
test_coverage

go tool cover -html="${test_coverage_file}" -o "${html_report_file}"

run_as_user "current" chmod "${coverage_file_mode}" "${html_report_file}"
}

# Test a single golang package
test_go_package()
{
local -r pkg="$1"
local -r user="$2"

printf "INFO: Running 'go test' as %s user on package '%s' with flags '%s'\n" \
"$user" "$pkg" "$go_test_flags"

run_as_user "$user" go test "$go_test_flags" -covermode=atomic -coverprofile=$tmp_coverage_file "$pkg"

# Check for the temporary coverage file since if will
# not be generated unless a package actually contains
# tests.
if [ -f "${tmp_coverage_file}" ]; then
# Save these package test results into the
# master coverage file.
run_as_user "$user" chmod "${coverage_file_mode}" "$tmp_coverage_file"
tail -n +2 "$tmp_coverage_file" >> "$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 "$@"

0 comments on commit 13c2577

Please sign in to comment.