...
1
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 }
81
View as plain text