From 78abff3e3986ea05594f7818c540078bce3c2832 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Fri, 2 Aug 2019 13:32:36 -0700 Subject: [PATCH] Add support for setting a test filter This is basically taking some stuff that make a custom shell function for. This takes a test filter, builds the appropriate TESTFLAGS, and sets the integration API test dirs that match the given filter to avoid building all test dirs. Signed-off-by: Brian Goff (cherry picked from commit 13064b155eb439a79adcf8f160ecf2b76f805bd4) Signed-off-by: Sebastiaan van Stijn --- Makefile | 1 + TESTING.md | 23 +++++++++++++++++++++++ hack/make/.integration-test-helpers | 29 ++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cda05e57d8..ed9372a16a 100644 --- a/Makefile +++ b/Makefile @@ -62,6 +62,7 @@ DOCKER_ENVS := \ -e TESTFLAGS \ -e TESTFLAGS_INTEGRATION \ -e TESTFLAGS_INTEGRATION_CLI \ + -e TEST_FILTER \ -e TIMEOUT \ -e VALIDATE_REPO \ -e VALIDATE_BRANCH \ diff --git a/TESTING.md b/TESTING.md index 958f6e92af..613c1b6435 100644 --- a/TESTING.md +++ b/TESTING.md @@ -67,6 +67,8 @@ If a remote daemon is detected, the test will be skipped. ## Running tests +### Unit Tests + To run the unit test suite: ``` @@ -82,12 +84,33 @@ The following environment variables may be used to run a subset of tests: * `TESTFLAGS` - flags passed to `go test`, to run tests which match a pattern use `TESTFLAGS="-test.run TestNameOrPrefix"` +### Integration Tests + To run the integration test suite: ``` make test-integration ``` +This make target runs both the "integration" suite and the "integration-cli" +suite. + +You can specify which integration test dirs to build and run by specifying +the list of dirs in the TEST_INTEGRATION_DIR environment variable. + +You can also explicitly skip either suite by setting (any value) in +TEST_SKIP_INTEGRATION and/or TEST_SKIP_INTEGRATION_CLI environment variables. + +Flags specific to each suite can be set in the TESTFLAGS_INTEGRATION and +TESTFLAGS_INTEGRATION_CLI environment variables. + +If all you want is to specity a test filter to run, you can set the +`TEST_FILTER` environment variable. This ends up getting passed directly to `go +test -run` (or `go test -check-f`, dpenending on the test suite). It will also +automatically set the other above mentioned environment variables accordingly. + +### Go Version + You can change a version of golang used for building stuff that is being tested by setting `GO_VERSION` variable, for example: diff --git a/hack/make/.integration-test-helpers b/hack/make/.integration-test-helpers index abe69474ae..484aa448c0 100644 --- a/hack/make/.integration-test-helpers +++ b/hack/make/.integration-test-helpers @@ -27,7 +27,34 @@ source "$MAKEDIR/.go-autogen" : ${TESTFLAGS:=} : ${TESTDEBUG:=} -integration_api_dirs=${TEST_INTEGRATION_DIR:-"$(go list -test -f '{{- if ne .ForTest "" -}}{{- .Dir -}}{{- end -}}' ./integration/...)"} +setup_integration_test_filter() { + if [ -z "${TEST_FILTER}" ]; then + return + fi + + if [ -z "${TEST_SKIP_INTEGRATION}" ]; then + : ${TEST_INTEGRATION_DIR:=$(grep -rl "func\ .*${TEST_FILTER}.*\(t\ \*testing\.T\)" ./integration | grep '_test\.go' | xargs -I file dirname file | uniq)} + if [ -z "${TEST_INTEGRATION_DIR}" ]; then + echo Skipping integration tests since the supplied filter \"${TEST_FILTER}\" omits all integration tests + TEST_SKIP_INTEGRATION=1 + else + TESTFLAGS_INTEGRATION+="-test.run ${TEST_FILTER}" + fi + fi + + if [ -z "${TEST_SKIP_INTEGRATION_CLI}" ]; then + # ease up on the filtering here since CLI suites are namespaced by an object + if grep -r "${TEST_FILTER}.*\(c\ \*check\.C\)" ./integration-cli | grep -q '_test\.go$'; then + TEST_SKIP_INTEGRATION_CLI=1 + echo Skipping integration-cli tests since the supplied filter \"${TEST_FILTER}\" omits all integration-cli tests + else + TESTFLAGS_INTEGRATION_CLI+="-check.f ${TEST_FILTER}" + fi + fi +} + +setup_integration_test_filter +integration_api_dirs=${TEST_INTEGRATION_DIR:-$(go list -test -f '{{- if ne .ForTest "" -}}{{- .Dir -}}{{- end -}}' ./integration/...)} run_test_integration() { set_platform_timeout