From bde64043ceb304ea51f5c431a65ff7008472a607 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Wed, 1 Jan 2025 14:38:43 -0800 Subject: [PATCH] Fix tests broken in earlier changes --- .../pkg/api/validation/objectmeta_test.go | 87 ++++++++++++------- .../pkg/util/validation/validation.go | 2 +- .../output_tests/format/doc_test.go | 17 +++- .../subfield/nonincluded/doc_test.go | 2 +- .../nonincluded/testdata/validate-false.json | 6 +- .../testdata/validate-false.json | 2 +- 6 files changed, 76 insertions(+), 40 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/api/validation/objectmeta_test.go b/staging/src/k8s.io/apimachinery/pkg/api/validation/objectmeta_test.go index 0331cf9f3b9a5..8eb47c870a35d 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/validation/objectmeta_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/validation/objectmeta_test.go @@ -17,7 +17,6 @@ limitations under the License. package validation import ( - "math/rand" "reflect" "strings" "testing" @@ -55,37 +54,61 @@ func TestValidateObjectMetaCustomName(t *testing.T) { // Ensure namespace names follow dns label format func TestValidateObjectMetaNamespaces(t *testing.T) { - errs := ValidateObjectMeta( - &metav1.ObjectMeta{Name: "test", Namespace: "foo.bar"}, - true, - func(s string, prefix bool) []string { - return nil - }, - field.NewPath("field")) - if len(errs) != 1 { - t.Fatalf("unexpected errors: %v", errs) - } - if !strings.Contains(errs[0].Error(), `Invalid value: "foo.bar"`) { - t.Errorf("unexpected error message: %v", errs) - } - maxLength := 63 - letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") - b := make([]rune, maxLength+1) - for i := range b { - b[i] = letters[rand.Intn(len(letters))] - } - errs = ValidateObjectMeta( - &metav1.ObjectMeta{Name: "test", Namespace: string(b)}, - true, - func(s string, prefix bool) []string { - return nil - }, - field.NewPath("field")) - if len(errs) != 2 { - t.Fatalf("unexpected errors: %v", errs) - } - if !strings.Contains(errs[0].Error(), "Invalid value") || !strings.Contains(errs[1].Error(), "Invalid value") { - t.Errorf("unexpected error message: %v", errs) + cases := []struct { + namespace string + invalid bool + errorContains string + }{{ + namespace: "foo", + }, { + namespace: "foo-bar", + }, { + namespace: "f4r", + }, { + namespace: "1234", + }, { + namespace: strings.Repeat("x", 64), + invalid: true, + errorContains: "must be no more than", + }, { + namespace: "Capital-first-letter", + invalid: true, + errorContains: "must start and end with lower-case alphanumeric", + }, { + namespace: "capital-last-letteR", + invalid: true, + errorContains: "must start and end with lower-case alphanumeric", + }, { + namespace: "capital-INTERIOR-letters", + invalid: true, + errorContains: "must contain only lower-case alphanumeric", + }, { + namespace: "foo.bar", + invalid: true, + errorContains: "must contain only lower-case alphanumeric", + }} + + for _, tc := range cases { + errs := ValidateObjectMeta( + &metav1.ObjectMeta{Name: "test", Namespace: tc.namespace}, + true, + func(s string, prefix bool) []string { + return nil + }, + field.NewPath("field")) + if tc.invalid && len(errs) == 0 { + t.Errorf("unexpected success for %q", tc.namespace) + } else if !tc.invalid && len(errs) != 0 { + t.Errorf("unexpected errors for %q: %v", tc.namespace, errs) + } else if tc.invalid { + if len(errs) != 1 { + t.Errorf("expected 1 error for %q: %v", tc.namespace, errs) + } else { + if !strings.Contains(errs[0].Error(), tc.errorContains) { + t.Errorf("unexpected error for %q: %v", tc.namespace, errs) + } + } + } } } diff --git a/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go b/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go index 77c45ac6362c0..00196e3fdfbdf 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go @@ -359,7 +359,7 @@ func IsValidPortName(port string) []string { // IsValidIP tests that the argument is a valid IP address. func IsValidIP(fldPath *field.Path, value string) field.ErrorList { - return validate.IP(operation.Context{}, fldPath, &value, nil) + return validate.IPSloppy(operation.Context{}, fldPath, &value, nil) } // IsValidIPv4Address tests that the argument is a valid IPv4 address. diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/format/doc_test.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/format/doc_test.go index 431c1252341bd..e4571aaf8d748 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/format/doc_test.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/format/doc_test.go @@ -29,12 +29,25 @@ func Test(t *testing.T) { DNSLabelField: "foo-bar", }).ExpectValid() + st.Value(&T{ + IPField: "abcd::1234", + DNSLabelField: "1234", + }).ExpectValid() + st.Value(&T{ IPField: "", DNSLabelField: "", }).ExpectInvalid( field.Invalid(field.NewPath("ipField"), "", "must be a valid IP address (e.g. 10.9.8.7 or 2001:db8::ffff)"), - field.Invalid(field.NewPath("dnsLabelField"), "", "must start and end with lower-case alphanumeric characters"), - field.Invalid(field.NewPath("dnsLabelField"), "", "must consist of lower-case alphanumeric characters or '-'"), + field.Invalid(field.NewPath("dnsLabelField"), "", "must contain at least 1 character"), + ) + + st.Value(&T{ + IPField: "Not an IP", + DNSLabelField: "Not a DNS label", + }).ExpectInvalid( + field.Invalid(field.NewPath("ipField"), "Not an IP", "must be a valid IP address (e.g. 10.9.8.7 or 2001:db8::ffff)"), + field.Invalid(field.NewPath("dnsLabelField"), "Not a DNS label", "must start and end with lower-case alphanumeric characters"), + field.Invalid(field.NewPath("dnsLabelField"), "Not a DNS label", "must contain only lower-case alphanumeric characters or '-'"), ) } diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/nonincluded/doc_test.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/nonincluded/doc_test.go index ac7016f1186be..414a067d56d2e 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/nonincluded/doc_test.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/nonincluded/doc_test.go @@ -25,5 +25,5 @@ func TestSubfieldObjectMetaValidationWithValidateFalse(t *testing.T) { st.Value(&T1{}). // check for subfield +k8s:validateFalse validation on T1.ObjectMeta.Name - ExpectValidateFalse("subfield T1.ObjectMeta.Name") + ExpectValidateFalse("subfield T1.(other.StructType).StringField") } diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/nonincluded/testdata/validate-false.json b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/nonincluded/testdata/validate-false.json index ba553af2f32d3..1c6f8392db710 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/nonincluded/testdata/validate-false.json +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/nonincluded/testdata/validate-false.json @@ -1,7 +1,7 @@ { - "*subfield.T1": { - "ObjectMeta.name": [ - "subfield T1.ObjectMeta.Name" + "*nonincluded.T1": { + "StructType.stringField": [ + "subfield T1.(other.StructType).StringField" ] } } \ No newline at end of file diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/validatefalse/testdata/validate-false.json b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/validatefalse/testdata/validate-false.json index 7e5a3f28c3427..c8fe323910426 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/validatefalse/testdata/validate-false.json +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/subfield/validatefalse/testdata/validate-false.json @@ -1,5 +1,5 @@ { - "*subfield.T1": { + "*validatefalse.T1": { "": [ "type T1" ],