Skip to content

Commit 07477c6

Browse files
yongruilinthockin
andcommitted
test: convert ValidateEndpointsCreate to use error Origin field in test
Update ValidateEndpointsCreate validation tests to use the new Origin field for more precise error comparisons. It leverage the Origin field instead of detailed error messages, improving test robustness and readability. Co-authored-by: Tim Hockin <[email protected]>
1 parent a488509 commit 07477c6

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

pkg/apis/core/validation/validation.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func ValidateAnnotations(annotations map[string]string, fldPath *field.Path) fie
133133
func ValidateDNS1123Label(value string, fldPath *field.Path) field.ErrorList {
134134
allErrs := field.ErrorList{}
135135
for _, msg := range validation.IsDNS1123Label(value) {
136-
allErrs = append(allErrs, field.Invalid(fldPath, value, msg))
136+
allErrs = append(allErrs, field.Invalid(fldPath, value, msg).WithOrigin("format=dns-label"))
137137
}
138138
return allErrs
139139
}
@@ -142,7 +142,7 @@ func ValidateDNS1123Label(value string, fldPath *field.Path) field.ErrorList {
142142
func ValidateQualifiedName(value string, fldPath *field.Path) field.ErrorList {
143143
allErrs := field.ErrorList{}
144144
for _, msg := range validation.IsQualifiedName(value) {
145-
allErrs = append(allErrs, field.Invalid(fldPath, value, msg))
145+
allErrs = append(allErrs, field.Invalid(fldPath, value, msg).WithOrigin("format=qualified-name"))
146146
}
147147
return allErrs
148148
}
@@ -7466,7 +7466,7 @@ func validateEndpointAddress(address *core.EndpointAddress, fldPath *field.Path)
74667466
// During endpoint update, verify that NodeName is a DNS subdomain and transition rules allow the update
74677467
if address.NodeName != nil {
74687468
for _, msg := range ValidateNodeName(*address.NodeName, false) {
7469-
allErrs = append(allErrs, field.Invalid(fldPath.Child("nodeName"), *address.NodeName, msg))
7469+
allErrs = append(allErrs, field.Invalid(fldPath.Child("nodeName"), *address.NodeName, msg).WithOrigin("format=dns-label"))
74707470
}
74717471
}
74727472
allErrs = append(allErrs, ValidateNonSpecialIP(address.IP, fldPath.Child("ip"))...)
@@ -7485,20 +7485,20 @@ func ValidateNonSpecialIP(ipAddress string, fldPath *field.Path) field.ErrorList
74857485
allErrs := field.ErrorList{}
74867486
ip := netutils.ParseIPSloppy(ipAddress)
74877487
if ip == nil {
7488-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "must be a valid IP address"))
7488+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "must be a valid IP address").WithOrigin("format=ip-sloppy"))
74897489
return allErrs
74907490
}
74917491
if ip.IsUnspecified() {
7492-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, fmt.Sprintf("may not be unspecified (%v)", ipAddress)))
7492+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, fmt.Sprintf("may not be unspecified (%v)", ipAddress)).WithOrigin("format=non-special-ip"))
74937493
}
74947494
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)"))
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"))
74967496
}
74977497
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)"))
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"))
74997499
}
75007500
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)"))
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"))
75027502
}
75037503
return allErrs
75047504
}
@@ -7511,7 +7511,7 @@ func validateEndpointPort(port *core.EndpointPort, requireName bool, fldPath *fi
75117511
allErrs = append(allErrs, ValidateDNS1123Label(port.Name, fldPath.Child("name"))...)
75127512
}
75137513
for _, msg := range validation.IsValidPortNum(int(port.Port)) {
7514-
allErrs = append(allErrs, field.Invalid(fldPath.Child("port"), port.Port, msg))
7514+
allErrs = append(allErrs, field.Invalid(fldPath.Child("port"), port.Port, msg).WithOrigin("portNum"))
75157515
}
75167516
if len(port.Protocol) == 0 {
75177517
allErrs = append(allErrs, field.Required(fldPath.Child("protocol"), ""))

pkg/apis/core/validation/validation_test.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9183,7 +9183,7 @@ func TestValidateContainers(t *testing.T) {
91839183
t.Fatal("expected error but received none")
91849184
}
91859185

9186-
if diff := cmp.Diff(tc.expectedErrors, errs, cmpopts.IgnoreFields(field.Error{}, "BadValue", "Detail")); diff != "" {
9186+
if diff := cmp.Diff(tc.expectedErrors, errs, cmpopts.IgnoreFields(field.Error{}, "BadValue", "Detail", "Origin")); diff != "" {
91879187
t.Errorf("unexpected diff in errors (-want, +got):\n%s", diff)
91889188
t.Errorf("INFO: all errors:\n%s", prettyErrorList(errs))
91899189
}
@@ -20674,7 +20674,7 @@ func TestValidateEndpointsCreate(t *testing.T) {
2067420674
errorCases := map[string]struct {
2067520675
endpoints core.Endpoints
2067620676
errorType field.ErrorType
20677-
errorDetail string
20677+
errorOrigin string
2067820678
}{
2067920679
"missing namespace": {
2068020680
endpoints: core.Endpoints{ObjectMeta: metav1.ObjectMeta{Name: "mysvc"}},
@@ -20685,14 +20685,12 @@ func TestValidateEndpointsCreate(t *testing.T) {
2068520685
errorType: "FieldValueRequired",
2068620686
},
2068720687
"invalid namespace": {
20688-
endpoints: core.Endpoints{ObjectMeta: metav1.ObjectMeta{Name: "mysvc", Namespace: "no@#invalid.;chars\"allowed"}},
20689-
errorType: "FieldValueInvalid",
20690-
errorDetail: dnsLabelErrMsg,
20688+
endpoints: core.Endpoints{ObjectMeta: metav1.ObjectMeta{Name: "mysvc", Namespace: "no@#invalid.;chars\"allowed"}},
20689+
errorType: "FieldValueInvalid",
2069120690
},
2069220691
"invalid name": {
20693-
endpoints: core.Endpoints{ObjectMeta: metav1.ObjectMeta{Name: "-_Invliad^&Characters", Namespace: "namespace"}},
20694-
errorType: "FieldValueInvalid",
20695-
errorDetail: dnsSubdomainLabelErrMsg,
20692+
endpoints: core.Endpoints{ObjectMeta: metav1.ObjectMeta{Name: "-_Invliad^&Characters", Namespace: "namespace"}},
20693+
errorType: "FieldValueInvalid",
2069620694
},
2069720695
"empty addresses": {
2069820696
endpoints: core.Endpoints{
@@ -20712,7 +20710,7 @@ func TestValidateEndpointsCreate(t *testing.T) {
2071220710
}},
2071320711
},
2071420712
errorType: "FieldValueInvalid",
20715-
errorDetail: "must be a valid IP address",
20713+
errorOrigin: "format=ip-sloppy",
2071620714
},
2071720715
"Multiple ports, one without name": {
2071820716
endpoints: core.Endpoints{
@@ -20733,7 +20731,7 @@ func TestValidateEndpointsCreate(t *testing.T) {
2073320731
}},
2073420732
},
2073520733
errorType: "FieldValueInvalid",
20736-
errorDetail: "between",
20734+
errorOrigin: "portNum",
2073720735
},
2073820736
"Invalid protocol": {
2073920737
endpoints: core.Endpoints{
@@ -20754,7 +20752,7 @@ func TestValidateEndpointsCreate(t *testing.T) {
2075420752
}},
2075520753
},
2075620754
errorType: "FieldValueInvalid",
20757-
errorDetail: "must be a valid IP address",
20755+
errorOrigin: "format=ip-sloppy",
2075820756
},
2075920757
"Port missing number": {
2076020758
endpoints: core.Endpoints{
@@ -20765,7 +20763,7 @@ func TestValidateEndpointsCreate(t *testing.T) {
2076520763
}},
2076620764
},
2076720765
errorType: "FieldValueInvalid",
20768-
errorDetail: "between",
20766+
errorOrigin: "portNum",
2076920767
},
2077020768
"Port missing protocol": {
2077120769
endpoints: core.Endpoints{
@@ -20786,7 +20784,7 @@ func TestValidateEndpointsCreate(t *testing.T) {
2078620784
}},
2078720785
},
2078820786
errorType: "FieldValueInvalid",
20789-
errorDetail: "loopback",
20787+
errorOrigin: "format=non-special-ip",
2079020788
},
2079120789
"Address is link-local": {
2079220790
endpoints: core.Endpoints{
@@ -20797,7 +20795,7 @@ func TestValidateEndpointsCreate(t *testing.T) {
2079720795
}},
2079820796
},
2079920797
errorType: "FieldValueInvalid",
20800-
errorDetail: "link-local",
20798+
errorOrigin: "format=non-special-ip",
2080120799
},
2080220800
"Address is link-local multicast": {
2080320801
endpoints: core.Endpoints{
@@ -20808,7 +20806,7 @@ func TestValidateEndpointsCreate(t *testing.T) {
2080820806
}},
2080920807
},
2081020808
errorType: "FieldValueInvalid",
20811-
errorDetail: "link-local multicast",
20809+
errorOrigin: "format=non-special-ip",
2081220810
},
2081320811
"Invalid AppProtocol": {
2081420812
endpoints: core.Endpoints{
@@ -20819,14 +20817,14 @@ func TestValidateEndpointsCreate(t *testing.T) {
2081920817
}},
2082020818
},
2082120819
errorType: "FieldValueInvalid",
20822-
errorDetail: "name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character",
20820+
errorOrigin: "format=qualified-name",
2082320821
},
2082420822
}
2082520823

