...
1
16
17 package validation
18
19 import (
20 "reflect"
21 "testing"
22
23 "k8s.io/apimachinery/pkg/util/validation/field"
24
25 gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
26 )
27
28 func TestValidateGatewayClassUpdate(t *testing.T) {
29 type args struct {
30 oldClass *gatewayv1a2.GatewayClass
31 newClass *gatewayv1a2.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: &gatewayv1a2.GatewayClass{
42 Spec: gatewayv1a2.GatewayClassSpec{
43 ControllerName: "foo",
44 },
45 },
46 newClass: &gatewayv1a2.GatewayClass{
47 Spec: gatewayv1a2.GatewayClassSpec{
48 ControllerName: "foo",
49 ParametersRef: &gatewayv1a2.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: &gatewayv1a2.GatewayClass{
63 Spec: gatewayv1a2.GatewayClassSpec{
64 ControllerName: "example.com/gateway",
65 },
66 },
67 newClass: &gatewayv1a2.GatewayClass{
68 Spec: gatewayv1a2.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: gatewayv1a2.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 }
99
View as plain text