@@ -86,6 +86,8 @@ func setupSuite(t *testing.T) (context.Context, client.Client) {
86
86
87
87
stopCAPI (ctx , t , c )
88
88
89
+ waitForCAPIAvailability (ctx , t , c )
90
+
89
91
// TODO(aleksi): make one manager per test / namespace (move to setupTest)?
90
92
91
93
mgr , err := ctrl .NewManager (restCfg , ctrl.Options {})
@@ -193,8 +195,16 @@ func startTestEnv(ctx context.Context, t *testing.T) *rest.Config {
193
195
CRDDirectoryPaths : []string {filepath .Join (".." , ".." , "config" , "crd" , "bases" )},
194
196
CRDInstallOptions : envtest.CRDInstallOptions {
195
197
ErrorIfPathMissing : true ,
198
+ MaxTime : 20 * time .Second ,
199
+ PollInterval : time .Second ,
196
200
CleanUpAfterUse : ! skipCleanupF ,
197
201
},
202
+ WebhookInstallOptions : envtest.WebhookInstallOptions {
203
+ // TODO paths?
204
+
205
+ MaxTime : 10 * time .Second ,
206
+ PollInterval : time .Second ,
207
+ },
198
208
ErrorIfCRDPathMissing : true ,
199
209
UseExistingCluster : pointer .BoolPtr (true ),
200
210
}
@@ -239,7 +249,7 @@ func startTestEnv(ctx context.Context, t *testing.T) *rest.Config {
239
249
return res .cfg
240
250
}
241
251
242
- // stopCAPI stops CAPI components so they don't interfere with our tests.
252
+ // stopCAPI stops CAPI components that we don't need so they don't interfere with our tests.
243
253
//
244
254
// Context cancelation is honored.
245
255
func stopCAPI (ctx context.Context , t * testing.T , c client.Client ) {
@@ -248,8 +258,9 @@ func stopCAPI(ctx context.Context, t *testing.T, c client.Client) {
248
258
t .Log ("Stopping CAPI components ..." )
249
259
250
260
var deployment appsv1.Deployment
261
+ key := client.ObjectKey {Namespace : "capi-system" , Name : "capi-controller-manager" }
251
262
252
- require .NoError (t , c .Get (ctx , client. ObjectKey { Namespace : "capi-system" , Name : "capi-controller-manager" } , & deployment ))
263
+ require .NoError (t , c .Get (ctx , key , & deployment ))
253
264
254
265
patchHelper , err := patch .NewHelper (& deployment , c )
255
266
require .NoError (t , err )
@@ -261,7 +272,7 @@ func stopCAPI(ctx context.Context, t *testing.T, c client.Client) {
261
272
for {
262
273
var deployment appsv1.Deployment
263
274
264
- require .NoError (t , c .Get (ctx , client. ObjectKey { Namespace : "capi-system" , Name : "capi-controller-manager" } , & deployment ))
275
+ require .NoError (t , c .Get (ctx , key , & deployment ))
265
276
266
277
if deployment .Status .Replicas == 0 {
267
278
break
@@ -279,3 +290,46 @@ func stopCAPI(ctx context.Context, t *testing.T, c client.Client) {
279
290
280
291
t .Log ("Done stopping CAPI components." )
281
292
}
293
+
294
+ // waitForCAPIAvailability waits for needed CAPI components availability.
295
+ //
296
+ // Context cancelation is honored.
297
+ func waitForCAPIAvailability (ctx context.Context , t * testing.T , c client.Client ) {
298
+ t .Helper ()
299
+
300
+ t .Log ("Waiting for CAPI availability ..." )
301
+
302
+ // TODO is is not entirely clear why we need webhooks
303
+
304
+ key := client.ObjectKey {Namespace : "capi-webhook-system" , Name : "capi-controller-manager" }
305
+
306
+ for {
307
+ var deployment appsv1.Deployment
308
+
309
+ require .NoError (t , c .Get (ctx , key , & deployment ))
310
+
311
+ var available bool
312
+ for _ , cond := range deployment .Status .Conditions {
313
+ if cond .Type != appsv1 .DeploymentAvailable {
314
+ continue
315
+ }
316
+
317
+ available = cond .Status == corev1 .ConditionTrue
318
+ }
319
+
320
+ if available {
321
+ break
322
+ }
323
+
324
+ t .Logf ("Waiting: %+v ..." , deployment .Status )
325
+
326
+ select {
327
+ case <- time .After (5 * time .Second ):
328
+ // nothing, continue
329
+ case <- ctx .Done ():
330
+ t .Fatalf ("Failed to wait for CAPI availability: %s." , ctx .Err ())
331
+ }
332
+ }
333
+
334
+ t .Log ("CAPI is available." )
335
+ }
0 commit comments