diff --git a/pkg/api/testing/validation.go b/pkg/api/testing/validation.go index 7aab0fd6d59..d4eb1ed59de 100644 --- a/pkg/api/testing/validation.go +++ b/pkg/api/testing/validation.go @@ -424,12 +424,21 @@ func verifyValidationEquivalence(t *testing.T, expectedErrs field.ErrorList, run testCtx := rest.WithAllDeclarativeEnforcedForTest(ctx) allDeclarativeErrs := runValidations(testCtx) + // In this mode, strategy.go validation remove all hand written validations errors which are marked covered by DV. + // so we have to filter out errors which are filtered out by strategy.go. + // This is because DV will not return those errors due to short circuiting of validations at the parent node. + filteredExpectedErrors := make(field.ErrorList, 0, len(expectedErrs)) + for _, err := range expectedErrs { + if !err.ShortCircuitedInDV { + filteredExpectedErrors = append(filteredExpectedErrors, err) + } + } // The matcher here is more specific to ensure that errors from Alpha rules // are included and matched correctly. // This also ensure that errors are coming from the declarative validations only. dvErrorMatcher := errOutputMatcher.ByValidationStabilityLevel().BySource() - if len(expectedErrs) > 0 { - dvErrorMatcher.Test(t, expectedErrs, allDeclarativeErrs) + if len(filteredExpectedErrors) > 0 { + dvErrorMatcher.Test(t, filteredExpectedErrors, allDeclarativeErrs) } else if len(allDeclarativeErrs) != 0 { t.Errorf("expected no errors, but got: %v", allDeclarativeErrs) }