Skip to content

Commit

Permalink
test: support go test arguments and interuptting containerized tests (#…
Browse files Browse the repository at this point in the history
…6580)

The patch adds a parameter `TEST_ARGS` to pass go test arguments when
running tests with make.

It also add `-it` to `docker run` to make the tests interruptable.

Signed-off-by: Quan Tian <[email protected]>
  • Loading branch information
tnqn committed Aug 5, 2024
1 parent f48b61e commit 167b728
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ GOLANGCI_LINT_VERSION := v1.54.0
GOLANGCI_LINT_BINDIR := $(CURDIR)/.golangci-bin
GOLANGCI_LINT_BIN := $(GOLANGCI_LINT_BINDIR)/$(GOLANGCI_LINT_VERSION)/golangci-lint

# Arguments to pass to go test invocation.
#
# Example:
# make test-unit TEST_ARGS="-run ^TestAddEgress$"
# make docker-test-integration TEST_ARGS="-run TestGoBGPLifecycle"
TEST_ARGS ?=

# If we have stdin we can run interactive so the tests running in docker can be interrupted.
INTERACTIVE_ARGS := $(shell [ -t 0 ] && echo "-it")

BUILD_TAG :=
ifndef CUSTOM_BUILD_TAG
BUILD_TAG = $(shell build/images/build-tag.sh)
Expand Down Expand Up @@ -189,9 +199,10 @@ $(DOCKER_CACHE):
# Since the WORKDIR is mounted from host, the $(id -u):$(id -g) user can access it.
# Inside the docker, the user is nameless and does not have a home directory. This is ok for our use case.
DOCKER_ENV := \
@docker run --rm -u $$(id -u):$$(id -g) \
@docker run $(INTERACTIVE_ARGS) --rm -u $$(id -u):$$(id -g) \
-e "GOCACHE=/tmp/gocache" \
-e "GOPATH=/tmp/gopath" \
-e "TEST_ARGS=$(TEST_ARGS)" \
-w /usr/src/antrea.io/antrea \
-v $(DOCKER_CACHE)/gopath:/tmp/gopath \
-v $(DOCKER_CACHE)/gocache:/tmp/gocache \
Expand All @@ -216,10 +227,11 @@ docker-test-unit: $(DOCKER_CACHE)
docker-test-integration: .coverage
@echo "===> Building Antrea Integration Test Docker image <==="
docker build -t antrea/test -f build/images/test/Dockerfile $(DOCKER_BUILD_ARGS) .
@docker run --privileged --rm \
@docker run $(INTERACTIVE_ARGS) --privileged --rm \
-e "GOCACHE=/tmp/gocache" \
-e "GOPATH=/tmp/gopath" \
-e "INCONTAINER=true" \
-e "TEST_ARGS=$(TEST_ARGS)" \
-w /usr/src/antrea.io/antrea \
-v $(DOCKER_CACHE)/gopath:/tmp/gopath \
-v $(DOCKER_CACHE)/gocache:/tmp/gocache \
Expand Down Expand Up @@ -266,15 +278,15 @@ add-copyright:
.linux-test-unit: .coverage
@echo
@echo "==> Running unit tests <=="
CGO_ENABLED=1 $(GO) test -race -coverpkg=antrea.io/antrea/cmd/...,antrea.io/antrea/pkg/...,antrea.io/antrea/multicluster/cmd/...,antrea.io/antrea/multicluster/controllers/... \
CGO_ENABLED=1 $(GO) test $(TEST_ARGS) -race -coverpkg=antrea.io/antrea/cmd/...,antrea.io/antrea/pkg/...,antrea.io/antrea/multicluster/cmd/...,antrea.io/antrea/multicluster/controllers/... \
-coverprofile=.coverage/coverage-unit.txt -covermode=atomic \
antrea.io/antrea/cmd/... antrea.io/antrea/pkg/... antrea.io/antrea/multicluster/cmd/... antrea.io/antrea/multicluster/controllers/...

.PHONY: .windows-test-unit
.windows-test-unit: .coverage
@echo
@echo "==> Running unit tests <=="
CGO_ENABLED=1 $(GO) test -race -coverpkg=antrea.io/antrea/cmd/...,antrea.io/antrea/pkg/... \
CGO_ENABLED=1 $(GO) test $(TEST_ARGS) -race -coverpkg=antrea.io/antrea/cmd/...,antrea.io/antrea/pkg/... \
-coverprofile=.coverage/coverage-unit.txt -covermode=atomic \
antrea.io/antrea/cmd/... antrea.io/antrea/pkg/...

Expand All @@ -288,7 +300,7 @@ tidy:
@echo
@echo "==> Running integration tests <=="
@echo "SOME TESTS WILL FAIL IF NOT RUN AS ROOT!"
$(GO) test -v -coverpkg=antrea.io/antrea/pkg/... -coverprofile=.coverage/coverage-integration.txt -covermode=atomic antrea.io/antrea/test/integration/...
$(GO) test $(TEST_ARGS) -v -coverpkg=antrea.io/antrea/pkg/... -coverprofile=.coverage/coverage-integration.txt -covermode=atomic antrea.io/antrea/test/integration/...

test-tidy:
@echo
Expand Down

0 comments on commit 167b728

Please sign in to comment.