Skip to content

Commit 80a662c

Browse files
committed
make tests even more reliable
It seems that pretty often, the wait loop that is meant to wait for all initializers to be gone is too quick and will finish even before kcp has assigned a cluster name (spec.cluster) to the Workspace, leading to test failures. This commit therefore extends the waitloop to also wait for the cluster ID. I also reworked some AI-written stuff where we were using unstructured unneccessarily. Instead now we just uses a native CRD type and can get rid of all the type casting. On-behalf-of: @SAP christoph.mewes@sap.com
1 parent 83a1003 commit 80a662c

File tree

2 files changed

+10
-36
lines changed

2 files changed

+10
-36
lines changed

test/e2e/clusterinit/wait_for_ready_test.go

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
kcptenancyv1alpha1 "github.com/kcp-dev/sdk/apis/tenancy/v1alpha1"
3232

3333
rbacv1 "k8s.io/api/rbac/v1"
34+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3435
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3536
"k8s.io/apimachinery/pkg/types"
3637
ctrlruntime "sigs.k8s.io/controller-runtime"
@@ -193,51 +194,21 @@ spec:
193194
// Verify the CRD exists in the target workspace and is Established
194195
targetClient := kcpClusterClient.Cluster(rootCluster.Join(targetWorkspace))
195196

196-
crd := utils.YAMLToUnstructured(t, `
197-
apiVersion: apiextensions.k8s.io/v1
198-
kind: CustomResourceDefinition
199-
metadata:
200-
name: widgets.example.com
201-
`)
202-
203-
if err := targetClient.Get(ctx, types.NamespacedName{Name: crd.GetName()}, crd); err != nil {
197+
crd := &apiextensionsv1.CustomResourceDefinition{}
198+
if err := targetClient.Get(ctx, types.NamespacedName{Name: "widgets.example.com"}, crd); err != nil {
204199
t.Fatalf("Failed to find CRD in target workspace: %v", err)
205200
}
206201

207202
// Verify the CRD has the Established condition set to True
208-
conditions, found, err := getConditions(crd.Object)
209-
if err != nil || !found {
210-
t.Fatal("CRD does not have conditions in status")
211-
}
212-
213203
established := false
214-
for _, c := range conditions {
215-
condition, ok := c.(map[string]any)
216-
if !ok {
217-
continue
218-
}
219-
220-
cType := condition["type"].(string)
221-
cStatus := condition["status"].(string)
222-
if cType == "Established" && cStatus == "True" {
204+
for _, condition := range crd.Status.Conditions {
205+
if condition.Type == apiextensionsv1.Established && condition.Status == apiextensionsv1.ConditionTrue {
223206
established = true
224207
break
225208
}
226209
}
227210

228211
if !established {
229-
t.Fatal("Expected CRD to have Established=True condition, but it was not found or not True")
230-
}
231-
}
232-
233-
func getConditions(obj map[string]any) ([]any, bool, error) {
234-
status, ok := obj["status"].(map[string]any)
235-
if !ok {
236-
return nil, false, nil
237-
}
238-
conditions, ok := status["conditions"].([]any)
239-
if !ok {
240-
return nil, false, nil
212+
t.Fatal("Expected CRD to have Established=True condition, but has not.")
241213
}
242-
return conditions, true, nil
243214
}

test/utils/fixtures.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ func WaitForWorkspaceInitialization(t *testing.T, ctx context.Context, clusterCl
9393
return false, err
9494
}
9595

96-
return len(ws.Status.Initializers) == 0, nil
96+
// Make sure to also look for a cluster ID, otherwise this wait loop might
97+
// be so fast, it finishes between the first initializer was added and before
98+
// kcp even assigned the cluster name.
99+
return ws.Spec.Cluster != "" && len(ws.Status.Initializers) == 0, nil
97100
})
98101
if err != nil {
99102
t.Fatalf("Failed to wait for workspace to be initialized: %v", err)

0 commit comments

Comments
 (0)