apply integration test: fix ordering test flake

Most of the time the managed fields were returned in the order as expected,
but not always. Semantically the order is irrelevant, so the comparison gets
relaxed to ignore it by normalizing first.
This commit is contained in:
Patrick Ohly
2025-09-03 16:50:42 +02:00
parent 00b6a4c7af
commit 7191ffc390

View File

@@ -83,12 +83,20 @@ func testOptionalListMapKey(tCtx ktesting.TContext) {
requireManagedFields := func(what string, obj *unstructured.Unstructured, expectedManagedFields any) {
tCtx.Helper()
actualManagedFields, _, _ := unstructured.NestedFieldCopy(obj.Object, "metadata", "managedFields")
// Strip non-deterministic time.
if actualManagedFields != nil {
managers := actualManagedFields.([]any)
for i := range managers {
// Strip non-deterministic time.
unstructured.RemoveNestedField(managers[i].(map[string]any), "time")
}
// Sort by manager. There should be at most one entry per manager, so
// no need for a tie breaker. Semantically the order is irrelevant,
// so we don't need to expect a specific one here.
//
// The order turned out to be non-deterministic (test flake!) without this.
slices.SortFunc(managers, func(a, b any) int {
return strings.Compare(a.(map[string]any)["manager"].(string), b.(map[string]any)["manager"].(string))
})
}
require.Equal(tCtx, dump(expectedManagedFields), dump(actualManagedFields), "%s:\n%s", what, dump(obj))
}