Skip to content

Commit 9ba4f50

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 9ba4f50

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

Makefile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export INSTALL_DEFAULT_CATALOGS := true
3232
# By default setup-envtest will write to $XDG_DATA_HOME, or $HOME/.local/share if that is not defined.
3333
# If $HOME is not set, we need to specify a binary directory to prevent an error in setup-envtest.
3434
# Useful for some CI/CD environments that set neither $XDG_DATA_HOME nor $HOME.
35-
SETUP_ENVTEST_BIN_DIR_OVERRIDE=
35+
SETUP_ENVTEST_BIN_DIR_OVERRIDE := --bin-dir $(ROOT_DIR)/bin/envtest-binaries
3636
ifeq ($(shell [[ $$HOME == "" || $$HOME == "/" ]] && [[ $$XDG_DATA_HOME == "" ]] && echo true ), true)
3737
SETUP_ENVTEST_BIN_DIR_OVERRIDE += --bin-dir /tmp/envtest-binaries
3838
endif
@@ -151,19 +151,26 @@ test-ext-dev-e2e: $(OPERATOR_SDK) $(KUSTOMIZE) $(KIND) #HELP Run extension creat
151151
test/extension-developer-e2e/setup.sh $(OPERATOR_SDK) $(CONTAINER_RUNTIME) $(KUSTOMIZE) $(KIND) $(KIND_CLUSTER_NAME) $(E2E_REGISTRY_NAMESPACE)
152152
go test -count=1 -v ./test/extension-developer-e2e/...
153153

154-
.PHONY: test-unit
155154
ENVTEST_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.x/')
156155
UNIT_TEST_DIRS := $(shell go list ./... | grep -v /test/)
157156
COVERAGE_UNIT_DIR := $(ROOT_DIR)/coverage/unit
157+
158+
.PHONY: setup-envtest #HELP Install setup-envtest binary in the bin directory.
159+
setup-envtest:
160+
mkdir -p $(ROOT_DIR)/bin
161+
GOBIN=$(ROOT_DIR)/bin go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
162+
$(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)
163+
164+
.PHONY: test-unit
158165
test-unit: $(SETUP_ENVTEST) #HELP Run the unit tests
159166
rm -rf $(COVERAGE_UNIT_DIR) && mkdir -p $(COVERAGE_UNIT_DIR)
160-
eval $$($(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)) && \
167+
KUBEBUILDER_ASSETS="$(shell $(SETUP_ENVTEST) use -p path $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE))" \
161168
CGO_ENABLED=1 go test \
162169
-tags '$(GO_BUILD_TAGS)' \
163170
-cover -coverprofile ${ROOT_DIR}/coverage/unit.out \
164171
-count=1 -race -short \
165172
$(UNIT_TEST_DIRS) \
166-
-test.gocoverdir=$(ROOT_DIR)/coverage/unit
173+
-test.gocoverdir=$(COVERAGE_UNIT_DIR)
167174

168175
.PHONY: image-registry
169176
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)