Skip to content

Commit c5cdc3d

Browse files
Configure ENVTEST Binaries for IDE Debugging
This change introduces downloading the specific binaries required to run ENVTEST-based tests into the project/bin directory. This setup makes it easier to configure tests for debugging directly in an IDE.
1 parent e5820ae commit c5cdc3d

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

Makefile

Lines changed: 10 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,25 @@ 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: $(SETUP_ENVTEST)
160+
mkdir -p $(ROOT_DIR)/bin
161+
$(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)
162+
163+
.PHONY: test-unit
158164
test-unit: $(SETUP_ENVTEST) #HELP Run the unit tests
159165
rm -rf $(COVERAGE_UNIT_DIR) && mkdir -p $(COVERAGE_UNIT_DIR)
160-
eval $$($(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)) && \
166+
KUBEBUILDER_ASSETS="$(shell $(SETUP_ENVTEST) use -p path $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE))" \
161167
CGO_ENABLED=1 go test \
162168
-tags '$(GO_BUILD_TAGS)' \
163169
-cover -coverprofile ${ROOT_DIR}/coverage/unit.out \
164170
-count=1 -race -short \
165171
$(UNIT_TEST_DIRS) \
166-
-test.gocoverdir=$(ROOT_DIR)/coverage/unit
172+
-test.gocoverdir=$(COVERAGE_UNIT_DIR)
167173

168174
.PHONY: image-registry
169175
E2E_REGISTRY_IMAGE=localhost/e2e-test-registry:devel

internal/controllers/suite_test.go

Lines changed: 29 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,21 @@ func TestMain(m *testing.M) {
162162
ErrorIfCRDPathMissing: true,
163163
}
164164

165+
// ENVTEST-based tests require specific binaries. By default, these binaries are located
166+
// in paths defined by controller-runtime. However, the `BinaryAssetsDirectory` needs
167+
// to be explicitly set when running tests directly (e.g., debugging tests in an IDE)
168+
// without using the Makefile targets.
169+
//
170+
// This is equivalent to configuring your IDE to export the `KUBEBUILDER_ASSETS` environment
171+
// variable before each test execution. The following function simplifies this process
172+
// by handling the configuration for you.
173+
//
174+
// To ensure the binaries are in the expected path without manual configuration, run:
175+
// `make setup-envtest`
176+
if getFirstFoundEnvTestBinaryDir() != "" {
177+
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
178+
}
179+
165180
var err error
166181
config, err = testEnv.Start()
167182
utilruntime.Must(err)
@@ -179,3 +194,15 @@ func TestMain(m *testing.M) {
179194
utilruntime.Must(testEnv.Stop())
180195
os.Exit(code)
181196
}
197+
198+
// getFirstFoundEnvTestBinaryDir finds and returns the first directory under the given path.
199+
func getFirstFoundEnvTestBinaryDir() string {
200+
basePath := filepath.Join("..", "..", "bin", "envtest-binaries", "k8s")
201+
entries, _ := os.ReadDir(basePath)
202+
for _, entry := range entries {
203+
if entry.IsDir() {
204+
return filepath.Join(basePath, entry.Name())
205+
}
206+
}
207+
return ""
208+
}

0 commit comments

Comments
 (0)