Skip to content

Commit

Permalink
fix: ensure envtest binaries are installed in project-specific bin di…
Browse files Browse the repository at this point in the history
…rectory

- 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.
  • Loading branch information
camilamacedo86 committed Nov 13, 2024
1 parent dc5721c commit 6ccb706
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ export WAIT_TIMEOUT := 60s
# Install default ClusterCatalogs
export INSTALL_DEFAULT_CATALOGS := true

# By default setup-envtest will write to $XDG_DATA_HOME, or $HOME/.local/share if that is not defined.
# If $HOME is not set, we need to specify a binary directory to prevent an error in setup-envtest.
# Useful for some CI/CD environments that set neither $XDG_DATA_HOME nor $HOME.
SETUP_ENVTEST_BIN_DIR_OVERRIDE=
ifeq ($(shell [[ $$HOME == "" || $$HOME == "/" ]] && [[ $$XDG_DATA_HOME == "" ]] && echo true ), true)
SETUP_ENVTEST_BIN_DIR_OVERRIDE += --bin-dir /tmp/envtest-binaries
endif

# bingo manages consistent tooling versions for things like kind, kustomize, etc.
include .bingo/Variables.mk

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

.PHONY: test-unit
# Define CGO_ENABLED based on the OS
# Define directories and versions
LOCALBIN := $(PWD)/bin
SETUP_ENVTEST := $(LOCALBIN)/setup-envtest
ENVTEST_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.x/')
CGO_ENABLED_VAL := $(if $(filter Linux, $(shell uname)),1,0)
UNIT_TEST_DIRS := $(shell go list ./... | grep -v /test/)
COVERAGE_UNIT_DIR := $(ROOT_DIR)/coverage/unit

.PHONY: setup-envtest test-unit

# Define directories and paths
LOCALBIN := $(PWD)/bin
SETUP_ENVTEST := $(LOCALBIN)/setup-envtest
SETUP_ENVTEST_BIN_DIR := $(LOCALBIN)/envtest-binaries
ENVTEST_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.x/')
CGO_ENABLED_VAL := $(if $(filter Linux, $(shell uname)),1,0)
UNIT_TEST_DIRS := $(shell go list ./... | grep -v /test/)
COVERAGE_UNIT_DIR := $(ROOT_DIR)/coverage/unit
test-unit: $(SETUP_ENVTEST) #HELP Run the unit tests

# Force the envtest binaries to be in ./bin/envtest-binaries
SETUP_ENVTEST_BIN_DIR_OVERRIDE := --bin-dir $(SETUP_ENVTEST_BIN_DIR)

# Ensure bin directory and install setup-envtest if not available
$(SETUP_ENVTEST): | $(LOCALBIN)
mkdir -p $(LOCALBIN)
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

.PHONY: setup-envtest #HELP Install setup-envtest binary in the bin directory.
setup-envtest: $(SETUP_ENVTEST)
$(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)

.PHONY: test-unit
test-unit: setup-envtest #HELP Run the unit tests
rm -rf $(COVERAGE_UNIT_DIR) && mkdir -p $(COVERAGE_UNIT_DIR)
eval $$($(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)) && \
CGO_ENABLED=$(CGO_ENABLED_VAL) go test \
-tags '$(GO_BUILD_TAGS)' \
-cover -coverprofile ${ROOT_DIR}/coverage/unit.out \
-count=1 -race -short \
$(UNIT_TEST_DIRS) \
-test.gocoverdir=$(ROOT_DIR)/coverage/unit
KUBEBUILDER_ASSETS="$(shell $(SETUP_ENVTEST) use -p path $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE))" \
CGO_ENABLED=$(CGO_ENABLED_VAL) go test \
-tags '$(GO_BUILD_TAGS)' \
-cover -coverprofile ${ROOT_DIR}/coverage/unit.out \
-count=1 -race -short \
$(UNIT_TEST_DIRS) \
-test.gocoverdir=$(ROOT_DIR)/coverage/unit

.PHONY: image-registry
E2E_REGISTRY_IMAGE=localhost/e2e-test-registry:devel
Expand Down

0 comments on commit 6ccb706

Please sign in to comment.