2082620824
for k, v := range errorCases {
2082720825
t.Run(k, func(t *testing.T) {
20828-
if errs := ValidateEndpointsCreate(&v.endpoints); len(errs) == 0 || errs[0].Type != v.errorType || !strings.Contains(errs[0].Detail, v.errorDetail) {
20829-
t.Errorf("Expected error type %s with detail %q, got %v", v.errorType, v.errorDetail, errs)
20826+
if errs := ValidateEndpointsCreate(&v.endpoints); len(errs) == 0 || errs[0].Type != v.errorType || errs[0].Origin != v.errorOrigin {
20827+
t.Errorf("Expected error type %s with origin %q, got %#v", v.errorType, v.errorOrigin, errs[0])
2083020828
}
2083120829
})
2083220830
}
@@ -21190,7 +21188,7 @@ func TestValidateSchedulingGates(t *testing.T) {
2119021188
for _, tt := range tests {
2119121189
t.Run(tt.name, func(t *testing.T) {
2119221190
errs := validateSchedulingGates(tt.schedulingGates, fieldPath)
21193-
if diff := cmp.Diff(tt.wantFieldErrors, errs); diff != "" {
21191+
if diff := cmp.Diff(tt.wantFieldErrors, errs, cmpopts.IgnoreFields(field.Error{}, "Detail", "Origin")); diff != "" {
2119421192
t.Errorf("unexpected field errors (-want, +got):\n%s", diff)
2119521193
}
2119621194
})

staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func IsValidPortName(port string) []string {
373373
func IsValidIP(fldPath *field.Path, value string) field.ErrorList {
374374
var allErrors field.ErrorList
375375
if netutils.ParseIPSloppy(value) == nil {
376-
allErrors = append(allErrors, field.Invalid(fldPath, value, "must be a valid IP address, (e.g. 10.9.8.7 or 2001:db8::ffff)"))
376+
allErrors = append(allErrors, field.Invalid(fldPath, value, "must be a valid IP address, (e.g. 10.9.8.7 or 2001:db8::ffff)").WithOrigin("format=ip-sloppy"))
377377
}
378378
return allErrors
379379
}

0 commit comments

Comments
 (0)