Skip to content

Commit ff5d402

Browse files
fix: configure ENVTEST binaries for IDE execution and project-specific setup
- Installs ENVTEST binaries in the project’s ./bin/envtest directory. - Avoids global paths to prevent permission issues and conflicts with other projects. - Centralizes binaries in ./bin for easier configuration and debugging in IDEs. This change enforces a local installation path for ENVTEST binaries to resolve permission issues which might be encountered by some contributors and to isolate project dependencies. The setup also simplifies IDE configuration for running and tests directly locally. (mainly for debug purposes)
1 parent 6d79092 commit ff5d402

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

Makefile

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

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

@@ -151,19 +154,26 @@ test-ext-dev-e2e: $(OPERATOR_SDK) $(KUSTOMIZE) $(KIND) #HELP Run extension creat
151154
test/extension-developer-e2e/setup.sh $(OPERATOR_SDK) $(CONTAINER_RUNTIME) $(KUSTOMIZE) $(KIND) $(KIND_CLUSTER_NAME) $(E2E_REGISTRY_NAMESPACE)
152155
go test -count=1 -v ./test/extension-developer-e2e/...
153156

154-
.PHONY: test-unit
155157
ENVTEST_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.x/')
156158
UNIT_TEST_DIRS := $(shell go list ./... | grep -v /test/)
157159
COVERAGE_UNIT_DIR := $(ROOT_DIR)/coverage/unit
158-
test-unit: $(SETUP_ENVTEST) #HELP Run the unit tests
160+
161+
.PHONY: setup-envtest #HELP Install setup-envtest binary in the bin directory.
162+
setup-envtest:
163+
mkdir -p $(ROOT_DIR)/bin
164+
GOBIN=$(ROOT_DIR)/bin go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
165+
$(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)
166+
167+
.PHONY: test-unit
168+
test-unit: setup-envtest #HELP Run the unit tests
159169
rm -rf $(COVERAGE_UNIT_DIR) && mkdir -p $(COVERAGE_UNIT_DIR)
160-
eval $$($(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)) && \
161-
CGO_ENABLED=1 go test \
162-
-tags '$(GO_BUILD_TAGS)' \
163-
-cover -coverprofile ${ROOT_DIR}/coverage/unit.out \
164-
-count=1 -race -short \
165-
$(UNIT_TEST_DIRS) \
166-
-test.gocoverdir=$(ROOT_DIR)/coverage/unit
170+
KUBEBUILDER_ASSETS="$(shell $(SETUP_ENVTEST) use -p path $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE))" \
171+
CGO_ENABLED=1 go test \
172+
-tags '$(GO_BUILD_TAGS)' \
173+
-cover -coverprofile ${ROOT_DIR}/coverage/unit.out \
174+
-count=1 -race -short \
175+
$(UNIT_TEST_DIRS) \
176+
-test.gocoverdir=$(COVERAGE_UNIT_DIR)
167177

168178
.PHONY: image-registry
169179
E2E_REGISTRY_IMAGE=localhost/e2e-test-registry:devel

internal/controllers/suite_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
"github.com/stretchr/testify/require"
2828
"k8s.io/apimachinery/pkg/api/meta"
29-
"k8s.io/apimachinery/pkg/runtime"
29+
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
3030
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3131
"k8s.io/client-go/rest"
3232
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -64,7 +64,7 @@ func (m *MockUnpacker) Cleanup(_ context.Context, _ *source.BundleSource) error
6464
func newClient(t *testing.T) client.Client {
6565
// TODO: this is a live client, which behaves differently than a cache client.
6666
// We may want to use a caching client instead to get closer to real behavior.
67-
sch := runtime.NewScheme()
67+
sch := apimachineryruntime.NewScheme()
6868
require.NoError(t, ocv1.AddToScheme(sch))
6969
cl, err := client.New(config, client.Options{Scheme: sch})
7070
require.NoError(t, err)
@@ -162,6 +162,15 @@ func TestMain(m *testing.M) {
162162
ErrorIfCRDPathMissing: true,
163163
}
164164

165+
// The BinaryAssetsDirectory is only necessary when we run tests directly
166+
// without using the target. (e.g., when debugging tests in an IDE)
167+
// If left unset, it will default to the path defined in controller-runtime (/usr/local/kubebuilder/).
168+
// Ensure that required binaries are set up in the bin directory bin/k8s/envtest-binaries for direct testing.
169+
// Run `make setup-envtest` to set up the binaries.
170+
if getFirstFoundEnvTestBinaryDir() != "" {
171+
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
172+
}
173+
165174
var err error
166175
config, err = testEnv.Start()
167176
utilruntime.Must(err)
@@ -179,3 +188,15 @@ func TestMain(m *testing.M) {
179188
utilruntime.Must(testEnv.Stop())
180189
os.Exit(code)
181190
}
191+
192+
// getFirstFoundEnvTestBinaryDir finds and returns the first directory under the given path.
193+
func getFirstFoundEnvTestBinaryDir() string {
194+
basePath := filepath.Join("..", "..", "bin", "envtest-binaries", "k8s")
195+
entries, _ := os.ReadDir(basePath)
196+
for _, entry := range entries {
197+
if entry.IsDir() {
198+
return filepath.Join(basePath, entry.Name())
199+
}
200+
}
201+
return ""
202+
}

0 commit comments

Comments
 (0)