Skip to content

Commit aab9c87

Browse files
committed
Update test utilities to allow creating multiple workspaces
Creating multiple workspaces during a test is required for testing functionality that only triggers when multiple workspaces exist (e.g. PVC cleanup) Signed-off-by: Angel Misevski <[email protected]>
1 parent ed731a4 commit aab9c87

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

controllers/workspace/devworkspace_controller_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ var _ = Describe("DevWorkspace Controller", func() {
170170
Context("Workspace Objects creation", func() {
171171

172172
BeforeEach(func() {
173-
createDevWorkspace("test-devworkspace.yaml")
173+
createDevWorkspace(devWorkspaceName, "test-devworkspace.yaml")
174174
})
175175

176176
AfterEach(func() {
@@ -206,7 +206,7 @@ var _ = Describe("DevWorkspace Controller", func() {
206206
})
207207

208208
It("Creates DevWorkspaceRouting", func() {
209-
devworkspace := getExistingDevWorkspace()
209+
devworkspace := getExistingDevWorkspace(devWorkspaceName)
210210
workspaceID := devworkspace.Status.DevWorkspaceId
211211

212212
By("Checking that DevWorkspaceRouting is created")
@@ -223,7 +223,7 @@ var _ = Describe("DevWorkspace Controller", func() {
223223
})
224224

225225
It("Syncs Routing mainURL to DevWorkspace", func() {
226-
devworkspace := getExistingDevWorkspace()
226+
devworkspace := getExistingDevWorkspace(devWorkspaceName)
227227
workspaceID := devworkspace.Status.DevWorkspaceId
228228

229229
By("Manually making Routing ready to continue")
@@ -239,7 +239,7 @@ var _ = Describe("DevWorkspace Controller", func() {
239239
})
240240

241241
It("Creates workspace metadata configmap", func() {
242-
devworkspace := getExistingDevWorkspace()
242+
devworkspace := getExistingDevWorkspace(devWorkspaceName)
243243
workspaceID := devworkspace.Status.DevWorkspaceId
244244

245245
By("Manually making Routing ready to continue")
@@ -266,7 +266,7 @@ var _ = Describe("DevWorkspace Controller", func() {
266266
})
267267

268268
It("Syncs the DevWorkspace ServiceAccount", func() {
269-
devworkspace := getExistingDevWorkspace()
269+
devworkspace := getExistingDevWorkspace(devWorkspaceName)
270270
workspaceID := devworkspace.Status.DevWorkspaceId
271271

272272
By("Manually making Routing ready to continue")
@@ -285,7 +285,7 @@ var _ = Describe("DevWorkspace Controller", func() {
285285
})
286286

287287
It("Syncs DevWorkspace Deployment", func() {
288-
devworkspace := getExistingDevWorkspace()
288+
devworkspace := getExistingDevWorkspace(devWorkspaceName)
289289
workspaceID := devworkspace.Status.DevWorkspaceId
290290

291291
By("Manually making Routing ready to continue")
@@ -304,7 +304,7 @@ var _ = Describe("DevWorkspace Controller", func() {
304304
})
305305

306306
It("Marks DevWorkspace as Running", func() {
307-
devworkspace := getExistingDevWorkspace()
307+
devworkspace := getExistingDevWorkspace(devWorkspaceName)
308308
workspaceID := devworkspace.Status.DevWorkspaceId
309309

310310
workspacecontroller.SetupHttpClientsForTesting(&http.Client{
@@ -354,7 +354,7 @@ var _ = Describe("DevWorkspace Controller", func() {
354354
},
355355
},
356356
})
357-
createDevWorkspace("test-devworkspace.yaml")
357+
createDevWorkspace(devWorkspaceName, "test-devworkspace.yaml")
358358
})
359359

360360
AfterEach(func() {
@@ -363,7 +363,7 @@ var _ = Describe("DevWorkspace Controller", func() {
363363
})
364364

365365
It("Mounts image pull secrets to the DevWorkspace Deployment", func() {
366-
devworkspace := getExistingDevWorkspace()
366+
devworkspace := getExistingDevWorkspace(devWorkspaceName)
367367
workspaceID := devworkspace.Status.DevWorkspaceId
368368

369369
By("Creating secrets for docker configs")
@@ -395,7 +395,7 @@ var _ = Describe("DevWorkspace Controller", func() {
395395
})
396396

397397
It("Manages git credentials for DevWorkspace", func() {
398-
devworkspace := getExistingDevWorkspace()
398+
devworkspace := getExistingDevWorkspace(devWorkspaceName)
399399
workspaceID := devworkspace.Status.DevWorkspaceId
400400

401401
By("Creating a secret for git credentials")
@@ -457,7 +457,7 @@ var _ = Describe("DevWorkspace Controller", func() {
457457
})
458458

459459
It("Automounts secrets and configmaps volumes", func() {
460-
devworkspace := getExistingDevWorkspace()
460+
devworkspace := getExistingDevWorkspace(devWorkspaceName)
461461
workspaceID := devworkspace.Status.DevWorkspaceId
462462

463463
By("Creating a automount secrets and configmaps")
@@ -519,7 +519,7 @@ var _ = Describe("DevWorkspace Controller", func() {
519519
})
520520

521521
It("Automounts secrets and configmaps env vars", func() {
522-
devworkspace := getExistingDevWorkspace()
522+
devworkspace := getExistingDevWorkspace(devWorkspaceName)
523523
workspaceID := devworkspace.Status.DevWorkspaceId
524524

525525
By("Creating a automount secrets and configmaps")
@@ -574,7 +574,7 @@ var _ = Describe("DevWorkspace Controller", func() {
574574
},
575575
},
576576
})
577-
createStartedDevWorkspace("test-devworkspace.yaml")
577+
createStartedDevWorkspace(devWorkspaceName, "test-devworkspace.yaml")
578578
})
579579

580580
AfterEach(func() {
@@ -587,7 +587,7 @@ var _ = Describe("DevWorkspace Controller", func() {
587587

588588
By("Setting DevWorkspace's .spec.started to false")
589589
Eventually(func() error {
590-
devworkspace = getExistingDevWorkspace()
590+
devworkspace = getExistingDevWorkspace(devWorkspaceName)
591591
devworkspace.Spec.Started = false
592592
return k8sClient.Update(ctx, devworkspace)
593593
}, timeout, interval).Should(Succeed(), "Update DevWorkspace to have .spec.started = false")
@@ -645,7 +645,7 @@ var _ = Describe("DevWorkspace Controller", func() {
645645

646646
By("Setting DevWorkspace's .spec.started to false")
647647
Eventually(func() error {
648-
devworkspace = getExistingDevWorkspace()
648+
devworkspace = getExistingDevWorkspace(devWorkspaceName)
649649
devworkspace.Spec.Started = false
650650
return k8sClient.Update(ctx, devworkspace)
651651
}, timeout, interval).Should(Succeed(), "Update DevWorkspace to have .spec.started = false")
@@ -693,7 +693,7 @@ var _ = Describe("DevWorkspace Controller", func() {
693693

694694
By("Set debug start annotation on DevWorkspace")
695695
Eventually(func() error {
696-
devworkspace = getExistingDevWorkspace()
696+
devworkspace = getExistingDevWorkspace(devWorkspaceName)
697697
if devworkspace.Annotations == nil {
698698
devworkspace.Annotations = map[string]string{}
699699
}
@@ -710,7 +710,7 @@ var _ = Describe("DevWorkspace Controller", func() {
710710

711711
By("Setting failing phase on workspace directly")
712712
Eventually(func() error {
713-
devworkspace = getExistingDevWorkspace()
713+
devworkspace = getExistingDevWorkspace(devWorkspaceName)
714714
devworkspace.Status.Phase = "Failing"
715715
devworkspace.Status.Conditions = append(devworkspace.Status.Conditions, dw.DevWorkspaceCondition{
716716
Type: dw.DevWorkspaceFailedStart,
@@ -735,7 +735,7 @@ var _ = Describe("DevWorkspace Controller", func() {
735735

736736
By("Setting failing phase on workspace directly")
737737
Eventually(func() error {
738-
devworkspace = getExistingDevWorkspace()
738+
devworkspace = getExistingDevWorkspace(devWorkspaceName)
739739
devworkspace.Status.Phase = "Failing"
740740
return k8sClient.Status().Update(ctx, devworkspace)
741741
}, timeout, interval).Should(Succeed(), "Should be able to set failing status on DevWorkspace")

controllers/workspace/util_test.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package controllers_test
1515

1616
import (
17+
"fmt"
1718
"path"
1819
"time"
1920

@@ -40,10 +41,10 @@ const (
4041

4142
var clock kubeclock.Clock = &kubeclock.RealClock{}
4243

43-
func createDevWorkspace(fromFile string) {
44+
func createDevWorkspace(name, fromFile string) {
4445
By("Loading DevWorkspace from test file")
4546
devworkspace := &dw.DevWorkspace{}
46-
err := loadObjectFromFile(devWorkspaceName, devworkspace, fromFile)
47+
err := loadObjectFromFile(name, devworkspace, fromFile)
4748
Expect(err).NotTo(HaveOccurred())
4849

4950
By("Creating DevWorkspace on cluster")
@@ -57,9 +58,9 @@ func createDevWorkspace(fromFile string) {
5758
}, 10*time.Second, 250*time.Millisecond).Should(BeTrue())
5859
}
5960

60-
func createStartedDevWorkspace(fromFile string) {
61-
createDevWorkspace(fromFile)
62-
devworkspace := getExistingDevWorkspace()
61+
func createStartedDevWorkspace(name, fromFile string) {
62+
createDevWorkspace(name, fromFile)
63+
devworkspace := getExistingDevWorkspace(name)
6364
workspaceID := devworkspace.Status.DevWorkspaceId
6465

6566
By("Manually making Routing ready to continue")
@@ -78,13 +79,16 @@ func createStartedDevWorkspace(fromFile string) {
7879
}, timeout, interval).Should(Equal(dw.DevWorkspaceStatusRunning), "Workspace did not enter Running phase before timeout")
7980
}
8081

81-
func getExistingDevWorkspace() *dw.DevWorkspace {
82-
By("Getting existing DevWorkspace")
82+
func getExistingDevWorkspace(name string) *dw.DevWorkspace {
83+
By(fmt.Sprintf("Getting existing DevWorkspace %s", name))
8384
devworkspace := &dw.DevWorkspace{}
84-
dwNN := namespacedName(devWorkspaceName, testNamespace)
85-
Expect(k8sClient.Get(ctx, dwNN, devworkspace)).Should(Succeed())
86-
workspaceID := devworkspace.Status.DevWorkspaceId
87-
Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set")
85+
dwNN := namespacedName(name, testNamespace)
86+
Eventually(func() (string, error) {
87+
if err := k8sClient.Get(ctx, dwNN, devworkspace); err != nil {
88+
return "", err
89+
}
90+
return devworkspace.Status.DevWorkspaceId, nil
91+
}, timeout, interval).Should(Not(BeEmpty()))
8892
return devworkspace
8993
}
9094

@@ -94,8 +98,13 @@ func deleteDevWorkspace(name string) {
9498
dw := &dw.DevWorkspace{}
9599
dw.Name = name
96100
dw.Namespace = testNamespace
97-
Expect(k8sClient.Delete(ctx, dw)).Should(Succeed())
98-
err := k8sClient.Get(ctx, dwNN, dw)
101+
// Do nothing if already deleted
102+
err := k8sClient.Delete(ctx, dw)
103+
if k8sErrors.IsNotFound(err) {
104+
return
105+
}
106+
Expect(err).Should(BeNil())
107+
err = k8sClient.Get(ctx, dwNN, dw)
99108
if err != nil {
100109
Expect(k8sErrors.IsNotFound(err)).Should(BeTrue(), "Unexpected error when deleting DevWorkspace: %s", err)
101110
return
@@ -121,7 +130,7 @@ func deleteDevWorkspace(name string) {
121130
Eventually(func() bool {
122131
err := k8sClient.Get(ctx, dwNN, dw)
123132
return err != nil && k8sErrors.IsNotFound(err)
124-
}).Should(BeTrue(), "DevWorkspace not deleted after timeout")
133+
}, 10*time.Second, 250*time.Millisecond).Should(BeTrue(), "DevWorkspace not deleted after timeout")
125134
}
126135

127136
func createObject(obj crclient.Object) {

0 commit comments

Comments
 (0)