@@ -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,15 @@ func TestMain(m *testing.M) {
162
162
ErrorIfCRDPathMissing : true ,
163
163
}
164
164
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
+
165
174
var err error
166
175
config , err = testEnv .Start ()
167
176
utilruntime .Must (err )
@@ -179,3 +188,15 @@ func TestMain(m *testing.M) {
179
188
utilruntime .Must (testEnv .Stop ())
180
189
os .Exit (code )
181
190
}
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