Skip to content

Commit c811170

Browse files
committed
Add an error matcher, convert 2 tests
I fixed up the TestValidateEndpointsCreate path to show the matcher instead of manual origin checking. I picked TestValidateTopologySpreadConstraints because it was the last failing test on my screen when I changed on of the commonly hard-coded error strings. I fixed exactly those validation errors that were needed to make this test pass. Some of the Origin values can be debated. The `field/testing.Matcher` interface allows tests to configure the criteria by which they want to match expected and actual errors. The hope is that everyone will use Origin for Invalid errors. There's some collateral impact for tests which use exact-comparisons and don't expect origins. These are all candidates for using the matcher.
1 parent 6b7e38f commit c811170

File tree

8 files changed

+361
-107
lines changed

8 files changed

+361
-107
lines changed

pkg/apis/apps/validation/validation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ func TestValidateStatefulSet(t *testing.T) {
523523
},
524524
}
525525

526-
cmpOpts := []cmp.Option{cmpopts.IgnoreFields(field.Error{}, "BadValue", "Detail"), cmpopts.SortSlices(func(a, b *field.Error) bool { return a.Error() < b.Error() })}
526+
cmpOpts := []cmp.Option{cmpopts.IgnoreFields(field.Error{}, "BadValue", "Detail", "Origin"), cmpopts.SortSlices(func(a, b *field.Error) bool { return a.Error() < b.Error() })}
527527
for _, testCase := range append(successCases, errorCases...) {
528528
name := testCase.name
529529
var testTitle string
@@ -936,7 +936,7 @@ func TestValidateStatefulSetUpdate(t *testing.T) {
936936
}
937937

938938
cmpOpts := []cmp.Option{
939-
cmpopts.IgnoreFields(field.Error{}, "BadValue", "Detail"),
939+
cmpopts.IgnoreFields(field.Error{}, "BadValue", "Detail", "Origin"),
940940
cmpopts.SortSlices(func(a, b *field.Error) bool { return a.Error() < b.Error() }),
941941
cmpopts.EquateEmpty(),
942942
}

pkg/apis/batch/validation/validation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var (
4848
timeZoneEmptySpace = " "
4949
)
5050

51-
var ignoreErrValueDetail = cmpopts.IgnoreFields(field.Error{}, "BadValue", "Detail")
51+
var ignoreErrValueDetail = cmpopts.IgnoreFields(field.Error{}, "BadValue", "Detail", "Origin")
5252

5353
func getValidManualSelector() *metav1.LabelSelector {
5454
return &metav1.LabelSelector{

pkg/apis/core/validation/validation.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,15 @@ func ValidateNonnegativeQuantity(value resource.Quantity, fldPath *field.Path) f
350350
return allErrs
351351
}
352352

353+
// Validates that given value is positive.
354+
func ValidatePositiveField(value int64, fldPath *field.Path) field.ErrorList {
355+
allErrs := field.ErrorList{}
356+
if value <= 0 {
357+
allErrs = append(allErrs, field.Invalid(fldPath, value, isNotPositiveErrorMsg).WithOrigin("minimum"))
358+
}
359+
return allErrs
360+
}
361+
353362
// Validates that a Quantity is positive
354363
func ValidatePositiveQuantityValue(value resource.Quantity, fldPath *field.Path) field.ErrorList {
355364
allErrs := field.ErrorList{}
@@ -7905,8 +7914,8 @@ func validateTopologySpreadConstraints(constraints []core.TopologySpreadConstrai
79057914

79067915
for i, constraint := range constraints {
79077916
subFldPath := fldPath.Index(i)
7908-
if err := ValidateMaxSkew(subFldPath.Child("maxSkew"), constraint.MaxSkew); err != nil {
7909-
allErrs = append(allErrs, err)
7917+
if errs := ValidatePositiveField(int64(constraint.MaxSkew), subFldPath.Child("maxSkew")); len(errs) > 0 {
7918+
allErrs = append(allErrs, errs...)
79107919
}
79117920
if err := ValidateTopologyKey(subFldPath.Child("topologyKey"), constraint.TopologyKey); err != nil {
79127921
allErrs = append(allErrs, err)
@@ -7934,26 +7943,16 @@ func validateTopologySpreadConstraints(constraints []core.TopologySpreadConstrai
79347943
return allErrs
79357944
}
79367945

7937-
// ValidateMaxSkew tests that the argument is a valid MaxSkew.
7938-
func ValidateMaxSkew(fldPath *field.Path, maxSkew int32) *field.Error {
7939-
if maxSkew <= 0 {
7940-
return field.Invalid(fldPath, maxSkew, isNotPositiveErrorMsg)
7941-
}
7942-
return nil
7943-
}
7944-
79457946
// validateMinDomains tests that the argument is a valid MinDomains.
79467947
func validateMinDomains(fldPath *field.Path, minDomains *int32, action core.UnsatisfiableConstraintAction) field.ErrorList {
79477948
if minDomains == nil {
79487949
return nil
79497950
}
79507951
var allErrs field.ErrorList
7951-
if *minDomains <= 0 {
7952-
allErrs = append(allErrs, field.Invalid(fldPath, minDomains, isNotPositiveErrorMsg))
7953-
}
7952+
allErrs = append(allErrs, ValidatePositiveField(int64(*minDomains), fldPath)...)
79547953
// When MinDomains is non-nil, whenUnsatisfiable must be DoNotSchedule.
79557954
if action != core.DoNotSchedule {
7956-
allErrs = append(allErrs, field.Invalid(fldPath, minDomains, fmt.Sprintf("can only use minDomains if whenUnsatisfiable=%s, not %s", core.DoNotSchedule, action)))
7955+
allErrs = append(allErrs, field.Invalid(fldPath, minDomains, fmt.Sprintf("can only use minDomains if whenUnsatisfiable=%s, not %s", core.DoNotSchedule, action)).WithOrigin("dependsOn"))
79577956
}
79587957
return allErrs
79597958
}
@@ -8077,7 +8076,7 @@ func validateMatchLabelKeysInTopologySpread(fldPath *field.Path, matchLabelKeys
80778076
for i, key := range matchLabelKeys {
80788077
allErrs = append(allErrs, unversionedvalidation.ValidateLabelName(key, fldPath.Index(i))...)
80798078
if labelSelectorKeys.Has(key) {
8080-
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), key, "exists in both matchLabelKeys and labelSelector"))
8079+
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), key, "exists in both matchLabelKeys and labelSelector").WithOrigin("overlappingKeys"))
80818080
}
80828081
}
80838082

0 commit comments

Comments
 (0)