Skip to content

Commit 84684ac

Browse files
committed
Add tests to cover overriding autogenerated workspace IDs
Signed-off-by: Angel Misevski <[email protected]>
1 parent 2027ed3 commit 84684ac

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

controllers/workspace/devworkspace_controller_test.go

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"net/http"
1919
"os"
2020
"path/filepath"
21-
"time"
2221

2322
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2423
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
@@ -53,11 +52,6 @@ func loadObjectFromFile(objName string, obj client.Object, filename string) erro
5352
}
5453

5554
var _ = Describe("DevWorkspace Controller", func() {
56-
const (
57-
timeout = 10 * time.Second
58-
interval = 250 * time.Millisecond
59-
)
60-
6155
Context("Basic DevWorkspace Tests", func() {
6256
It("Sets DevWorkspace ID and Starting status", func() {
6357
By("Reading DevWorkspace from testdata file")
@@ -99,6 +93,86 @@ var _ = Describe("DevWorkspace Controller", func() {
9993
Expect(startingCondition).ShouldNot(BeNil(), "Should have 'Starting' condition")
10094
Expect(startingCondition.Status).Should(Equal(corev1.ConditionTrue), "Starting condition should be 'true'")
10195
})
96+
97+
It("Allows overriding the DevWorkspace ID", func() {
98+
By("Reading DevWorkspace from testdata file")
99+
devworkspace := &dw.DevWorkspace{}
100+
err := loadObjectFromFile(devWorkspaceName, devworkspace, "test-devworkspace.yaml")
101+
Expect(err).NotTo(HaveOccurred())
102+
103+
if devworkspace.Annotations == nil {
104+
devworkspace.Annotations = map[string]string{}
105+
}
106+
devworkspace.Annotations[constants.WorkspaceIdOverrideAnnotation] = "test-workspace-id"
107+
108+
By("Creating a new DevWorkspace")
109+
Expect(k8sClient.Create(ctx, devworkspace)).Should(Succeed())
110+
dwNamespacedName := types.NamespacedName{
111+
Namespace: testNamespace,
112+
Name: devWorkspaceName,
113+
}
114+
defer deleteDevWorkspace(devWorkspaceName)
115+
116+
createdDW := &dw.DevWorkspace{}
117+
Eventually(func() bool {
118+
err := k8sClient.Get(ctx, dwNamespacedName, createdDW)
119+
return err == nil
120+
}, timeout, interval).Should(BeTrue(), "DevWorkspace should exist in cluster")
121+
122+
By("Checking DevWorkspace ID has been set")
123+
Eventually(func() (devworkspaceID string, err error) {
124+
if err := k8sClient.Get(ctx, dwNamespacedName, createdDW); err != nil {
125+
return "", err
126+
}
127+
return createdDW.Status.DevWorkspaceId, nil
128+
}, timeout, interval).Should(Not(Equal("")), "Should set DevWorkspace ID after creation")
129+
Expect(createdDW.Status.DevWorkspaceId).Should(Equal("test-workspace-id"), "DevWorkspace ID should be set from override annotation")
130+
})
131+
132+
It("Forbids duplicate workspace IDs from override", func() {
133+
By("Reading DevWorkspace from testdata file")
134+
devworkspace := &dw.DevWorkspace{}
135+
err := loadObjectFromFile(devWorkspaceName, devworkspace, "test-devworkspace.yaml")
136+
Expect(err).NotTo(HaveOccurred())
137+
138+
if devworkspace.Annotations == nil {
139+
devworkspace.Annotations = map[string]string{}
140+
}
141+
devworkspace.Annotations[constants.WorkspaceIdOverrideAnnotation] = "test-workspace-id"
142+
143+
devworkspace2 := devworkspace.DeepCopy()
144+
devworkspace2.Name = fmt.Sprintf("%s-dupe", devworkspace2.Name)
145+
146+
By("Creating a new DevWorkspace")
147+
Expect(k8sClient.Create(ctx, devworkspace)).Should(Succeed())
148+
dwNamespacedName := types.NamespacedName{
149+
Namespace: testNamespace,
150+
Name: devWorkspaceName,
151+
}
152+
defer deleteDevWorkspace(devWorkspaceName)
153+
154+
createdDW := &dw.DevWorkspace{}
155+
Eventually(func() bool {
156+
err := k8sClient.Get(ctx, dwNamespacedName, createdDW)
157+
return err == nil
158+
}, timeout, interval).Should(BeTrue(), "DevWorkspace should exist in cluster")
159+
160+
By("Creating a DevWorkspace that duplicates the workspace ID of the first")
161+
Expect(k8sClient.Create(ctx, devworkspace2)).Should(Succeed())
162+
defer deleteDevWorkspace(devworkspace2.Name)
163+
164+
By("Checking that duplicate DevWorkspace enters failed Phase")
165+
createdDW2 := &dw.DevWorkspace{}
166+
Eventually(func() (phase dw.DevWorkspacePhase, err error) {
167+
if err := k8sClient.Get(ctx, types.NamespacedName{
168+
Name: devworkspace2.Name,
169+
Namespace: testNamespace,
170+
}, createdDW2); err != nil {
171+
return "", err
172+
}
173+
return createdDW2.Status.Phase, nil
174+
}, timeout, interval).Should(Equal(dw.DevWorkspaceStatusFailed), "DevWorkspace with duplicate ID should fail to start")
175+
})
102176
})
103177

104178
Context("Workspace Objects creation", func() {

controllers/workspace/util_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ import (
3232
"k8s.io/apimachinery/pkg/types"
3333
)
3434

35+
const (
36+
timeout = 10 * time.Second
37+
interval = 250 * time.Millisecond
38+
)
39+
3540
func createDevWorkspace(fromFile string) {
3641
By("Loading DevWorkspace from test file")
3742
devworkspace := &dw.DevWorkspace{}

0 commit comments

Comments
 (0)