sigs.k8s.io/gateway-api@v1.0.0/apis/v1beta1/validation/gatewayclass_test.go (about) 1 /* 2 Copyright 2021 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 validation 18 19 import ( 20 "reflect" 21 "testing" 22 23 "k8s.io/apimachinery/pkg/util/validation/field" 24 25 gatewayv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1" 26 ) 27 28 func TestValidateGatewayClassUpdate(t *testing.T) { 29 type args struct { 30 oldClass *gatewayv1b1.GatewayClass 31 newClass *gatewayv1b1.GatewayClass 32 } 33 tests := []struct { 34 name string 35 args args 36 want field.ErrorList 37 }{ 38 { 39 name: "changing parameters reference is allowed", 40 args: args{ 41 oldClass: &gatewayv1b1.GatewayClass{ 42 Spec: gatewayv1b1.GatewayClassSpec{ 43 ControllerName: "foo", 44 }, 45 }, 46 newClass: &gatewayv1b1.GatewayClass{ 47 Spec: gatewayv1b1.GatewayClassSpec{ 48 ControllerName: "foo", 49 ParametersRef: &gatewayv1b1.ParametersReference{ 50 Group: "example.com", 51 Kind: "GatewayClassConfig", 52 Name: "foo", 53 }, 54 }, 55 }, 56 }, 57 want: nil, 58 }, 59 { 60 name: "changing controller field results in an error", 61 args: args{ 62 oldClass: &gatewayv1b1.GatewayClass{ 63 Spec: gatewayv1b1.GatewayClassSpec{ 64 ControllerName: "example.com/gateway", 65 }, 66 }, 67 newClass: &gatewayv1b1.GatewayClass{ 68 Spec: gatewayv1b1.GatewayClassSpec{ 69 ControllerName: "example.org/gateway", 70 }, 71 }, 72 }, 73 want: field.ErrorList{ 74 { 75 Type: field.ErrorTypeInvalid, 76 Field: "spec.controllerName", 77 Detail: "cannot update an immutable field", 78 BadValue: gatewayv1b1.GatewayController("example.org/gateway"), 79 }, 80 }, 81 }, 82 { 83 name: "nil input result in no errors", 84 args: args{ 85 oldClass: nil, 86 newClass: nil, 87 }, 88 want: nil, 89 }, 90 } 91 for _, tc := range tests { 92 t.Run(tc.name, func(t *testing.T) { 93 if got := ValidateGatewayClassUpdate(tc.args.oldClass, tc.args.newClass); !reflect.DeepEqual(got, tc.want) { 94 t.Errorf("ValidateGatewayClassUpdate() = %v, want %v", got, tc.want) 95 } 96 }) 97 } 98 }