-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathroutes_test.go
More file actions
209 lines (182 loc) · 7.41 KB
/
routes_test.go
File metadata and controls
209 lines (182 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package gateway
import (
testutils "github.com/ngrok/ngrok-operator/internal/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)
var _ = Describe("parentRefIsGateway", func() {
DescribeTable("returns true only when the ref resolves to a Gateway",
func(group *gatewayv1.Group, kind *gatewayv1.Kind, want bool) {
ref := gatewayv1.ParentReference{
Name: "some-gateway",
Group: group,
Kind: kind,
}
Expect(parentRefIsGateway(ref)).To(Equal(want))
},
Entry("nil Group and nil Kind defaults to Gateway", nil, nil, true),
Entry("explicit gateway API group and Gateway kind", ptr.To(gatewayv1.Group(gatewayv1.GroupName)), ptr.To(gatewayv1.Kind("Gateway")), true),
Entry("explicit gateway API group and nil Kind", ptr.To(gatewayv1.Group(gatewayv1.GroupName)), nil, true),
Entry("nil Group and explicit Gateway kind", nil, ptr.To(gatewayv1.Kind("Gateway")), true),
Entry("Service kind returns false", nil, ptr.To(gatewayv1.Kind("Service")), false),
Entry("core group returns false", ptr.To(gatewayv1.Group("")), ptr.To(gatewayv1.Kind("Gateway")), false),
Entry("unknown group returns false", ptr.To(gatewayv1.Group("some.other.io")), nil, false),
)
})
var _ = Describe("routeReferencesNgrokGateway", Ordered, func() {
var (
managedGatewayClass *gatewayv1.GatewayClass
unmanagedGatewayClass *gatewayv1.GatewayClass
)
BeforeAll(func(ctx SpecContext) {
By("creating a managed GatewayClass and waiting for acceptance")
managedGatewayClass = testutils.NewGatewayClass(true)
CreateGatewayClassAndWaitForAcceptance(ctx, managedGatewayClass, testutils.DefaultTimeout, testutils.DefaultInterval)
By("creating an unmanaged GatewayClass")
unmanagedGatewayClass = testutils.NewGatewayClass(false)
Expect(k8sClient.Create(ctx, unmanagedGatewayClass)).To(Succeed())
})
AfterAll(func(ctx SpecContext) {
DeleteAllGatewayClasses(ctx, testutils.DefaultTimeout, testutils.DefaultInterval)
})
When("parentRefs is empty", func() {
It("returns false with no error", func(ctx SpecContext) {
ok, err := routeReferencesNgrokGateway(ctx, k8sClient, "default", nil)
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeFalse())
})
})
When("the parentRef is not a Gateway kind", func() {
It("returns false with no error", func(ctx SpecContext) {
refs := []gatewayv1.ParentReference{
{
Group: ptr.To(gatewayv1.Group("")),
Kind: ptr.To(gatewayv1.Kind("Service")),
Name: "some-service",
},
}
ok, err := routeReferencesNgrokGateway(ctx, k8sClient, "default", refs)
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeFalse())
})
})
When("the parentRef points to a Gateway that does not exist", func() {
It("returns false with no error (cannot confirm ownership)", func(ctx SpecContext) {
refs := []gatewayv1.ParentReference{
{Name: "nonexistent-gateway"},
}
ok, err := routeReferencesNgrokGateway(ctx, k8sClient, "default", refs)
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeFalse())
})
})
When("the parentRef points to a Gateway with a managed GatewayClass", func() {
var gw *gatewayv1.Gateway
BeforeEach(func(ctx SpecContext) {
gw = newGateway(managedGatewayClass)
CreateGatewayAndWaitForAcceptance(ctx, gw, testutils.DefaultTimeout, testutils.DefaultInterval)
})
AfterEach(func(ctx SpecContext) {
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, gw))).To(Succeed())
})
It("returns true when namespace is inferred from the route", func(ctx SpecContext) {
refs := []gatewayv1.ParentReference{
{Name: gatewayv1.ObjectName(gw.Name)},
}
ok, err := routeReferencesNgrokGateway(ctx, k8sClient, gw.Namespace, refs)
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeTrue())
})
It("returns true when the namespace is specified explicitly on the parentRef", func(ctx SpecContext) {
refs := []gatewayv1.ParentReference{
{
Name: gatewayv1.ObjectName(gw.Name),
Namespace: ptr.To(gatewayv1.Namespace(gw.Namespace)),
},
}
// Pass a different default namespace to confirm the explicit ref namespace takes precedence.
ok, err := routeReferencesNgrokGateway(ctx, k8sClient, "other-namespace", refs)
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeTrue())
})
})
When("the parentRef points to a Gateway with an unmanaged GatewayClass", func() {
var gw *gatewayv1.Gateway
BeforeEach(func(ctx SpecContext) {
gw = &gatewayv1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Name: testutils.RandomName("gw"),
Namespace: "default",
},
Spec: gatewayv1.GatewaySpec{
GatewayClassName: gatewayv1.ObjectName(unmanagedGatewayClass.Name),
Listeners: []gatewayv1.Listener{
{Name: "http", Port: 80, Protocol: gatewayv1.HTTPProtocolType},
},
},
}
Expect(k8sClient.Create(ctx, gw)).To(Succeed())
})
AfterEach(func(ctx SpecContext) {
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, gw))).To(Succeed())
})
It("returns false", func(ctx SpecContext) {
refs := []gatewayv1.ParentReference{
{Name: gatewayv1.ObjectName(gw.Name)},
}
ok, err := routeReferencesNgrokGateway(ctx, k8sClient, gw.Namespace, refs)
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeFalse())
})
})
When("multiple parentRefs include one that references an ngrok Gateway", func() {
var managedGw *gatewayv1.Gateway
BeforeEach(func(ctx SpecContext) {
managedGw = newGateway(managedGatewayClass)
CreateGatewayAndWaitForAcceptance(ctx, managedGw, testutils.DefaultTimeout, testutils.DefaultInterval)
})
AfterEach(func(ctx SpecContext) {
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, managedGw))).To(Succeed())
})
It("returns true", func(ctx SpecContext) {
refs := []gatewayv1.ParentReference{
{Name: "nonexistent-gateway"},
{Name: gatewayv1.ObjectName(managedGw.Name)},
}
ok, err := routeReferencesNgrokGateway(ctx, k8sClient, managedGw.Namespace, refs)
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeTrue())
})
})
When("the parentRef GatewayClass does not exist", func() {
var gw *gatewayv1.Gateway
BeforeEach(func(ctx SpecContext) {
// Create a Gateway referencing a GatewayClass that we'll use the kginkgo helper to verify is accepted first,
// then manually verify the GatewayClass was accepted to demonstrate the helper.
gw = newGateway(managedGatewayClass)
CreateGatewayAndWaitForAcceptance(ctx, gw, testutils.DefaultTimeout, testutils.DefaultInterval)
// Use the kginkgo EventuallyWithGatewayClass helper to confirm the GatewayClass is accepted.
kginkgo.EventuallyWithGatewayClass(ctx, managedGatewayClass, func(g Gomega, fetched *gatewayv1.GatewayClass) {
cond := meta.FindStatusCondition(fetched.Status.Conditions, string(gatewayv1.GatewayClassConditionStatusAccepted))
g.Expect(cond).NotTo(BeNil())
g.Expect(cond.Status).To(Equal(metav1.ConditionTrue))
})
})
AfterEach(func(ctx SpecContext) {
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, gw))).To(Succeed())
})
It("returns true when the Gateway's GatewayClass is managed and accepted", func(ctx SpecContext) {
refs := []gatewayv1.ParentReference{
{Name: gatewayv1.ObjectName(gw.Name)},
}
ok, err := routeReferencesNgrokGateway(ctx, k8sClient, gw.Namespace, refs)
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeTrue())
})
})
})