k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/resource/resourceslice/strategy_test.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package resourceslice 18 19 import ( 20 "testing" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 genericapirequest "k8s.io/apiserver/pkg/endpoints/request" 24 "k8s.io/kubernetes/pkg/apis/resource" 25 ) 26 27 var slice = &resource.ResourceSlice{ 28 ObjectMeta: metav1.ObjectMeta{ 29 Name: "valid-class", 30 }, 31 NodeName: "valid-node-name", 32 DriverName: "testdriver.example.com", 33 ResourceModel: resource.ResourceModel{ 34 NamedResources: &resource.NamedResourcesResources{}, 35 }, 36 } 37 38 func TestClassStrategy(t *testing.T) { 39 if Strategy.NamespaceScoped() { 40 t.Errorf("ResourceSlice must not be namespace scoped") 41 } 42 if Strategy.AllowCreateOnUpdate() { 43 t.Errorf("ResourceSlice should not allow create on update") 44 } 45 } 46 47 func TestClassStrategyCreate(t *testing.T) { 48 ctx := genericapirequest.NewDefaultContext() 49 slice := slice.DeepCopy() 50 51 Strategy.PrepareForCreate(ctx, slice) 52 errs := Strategy.Validate(ctx, slice) 53 if len(errs) != 0 { 54 t.Errorf("unexpected error validating for create %v", errs) 55 } 56 } 57 58 func TestClassStrategyUpdate(t *testing.T) { 59 t.Run("no-changes-okay", func(t *testing.T) { 60 ctx := genericapirequest.NewDefaultContext() 61 slice := slice.DeepCopy() 62 newClass := slice.DeepCopy() 63 newClass.ResourceVersion = "4" 64 65 Strategy.PrepareForUpdate(ctx, newClass, slice) 66 errs := Strategy.ValidateUpdate(ctx, newClass, slice) 67 if len(errs) != 0 { 68 t.Errorf("unexpected validation errors: %v", errs) 69 } 70 }) 71 72 t.Run("name-change-not-allowed", func(t *testing.T) { 73 ctx := genericapirequest.NewDefaultContext() 74 slice := slice.DeepCopy() 75 newClass := slice.DeepCopy() 76 newClass.Name = "valid-class-2" 77 newClass.ResourceVersion = "4" 78 79 Strategy.PrepareForUpdate(ctx, newClass, slice) 80 errs := Strategy.ValidateUpdate(ctx, newClass, slice) 81 if len(errs) == 0 { 82 t.Errorf("expected a validation error") 83 } 84 }) 85 }