mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-02 03:08:15 +00:00
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.
This commit is contained in:
@@ -350,6 +350,15 @@ func ValidateNonnegativeQuantity(value resource.Quantity, fldPath *field.Path) f
|
||||
return allErrs
|
||||
}
|
||||
|
||||
// Validates that given value is positive.
|
||||
func ValidatePositiveField(value int64, fldPath *field.Path) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
if value <= 0 {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath, value, isNotPositiveErrorMsg).WithOrigin("minimum"))
|
||||
}
|
||||
return allErrs
|
||||
}
|
||||
|
||||
// Validates that a Quantity is positive
|
||||
func ValidatePositiveQuantityValue(value resource.Quantity, fldPath *field.Path) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
@@ -7905,8 +7914,8 @@ func validateTopologySpreadConstraints(constraints []core.TopologySpreadConstrai
|
||||
|
||||
for i, constraint := range constraints {
|
||||
subFldPath := fldPath.Index(i)
|
||||
if err := ValidateMaxSkew(subFldPath.Child("maxSkew"), constraint.MaxSkew); err != nil {
|
||||
allErrs = append(allErrs, err)
|
||||
if errs := ValidatePositiveField(int64(constraint.MaxSkew), subFldPath.Child("maxSkew")); len(errs) > 0 {
|
||||
allErrs = append(allErrs, errs...)
|
||||
}
|
||||
if err := ValidateTopologyKey(subFldPath.Child("topologyKey"), constraint.TopologyKey); err != nil {
|
||||
allErrs = append(allErrs, err)
|
||||
@@ -7934,26 +7943,16 @@ func validateTopologySpreadConstraints(constraints []core.TopologySpreadConstrai
|
||||
return allErrs
|
||||
}
|
||||
|
||||
// ValidateMaxSkew tests that the argument is a valid MaxSkew.
|
||||
func ValidateMaxSkew(fldPath *field.Path, maxSkew int32) *field.Error {
|
||||
if maxSkew <= 0 {
|
||||
return field.Invalid(fldPath, maxSkew, isNotPositiveErrorMsg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateMinDomains tests that the argument is a valid MinDomains.
|
||||
func validateMinDomains(fldPath *field.Path, minDomains *int32, action core.UnsatisfiableConstraintAction) field.ErrorList {
|
||||
if minDomains == nil {
|
||||
return nil
|
||||
}
|
||||
var allErrs field.ErrorList
|
||||
if *minDomains <= 0 {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath, minDomains, isNotPositiveErrorMsg))
|
||||
}
|
||||
allErrs = append(allErrs, ValidatePositiveField(int64(*minDomains), fldPath)...)
|
||||
// When MinDomains is non-nil, whenUnsatisfiable must be DoNotSchedule.
|
||||
if action != core.DoNotSchedule {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath, minDomains, fmt.Sprintf("can only use minDomains if whenUnsatisfiable=%s, not %s", core.DoNotSchedule, action)))
|
||||
allErrs = append(allErrs, field.Invalid(fldPath, minDomains, fmt.Sprintf("can only use minDomains if whenUnsatisfiable=%s, not %s", core.DoNotSchedule, action)).WithOrigin("dependsOn"))
|
||||
}
|
||||
return allErrs
|
||||
}
|
||||
@@ -8077,7 +8076,7 @@ func validateMatchLabelKeysInTopologySpread(fldPath *field.Path, matchLabelKeys
|
||||
for i, key := range matchLabelKeys {
|
||||
allErrs = append(allErrs, unversionedvalidation.ValidateLabelName(key, fldPath.Index(i))...)
|
||||
if labelSelectorKeys.Has(key) {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), key, "exists in both matchLabelKeys and labelSelector"))
|
||||
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), key, "exists in both matchLabelKeys and labelSelector").WithOrigin("overlappingKeys"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user