Skip to content

Commit bec5b49

Browse files
fix: ensure envtest binaries are installed in project-specific bin directory
- Redirects envtest binaries to be installed in the project's ./bin/envtest-binaries directory. - Avoids using global paths, preventing permission issues for contributors and reducing risk of conflicts with other projects. - Centralizes all project binaries in ./bin for consistency, making it easier to configure and debug tests in IDEs. This change enforces a local installation path for envtest binaries to address permission issues some contributors faced with global installs. By centralizing binaries in the project-specific ./bin directory, we also prevent interference with other projects that might rely on different versions or setups. This setup supports seamless IDE configuration for running and debugging tests locally.
1 parent dc5721c commit bec5b49

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

Makefile

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ export WAIT_TIMEOUT := 60s
2929
# Install default ClusterCatalogs
3030
export INSTALL_DEFAULT_CATALOGS := true
3131

32-
# By default setup-envtest will write to $XDG_DATA_HOME, or $HOME/.local/share if that is not defined.
33-
# If $HOME is not set, we need to specify a binary directory to prevent an error in setup-envtest.
34-
# Useful for some CI/CD environments that set neither $XDG_DATA_HOME nor $HOME.
35-
SETUP_ENVTEST_BIN_DIR_OVERRIDE=
36-
ifeq ($(shell [[ $$HOME == "" || $$HOME == "/" ]] && [[ $$XDG_DATA_HOME == "" ]] && echo true ), true)
37-
SETUP_ENVTEST_BIN_DIR_OVERRIDE += --bin-dir /tmp/envtest-binaries
38-
endif
39-
4032
# bingo manages consistent tooling versions for things like kind, kustomize, etc.
4133
include .bingo/Variables.mk
4234

@@ -151,21 +143,50 @@ test-ext-dev-e2e: $(OPERATOR_SDK) $(KUSTOMIZE) $(KIND) #HELP Run extension creat
151143
test/extension-developer-e2e/setup.sh $(OPERATOR_SDK) $(CONTAINER_RUNTIME) $(KUSTOMIZE) $(KIND) $(KIND_CLUSTER_NAME) $(E2E_REGISTRY_NAMESPACE)
152144
go test -count=1 -v ./test/extension-developer-e2e/...
153145

154-
.PHONY: test-unit
155-
# Define CGO_ENABLED based on the OS
146+
# Define directories and versions
147+
LOCALBIN := $(PWD)/bin
148+
SETUP_ENVTEST := $(LOCALBIN)/setup-envtest
149+
ENVTEST_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.x/')
156150
CGO_ENABLED_VAL := $(if $(filter Linux, $(shell uname)),1,0)
151+
UNIT_TEST_DIRS := $(shell go list ./... | grep -v /test/)
152+
COVERAGE_UNIT_DIR := $(ROOT_DIR)/coverage/unit
153+
154+
.PHONY: setup-envtest test-unit
155+
156+
# Define directories and paths
157+
LOCALBIN := $(PWD)/bin
158+
SETUP_ENVTEST := $(LOCALBIN)/setup-envtest
159+
SETUP_ENVTEST_BIN_DIR := $(LOCALBIN)/envtest-binaries
157160
ENVTEST_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.x/')
161+
CGO_ENABLED_VAL := $(if $(filter Linux, $(shell uname)),1,0)
158162
UNIT_TEST_DIRS := $(shell go list ./... | grep -v /test/)
159163
COVERAGE_UNIT_DIR := $(ROOT_DIR)/coverage/unit
160-
test-unit: $(SETUP_ENVTEST) #HELP Run the unit tests
164+
165+
# Force the envtest binaries to be in ./bin/envtest-binaries
166+
SETUP_ENVTEST_BIN_DIR_OVERRIDE := --bin-dir $(SETUP_ENVTEST_BIN_DIR)
167+
168+
# Ensure bin directory exists
169+
$(LOCALBIN):
170+
mkdir -p $(LOCALBIN)
171+
172+
# Ensure bin directory and install setup-envtest if not available
173+
$(SETUP_ENVTEST): $(LOCALBIN)
174+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
175+
176+
.PHONY: setup-envtest #HELP Install setup-envtest binary in the bin directory.
177+
setup-envtest: $(SETUP_ENVTEST)
178+
$(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)
179+
180+
.PHONY: test-unit
181+
test-unit: setup-envtest #HELP Run the unit tests
161182
rm -rf $(COVERAGE_UNIT_DIR) && mkdir -p $(COVERAGE_UNIT_DIR)
162-
eval $$($(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)) && \
163-
CGO_ENABLED=$(CGO_ENABLED_VAL) go test \
164-
-tags '$(GO_BUILD_TAGS)' \
165-
-cover -coverprofile ${ROOT_DIR}/coverage/unit.out \
166-
-count=1 -race -short \
167-
$(UNIT_TEST_DIRS) \
168-
-test.gocoverdir=$(ROOT_DIR)/coverage/unit
183+
KUBEBUILDER_ASSETS="$(shell $(SETUP_ENVTEST) use -p path $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE))" \
184+
CGO_ENABLED=$(CGO_ENABLED_VAL) go test \
185+
-tags '$(GO_BUILD_TAGS)' \
186+
-cover -coverprofile ${ROOT_DIR}/coverage/unit.out \
187+
-count=1 -race -short \
188+
$(UNIT_TEST_DIRS) \
189+
-test.gocoverdir=$(ROOT_DIR)/coverage/unit
169190

170191
.PHONY: image-registry
171192
E2E_REGISTRY_IMAGE=localhost/e2e-test-registry:devel

0 commit comments

Comments
 (0)