Skip to content

Commit

Permalink
Merge pull request #87 from thockin/validation-gen_broken_tests
Browse files Browse the repository at this point in the history
Fix tests broken in earlier changes
  • Loading branch information
yongruilin authored Jan 6, 2025
2 parents 130d0dc + bde6404 commit 482b49c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package validation

import (
"math/rand"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -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)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 '-'"),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"*subfield.T1": {
"ObjectMeta.name": [
"subfield T1.ObjectMeta.Name"
"*nonincluded.T1": {
"StructType.stringField": [
"subfield T1.(other.StructType).StringField"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"*subfield.T1": {
"*validatefalse.T1": {
"": [
"type T1"
],
Expand Down

0 comments on commit 482b49c

Please sign in to comment.