Skip to content

Commit 8fef40d

Browse files
committed
Rework controller test utilities to reduce repetition
Extract common test functionality to functions that can be reused to make test code less painfully long. Signed-off-by: Angel Misevski <[email protected]>
1 parent 579484d commit 8fef40d

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

controllers/workspace/devworkspace_controller_test.go

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,12 @@ var _ = Describe("DevWorkspace Controller", func() {
104104
Context("Workspace Objects creation", func() {
105105

106106
BeforeEach(func() {
107-
By("Reading DevWorkspace from testdata file")
108-
devworkspace := &dw.DevWorkspace{}
109-
err := loadObjectFromFile(devWorkspaceName, devworkspace, "test-devworkspace.yaml")
110-
Expect(err).NotTo(HaveOccurred())
111-
112-
By("Creating a new DevWorkspace")
113-
Expect(k8sClient.Create(ctx, devworkspace)).Should(Succeed())
114-
createdDW := &dw.DevWorkspace{}
115-
Eventually(func() bool {
116-
if err := k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, createdDW); err != nil {
117-
return false
118-
}
119-
return createdDW.Status.DevWorkspaceId != ""
120-
}, timeout, interval).Should(BeTrue())
107+
createDevWorkspace("test-devworkspace.yaml")
121108
})
122109

123110
AfterEach(func() {
124111
deleteDevWorkspace(devWorkspaceName)
112+
workspacecontroller.SetupHttpClientsForTesting(getBasicTestHttpClient())
125113
})
126114

127115
It("Creates roles and rolebindings", func() {
@@ -152,11 +140,8 @@ var _ = Describe("DevWorkspace Controller", func() {
152140
})
153141

154142
It("Creates DevWorkspaceRouting", func() {
155-
By("Getting existing DevWorkspace from cluster")
156-
devworkspace := &dw.DevWorkspace{}
157-
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace)).Should(Succeed())
143+
devworkspace := getExistingDevWorkspace()
158144
workspaceID := devworkspace.Status.DevWorkspaceId
159-
Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set")
160145

161146
By("Checking that DevWorkspaceRouting is created")
162147
dwr := &controllerv1alpha1.DevWorkspaceRouting{}
@@ -172,11 +157,8 @@ var _ = Describe("DevWorkspace Controller", func() {
172157
})
173158

174159
It("Syncs Routing mainURL to DevWorkspace", func() {
175-
By("Getting existing DevWorkspace from cluster")
176-
devworkspace := &dw.DevWorkspace{}
177-
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace)).Should(Succeed())
160+
devworkspace := getExistingDevWorkspace()
178161
workspaceID := devworkspace.Status.DevWorkspaceId
179-
Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set")
180162

181163
By("Manually making Routing ready to continue")
182164
markRoutingReady("test-url", common.DevWorkspaceRoutingName(workspaceID))
@@ -191,11 +173,8 @@ var _ = Describe("DevWorkspace Controller", func() {
191173
})
192174

193175
It("Creates workspace metadata configmap", func() {
194-
By("Getting existing DevWorkspace from cluster")
195-
devworkspace := &dw.DevWorkspace{}
196-
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace)).Should(Succeed())
176+
devworkspace := getExistingDevWorkspace()
197177
workspaceID := devworkspace.Status.DevWorkspaceId
198-
Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set")
199178

200179
By("Manually making Routing ready to continue")
201180
markRoutingReady("test-url", common.DevWorkspaceRoutingName(workspaceID))
@@ -224,11 +203,8 @@ var _ = Describe("DevWorkspace Controller", func() {
224203
})
225204

226205
It("Syncs the DevWorkspace ServiceAccount", func() {
227-
By("Getting existing DevWorkspace from cluster")
228-
devworkspace := &dw.DevWorkspace{}
229-
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace)).Should(Succeed())
206+
devworkspace := getExistingDevWorkspace()
230207
workspaceID := devworkspace.Status.DevWorkspaceId
231-
Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set")
232208

233209
By("Manually making Routing ready to continue")
234210
markRoutingReady("test-url", common.DevWorkspaceRoutingName(workspaceID))
@@ -249,11 +225,8 @@ var _ = Describe("DevWorkspace Controller", func() {
249225
})
250226

251227
It("Syncs DevWorkspace Deployment", func() {
252-
By("Getting existing DevWorkspace from cluster")
253-
devworkspace := &dw.DevWorkspace{}
254-
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace)).Should(Succeed())
228+
devworkspace := getExistingDevWorkspace()
255229
workspaceID := devworkspace.Status.DevWorkspaceId
256-
Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set")
257230

258231
By("Manually making Routing ready to continue")
259232
markRoutingReady("test-url", common.DevWorkspaceRoutingName(workspaceID))
@@ -274,12 +247,8 @@ var _ = Describe("DevWorkspace Controller", func() {
274247
})
275248

276249
It("Marks DevWorkspace as Running", func() {
277-
By("Getting existing DevWorkspace from cluster")
278-
devworkspace := &dw.DevWorkspace{}
279-
dwNN := types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}
280-
Expect(k8sClient.Get(ctx, dwNN, devworkspace)).Should(Succeed())
250+
devworkspace := getExistingDevWorkspace()
281251
workspaceID := devworkspace.Status.DevWorkspaceId
282-
Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set")
283252

284253
workspacecontroller.SetupHttpClientsForTesting(&http.Client{
285254
Transport: &testutil.TestRoundTripper{
@@ -298,7 +267,10 @@ var _ = Describe("DevWorkspace Controller", func() {
298267

299268
currDW := &dw.DevWorkspace{}
300269
Eventually(func() (dw.DevWorkspacePhase, error) {
301-
err := k8sClient.Get(ctx, dwNN, currDW)
270+
err := k8sClient.Get(ctx, types.NamespacedName{
271+
Name: devworkspace.Name,
272+
Namespace: devworkspace.Namespace,
273+
}, currDW)
302274
if err != nil {
303275
return "", err
304276
}

controllers/workspace/util_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,42 @@ import (
1818

1919
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2020
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
21+
. "github.com/onsi/ginkgo/v2"
2122
. "github.com/onsi/gomega"
23+
2224
appsv1 "k8s.io/api/apps/v1"
2325
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
2426
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2527
"k8s.io/apimachinery/pkg/types"
2628
)
2729

30+
func createDevWorkspace(fromFile string) {
31+
By("Loading DevWorkspace from test file")
32+
devworkspace := &dw.DevWorkspace{}
33+
err := loadObjectFromFile(devWorkspaceName, devworkspace, fromFile)
34+
Expect(err).NotTo(HaveOccurred())
35+
36+
By("Creating DevWorkspace on cluster")
37+
Expect(k8sClient.Create(ctx, devworkspace)).Should(Succeed())
38+
createdDW := &dw.DevWorkspace{}
39+
Eventually(func() bool {
40+
if err := k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, createdDW); err != nil {
41+
return false
42+
}
43+
return createdDW.Status.DevWorkspaceId != ""
44+
}, 10*time.Second, 250*time.Millisecond).Should(BeTrue())
45+
}
46+
47+
func getExistingDevWorkspace() *dw.DevWorkspace {
48+
By("Getting existing DevWorkspace")
49+
devworkspace := &dw.DevWorkspace{}
50+
dwNN := types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}
51+
Expect(k8sClient.Get(ctx, dwNN, devworkspace)).Should(Succeed())
52+
workspaceID := devworkspace.Status.DevWorkspaceId
53+
Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set")
54+
return devworkspace
55+
}
56+
2857
// deleteDevWorkspace forces a DevWorkspace to be deleted by removing all finalizers
2958
func deleteDevWorkspace(name string) {
3059
dwNN := types.NamespacedName{Name: name, Namespace: testNamespace}

0 commit comments

Comments
 (0)