sigs.k8s.io/kueue@v0.6.2/pkg/webhooks/localqueue_webhook_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 webhooks 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 "github.com/google/go-cmp/cmp/cmpopts" 24 "k8s.io/apimachinery/pkg/util/validation/field" 25 26 kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1" 27 testingutil "sigs.k8s.io/kueue/pkg/util/testing" 28 ) 29 30 const ( 31 testLocalQueueName = "test-queue" 32 testLocalQueueNamespace = "test-queue-ns" 33 ) 34 35 func TestValidateLocalQueueCreate(t *testing.T) { 36 testCases := map[string]struct { 37 queue *kueue.LocalQueue 38 wantErr field.ErrorList 39 }{ 40 "should reject queue creation with an invalid clusterQueue": { 41 queue: testingutil.MakeLocalQueue(testLocalQueueName, testLocalQueueNamespace).ClusterQueue("invalid_cluster_queue").Obj(), 42 wantErr: field.ErrorList{ 43 field.Invalid(field.NewPath("spec").Child("clusterQueue"), "invalid_name", ""), 44 }, 45 }, 46 } 47 for name, tc := range testCases { 48 t.Run(name, func(t *testing.T) { 49 errList := ValidateLocalQueue(tc.queue) 50 if diff := cmp.Diff(tc.wantErr, errList, cmpopts.IgnoreFields(field.Error{}, "Detail", "BadValue")); diff != "" { 51 t.Errorf("ValidateLocalQueueCreate() mismatch (-want +got):\n%s", diff) 52 } 53 }) 54 } 55 } 56 57 func TestValidateLocalQueueUpdate(t *testing.T) { 58 testCases := map[string]struct { 59 before, after *kueue.LocalQueue 60 wantErr field.ErrorList 61 }{ 62 "clusterQueue cannot be updated": { 63 before: testingutil.MakeLocalQueue(testLocalQueueName, testLocalQueueNamespace).ClusterQueue("foo").Obj(), 64 after: testingutil.MakeLocalQueue(testLocalQueueName, testLocalQueueNamespace).ClusterQueue("bar").Obj(), 65 wantErr: field.ErrorList{ 66 field.Invalid(field.NewPath("spec").Child("clusterQueue"), nil, ""), 67 }, 68 }, 69 "status could be updated": { 70 before: testingutil.MakeLocalQueue(testLocalQueueName, testLocalQueueNamespace).Obj(), 71 after: testingutil.MakeLocalQueue(testLocalQueueName, testLocalQueueNamespace).PendingWorkloads(10).Obj(), 72 wantErr: field.ErrorList{}, 73 }, 74 } 75 for name, tc := range testCases { 76 t.Run(name, func(t *testing.T) { 77 errList := ValidateLocalQueueUpdate(tc.before, tc.after) 78 if diff := cmp.Diff(tc.wantErr, errList, cmpopts.IgnoreFields(field.Error{}, "Detail", "BadValue")); diff != "" { 79 t.Errorf("ValidateLocalQueueUpdate() mismatch (-want +got):\n%s", diff) 80 } 81 }) 82 } 83 }