Skip to content

Commit 1a88100

Browse files
committed
test: Ensure ownerRef assertions for all Kinds are evaluated
The assertFuncs are passed as a list. Previously, if two assertFuncs were defined for the same Kind, the assertFunc that appeared later in the list would overwrite the one that appeared earlier. If someone introduced a second assertFunc for a given Kind, they would receive no signal that the first assertFunc was overwritten. That would result in missed assertions. With this change, if two or more assertFuncs are defined for the same Kind, both are evaluated.
1 parent 180b50f commit 1a88100

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

test/framework/ownerreference_helpers.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ func ValidateOwnerReferencesResilience(ctx context.Context, proxy ClusterProxy,
102102
}
103103

104104
func AssertOwnerReferences(namespace, kubeconfigPath string, ownerGraphFilterFunction clusterctlcluster.GetOwnerGraphFilterFunction, assertFuncs ...map[string]func(reference []metav1.OwnerReference) error) {
105-
allAssertFuncs := map[string]func(reference []metav1.OwnerReference) error{}
105+
allAssertFuncs := map[string][]func(reference []metav1.OwnerReference) error{}
106106
for _, m := range assertFuncs {
107107
for k, v := range m {
108-
allAssertFuncs[k] = v
108+
allAssertFuncs[k] = append(allAssertFuncs[k], v)
109109
}
110110
}
111111
Eventually(func() error {
@@ -125,8 +125,10 @@ func AssertOwnerReferences(namespace, kubeconfigPath string, ownerGraphFilterFun
125125
allErrs = append(allErrs, fmt.Errorf("kind %s does not have an associated ownerRef assertion function", v.Object.Kind))
126126
continue
127127
}
128-
if err := allAssertFuncs[v.Object.Kind](v.Owners); err != nil {
129-
allErrs = append(allErrs, errors.Wrapf(err, "Unexpected ownerReferences for %s/%s", v.Object.Kind, v.Object.Name))
128+
for _, f := range allAssertFuncs[v.Object.Kind] {
129+
if err := f(v.Owners); err != nil {
130+
allErrs = append(allErrs, errors.Wrapf(err, "Unexpected ownerReferences for %s/%s", v.Object.Kind, v.Object.Name))
131+
}
130132
}
131133
}
132134
return kerrors.NewAggregate(allErrs)
@@ -308,7 +310,6 @@ var DockerInfraOwnerReferenceAssertions = map[string]func([]metav1.OwnerReferenc
308310
dockerMachineKind: func(owners []metav1.OwnerReference) error {
309311
// The DockerMachine must be owned and controlled by a Machine or a DockerMachinePool.
310312
return HasOneOfExactOwners(owners, []metav1.OwnerReference{machineController}, []metav1.OwnerReference{machineController, dockerMachinePoolController})
311-
312313
},
313314
dockerMachineTemplateKind: func(owners []metav1.OwnerReference) error {
314315
// Base DockerMachineTemplates referenced in a ClusterClass must be owned by the ClusterClass.

0 commit comments

Comments
 (0)