123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- /*
- Copyright 2018 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"
- "k8s.io/apimachinery/pkg/api/resource"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- utilfeature "k8s.io/apiserver/pkg/util/feature"
- featuregatetesting "k8s.io/component-base/featuregate/testing"
- "k8s.io/kubernetes/pkg/apis/core"
- "k8s.io/kubernetes/pkg/apis/node"
- "k8s.io/kubernetes/pkg/features"
- utilpointer "k8s.io/utils/pointer"
- "github.com/stretchr/testify/assert"
- )
- func TestValidateRuntimeClass(t *testing.T) {
- tests := []struct {
- name string
- rc node.RuntimeClass
- expectError bool
- }{{
- name: "invalid name",
- expectError: true,
- rc: node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{Name: "&!@#"},
- Handler: "foo",
- },
- }, {
- name: "invalid Handler name",
- expectError: true,
- rc: node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Handler: "&@#$",
- },
- }, {
- name: "invalid empty RuntimeClass",
- expectError: true,
- rc: node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{Name: "empty"},
- },
- }, {
- name: "valid Handler",
- expectError: false,
- rc: node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Handler: "bar-baz",
- },
- }}
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- errs := ValidateRuntimeClass(&test.rc)
- if test.expectError {
- assert.NotEmpty(t, errs)
- } else {
- assert.Empty(t, errs)
- }
- })
- }
- }
- func TestValidateRuntimeUpdate(t *testing.T) {
- old := node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Handler: "bar",
- }
- tests := []struct {
- name string
- expectError bool
- old, new node.RuntimeClass
- }{{
- name: "valid metadata update",
- old: old,
- new: node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar"},
- },
- Handler: "bar",
- },
- }, {
- name: "invalid metadata update",
- expectError: true,
- old: old,
- new: node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "empty",
- ClusterName: "somethingelse", // immutable
- },
- Handler: "bar",
- },
- }, {
- name: "invalid Handler update",
- expectError: true,
- old: old,
- new: node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Handler: "somethingelse",
- },
- }}
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- // So we don't need to write it in every test case...
- test.old.ObjectMeta.ResourceVersion = "1"
- test.new.ObjectMeta.ResourceVersion = "1"
- errs := ValidateRuntimeClassUpdate(&test.new, &test.old)
- if test.expectError {
- assert.NotEmpty(t, errs)
- } else {
- assert.Empty(t, errs)
- }
- })
- }
- }
- func TestValidateOverhead(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodOverhead, true)()
- successCase := []struct {
- Name string
- overhead *node.Overhead
- }{
- {
- Name: "Overhead with valid cpu and memory resources",
- overhead: &node.Overhead{
- PodFixed: core.ResourceList{
- core.ResourceName(core.ResourceCPU): resource.MustParse("10"),
- core.ResourceName(core.ResourceMemory): resource.MustParse("10G"),
- },
- },
- },
- }
- for _, tc := range successCase {
- rc := &node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Handler: "bar",
- Overhead: tc.overhead,
- }
- if errs := ValidateRuntimeClass(rc); len(errs) != 0 {
- t.Errorf("%q unexpected error: %v", tc.Name, errs)
- }
- }
- errorCase := []struct {
- Name string
- overhead *node.Overhead
- }{
- {
- Name: "Invalid Resources",
- overhead: &node.Overhead{
- PodFixed: core.ResourceList{
- core.ResourceName("my.org"): resource.MustParse("10m"),
- },
- },
- },
- }
- for _, tc := range errorCase {
- rc := &node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Handler: "bar",
- Overhead: tc.overhead,
- }
- if errs := ValidateRuntimeClass(rc); len(errs) == 0 {
- t.Errorf("%q expected error", tc.Name)
- }
- }
- }
- func TestValidateScheduling(t *testing.T) {
- tests := []struct {
- name string
- scheduling *node.Scheduling
- expectErrs int
- }{{
- name: "valid scheduling",
- scheduling: &node.Scheduling{
- NodeSelector: map[string]string{"valid": "yes"},
- Tolerations: []core.Toleration{{
- Key: "valid",
- Operator: core.TolerationOpExists,
- Effect: core.TaintEffectNoSchedule,
- }},
- },
- }, {
- name: "empty scheduling",
- scheduling: &node.Scheduling{},
- }, {
- name: "invalid nodeSelector",
- scheduling: &node.Scheduling{
- NodeSelector: map[string]string{"not a valid key!!!": "nope"},
- },
- expectErrs: 1,
- }, {
- name: "invalid toleration",
- scheduling: &node.Scheduling{
- Tolerations: []core.Toleration{{
- Key: "valid",
- Operator: core.TolerationOpExists,
- Effect: core.TaintEffectNoSchedule,
- }, {
- Key: "not a valid key!!!",
- Operator: core.TolerationOpExists,
- Effect: core.TaintEffectNoSchedule,
- }},
- },
- expectErrs: 1,
- }, {
- name: "duplicate tolerations",
- scheduling: &node.Scheduling{
- Tolerations: []core.Toleration{{
- Key: "valid",
- Operator: core.TolerationOpExists,
- Effect: core.TaintEffectNoExecute,
- TolerationSeconds: utilpointer.Int64Ptr(5),
- }, {
- Key: "valid",
- Operator: core.TolerationOpExists,
- Effect: core.TaintEffectNoExecute,
- TolerationSeconds: utilpointer.Int64Ptr(10),
- }},
- },
- expectErrs: 1,
- }, {
- name: "invalid scheduling",
- scheduling: &node.Scheduling{
- NodeSelector: map[string]string{"not a valid key!!!": "nope"},
- Tolerations: []core.Toleration{{
- Key: "valid",
- Operator: core.TolerationOpExists,
- Effect: core.TaintEffectNoSchedule,
- }, {
- Key: "not a valid toleration key!!!",
- Operator: core.TolerationOpExists,
- Effect: core.TaintEffectNoSchedule,
- }},
- },
- expectErrs: 2,
- }}
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- rc := &node.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Handler: "bar",
- Scheduling: test.scheduling,
- }
- assert.Len(t, ValidateRuntimeClass(rc), test.expectErrs)
- })
- }
- }
|