@@ -26,7 +26,7 @@ import (
26
26
27
27
"github.com/stretchr/testify/require"
28
28
"k8s.io/apimachinery/pkg/api/meta"
29
- "k8s.io/apimachinery/pkg/runtime"
29
+ apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
30
30
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
31
31
"k8s.io/client-go/rest"
32
32
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -64,7 +64,7 @@ func (m *MockUnpacker) Cleanup(_ context.Context, _ *source.BundleSource) error
64
64
func newClient (t * testing.T ) client.Client {
65
65
// TODO: this is a live client, which behaves differently than a cache client.
66
66
// We may want to use a caching client instead to get closer to real behavior.
67
- sch := runtime .NewScheme ()
67
+ sch := apimachineryruntime .NewScheme ()
68
68
require .NoError (t , ocv1 .AddToScheme (sch ))
69
69
cl , err := client .New (config , client.Options {Scheme : sch })
70
70
require .NoError (t , err )
@@ -162,6 +162,21 @@ func TestMain(m *testing.M) {
162
162
ErrorIfCRDPathMissing : true ,
163
163
}
164
164
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
+
165
180
var err error
166
181
config , err = testEnv .Start ()
167
182
utilruntime .Must (err )
@@ -179,3 +194,15 @@ func TestMain(m *testing.M) {
179
194
utilruntime .Must (testEnv .Stop ())
180
195
os .Exit (code )
181
196
}
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