-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathdevworkspacerouting_controller_test.go
More file actions
499 lines (439 loc) · 27.3 KB
/
devworkspacerouting_controller_test.go
File metadata and controls
499 lines (439 loc) · 27.3 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright (c) 2019-2025 Red Hat, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package devworkspacerouting_test
import (
"fmt"
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
"github.com/devfile/devworkspace-operator/pkg/common"
"github.com/devfile/devworkspace-operator/pkg/config"
"github.com/devfile/devworkspace-operator/pkg/constants"
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
routeV1 "github.com/openshift/api/route/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/intstr"
)
var _ = Describe("DevWorkspaceRouting Controller", func() {
Context("Basic DevWorkspaceRouting Tests", func() {
It("Gets Ready Status on OpenShift", func() {
By("Setting infrastructure to OpenShift")
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
By("Creating a new DevWorkspaceRouting object")
dwrNamespacedName := namespacedName(devWorkspaceRoutingName, testNamespace)
createdDWR := createDWR(testWorkspaceID, devWorkspaceRoutingName)
Eventually(func() bool {
err := k8sClient.Get(ctx, dwrNamespacedName, createdDWR)
return err == nil
}, timeout, interval).Should(BeTrue(), "DevWorkspaceRouting should exist in cluster")
By("Checking DevWorkspaceRouting Status is updated to Ready")
Eventually(func() (phase controllerv1alpha1.DevWorkspaceRoutingPhase, err error) {
if err := k8sClient.Get(ctx, dwrNamespacedName, createdDWR); err != nil {
return "", err
}
return createdDWR.Status.Phase, nil
}, timeout, interval).Should(Equal(controllerv1alpha1.RoutingReady), "DevWorkspaceRouting should have Ready phase")
Expect(createdDWR.Status.Message).ShouldNot(BeNil(), "Status message should be set for preparing DevWorkspaceRoutings")
Expect(createdDWR.Status.Message).Should(Equal("DevWorkspaceRouting prepared"), "Status message should indicate that the DevWorkspaceRouting is prepared")
deleteDevWorkspaceRouting(devWorkspaceRoutingName)
deleteService(common.ServiceName(testWorkspaceID), testNamespace)
deleteService(common.EndpointName(discoverableEndpointName), testNamespace)
deleteRoute(exposedEndPointName, testNamespace)
deleteRoute(discoverableEndpointName, testNamespace)
})
It("Gets Ready Status on Kubernetes", func() {
By("Setting infrastructure to Kubernetes")
infrastructure.InitializeForTesting(infrastructure.Kubernetes)
By("Creating a new DevWorkspaceRouting object")
dwrNamespacedName := namespacedName(devWorkspaceRoutingName, testNamespace)
createdDWR := createDWR(testWorkspaceID, devWorkspaceRoutingName)
Eventually(func() bool {
err := k8sClient.Get(ctx, dwrNamespacedName, createdDWR)
return err == nil
}, timeout, interval).Should(BeTrue(), "DevWorkspaceRouting should exist in cluster")
By("Checking DevWorkspaceRouting Status is updated to Ready")
Eventually(func() (phase controllerv1alpha1.DevWorkspaceRoutingPhase, err error) {
if err := k8sClient.Get(ctx, dwrNamespacedName, createdDWR); err != nil {
return "", err
}
return createdDWR.Status.Phase, nil
}, timeout, interval).Should(Equal(controllerv1alpha1.RoutingReady), "DevWorkspaceRouting should have Ready phase")
Expect(createdDWR.Status.Message).ShouldNot(BeNil(), "Status message should be set for preparing DevWorkspaceRoutings")
Expect(createdDWR.Status.Message).Should(Equal("DevWorkspaceRouting prepared"), "Status message should indicate that the DevWorkspaceRouting is prepared")
deleteDevWorkspaceRouting(devWorkspaceRoutingName)
deleteService(common.ServiceName(testWorkspaceID), testNamespace)
deleteService(common.EndpointName(discoverableEndpointName), testNamespace)
deleteIngress(exposedEndPointName, testNamespace)
deleteIngress(discoverableEndpointName, testNamespace)
})
Context("Kubernetes - DevWorkspaceRouting Objects creation", func() {
BeforeEach(func() {
infrastructure.InitializeForTesting(infrastructure.Kubernetes)
createDWR(testWorkspaceID, devWorkspaceRoutingName)
})
AfterEach(func() {
deleteDevWorkspaceRouting(devWorkspaceRoutingName)
deleteService(common.ServiceName(testWorkspaceID), testNamespace)
deleteService(common.EndpointName(discoverableEndpointName), testNamespace)
deleteIngress(exposedEndPointName, testNamespace)
deleteIngress(discoverableEndpointName, testNamespace)
})
It("Creates services", func() {
createdDWR := getExistingDevWorkspaceRouting(devWorkspaceRoutingName)
By("Checking single service is created for all exposed endpoints ")
createdService := &corev1.Service{}
serviceNamespacedName := namespacedName(common.ServiceName(testWorkspaceID), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, serviceNamespacedName, createdService)
return err == nil
}, timeout, interval).Should(BeTrue(), "Service should exist in cluster")
Expect(createdService.ObjectMeta.Annotations).Should(HaveKeyWithValue(serviceAnnotationKey, serviceAnnotationValue), "Service should have annotation")
Expect(createdService.Spec.Selector).Should(Equal(createdDWR.Spec.PodSelector), "Service should have pod selector from DevWorkspace metadata")
Expect(createdService.Labels).Should(Equal(ExpectedLabels), "Service should contain DevWorkspace ID label")
expectedOwnerReference := devWorkspaceRoutingOwnerRef(createdDWR)
Expect(createdService.OwnerReferences).Should(ContainElement(expectedOwnerReference), "Service should be owned by DevWorkspaceRouting")
By("Checking service has expected ports")
var expectedServicePorts []corev1.ServicePort
expectedServicePorts = append(expectedServicePorts, corev1.ServicePort{
Name: common.EndpointName(exposedEndPointName),
Protocol: corev1.ProtocolTCP,
Port: int32(exposedTargetPort),
TargetPort: intstr.FromInt(exposedTargetPort),
})
expectedServicePorts = append(expectedServicePorts, corev1.ServicePort{
Name: common.EndpointName(discoverableEndpointName),
Protocol: corev1.ProtocolTCP,
Port: int32(discoverableTargetPort),
TargetPort: intstr.FromInt(discoverableTargetPort),
})
Expect(len(createdService.Spec.Ports)).Should(Equal(2), fmt.Sprintf("Only two ports should be exposed: %s and %s. The remaining endpoint in the DevWorkspaceRouting spec has None exposure.", exposedEndPointName, discoverableEndpointName))
Expect(createdService.Spec.Ports).Should(Equal(expectedServicePorts), "Service should contain expected ports")
By("Checking service is created for discoverable endpoint")
discoverableEndpointService := &corev1.Service{}
discoverableEndpointServiceNamespacedName := namespacedName(common.EndpointName(discoverableEndpointName), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, discoverableEndpointServiceNamespacedName, discoverableEndpointService)
return err == nil
}, timeout, interval).Should(BeTrue(), "Service for discoverable endpoint should exist in cluster")
Expect(len(discoverableEndpointService.Spec.Ports)).Should(Equal(1), "Service for discoverable endpoint should only have a single port")
Expect(discoverableEndpointService.Spec.Ports[0].Port).Should(Equal(int32(discoverableTargetPort)))
Expect(discoverableEndpointService.Spec.Ports[0].TargetPort).Should(Equal(intstr.FromInt(discoverableTargetPort)))
Expect(discoverableEndpointService.ObjectMeta.Annotations).Should(HaveKeyWithValue(constants.DevWorkspaceDiscoverableServiceAnnotation, "true"), "Service type should have discoverable service annotation")
By("Checking service is not created for non-exposed endpoint")
nonExposedService := &corev1.Service{}
nonExposedServiceNamespacedName := namespacedName(common.RouteName(testWorkspaceID, nonExposedEndpointName), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, nonExposedServiceNamespacedName, nonExposedService)
return k8sErrors.IsNotFound(err)
}, timeout, interval).Should(BeTrue(), "Route for non-exposed endpoint should not exist in cluster")
})
It("Creates ingress", func() {
createdDWR := getExistingDevWorkspaceRouting(devWorkspaceRoutingName)
By("Checking ingress is created")
createdIngress := networkingv1.Ingress{}
ingressNamespacedName := namespacedName(common.RouteName(testWorkspaceID, exposedEndPointName), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, ingressNamespacedName, &createdIngress)
return err == nil
}, timeout, interval).Should(BeTrue(), "Ingress should exist in cluster")
Expect(createdIngress.Labels).Should(Equal(ExpectedLabels), "Ingress should contain DevWorkspace ID label")
expectedOwnerReference := devWorkspaceRoutingOwnerRef(createdDWR)
Expect(createdIngress.OwnerReferences).Should(ContainElement(expectedOwnerReference), "Ingress should be owned by DevWorkspaceRouting")
Expect(createdIngress.ObjectMeta.Annotations).Should(HaveKeyWithValue(constants.DevWorkspaceEndpointNameAnnotation, exposedEndPointName), "Ingress should have endpoint name annotation")
Expect(createdIngress.ObjectMeta.Annotations).Should(HaveKeyWithValue(endpointAnnotationKey, endpointAnnotationValue), "Ingress should have annotation from endpoint")
By("Checking ingress points to service")
createdService := &corev1.Service{}
serviceNamespacedName := namespacedName(common.ServiceName(testWorkspaceID), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, serviceNamespacedName, createdService)
return err == nil
}, timeout, interval).Should(BeTrue(), "Service should exist in cluster")
var targetPorts []intstr.IntOrString
var ports []int32
for _, servicePort := range createdService.Spec.Ports {
targetPorts = append(targetPorts, servicePort.TargetPort)
ports = append(ports, servicePort.Port)
}
Expect(len(createdIngress.Spec.Rules)).Should(Equal(1), "Expected only a single rule for the ingress")
ingressRule := createdIngress.Spec.Rules[0]
Expect(ingressRule.HTTP.Paths[0].Backend.Service.Name).Should(Equal(createdService.Name), "Ingress backend service name should be service name")
Expect(ports).Should(ContainElement(ingressRule.HTTP.Paths[0].Backend.Service.Port.Number), "Ingress backend service port should be in service ports")
Expect(targetPorts).Should(ContainElement(intstr.FromInt(int(ingressRule.HTTP.Paths[0].Backend.Service.Port.Number))), "Ingress backend service port should be service target ports")
By("Checking ingress is created for discoverable endpoint")
discoverableEndpointIngress := networkingv1.Ingress{}
discoverableEndpointIngressNN := namespacedName(common.RouteName(testWorkspaceID, discoverableEndpointName), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, discoverableEndpointIngressNN, &discoverableEndpointIngress)
return err == nil
}, timeout, interval).Should(BeTrue(), "Ingress should exist in cluster")
Expect(discoverableEndpointIngress.Labels).Should(Equal(ExpectedLabels), "Ingress should contain DevWorkspace ID label")
Expect(discoverableEndpointIngress.OwnerReferences).Should(ContainElement(expectedOwnerReference), "Ingress should be owned by DevWorkspaceRouting")
Expect(discoverableEndpointIngress.ObjectMeta.Annotations).Should(HaveKeyWithValue(constants.DevWorkspaceEndpointNameAnnotation, discoverableEndpointName), "Ingress should have endpoint name annotation")
Expect(discoverableEndpointIngress.ObjectMeta.Annotations).Should(HaveKeyWithValue(endpointAnnotationKey, endpointAnnotationValue), "Ingress should have annotation from endpoint")
By("Checking ingress for discoverable endpoint points to service")
Expect(len(discoverableEndpointIngress.Spec.Rules)).Should(Equal(1), "Expected only a single rule for the ingress")
ingressRule = createdIngress.Spec.Rules[0]
Expect(ingressRule.HTTP.Paths[0].Backend.Service.Name).Should(Equal(createdService.Name), "Ingress backend service name should be service name")
Expect(ports).Should(ContainElement(ingressRule.HTTP.Paths[0].Backend.Service.Port.Number), "Ingress backend service port should be in service ports")
Expect(targetPorts).Should(ContainElement(intstr.FromInt(int(ingressRule.HTTP.Paths[0].Backend.Service.Port.Number))), "Ingress backend service port should be service target ports")
By("Checking ingress is not created for non-exposed endpoint")
nonExposedIngress := networkingv1.Ingress{}
nonExposedIngressNamespacedName := namespacedName(common.RouteName(testWorkspaceID, nonExposedEndpointName), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, nonExposedIngressNamespacedName, &nonExposedIngress)
return k8sErrors.IsNotFound(err)
}, timeout, interval).Should(BeTrue(), "Ingress for non-exposed endpoint should not exist in cluster")
})
})
Context("OpenShift - DevWorkspaceRouting Objects creation", func() {
BeforeEach(func() {
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
createDWR(testWorkspaceID, devWorkspaceRoutingName)
})
AfterEach(func() {
deleteDevWorkspaceRouting(devWorkspaceRoutingName)
deleteService(common.ServiceName(testWorkspaceID), testNamespace)
deleteService(common.EndpointName(discoverableEndpointName), testNamespace)
deleteRoute(exposedEndPointName, testNamespace)
deleteRoute(discoverableEndpointName, testNamespace)
})
It("Creates services", func() {
createdDWR := getExistingDevWorkspaceRouting(devWorkspaceRoutingName)
By("Checking single service for all exposed endpoints is created")
createdService := &corev1.Service{}
serviceNamespacedName := namespacedName(common.ServiceName(testWorkspaceID), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, serviceNamespacedName, createdService)
return err == nil
}, timeout, interval).Should(BeTrue(), "Service should exist in cluster")
Expect(createdService.Spec.Selector).Should(Equal(createdDWR.Spec.PodSelector), "Service should have pod selector from DevWorkspaceRouting")
Expect(createdService.Labels).Should(Equal(ExpectedLabels), "Service should contain DevWorkspace ID label")
expectedOwnerReference := devWorkspaceRoutingOwnerRef(createdDWR)
Expect(createdService.OwnerReferences).Should(ContainElement(expectedOwnerReference), "Service should be owned by DevWorkspaceRouting")
By("Checking service has expected ports")
var expectedServicePorts []corev1.ServicePort
expectedServicePorts = append(expectedServicePorts, corev1.ServicePort{
Name: common.EndpointName(exposedEndPointName),
Protocol: corev1.ProtocolTCP,
Port: int32(exposedTargetPort),
TargetPort: intstr.FromInt(exposedTargetPort),
})
expectedServicePorts = append(expectedServicePorts, corev1.ServicePort{
Name: common.EndpointName(discoverableEndpointName),
Protocol: corev1.ProtocolTCP,
Port: int32(discoverableTargetPort),
TargetPort: intstr.FromInt(discoverableTargetPort),
})
Expect(createdService.Spec.Ports).Should(Equal(expectedServicePorts), "Service should contain expected ports")
By("Checking service is created for discoverable endpoint")
discoverableEndpointService := &corev1.Service{}
discoverableEndpointServiceNamespacedName := namespacedName(common.EndpointName(discoverableEndpointName), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, discoverableEndpointServiceNamespacedName, discoverableEndpointService)
return err == nil
}, timeout, interval).Should(BeTrue(), "Service for discoverable endpoint should exist in cluster")
Expect(len(discoverableEndpointService.Spec.Ports)).Should(Equal(1), "Service for discoverable endpoint should only have a single port")
Expect(discoverableEndpointService.Spec.Ports[0].Port).Should(Equal(int32(discoverableTargetPort)))
Expect(discoverableEndpointService.Spec.Ports[0].TargetPort).Should(Equal(intstr.FromInt(discoverableTargetPort)))
Expect(discoverableEndpointService.ObjectMeta.Annotations).Should(HaveKeyWithValue(constants.DevWorkspaceDiscoverableServiceAnnotation, "true"), "Service type should have discoverable service annotation")
By("Checking service is not created for non-exposed endpoint")
nonExposedService := &corev1.Service{}
nonExposedServiceNamespacedName := namespacedName(common.RouteName(testWorkspaceID, nonExposedEndpointName), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, nonExposedServiceNamespacedName, nonExposedService)
return k8sErrors.IsNotFound(err)
}, timeout, interval).Should(BeTrue(), "Route for non-exposed endpoint should not exist in cluster")
})
It("Creates route", func() {
createdDWR := getExistingDevWorkspaceRouting(devWorkspaceRoutingName)
By("Checking route is created")
createdRoute := routeV1.Route{}
routeNamespacedName := namespacedName(common.RouteName(testWorkspaceID, exposedEndPointName), testNamespace)
Eventually(func() error {
err := k8sClient.Get(ctx, routeNamespacedName, &createdRoute)
return err
}, timeout, interval).Should(BeNil(), "Route should exist in cluster")
Expect(createdRoute.Labels).Should(Equal(ExpectedLabels), "Route should contain DevWorkspace ID label")
expectedOwnerReference := devWorkspaceRoutingOwnerRef(createdDWR)
Expect(createdRoute.OwnerReferences).Should(ContainElement(expectedOwnerReference), "Route should be owned by DevWorkspaceRouting")
Expect(createdRoute.ObjectMeta.Annotations).Should(HaveKeyWithValue(constants.DevWorkspaceEndpointNameAnnotation, exposedEndPointName), "Route should have endpoint name annotation")
Expect(createdRoute.ObjectMeta.Annotations).Should(HaveKeyWithValue(endpointAnnotationKey, endpointAnnotationValue), "Route should have annotation from endpoint")
By("Checking route points to service")
createdService := &corev1.Service{}
serviceNamespacedName := namespacedName(common.ServiceName(testWorkspaceID), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, serviceNamespacedName, createdService)
return err == nil
}, timeout, interval).Should(BeTrue(), "Service should exist in cluster")
var targetPorts []intstr.IntOrString
var ports []int32
for _, servicePort := range createdService.Spec.Ports {
targetPorts = append(targetPorts, servicePort.TargetPort)
ports = append(ports, servicePort.Port)
}
Expect(targetPorts).Should(ContainElement(createdRoute.Spec.Port.TargetPort), "Route target port should be in service target ports")
Expect(ports).Should(ContainElement(createdRoute.Spec.Port.TargetPort.IntVal), "Route target port should be in service ports")
Expect(createdRoute.Spec.To.Name).Should(Equal(createdService.Name), "Route target reference should be service name")
By("Checking route is created for discoverable endpoint")
discoverableRoute := routeV1.Route{}
dsicoverableRouteNamespacedName := namespacedName(common.RouteName(testWorkspaceID, discoverableEndpointName), testNamespace)
Eventually(func() error {
err := k8sClient.Get(ctx, dsicoverableRouteNamespacedName, &discoverableRoute)
return err
}, timeout, interval).Should(BeNil(), "Route should exist in cluster")
Expect(discoverableRoute.Labels).Should(Equal(ExpectedLabels), "Route should contain DevWorkspace ID label")
Expect(discoverableRoute.OwnerReferences).Should(ContainElement(expectedOwnerReference), "Route should be owned by DevWorkspaceRouting")
Expect(discoverableRoute.ObjectMeta.Annotations).Should(HaveKeyWithValue(constants.DevWorkspaceEndpointNameAnnotation, discoverableEndpointName), "Route should have endpoint name annotation")
Expect(discoverableRoute.ObjectMeta.Annotations).Should(HaveKeyWithValue(endpointAnnotationKey, endpointAnnotationValue), "Route should have annotation from endpoint")
By("Checking route for discoverable endpoint points to service")
Expect(targetPorts).Should(ContainElement(discoverableRoute.Spec.Port.TargetPort), "Route target port should be in service target ports")
Expect(ports).Should(ContainElement(discoverableRoute.Spec.Port.TargetPort.IntVal), "Route target port should be in service ports")
Expect(discoverableRoute.Spec.To.Name).Should(Equal(createdService.Name), "Route target reference should be service name")
By("Checking route is not created for non-exposed endpoint")
nonExposedRoute := routeV1.Route{}
nonExposedRouteNamespacedName := namespacedName(common.RouteName(testWorkspaceID, nonExposedEndpointName), testNamespace)
Eventually(func() bool {
err := k8sClient.Get(ctx, nonExposedRouteNamespacedName, &nonExposedRoute)
return k8sErrors.IsNotFound(err)
}, timeout, interval).Should(BeTrue(), "Route for non-exposed endpoint should not exist in cluster")
})
})
})
Context("Failure cases", func() {
BeforeEach(func() {
config.SetGlobalConfigForTesting(testControllerCfg)
infrastructure.InitializeForTesting(infrastructure.Kubernetes)
createDWR(testWorkspaceID, devWorkspaceRoutingName)
getReadyDevWorkspaceRouting(testWorkspaceID)
})
AfterEach(func() {
config.SetGlobalConfigForTesting(testControllerCfg)
deleteDevWorkspaceRouting(devWorkspaceRoutingName)
deleteService(common.ServiceName(testWorkspaceID), testNamespace)
deleteService(common.EndpointName(discoverableEndpointName), testNamespace)
deleteIngress(exposedEndPointName, testNamespace)
deleteIngress(discoverableEndpointName, testNamespace)
})
It("Fails DevWorkspaceRouting with no routing class", func() {
By("Creating DevWorkspaceRouting with no routing class")
mainAttributes := controllerv1alpha1.Attributes{}
mainAttributes.PutString("type", "main")
machineEndpointsMap := map[string]controllerv1alpha1.EndpointList{
testMachineName: {
controllerv1alpha1.Endpoint{
Name: exposedEndPointName,
Exposure: controllerv1alpha1.PublicEndpointExposure,
Attributes: mainAttributes,
TargetPort: exposedTargetPort,
},
},
}
dwr := &controllerv1alpha1.DevWorkspaceRouting{
Spec: controllerv1alpha1.DevWorkspaceRoutingSpec{
DevWorkspaceId: testWorkspaceID,
RoutingClass: "",
Endpoints: machineEndpointsMap,
PodSelector: map[string]string{
constants.DevWorkspaceIDLabel: testWorkspaceID,
},
},
}
// Choose a unique name because a DWR will have already been created in the BeforeEach,
// causing a conflict if we try reusing the same name
dwrName := "dwr-with-no-routing-class"
dwr.SetName(dwrName)
dwr.SetNamespace(testNamespace)
Expect(k8sClient.Create(ctx, dwr)).Should(Succeed())
By("Checking that the DevWorkspaceRouting's has the failed status")
dwrNamespacedName := namespacedName(dwrName, testNamespace)
createdDWR := &controllerv1alpha1.DevWorkspaceRouting{}
Eventually(func() (bool, error) {
err := k8sClient.Get(ctx, dwrNamespacedName, createdDWR)
if err != nil {
return false, err
}
return createdDWR.Status.Phase == controllerv1alpha1.RoutingFailed, nil
}, timeout, interval).Should(BeTrue(), "DevWorkspaceRouting should be in failed phase")
Expect(createdDWR.Status.Message).Should(Equal("DevWorkspaceRouting requires field routingClass to be set"),
"DevWorkspaceRouting status message should indicate that the routingClass must be set")
// Manual cleanup since we specified a DWR name that is different than what is specified in the AfterEach
// No ingresses or services owned by the DWR-with-no-routing-class will be created, as it entered the failed phase early
deleteDevWorkspaceRouting(dwrName)
})
It("Fails DevWorkspaceRouting when routing class removed", func() {
By("Removing DevWorkspaceRouting's routing class")
Eventually(func() error {
createdDWR := getReadyDevWorkspaceRouting(devWorkspaceRoutingName)
createdDWR.Spec.RoutingClass = ""
return k8sClient.Update(ctx, createdDWR)
}, timeout, interval).Should(Succeed(), "DevWorkspaceRouting routing class should be updated on cluster")
By("Checking that the DevWorkspaceRouting's has the failed status")
dwrNamespacedName := namespacedName(devWorkspaceRoutingName, testNamespace)
updatedDWR := &controllerv1alpha1.DevWorkspaceRouting{}
Eventually(func() (bool, error) {
err := k8sClient.Get(ctx, dwrNamespacedName, updatedDWR)
if err != nil {
return false, err
}
return updatedDWR.Status.Phase == controllerv1alpha1.RoutingFailed, nil
}, timeout, interval).Should(BeTrue(), "DevWorkspaceRouting should be in failed phase")
Expect(updatedDWR.Status.Message).Should(Equal("DevWorkspaceRouting requires field routingClass to be set"),
"DevWorkspaceRouting status message should indicate that the routingClass must be set")
})
It("Fails DevWorkspaceRouting with cluster-tls routing class on Kubernetes", func() {
By("Setting cluster-tls DevWorkspaceRouting's routing class")
Eventually(func() error {
createdDWR := getReadyDevWorkspaceRouting(devWorkspaceRoutingName)
createdDWR.Spec.RoutingClass = "cluster-tls"
return k8sClient.Update(ctx, createdDWR)
}, timeout, interval).Should(Succeed(), "DevWorkspaceRouting routing class should be updated on cluster")
By("Checking that the DevWorkspaceRouting's has the failed status")
dwrNamespacedName := namespacedName(devWorkspaceRoutingName, testNamespace)
updatedDWR := &controllerv1alpha1.DevWorkspaceRouting{}
Eventually(func() (bool, error) {
err := k8sClient.Get(ctx, dwrNamespacedName, updatedDWR)
if err != nil {
return false, err
}
return updatedDWR.Status.Phase == controllerv1alpha1.RoutingFailed, nil
}, timeout, interval).Should(BeTrue(), "DevWorkspaceRouting should be in failed phase")
})
It("Fails DevWorkspaceRouting when cluster host suffix missing on Kubernetes", func() {
By("Removing cluster host suffix from DevWorkspace Operator Configuration")
dwoc := testControllerCfg.DeepCopy()
dwoc.Routing = &controllerv1alpha1.RoutingConfig{
ClusterHostSuffix: "",
}
config.SetGlobalConfigForTesting(dwoc)
By("Triggering a reconcile")
Eventually(func() error {
createdDWR := getReadyDevWorkspaceRouting(devWorkspaceRoutingName)
createdDWR.Annotations = make(map[string]string)
createdDWR.Annotations["test"] = "test"
return k8sClient.Update(ctx, createdDWR)
}, timeout, interval).Should(Succeed(), "DevWorkspaceRouting annotations should be updated on cluster")
By("Checking that the DevWorkspaceRouting's has the failed status")
dwrNamespacedName := namespacedName(devWorkspaceRoutingName, testNamespace)
updatedDWR := &controllerv1alpha1.DevWorkspaceRouting{}
Eventually(func() (controllerv1alpha1.DevWorkspaceRoutingPhase, error) {
err := k8sClient.Get(ctx, dwrNamespacedName, updatedDWR)
if err != nil {
return "", err
}
return updatedDWR.Status.Phase, nil
}, timeout, interval).Should(Equal(controllerv1alpha1.RoutingFailed), "DevWorkspaceRouting should be in failed phase")
})
})
})