kubernetes/pkg/apis/resource/structured/namedresources/validation/validation_test.go
Patrick Ohly d4d5ade7f5 dra: add "named resources" structured parameter model
Like the current device plugin interface, a DRA driver using this model
announces a list of resource instances. In contrast to device plugins, this
list is made available to the scheduler together with attributes that can be
used to select suitable instances when they are not all alike.

Because this is the first structured parameter model, some checks that
previously were not possible, in particular "is one structured parameter field
set", now gets enabled. Adding another structured parameter model will be
similar.

The applyconfigs code generator assumes that all types in an API are defined in
a single package. If it wasn't for that, it would be possible to place the
"named resources" types in separate packages, which makes their names in the Go
code more natural and provides an indication of their stability level because
the package name could include a version.
2024-03-07 22:21:16 +01:00

150 lines
6.2 KiB
Go

/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package validation
import (
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/validation/field"
resourceapi "k8s.io/kubernetes/pkg/apis/resource"
"k8s.io/utils/ptr"
)
func testResources(instances []resourceapi.NamedResourcesInstance) *resourceapi.NamedResourcesResources {
resources := &resourceapi.NamedResourcesResources{
Instances: instances,
}
return resources
}
func TestValidateResources(t *testing.T) {
goodName := "foo"
badName := "!@#$%^"
quantity := resource.MustParse("1")
scenarios := map[string]struct {
resources *resourceapi.NamedResourcesResources
wantFailures field.ErrorList
}{
"empty": {
resources: testResources(nil),
},
"good": {
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: goodName}}),
},
"bad-name": {
wantFailures: field.ErrorList{field.Invalid(field.NewPath("instances").Index(0).Child("name"), badName, "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')")},
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: badName}}),
},
"duplicate-name": {
wantFailures: field.ErrorList{field.Duplicate(field.NewPath("instances").Index(1).Child("name"), goodName)},
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: goodName}, {Name: goodName}}),
},
"quantity": {
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: goodName, Attributes: []resourceapi.NamedResourcesAttribute{{Name: goodName, NamedResourcesAttributeValue: resourceapi.NamedResourcesAttributeValue{QuantityValue: &quantity}}}}}),
},
"bool": {
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: goodName, Attributes: []resourceapi.NamedResourcesAttribute{{Name: goodName, NamedResourcesAttributeValue: resourceapi.NamedResourcesAttributeValue{BoolValue: ptr.To(true)}}}}}),
},
"int": {
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: goodName, Attributes: []resourceapi.NamedResourcesAttribute{{Name: goodName, NamedResourcesAttributeValue: resourceapi.NamedResourcesAttributeValue{IntValue: ptr.To(int64(1))}}}}}),
},
"int-slice": {
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: goodName, Attributes: []resourceapi.NamedResourcesAttribute{{Name: goodName, NamedResourcesAttributeValue: resourceapi.NamedResourcesAttributeValue{IntSliceValue: &resourceapi.NamedResourcesIntSlice{Ints: []int64{1, 2, 3}}}}}}}),
},
"string": {
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: goodName, Attributes: []resourceapi.NamedResourcesAttribute{{Name: goodName, NamedResourcesAttributeValue: resourceapi.NamedResourcesAttributeValue{StringValue: ptr.To("hello")}}}}}),
},
"string-slice": {
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: goodName, Attributes: []resourceapi.NamedResourcesAttribute{{Name: goodName, NamedResourcesAttributeValue: resourceapi.NamedResourcesAttributeValue{StringSliceValue: &resourceapi.NamedResourcesStringSlice{Strings: []string{"hello"}}}}}}}),
},
// TODO: semver
"empty-attribute": {
wantFailures: field.ErrorList{field.Required(field.NewPath("instances").Index(0).Child("attributes").Index(0), "exactly one value must be set")},
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: goodName, Attributes: []resourceapi.NamedResourcesAttribute{{Name: goodName}}}}),
},
"duplicate-value": {
wantFailures: field.ErrorList{field.Invalid(field.NewPath("instances").Index(0).Child("attributes").Index(0), []string{"bool", "int"}, "exactly one field must be set, not several")},
resources: testResources([]resourceapi.NamedResourcesInstance{{Name: goodName, Attributes: []resourceapi.NamedResourcesAttribute{{Name: goodName, NamedResourcesAttributeValue: resourceapi.NamedResourcesAttributeValue{BoolValue: ptr.To(true), IntValue: ptr.To(int64(1))}}}}}),
},
}
for name, scenario := range scenarios {
t.Run(name, func(t *testing.T) {
errs := ValidateResources(scenario.resources, nil)
assert.Equal(t, scenario.wantFailures, errs)
})
}
}
func TestValidateSelector(t *testing.T) {
scenarios := map[string]struct {
selector string
wantFailures field.ErrorList
}{
"okay": {
selector: "true",
},
"empty": {
selector: "",
wantFailures: field.ErrorList{field.Required(nil, "")},
},
"undefined": {
selector: "nosuchvar",
wantFailures: field.ErrorList{field.Invalid(nil, "nosuchvar", "compilation failed: ERROR: <input>:1:1: undeclared reference to 'nosuchvar' (in container '')\n | nosuchvar\n | ^")},
},
"wrong-type": {
selector: "1",
wantFailures: field.ErrorList{field.Invalid(nil, "1", "must evaluate to bool")},
},
"quantity": {
selector: `attributes.quantity["name"].isGreaterThan(quantity("0"))`,
},
"bool": {
selector: `attributes.bool["name"]`,
},
"int": {
selector: `attributes.int["name"] > 0`,
},
"intslice": {
selector: `attributes.intslice["name"].isSorted()`,
},
"string": {
selector: `attributes.string["name"] == "fish"`,
},
"stringslice": {
selector: `attributes.stringslice["name"].isSorted()`,
},
// TODO: semver
}
for name, scenario := range scenarios {
t.Run(name, func(t *testing.T) {
// At the moment, there's no difference between stored and new expressions.
// This uses the stricter validation.
opts := Options{
StoredExpressions: false,
}
errs := validateSelector(opts, scenario.selector, nil)
assert.Equal(t, scenario.wantFailures, errs)
})
}
}