adding required and other test cases

This commit is contained in:
darshansreenivas 2025-12-06 11:02:19 -08:00
parent 897adb7b48
commit ce6ea2f4b0
11 changed files with 57 additions and 4 deletions

View file

@ -77,6 +77,10 @@ func Validate_RuntimeClass(ctx context.Context, op operation.Operation, fldPath
errs = append(errs, e...)
earlyReturn = true
}
if e := validate.RequiredValue(ctx, op, fldPath, obj, oldObj); len(e) != 0 {
errs = append(errs, e...)
earlyReturn = true
}
if earlyReturn {
return // do not proceed
}

View file

@ -116,6 +116,10 @@ func Validate_RuntimeClassSpec(ctx context.Context, op operation.Operation, fldP
errs = append(errs, e...)
earlyReturn = true
}
if e := validate.RequiredValue(ctx, op, fldPath, obj, oldObj); len(e) != 0 {
errs = append(errs, e...)
earlyReturn = true
}
if earlyReturn {
return // do not proceed
}

View file

@ -77,6 +77,10 @@ func Validate_RuntimeClass(ctx context.Context, op operation.Operation, fldPath
errs = append(errs, e...)
earlyReturn = true
}
if e := validate.RequiredValue(ctx, op, fldPath, obj, oldObj); len(e) != 0 {
errs = append(errs, e...)
earlyReturn = true
}
if earlyReturn {
return // do not proceed
}

View file

@ -38,9 +38,12 @@ var NodeNormalizationRules = []field.NormalizationRule{
// ValidateRuntimeClass validates the RuntimeClass
func ValidateRuntimeClass(rc *node.RuntimeClass) field.ErrorList {
allErrs := apivalidation.ValidateObjectMeta(&rc.ObjectMeta, false, apivalidation.NameIsDNSSubdomain, field.NewPath("metadata"))
for _, msg := range apivalidation.NameIsDNSLabel(rc.Handler, false) {
allErrs = append(allErrs, field.Invalid(field.NewPath("handler"), rc.Handler, msg)).MarkCoveredByDeclarative().WithOrigin("format=k8s-short-name")
if rc.Handler == "" {
allErrs = append(allErrs, field.Required(field.NewPath("handler"), "")).MarkCoveredByDeclarative()
} else {
for _, msg := range apivalidation.NameIsDNSLabel(rc.Handler, false) {
allErrs = append(allErrs, field.Invalid(field.NewPath("handler"), rc.Handler, msg)).MarkCoveredByDeclarative().WithOrigin("format=k8s-short-name")
}
}
if rc.Overhead != nil {

View file

@ -63,10 +63,36 @@ func TestRuntimeClass_DeclarativeValidate_Create(t *testing.T) {
}),
expectedErrs: field.ErrorList{},
},
"invalid handler dns label": {
"empty handler": {
obj: mkRuntimeClassHandlerOnly(func(rc *node.RuntimeClass) {
rc.Handler = ""
}),
expectedErrs: field.ErrorList{
field.Required(field.NewPath("handler"), "must be a valid DNS label"),
},
},
"handler with special characters": {
obj: mkRuntimeClassHandlerOnly(func(rc *node.RuntimeClass) {
rc.Handler = "asasdasda&^%"
}),
expectedErrs: field.ErrorList{
field.Invalid(field.NewPath("handler"),
"", "").WithOrigin("format=k8s-short-name"),
},
},
"handler with uppercase and special characters": {
obj: mkRuntimeClassHandlerOnly(func(rc *node.RuntimeClass) {
rc.Handler = "asasdasda&^%&^%$UUUUUUU"
}),
expectedErrs: field.ErrorList{
field.Invalid(field.NewPath("handler"),
"", "").WithOrigin("format=k8s-short-name"),
},
},
"handler exceeds length with invalid characters": {
obj: mkRuntimeClassHandlerOnly(func(rc *node.RuntimeClass) {
rc.Handler = "asasdasda&^%&^%$UUUUUUUaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbcccccccccccccccccccccccc"
}),
expectedErrs: field.ErrorList{
field.Invalid(field.NewPath("handler"),
"", "").WithOrigin("format=k8s-short-name"),

View file

@ -61,6 +61,8 @@ message RuntimeClass {
// and is immutable.
// +k8s:format="k8s-short-name"
// +k8s:immutable
// +required
// +k8s:required
optional string handler = 2;
// overhead represents the resource overhead associated with running a pod for a

View file

@ -52,6 +52,8 @@ type RuntimeClass struct {
// and is immutable.
// +k8s:format="k8s-short-name"
// +k8s:immutable
// +required
// +k8s:required
Handler string `json:"handler" protobuf:"bytes,2,opt,name=handler"`
// overhead represents the resource overhead associated with running a pod for a

View file

@ -82,6 +82,8 @@ message RuntimeClassSpec {
// requirements, and is immutable.
// +k8s:format="k8s-short-name"
// +k8s:immutable
// +required
// +k8s:required
optional string runtimeHandler = 1;
// overhead represents the resource overhead associated with running a pod for a

View file

@ -61,6 +61,8 @@ type RuntimeClassSpec struct {
// requirements, and is immutable.
// +k8s:format="k8s-short-name"
// +k8s:immutable
// +required
// +k8s:required
RuntimeHandler string `json:"runtimeHandler" protobuf:"bytes,1,opt,name=runtimeHandler"`
// overhead represents the resource overhead associated with running a pod for a

View file

@ -61,6 +61,8 @@ message RuntimeClass {
// and is immutable.
// +k8s:format="k8s-short-name"
// +k8s:immutable
// +required
// +k8s:required
optional string handler = 2;
// overhead represents the resource overhead associated with running a pod for a

View file

@ -53,6 +53,8 @@ type RuntimeClass struct {
// and is immutable.
// +k8s:format="k8s-short-name"
// +k8s:immutable
// +required
// +k8s:required
Handler string `json:"handler" protobuf:"bytes,2,opt,name=handler"`
// overhead represents the resource overhead associated with running a pod for a