Skip to content

Commit ecc03ff

Browse files
committed
Add DevWorkspaceRouting controller envtest failure cases
Signed-off-by: Andrew Obuchowicz <aobuchow@redhat.com>
1 parent c5f95db commit ecc03ff

2 files changed

Lines changed: 152 additions & 0 deletions

File tree

controllers/controller/devworkspacerouting/devworkspacerouting_controller_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
2020
"github.com/devfile/devworkspace-operator/pkg/common"
21+
"github.com/devfile/devworkspace-operator/pkg/config"
2122
"github.com/devfile/devworkspace-operator/pkg/constants"
2223
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
2324
. "github.com/onsi/ginkgo/v2"
@@ -372,6 +373,145 @@ var _ = Describe("DevWorkspaceRouting Controller", func() {
372373
}, timeout, interval).Should(BeTrue(), "Route for non-exposed endpoint should not exist in cluster")
373374
})
374375
})
376+
})
377+
378+
Context("Failure cases", func() {
379+
BeforeEach(func() {
380+
config.SetGlobalConfigForTesting(testControllerCfg)
381+
infrastructure.InitializeForTesting(infrastructure.Kubernetes)
382+
createDWR(testWorkspaceID, devWorkspaceRoutingName)
383+
getReadyDevWorkspaceRouting(testWorkspaceID)
384+
})
385+
386+
AfterEach(func() {
387+
config.SetGlobalConfigForTesting(testControllerCfg)
388+
deleteDevWorkspaceRouting(devWorkspaceRoutingName)
389+
deleteService(common.ServiceName(testWorkspaceID), testNamespace)
390+
deleteService(common.EndpointName(discoverableEndpointName), testNamespace)
391+
deleteIngress(exposedEndPointName, testNamespace)
392+
deleteIngress(discoverableEndpointName, testNamespace)
393+
})
394+
It("Fails DevWorkspaceRouting with no routing class", func() {
395+
By("Creating DevWorkspaceRouting with no routing class")
396+
mainAttributes := controllerv1alpha1.Attributes{}
397+
mainAttributes.PutString("type", "main")
398+
machineEndpointsMap := map[string]controllerv1alpha1.EndpointList{
399+
testMachineName: {
400+
controllerv1alpha1.Endpoint{
401+
Name: exposedEndPointName,
402+
Exposure: controllerv1alpha1.PublicEndpointExposure,
403+
Attributes: mainAttributes,
404+
TargetPort: exposedTargetPort,
405+
},
406+
},
407+
}
408+
409+
dwr := &controllerv1alpha1.DevWorkspaceRouting{
410+
Spec: controllerv1alpha1.DevWorkspaceRoutingSpec{
411+
DevWorkspaceId: testWorkspaceID,
412+
RoutingClass: "",
413+
Endpoints: machineEndpointsMap,
414+
PodSelector: map[string]string{
415+
constants.DevWorkspaceIDLabel: testWorkspaceID,
416+
},
417+
},
418+
}
419+
// Choose a unique name because a DWR will have already been created in the BeforeEach,
420+
// causing a conflict if we try reusing the same name
421+
dwrName := "dwr-with-no-routing-class"
422+
dwr.SetName(dwrName)
423+
dwr.SetNamespace(testNamespace)
424+
Expect(k8sClient.Create(ctx, dwr)).Should(Succeed())
425+
426+
By("Checking that the DevWorkspaceRouting's has the failed status")
427+
dwrNamespacedName := namespacedName(dwrName, testNamespace)
428+
createdDWR := &controllerv1alpha1.DevWorkspaceRouting{}
429+
Eventually(func() (bool, error) {
430+
err := k8sClient.Get(ctx, dwrNamespacedName, createdDWR)
431+
if err != nil {
432+
return false, err
433+
}
434+
return createdDWR.Status.Phase == controllerv1alpha1.RoutingFailed, nil
435+
}, timeout, interval).Should(BeTrue(), "DevWorkspaceRouting should be in failed phase")
436+
437+
Expect(createdDWR.Status.Message).Should(Equal("DevWorkspaceRouting requires field routingClass to be set"),
438+
"DevWorkspaceRouting status message should indicate that the routingClass must be set")
439+
440+
// Manual cleanup since we specified a DWR name that is different than what is specified in the AfterEach
441+
// No ingresses or services owned by the DWR-with-no-routing-class will be created, as it entered the failed phase early
442+
deleteDevWorkspaceRouting(dwrName)
443+
})
375444

445+
It("Fails DevWorkspaceRouting when routing class removed", func() {
446+
By("Removing DevWorkspaceRouting's routing class")
447+
Eventually(func() error {
448+
createdDWR := getReadyDevWorkspaceRouting(devWorkspaceRoutingName)
449+
createdDWR.Spec.RoutingClass = ""
450+
return k8sClient.Update(ctx, createdDWR)
451+
}, timeout, interval).Should(Succeed(), "DevWorkspaceRouting routing class should be updated on cluster")
452+
453+
By("Checking that the DevWorkspaceRouting's has the failed status")
454+
dwrNamespacedName := namespacedName(devWorkspaceRoutingName, testNamespace)
455+
updatedDWR := &controllerv1alpha1.DevWorkspaceRouting{}
456+
Eventually(func() (bool, error) {
457+
err := k8sClient.Get(ctx, dwrNamespacedName, updatedDWR)
458+
if err != nil {
459+
return false, err
460+
}
461+
return updatedDWR.Status.Phase == controllerv1alpha1.RoutingFailed, nil
462+
}, timeout, interval).Should(BeTrue(), "DevWorkspaceRouting should be in failed phase")
463+
464+
Expect(updatedDWR.Status.Message).Should(Equal("DevWorkspaceRouting requires field routingClass to be set"),
465+
"DevWorkspaceRouting status message should indicate that the routingClass must be set")
466+
467+
})
468+
469+
It("Fails DevWorkspaceRouting with cluster-tls routing class on Kubernetes", func() {
470+
By("Setting cluster-tls DevWorkspaceRouting's routing class")
471+
Eventually(func() error {
472+
createdDWR := getReadyDevWorkspaceRouting(devWorkspaceRoutingName)
473+
createdDWR.Spec.RoutingClass = "cluster-tls"
474+
return k8sClient.Update(ctx, createdDWR)
475+
}, timeout, interval).Should(Succeed(), "DevWorkspaceRouting routing class should be updated on cluster")
476+
477+
By("Checking that the DevWorkspaceRouting's has the failed status")
478+
dwrNamespacedName := namespacedName(devWorkspaceRoutingName, testNamespace)
479+
updatedDWR := &controllerv1alpha1.DevWorkspaceRouting{}
480+
Eventually(func() (bool, error) {
481+
err := k8sClient.Get(ctx, dwrNamespacedName, updatedDWR)
482+
if err != nil {
483+
return false, err
484+
}
485+
return updatedDWR.Status.Phase == controllerv1alpha1.RoutingFailed, nil
486+
}, timeout, interval).Should(BeTrue(), "DevWorkspaceRouting should be in failed phase")
487+
})
488+
489+
It("Fails DevWorkspaceRouting when cluster host suffix missing on Kubernetes", func() {
490+
By("Removing cluster host suffix from DevWorkspace Operator Configuration")
491+
dwoc := testControllerCfg.DeepCopy()
492+
dwoc.Routing = &controllerv1alpha1.RoutingConfig{
493+
ClusterHostSuffix: "",
494+
}
495+
config.SetGlobalConfigForTesting(dwoc)
496+
497+
By("Triggering a reconcile")
498+
Eventually(func() error {
499+
createdDWR := getReadyDevWorkspaceRouting(devWorkspaceRoutingName)
500+
createdDWR.Annotations = make(map[string]string)
501+
createdDWR.Annotations["test"] = "test"
502+
return k8sClient.Update(ctx, createdDWR)
503+
}, timeout, interval).Should(Succeed(), "DevWorkspaceRouting annotations should be updated on cluster")
504+
505+
By("Checking that the DevWorkspaceRouting's has the failed status")
506+
dwrNamespacedName := namespacedName(devWorkspaceRoutingName, testNamespace)
507+
updatedDWR := &controllerv1alpha1.DevWorkspaceRouting{}
508+
Eventually(func() (controllerv1alpha1.DevWorkspaceRoutingPhase, error) {
509+
err := k8sClient.Get(ctx, dwrNamespacedName, updatedDWR)
510+
if err != nil {
511+
return "", err
512+
}
513+
return updatedDWR.Status.Phase, nil
514+
}, timeout, interval).Should(Equal(controllerv1alpha1.RoutingFailed), "DevWorkspaceRouting should be in failed phase")
515+
})
376516
})
377517
})

controllers/controller/devworkspacerouting/util_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ func getExistingDevWorkspaceRouting(name string) *controllerv1alpha1.DevWorkspac
149149
return dwr
150150
}
151151

152+
func getReadyDevWorkspaceRouting(name string) *controllerv1alpha1.DevWorkspaceRouting {
153+
dwr := getExistingDevWorkspaceRouting(name)
154+
dwrNamespacedName := namespacedName(devWorkspaceRoutingName, testNamespace)
155+
Eventually(func() (controllerv1alpha1.DevWorkspaceRoutingPhase, error) {
156+
if err := k8sClient.Get(ctx, dwrNamespacedName, dwr); err != nil {
157+
return "", err
158+
}
159+
return controllerv1alpha1.DevWorkspaceRoutingPhase(dwr.Status.Phase), nil
160+
}, timeout, interval).Should(Equal(controllerv1alpha1.RoutingReady), "Ready DevWorkspaceRouting should exist in cluster")
161+
return dwr
162+
}
163+
152164
func deleteService(serviceName string, namespace string) {
153165
createdService := &corev1.Service{}
154166
serviceNamespacedName := namespacedName(serviceName, namespace)

0 commit comments

Comments
 (0)