From 0aff5aaa39d60afa29e47340fb0f0260f57c3e68 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 25 Mar 2022 20:31:34 +1100 Subject: [PATCH] runtime: Simplify package listing in go-test.sh go-test.sh defaults to testing all the packages listed by go list, except for a number filtered out. It turns out that none of those filters are necessary any more: * We've long required a Go newer than 1.9 which means the vendor filter isn't needed * The agent filter doesn't do anything now that we've moved to the Kata 2.x unified repo * The tests filters don't hit anything on the list of modules in src/runtime (which is the only user of the script) But since we don't need to filter anything out any more, we don't even need to iterate through a list ourselves. We can simply pass "./..." directly to go test and it will iterate through all the sub-packages itself. Interestingly this more than doubles the speed of "make test" for me - I suspect because go test's internal paralellism works better over a larger pool of tests. This also lets us remove handling of non-existent coverage files from test_go_package(), since with default options we will no longer test packages without tests by default. If the user explicitly requests testing of a package with no tests, then failing makes sense. Signed-off-by: David Gibson --- src/runtime/go-test.sh | 54 +++++++----------------------------------- 1 file changed, 9 insertions(+), 45 deletions(-) diff --git a/src/runtime/go-test.sh b/src/runtime/go-test.sh index 2f6f1865e..a47fa3265 100755 --- a/src/runtime/go-test.sh +++ b/src/runtime/go-test.sh @@ -12,7 +12,6 @@ typeset -A long_options long_options=( [help]="Show usage" - [list]="List available packages" [package:]="Specify test package to run" ) @@ -25,23 +24,12 @@ 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. +# Temporary coverage file created for a "go test" run. The results in +# this file will be added to the master coverage file. tmp_coverage_file="${test_coverage_file}.tmp" warn() @@ -90,11 +78,6 @@ Commands: 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 @@ -126,15 +109,9 @@ test_go_package() 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" tail -n +2 "$tmp_coverage_file" >> "$test_coverage_file" - rm -f "$tmp_coverage_file" - fi + # Merge test results into the master coverage file. + run_as_user "$user" tail -n +2 "$tmp_coverage_file" >> "$test_coverage_file" + rm -f "$tmp_coverage_file" } # Run all tests and generate a test coverage file. @@ -161,19 +138,15 @@ test_coverage() 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 + for user in $users; do + test_go_package "$package" "$user" done } # Run the tests locally test_local() { - for pkg in $test_packages; do - eval go test "$go_test_flags" "$pkg" - done + eval go test "$go_test_flags" "$package" } main() @@ -187,7 +160,7 @@ main() --longoptions="$long_option_names" \ -- "$@") - local package + package="./..." eval set -- "$args" [ $? -ne 0 ] && { usage >&2; exit 1; } @@ -196,7 +169,6 @@ main() do case "$1" in -h|--help) usage; exit 0 ;; - --list) list_packages; exit 0 ;; --package) package="$2"; shift 2;; --) shift; break ;; esac @@ -204,12 +176,6 @@ main() shift done - if [ -n "$package" ]; then - test_packages="$package" - else - test_packages="$all_test_packages" - fi - # Consume getopt cruft [ "$1" = "--" ] && shift @@ -220,8 +186,6 @@ main() 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"