sigs.k8s.io/gateway-api@v1.0.0/pkg/test/cel/gatewayclass_test.go (about) 1 /* 2 Copyright 2023 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 main 18 19 import ( 20 "context" 21 "fmt" 22 "strings" 23 "testing" 24 "time" 25 26 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" 27 28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 ) 30 31 func TestValidateGatewayClassUpdate(t *testing.T) { 32 ctx := context.Background() 33 baseGatewayClass := gatewayv1.GatewayClass{ 34 ObjectMeta: metav1.ObjectMeta{ 35 Name: "foo", 36 }, 37 Spec: gatewayv1.GatewayClassSpec{ 38 ControllerName: "example.net/gateway-controller", 39 }, 40 } 41 42 testCases := []struct { 43 desc string 44 creationMutate func(gw *gatewayv1.GatewayClass) 45 updationMutate func(gw *gatewayv1.GatewayClass) 46 wantError string 47 }{ 48 { 49 desc: "cannot upgrade controllerName", 50 creationMutate: func(gwc *gatewayv1.GatewayClass) { 51 gwc.Spec.ControllerName = "example.net/gateway-controller-1" 52 }, 53 updationMutate: func(gwc *gatewayv1.GatewayClass) { 54 gwc.Spec.ControllerName = "example.net/gateway-controller-2" 55 }, 56 wantError: "Value is immutable", 57 }, 58 } 59 60 for _, tc := range testCases { 61 t.Run(tc.desc, func(t *testing.T) { 62 gwc := baseGatewayClass.DeepCopy() 63 gwc.Name = fmt.Sprintf("foo-%v", time.Now().UnixNano()) 64 65 tc.creationMutate(gwc) 66 if err := k8sClient.Create(ctx, gwc); err != nil { 67 t.Fatalf("Failed to create GatewayClass: %v", err) 68 } 69 tc.updationMutate(gwc) 70 err := k8sClient.Update(ctx, gwc) 71 72 if (tc.wantError != "") != (err != nil) { 73 t.Fatalf("Unexpected error while updating GatewayClass; got err=\n%v\n;want error=%v", err, tc.wantError != "") 74 } 75 if tc.wantError != "" && !strings.Contains(strings.ToLower(err.Error()), strings.ToLower(tc.wantError)) { 76 t.Fatalf("Unexpected error while updating GatewayClass; got err=\n%v\n;want substring within error=%q", err, tc.wantError) 77 } 78 }) 79 } 80 }