1
2
3
4
19
20 package main
21
22 import (
23 "fmt"
24 "testing"
25 "time"
26
27 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
28
29 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30 )
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 func TestHTTPRouteParentRefStandard(t *testing.T) {
48 tests := []struct {
49 name string
50 wantErrors []string
51 parentRefs []gatewayv1.ParentReference
52 }{
53 {
54 name: "invalid because duplicate parent refs without section name",
55 wantErrors: []string{"sectionName must be unique when parentRefs includes 2 or more references to the same parent"},
56 parentRefs: []gatewayv1.ParentReference{{
57 Kind: ptrTo(gatewayv1.Kind("Gateway")),
58 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
59 Name: "example",
60 }, {
61 Kind: ptrTo(gatewayv1.Kind("Gateway")),
62 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
63 Name: "example",
64 }},
65 },
66 {
67 name: "invalid because duplicate parent refs with only one section name",
68 wantErrors: []string{"sectionName must be specified when parentRefs includes 2 or more references to the same parent"},
69 parentRefs: []gatewayv1.ParentReference{{
70 Kind: ptrTo(gatewayv1.Kind("Gateway")),
71 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
72 Name: "example",
73 }, {
74 Kind: ptrTo(gatewayv1.Kind("Gateway")),
75 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
76 Name: "example",
77 SectionName: ptrTo(gatewayv1.SectionName("foo")),
78 }},
79 },
80 {
81 name: "invalid because duplicate parent refs with duplicate section names",
82 wantErrors: []string{"sectionName must be unique when parentRefs includes 2 or more references to the same parent"},
83 parentRefs: []gatewayv1.ParentReference{{
84 Kind: ptrTo(gatewayv1.Kind("Gateway")),
85 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
86 Name: "example",
87 SectionName: ptrTo(gatewayv1.SectionName("foo")),
88 }, {
89 Kind: ptrTo(gatewayv1.Kind("Gateway")),
90 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
91 Name: "example",
92 SectionName: ptrTo(gatewayv1.SectionName("foo")),
93 }},
94 },
95 {
96 name: "valid single parentRef without sectionName",
97 wantErrors: []string{},
98 parentRefs: []gatewayv1.ParentReference{{
99 Kind: ptrTo(gatewayv1.Kind("Gateway")),
100 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
101 Name: "example",
102 }},
103 },
104 {
105 name: "valid single parentRef with sectionName",
106 wantErrors: []string{},
107 parentRefs: []gatewayv1.ParentReference{{
108 Kind: ptrTo(gatewayv1.Kind("Gateway")),
109 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
110 Name: "example",
111 SectionName: ptrTo(gatewayv1.SectionName("foo")),
112 }},
113 },
114 {
115 name: "valid because duplicate parent refs with different section names",
116 wantErrors: []string{},
117 parentRefs: []gatewayv1.ParentReference{{
118 Kind: ptrTo(gatewayv1.Kind("Gateway")),
119 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
120 Name: "example",
121 SectionName: ptrTo(gatewayv1.SectionName("foo")),
122 }, {
123 Kind: ptrTo(gatewayv1.Kind("Gateway")),
124 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
125 Name: "example",
126 SectionName: ptrTo(gatewayv1.SectionName("bar")),
127 }},
128 },
129 {
130 name: "valid because duplicate parent refs with different names",
131 wantErrors: []string{},
132 parentRefs: []gatewayv1.ParentReference{{
133 Kind: ptrTo(gatewayv1.Kind("Gateway")),
134 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
135 Name: "example",
136 SectionName: ptrTo(gatewayv1.SectionName("foo")),
137 }, {
138 Kind: ptrTo(gatewayv1.Kind("Gateway")),
139 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
140 Name: "example2",
141 SectionName: ptrTo(gatewayv1.SectionName("foo")),
142 }},
143 },
144 {
145 name: "valid because first parentRef has namespace while second doesn't",
146 wantErrors: []string{},
147 parentRefs: []gatewayv1.ParentReference{{
148 Kind: ptrTo(gatewayv1.Kind("Gateway")),
149 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
150 Name: "example",
151 Namespace: ptrTo(gatewayv1.Namespace("test")),
152 }, {
153 Kind: ptrTo(gatewayv1.Kind("Gateway")),
154 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
155 Name: "example",
156 }},
157 },
158 {
159 name: "valid because second parentRef has namespace while first doesn't",
160 wantErrors: []string{},
161 parentRefs: []gatewayv1.ParentReference{{
162 Kind: ptrTo(gatewayv1.Kind("Gateway")),
163 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
164 Name: "example",
165 }, {
166 Kind: ptrTo(gatewayv1.Kind("Gateway")),
167 Group: ptrTo(gatewayv1.Group("gateway.networking.k8s.io")),
168 Name: "example",
169 Namespace: ptrTo(gatewayv1.Namespace("test")),
170 }},
171 },
172 }
173
174 for _, tc := range tests {
175 t.Run(tc.name, func(t *testing.T) {
176 route := &gatewayv1.HTTPRoute{
177 ObjectMeta: metav1.ObjectMeta{
178 Name: fmt.Sprintf("foo-%v", time.Now().UnixNano()),
179 Namespace: metav1.NamespaceDefault,
180 },
181 Spec: gatewayv1.HTTPRouteSpec{
182 CommonRouteSpec: gatewayv1.CommonRouteSpec{
183 ParentRefs: tc.parentRefs,
184 },
185 },
186 }
187 validateHTTPRoute(t, route, tc.wantErrors)
188 })
189 }
190 }
191
View as plain text