@@ -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
6464func 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