Skip to content

Commit bc4105d

Browse files
committed
test: wait for CAPI availability
That makes tests more stable. Signed-off-by: Alexey Palazhchenko <[email protected]>
1 parent c82b8ab commit bc4105d

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

internal/integration/setup_test.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func setupSuite(t *testing.T) (context.Context, client.Client) {
8686

8787
stopCAPI(ctx, t, c)
8888

89+
waitForCAPIAvailability(ctx, t, c)
90+
8991
// TODO(aleksi): make one manager per test / namespace (move to setupTest)?
9092

9193
mgr, err := ctrl.NewManager(restCfg, ctrl.Options{})
@@ -193,8 +195,16 @@ func startTestEnv(ctx context.Context, t *testing.T) *rest.Config {
193195
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
194196
CRDInstallOptions: envtest.CRDInstallOptions{
195197
ErrorIfPathMissing: true,
198+
MaxTime: 20 * time.Second,
199+
PollInterval: time.Second,
196200
CleanUpAfterUse: !skipCleanupF,
197201
},
202+
WebhookInstallOptions: envtest.WebhookInstallOptions{
203+
// TODO paths?
204+
205+
MaxTime: 10 * time.Second,
206+
PollInterval: time.Second,
207+
},
198208
ErrorIfCRDPathMissing: true,
199209
UseExistingCluster: pointer.BoolPtr(true),
200210
}
@@ -239,7 +249,7 @@ func startTestEnv(ctx context.Context, t *testing.T) *rest.Config {
239249
return res.cfg
240250
}
241251

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.
243253
//
244254
// Context cancelation is honored.
245255
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) {
248258
t.Log("Stopping CAPI components ...")
249259

250260
var deployment appsv1.Deployment
261+
key := client.ObjectKey{Namespace: "capi-system", Name: "capi-controller-manager"}
251262

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))
253264

254265
patchHelper, err := patch.NewHelper(&deployment, c)
255266
require.NoError(t, err)
@@ -261,7 +272,7 @@ func stopCAPI(ctx context.Context, t *testing.T, c client.Client) {
261272
for {
262273
var deployment appsv1.Deployment
263274

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))
265276

266277
if deployment.Status.Replicas == 0 {
267278
break
@@ -279,3 +290,46 @@ func stopCAPI(ctx context.Context, t *testing.T, c client.Client) {
279290

280291
t.Log("Done stopping CAPI components.")
281292
}
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

Comments
 (0)