Skip to content

Commit ed731a4

Browse files
committed
Extract test functionality to avoid repetition and reduce verbosity
Signed-off-by: Angel Misevski <[email protected]>
1 parent e5fe838 commit ed731a4

File tree

2 files changed

+37
-95
lines changed

2 files changed

+37
-95
lines changed

controllers/workspace/devworkspace_controller_test.go

Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
rbacv1 "k8s.io/api/rbac/v1"
3636
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
3737
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
38-
"k8s.io/apimachinery/pkg/types"
3938
"sigs.k8s.io/controller-runtime/pkg/client"
4039
"sigs.k8s.io/yaml"
4140
)
@@ -66,10 +65,7 @@ var _ = Describe("DevWorkspace Controller", func() {
6665

6766
By("Creating a new DevWorkspace")
6867
Expect(k8sClient.Create(ctx, devworkspace)).Should(Succeed())
69-
dwNamespacedName := types.NamespacedName{
70-
Namespace: testNamespace,
71-
Name: devWorkspaceName,
72-
}
68+
dwNamespacedName := namespacedName(devWorkspaceName, testNamespace)
7369
defer deleteDevWorkspace(devWorkspaceName)
7470

7571
createdDW := &dw.DevWorkspace{}
@@ -112,10 +108,7 @@ var _ = Describe("DevWorkspace Controller", func() {
112108

113109
By("Creating a new DevWorkspace")
114110
Expect(k8sClient.Create(ctx, devworkspace)).Should(Succeed())
115-
dwNamespacedName := types.NamespacedName{
116-
Namespace: testNamespace,
117-
Name: devWorkspaceName,
118-
}
111+
dwNamespacedName := namespacedName(devWorkspaceName, testNamespace)
119112
defer deleteDevWorkspace(devWorkspaceName)
120113

121114
createdDW := &dw.DevWorkspace{}
@@ -150,10 +143,7 @@ var _ = Describe("DevWorkspace Controller", func() {
150143

151144
By("Creating a new DevWorkspace")
152145
Expect(k8sClient.Create(ctx, devworkspace)).Should(Succeed())
153-
dwNamespacedName := types.NamespacedName{
154-
Namespace: testNamespace,
155-
Name: devWorkspaceName,
156-
}
146+
dwNamespacedName := namespacedName(devWorkspaceName, testNamespace)
157147
defer deleteDevWorkspace(devWorkspaceName)
158148

159149
createdDW := &dw.DevWorkspace{}
@@ -169,10 +159,7 @@ var _ = Describe("DevWorkspace Controller", func() {
169159
By("Checking that duplicate DevWorkspace enters failed Phase")
170160
createdDW2 := &dw.DevWorkspace{}
171161
Eventually(func() (phase dw.DevWorkspacePhase, err error) {
172-
if err := k8sClient.Get(ctx, types.NamespacedName{
173-
Name: devworkspace2.Name,
174-
Namespace: testNamespace,
175-
}, createdDW2); err != nil {
162+
if err := k8sClient.Get(ctx, namespacedName(devworkspace2.Name, testNamespace), createdDW2); err != nil {
176163
return "", err
177164
}
178165
return createdDW2.Status.Phase, nil
@@ -195,7 +182,7 @@ var _ = Describe("DevWorkspace Controller", func() {
195182
By("Checking that common role is created")
196183
dwRole := &rbacv1.Role{}
197184
Eventually(func() bool {
198-
if err := k8sClient.Get(ctx, types.NamespacedName{Name: common.WorkspaceRoleName(), Namespace: testNamespace}, dwRole); err != nil {
185+
if err := k8sClient.Get(ctx, namespacedName(common.WorkspaceRoleName(), testNamespace), dwRole); err != nil {
199186
return false
200187
}
201188
return true
@@ -204,7 +191,7 @@ var _ = Describe("DevWorkspace Controller", func() {
204191
By("Checking that common rolebinding is created")
205192
dwRoleBinding := &rbacv1.RoleBinding{}
206193
Eventually(func() bool {
207-
if err := k8sClient.Get(ctx, types.NamespacedName{Name: common.WorkspaceRolebindingName(), Namespace: testNamespace}, dwRoleBinding); err != nil {
194+
if err := k8sClient.Get(ctx, namespacedName(common.WorkspaceRolebindingName(), testNamespace), dwRoleBinding); err != nil {
208195
return false
209196
}
210197
return true
@@ -226,7 +213,7 @@ var _ = Describe("DevWorkspace Controller", func() {
226213
dwr := &controllerv1alpha1.DevWorkspaceRouting{}
227214
dwrName := common.DevWorkspaceRoutingName(workspaceID)
228215
Eventually(func() error {
229-
return k8sClient.Get(ctx, types.NamespacedName{Name: dwrName, Namespace: testNamespace}, dwr)
216+
return k8sClient.Get(ctx, namespacedName(dwrName, testNamespace), dwr)
230217
}, timeout, interval).Should(Succeed(), "DevWorkspaceRouting is present on cluster")
231218

232219
Expect(string(dwr.Spec.RoutingClass)).Should(Equal(devworkspace.Spec.RoutingClass), "RoutingClass should be propagated to DevWorkspaceRouting")
@@ -243,7 +230,7 @@ var _ = Describe("DevWorkspace Controller", func() {
243230
markRoutingReady("test-url", common.DevWorkspaceRoutingName(workspaceID))
244231

245232
Eventually(func() (string, error) {
246-
if err := k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace); err != nil {
233+
if err := k8sClient.Get(ctx, namespacedName(devWorkspaceName, testNamespace), devworkspace); err != nil {
247234
return "", err
248235
}
249236
return devworkspace.Status.MainUrl, nil
@@ -260,10 +247,7 @@ var _ = Describe("DevWorkspace Controller", func() {
260247

261248
metadataCM := &corev1.ConfigMap{}
262249
Eventually(func() error {
263-
cmNN := types.NamespacedName{
264-
Name: common.MetadataConfigMapName(workspaceID),
265-
Namespace: testNamespace,
266-
}
250+
cmNN := namespacedName(common.MetadataConfigMapName(workspaceID), testNamespace)
267251
return k8sClient.Get(ctx, cmNN, metadataCM)
268252
}, timeout, interval).Should(Succeed(), "Should create workspace metadata configmap")
269253

@@ -290,10 +274,7 @@ var _ = Describe("DevWorkspace Controller", func() {
290274

291275
sa := &corev1.ServiceAccount{}
292276
Eventually(func() error {
293-
saNN := types.NamespacedName{
294-
Name: common.ServiceAccountName(workspaceID),
295-
Namespace: testNamespace,
296-
}
277+
saNN := namespacedName(common.ServiceAccountName(workspaceID), testNamespace)
297278
return k8sClient.Get(ctx, saNN, sa)
298279
}, timeout, interval).Should(Succeed(), "Should create DevWorkspace ServiceAccount")
299280

@@ -312,10 +293,7 @@ var _ = Describe("DevWorkspace Controller", func() {
312293

313294
deploy := &appsv1.Deployment{}
314295
Eventually(func() error {
315-
deployNN := types.NamespacedName{
316-
Name: common.DeploymentName(workspaceID),
317-
Namespace: testNamespace,
318-
}
296+
deployNN := namespacedName(common.DeploymentName(workspaceID), testNamespace)
319297
return k8sClient.Get(ctx, deployNN, deploy)
320298
}, timeout, interval).Should(Succeed(), "Should create DevWorkspace Deployment")
321299

@@ -346,10 +324,7 @@ var _ = Describe("DevWorkspace Controller", func() {
346324

347325
currDW := &dw.DevWorkspace{}
348326
Eventually(func() (dw.DevWorkspacePhase, error) {
349-
err := k8sClient.Get(ctx, types.NamespacedName{
350-
Name: devworkspace.Name,
351-
Namespace: devworkspace.Namespace,
352-
}, currDW)
327+
err := k8sClient.Get(ctx, namespacedName(devworkspace.Name, devworkspace.Namespace), currDW)
353328
if err != nil {
354329
return "", err
355330
}
@@ -410,10 +385,7 @@ var _ = Describe("DevWorkspace Controller", func() {
410385
markRoutingReady(testURL, common.DevWorkspaceRoutingName(workspaceID))
411386

412387
deploy := &appsv1.Deployment{}
413-
deployNN := types.NamespacedName{
414-
Name: common.DeploymentName(workspaceID),
415-
Namespace: testNamespace,
416-
}
388+
deployNN := namespacedName(common.DeploymentName(workspaceID), testNamespace)
417389
Eventually(func() error {
418390
return k8sClient.Get(ctx, deployNN, deploy)
419391
}, timeout, interval).Should(Succeed(), "Getting workspace deployment from cluster")
@@ -439,10 +411,7 @@ var _ = Describe("DevWorkspace Controller", func() {
439411
markRoutingReady(testURL, common.DevWorkspaceRoutingName(workspaceID))
440412

441413
deploy := &appsv1.Deployment{}
442-
deployNN := types.NamespacedName{
443-
Name: common.DeploymentName(workspaceID),
444-
Namespace: testNamespace,
445-
}
414+
deployNN := namespacedName(common.DeploymentName(workspaceID), testNamespace)
446415
Eventually(func() error {
447416
return k8sClient.Get(ctx, deployNN, deploy)
448417
}, timeout, interval).Should(Succeed(), "Getting workspace deployment from cluster")
@@ -525,10 +494,7 @@ var _ = Describe("DevWorkspace Controller", func() {
525494
markRoutingReady(testURL, common.DevWorkspaceRoutingName(workspaceID))
526495

527496
deploy := &appsv1.Deployment{}
528-
deployNN := types.NamespacedName{
529-
Name: common.DeploymentName(workspaceID),
530-
Namespace: testNamespace,
531-
}
497+
deployNN := namespacedName(common.DeploymentName(workspaceID), testNamespace)
532498
Eventually(func() error {
533499
return k8sClient.Get(ctx, deployNN, deploy)
534500
}, timeout, interval).Should(Succeed(), "Getting workspace deployment from cluster")
@@ -572,10 +538,7 @@ var _ = Describe("DevWorkspace Controller", func() {
572538
By("Manually making Routing ready to continue")
573539
markRoutingReady(testURL, common.DevWorkspaceRoutingName(workspaceID))
574540
deploy := &appsv1.Deployment{}
575-
deployNN := types.NamespacedName{
576-
Name: common.DeploymentName(workspaceID),
577-
Namespace: testNamespace,
578-
}
541+
deployNN := namespacedName(common.DeploymentName(workspaceID), testNamespace)
579542
Eventually(func() error {
580543
return k8sClient.Get(ctx, deployNN, deploy)
581544
}, timeout, interval).Should(Succeed(), "Getting workspace deployment from cluster")
@@ -632,10 +595,7 @@ var _ = Describe("DevWorkspace Controller", func() {
632595
By("Adds devworkspace-started annotation to false on DevWorkspaceRouting")
633596
Eventually(func() (string, error) {
634597
dwr := &controllerv1alpha1.DevWorkspaceRouting{}
635-
if err := k8sClient.Get(ctx, types.NamespacedName{
636-
Name: common.DevWorkspaceRoutingName(devworkspace.Status.DevWorkspaceId),
637-
Namespace: testNamespace,
638-
}, dwr); err != nil {
598+
if err := k8sClient.Get(ctx, namespacedName(common.DevWorkspaceRoutingName(devworkspace.Status.DevWorkspaceId), testNamespace), dwr); err != nil {
639599
return "", err
640600
}
641601
annotation, ok := dwr.Annotations[constants.DevWorkspaceStartedStatusAnnotation]
@@ -648,10 +608,7 @@ var _ = Describe("DevWorkspace Controller", func() {
648608
By("Checking that workspace deployment is scaled to zero")
649609
Eventually(func() (replicas int32, err error) {
650610
deploy := &appsv1.Deployment{}
651-
if err := k8sClient.Get(ctx, types.NamespacedName{
652-
Name: common.DeploymentName(devworkspace.Status.DevWorkspaceId),
653-
Namespace: testNamespace,
654-
}, deploy); err != nil {
611+
if err := k8sClient.Get(ctx, namespacedName(common.DeploymentName(devworkspace.Status.DevWorkspaceId), testNamespace), deploy); err != nil {
655612
return -1, err
656613
}
657614
return *deploy.Spec.Replicas, nil
@@ -662,10 +619,7 @@ var _ = Describe("DevWorkspace Controller", func() {
662619

663620
currDW := &dw.DevWorkspace{}
664621
Eventually(func() (dw.DevWorkspacePhase, error) {
665-
if err := k8sClient.Get(ctx, types.NamespacedName{
666-
Name: devworkspace.Name,
667-
Namespace: devworkspace.Namespace,
668-
}, currDW); err != nil {
622+
if err := k8sClient.Get(ctx, namespacedName(devworkspace.Name, devworkspace.Namespace), currDW); err != nil {
669623
return "", err
670624
}
671625
GinkgoWriter.Printf("Waiting for DevWorkspace to enter Stopped phase -- Phase: %s, Message %s\n", currDW.Status.Phase, currDW.Status.Message)
@@ -705,10 +659,7 @@ var _ = Describe("DevWorkspace Controller", func() {
705659
}
706660
for _, obj := range objects {
707661
Eventually(func() error {
708-
err := k8sClient.Get(ctx, types.NamespacedName{
709-
Name: obj.GetName(),
710-
Namespace: testNamespace,
711-
}, obj)
662+
err := k8sClient.Get(ctx, namespacedName(obj.GetName(), testNamespace), obj)
712663
switch {
713664
case err == nil:
714665
return fmt.Errorf("Object exists")
@@ -722,10 +673,7 @@ var _ = Describe("DevWorkspace Controller", func() {
722673

723674
currDW := &dw.DevWorkspace{}
724675
Eventually(func() (dw.DevWorkspacePhase, error) {
725-
if err := k8sClient.Get(ctx, types.NamespacedName{
726-
Name: devworkspace.Name,
727-
Namespace: devworkspace.Namespace,
728-
}, currDW); err != nil {
676+
if err := k8sClient.Get(ctx, namespacedName(devworkspace.Name, devworkspace.Namespace), currDW); err != nil {
729677
return "", err
730678
}
731679
GinkgoWriter.Printf("Waiting for DevWorkspace to enter Stopped phase -- Phase: %s, Message %s\n", currDW.Status.Phase, currDW.Status.Message)

controllers/workspace/util_test.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func createDevWorkspace(fromFile string) {
5050
Expect(k8sClient.Create(ctx, devworkspace)).Should(Succeed())
5151
createdDW := &dw.DevWorkspace{}
5252
Eventually(func() bool {
53-
if err := k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, createdDW); err != nil {
53+
if err := k8sClient.Get(ctx, namespacedName(devWorkspaceName, testNamespace), createdDW); err != nil {
5454
return false
5555
}
5656
return createdDW.Status.DevWorkspaceId != ""
@@ -70,11 +70,7 @@ func createStartedDevWorkspace(fromFile string) {
7070

7171
currDW := &dw.DevWorkspace{}
7272
Eventually(func() (dw.DevWorkspacePhase, error) {
73-
err := k8sClient.Get(ctx, types.NamespacedName{
74-
Name: devworkspace.Name,
75-
Namespace: devworkspace.Namespace,
76-
}, currDW)
77-
if err != nil {
73+
if err := k8sClient.Get(ctx, namespacedName(devworkspace.Name, devworkspace.Namespace), currDW); err != nil {
7874
return "", err
7975
}
8076
GinkgoWriter.Printf("Waiting for DevWorkspace to enter running phase -- Phase: %s, Message %s\n", currDW.Status.Phase, currDW.Status.Message)
@@ -85,7 +81,7 @@ func createStartedDevWorkspace(fromFile string) {
8581
func getExistingDevWorkspace() *dw.DevWorkspace {
8682
By("Getting existing DevWorkspace")
8783
devworkspace := &dw.DevWorkspace{}
88-
dwNN := types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}
84+
dwNN := namespacedName(devWorkspaceName, testNamespace)
8985
Expect(k8sClient.Get(ctx, dwNN, devworkspace)).Should(Succeed())
9086
workspaceID := devworkspace.Status.DevWorkspaceId
9187
Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set")
@@ -94,7 +90,7 @@ func getExistingDevWorkspace() *dw.DevWorkspace {
9490

9591
// deleteDevWorkspace forces a DevWorkspace to be deleted by removing all finalizers
9692
func deleteDevWorkspace(name string) {
97-
dwNN := types.NamespacedName{Name: name, Namespace: testNamespace}
93+
dwNN := namespacedName(name, testNamespace)
9894
dw := &dw.DevWorkspace{}
9995
dw.Name = name
10096
dw.Namespace = testNamespace
@@ -131,23 +127,20 @@ func deleteDevWorkspace(name string) {
131127
func createObject(obj crclient.Object) {
132128
Expect(k8sClient.Create(ctx, obj)).Should(Succeed())
133129
Eventually(func() error {
134-
return k8sClient.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
130+
return k8sClient.Get(ctx, namespacedName(obj.GetName(), obj.GetNamespace()), obj)
135131
}, 10*time.Second, 250*time.Millisecond).Should(Succeed(), "Creating %s with name %s", obj.GetObjectKind(), obj.GetName())
136132
}
137133

138134
func deleteObject(obj crclient.Object) {
139135
Expect(k8sClient.Delete(ctx, obj)).Should(Succeed())
140136
Eventually(func() bool {
141-
err := k8sClient.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
137+
err := k8sClient.Get(ctx, namespacedName(obj.GetName(), obj.GetNamespace()), obj)
142138
return k8sErrors.IsNotFound(err)
143139
}, 10*time.Second, 250*time.Millisecond).Should(BeTrue(), "Deleting %s with name %s", obj.GetObjectKind(), obj.GetName())
144140
}
145141

146142
func markRoutingReady(mainUrl, routingName string) {
147-
namespacedName := types.NamespacedName{
148-
Name: routingName,
149-
Namespace: testNamespace,
150-
}
143+
namespacedName := namespacedName(routingName, testNamespace)
151144
routing := &controllerv1alpha1.DevWorkspaceRouting{}
152145
Eventually(func() error {
153146
err := k8sClient.Get(ctx, namespacedName, routing)
@@ -172,10 +165,7 @@ func markRoutingReady(mainUrl, routingName string) {
172165
}
173166

174167
func markDeploymentReady(deploymentName string) {
175-
namespacedName := types.NamespacedName{
176-
Name: deploymentName,
177-
Namespace: testNamespace,
178-
}
168+
namespacedName := namespacedName(deploymentName, testNamespace)
179169
deploy := &appsv1.Deployment{}
180170
Eventually(func() error {
181171
err := k8sClient.Get(ctx, namespacedName, deploy)
@@ -191,10 +181,7 @@ func markDeploymentReady(deploymentName string) {
191181
}
192182

193183
func scaleDeploymentToZero(deploymentName string) {
194-
namespacedName := types.NamespacedName{
195-
Name: deploymentName,
196-
Namespace: testNamespace,
197-
}
184+
namespacedName := namespacedName(deploymentName, testNamespace)
198185
deploy := &appsv1.Deployment{}
199186
Eventually(func() error {
200187
err := k8sClient.Get(ctx, namespacedName, deploy)
@@ -299,3 +286,10 @@ func volumeMountFromConfigMap(cm *corev1.ConfigMap, mountPath, subPath string) c
299286
SubPath: subPath,
300287
}
301288
}
289+
290+
func namespacedName(name, namespace string) types.NamespacedName {
291+
return types.NamespacedName{
292+
Name: name,
293+
Namespace: namespace,
294+
}
295+
}

0 commit comments

Comments
 (0)