Skip to content

Commit aad87f2

Browse files
authored
Merge pull request kubernetes#130388 from thockin/error_matcher_and_origin
Add an error matcher, convert 2 tests
2 parents d496b6c + c811170 commit aad87f2

File tree

9 files changed

+368
-114
lines changed

9 files changed

+368
-114
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: 20 additions & 21 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{}
@@ -6266,7 +6275,7 @@ func ValidateReplicationControllerSpec(spec, oldSpec *core.ReplicationController
62666275
allErrs := field.ErrorList{}
62676276
allErrs = append(allErrs, ValidateNonnegativeField(int64(spec.MinReadySeconds), fldPath.Child("minReadySeconds"))...)
62686277
allErrs = append(allErrs, ValidateNonEmptySelector(spec.Selector, fldPath.Child("selector"))...)
6269-
allErrs = append(allErrs, ValidateNonnegativeField(int64(spec.Replicas), fldPath.Child("replicas")).WithOrigin("minimum")...)
6278+
allErrs = append(allErrs, ValidateNonnegativeField(int64(spec.Replicas), fldPath.Child("replicas"))...)
62706279
allErrs = append(allErrs, ValidatePodTemplateSpecForRC(spec.Template, spec.Selector, spec.Replicas, fldPath.Child("template"), opts)...)
62716280
return allErrs
62726281
}
@@ -7489,18 +7498,18 @@ func ValidateNonSpecialIP(ipAddress string, fldPath *field.Path) field.ErrorList
74897498
return allErrs
74907499
}
74917500
if ip.IsUnspecified() {
7492-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, fmt.Sprintf("may not be unspecified (%v)", ipAddress)).WithOrigin("format=non-special-ip"))
7501+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, fmt.Sprintf("may not be unspecified (%v)", ipAddress)))
74937502
}
74947503
if ip.IsLoopback() {
7495-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the loopback range (127.0.0.0/8, ::1/128)").WithOrigin("format=non-special-ip"))
7504+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the loopback range (127.0.0.0/8, ::1/128)"))
74967505
}
74977506
if ip.IsLinkLocalUnicast() {
7498-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the link-local range (169.254.0.0/16, fe80::/10)").WithOrigin("format=non-special-ip"))
7507+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the link-local range (169.254.0.0/16, fe80::/10)"))
74997508
}
75007509
if ip.IsLinkLocalMulticast() {
7501-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the link-local multicast range (224.0.0.0/24, ff02::/10)").WithOrigin("format=non-special-ip"))
7510+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the link-local multicast range (224.0.0.0/24, ff02::/10)"))
75027511
}
7503-
return allErrs
7512+
return allErrs.WithOrigin("format=non-special-ip")
75047513
}
75057514

75067515
func validateEndpointPort(port *core.EndpointPort, requireName bool, fldPath *field.Path) 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)