|
14 | 14 | package controllers_test |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "fmt" |
17 | 18 | "os" |
18 | 19 | "path/filepath" |
19 | 20 | "time" |
20 | 21 |
|
21 | 22 | dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" |
| 23 | + controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" |
| 24 | + "github.com/devfile/devworkspace-operator/pkg/common" |
22 | 25 | "github.com/devfile/devworkspace-operator/pkg/conditions" |
| 26 | + "github.com/devfile/devworkspace-operator/pkg/constants" |
23 | 27 | . "github.com/onsi/ginkgo/v2" |
24 | 28 | . "github.com/onsi/gomega" |
| 29 | + appsv1 "k8s.io/api/apps/v1" |
25 | 30 | corev1 "k8s.io/api/core/v1" |
26 | 31 | "k8s.io/apimachinery/pkg/types" |
27 | 32 | "sigs.k8s.io/controller-runtime/pkg/client" |
@@ -91,6 +96,179 @@ var _ = Describe("DevWorkspace Controller", func() { |
91 | 96 | Expect(startingCondition).ShouldNot(BeNil(), "Should have 'Starting' condition") |
92 | 97 | Expect(startingCondition.Status).Should(Equal(corev1.ConditionTrue), "Starting condition should be 'true'") |
93 | 98 | }) |
| 99 | + }) |
| 100 | + |
| 101 | + Context("Workspace Objects creation", func() { |
| 102 | + |
| 103 | + BeforeEach(func() { |
| 104 | + By("Reading DevWorkspace from testdata file") |
| 105 | + devworkspace := &dw.DevWorkspace{} |
| 106 | + err := loadObjectFromFile(devWorkspaceName, devworkspace, "test-devworkspace.yaml") |
| 107 | + Expect(err).NotTo(HaveOccurred()) |
| 108 | + |
| 109 | + By("Creating a new DevWorkspace") |
| 110 | + Expect(k8sClient.Create(ctx, devworkspace)).Should(Succeed()) |
| 111 | + createdDW := &dw.DevWorkspace{} |
| 112 | + Eventually(func() bool { |
| 113 | + if err := k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, createdDW); err != nil { |
| 114 | + return false |
| 115 | + } |
| 116 | + return createdDW.Status.DevWorkspaceId != "" |
| 117 | + }, timeout, interval).Should(BeTrue()) |
| 118 | + }) |
| 119 | + |
| 120 | + AfterEach(func() { |
| 121 | + deleteDevWorkspace(devWorkspaceName) |
| 122 | + }) |
| 123 | + |
| 124 | + It("Creates roles and rolebindings", func() { |
| 125 | + By("Checking that common role is created") |
| 126 | + dwRole := &rbacv1.Role{} |
| 127 | + Eventually(func() bool { |
| 128 | + if err := k8sClient.Get(ctx, types.NamespacedName{Name: common.WorkspaceRoleName(), Namespace: testNamespace}, dwRole); err != nil { |
| 129 | + return false |
| 130 | + } |
| 131 | + return true |
| 132 | + }, timeout, interval).Should(BeTrue(), "Common Role should be created for DevWorkspace") |
| 133 | + |
| 134 | + By("Checking that common rolebinding is created") |
| 135 | + dwRoleBinding := &rbacv1.RoleBinding{} |
| 136 | + Eventually(func() bool { |
| 137 | + if err := k8sClient.Get(ctx, types.NamespacedName{Name: common.WorkspaceRolebindingName(), Namespace: testNamespace}, dwRoleBinding); err != nil { |
| 138 | + return false |
| 139 | + } |
| 140 | + return true |
| 141 | + }, timeout, interval).Should(BeTrue(), "Common RoleBinding should be created for DevWorkspace") |
| 142 | + Expect(dwRoleBinding.RoleRef.Name).Should(Equal(dwRole.Name), "Rolebinding should refer to DevWorkspace role") |
| 143 | + expectedSubject := rbacv1.Subject{ |
| 144 | + APIGroup: "rbac.authorization.k8s.io", |
| 145 | + Kind: "Group", |
| 146 | + Name: fmt.Sprintf("system:serviceaccounts:%s", testNamespace), |
| 147 | + } |
| 148 | + Expect(dwRoleBinding.Subjects).Should(ContainElement(expectedSubject), "Rolebinding should bind to serviceaccounts in current namespace") |
| 149 | + }) |
| 150 | + |
| 151 | + It("Creates DevWorkspaceRouting", func() { |
| 152 | + By("Getting existing DevWorkspace from cluster") |
| 153 | + devworkspace := &dw.DevWorkspace{} |
| 154 | + Expect(k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace)).Should(Succeed()) |
| 155 | + workspaceID := devworkspace.Status.DevWorkspaceId |
| 156 | + Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set") |
| 157 | + |
| 158 | + By("Checking that DevWorkspaceRouting is created") |
| 159 | + dwr := &controllerv1alpha1.DevWorkspaceRouting{} |
| 160 | + dwrName := common.DevWorkspaceRoutingName(workspaceID) |
| 161 | + Eventually(func() error { |
| 162 | + return k8sClient.Get(ctx, types.NamespacedName{Name: dwrName, Namespace: testNamespace}, dwr) |
| 163 | + }, timeout, interval).Should(Succeed(), "DevWorkspaceRouting is present on cluster") |
| 164 | + |
| 165 | + Expect(string(dwr.Spec.RoutingClass)).Should(Equal(devworkspace.Spec.RoutingClass), "RoutingClass should be propagated to DevWorkspaceRouting") |
| 166 | + expectedOwnerReference := devworkspaceOwnerRef(devworkspace) |
| 167 | + Expect(dwr.OwnerReferences).Should(ContainElement(expectedOwnerReference), "Routing should be owned by DevWorkspace") |
| 168 | + Expect(dwr.Labels[constants.DevWorkspaceIDLabel]).Should(Equal(workspaceID), "Object should be labelled with DevWorkspace ID") |
| 169 | + }) |
| 170 | + |
| 171 | + It("Syncs Routing mainURL to DevWorkspace", func() { |
| 172 | + By("Getting existing DevWorkspace from cluster") |
| 173 | + devworkspace := &dw.DevWorkspace{} |
| 174 | + Expect(k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace)).Should(Succeed()) |
| 175 | + workspaceID := devworkspace.Status.DevWorkspaceId |
| 176 | + Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set") |
| 177 | + |
| 178 | + By("Manually making Routing ready to continue") |
| 179 | + markRoutingReady("test-url", common.DevWorkspaceRoutingName(workspaceID)) |
| 180 | + |
| 181 | + Eventually(func() (string, error) { |
| 182 | + if err := k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace); err != nil { |
| 183 | + return "", err |
| 184 | + } |
| 185 | + return devworkspace.Status.MainUrl, nil |
| 186 | + }, timeout, interval).Should(Equal("test-url")) |
| 187 | + |
| 188 | + }) |
| 189 | + |
| 190 | + It("Creates workspace metadata configmap", func() { |
| 191 | + By("Getting existing DevWorkspace from cluster") |
| 192 | + devworkspace := &dw.DevWorkspace{} |
| 193 | + Expect(k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace)).Should(Succeed()) |
| 194 | + workspaceID := devworkspace.Status.DevWorkspaceId |
| 195 | + Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set") |
| 196 | + |
| 197 | + By("Manually making Routing ready to continue") |
| 198 | + markRoutingReady("test-url", common.DevWorkspaceRoutingName(workspaceID)) |
| 199 | + |
| 200 | + metadataCM := &corev1.ConfigMap{} |
| 201 | + Eventually(func() error { |
| 202 | + cmNN := types.NamespacedName{ |
| 203 | + Name: common.MetadataConfigMapName(workspaceID), |
| 204 | + Namespace: testNamespace, |
| 205 | + } |
| 206 | + return k8sClient.Get(ctx, cmNN, metadataCM) |
| 207 | + }, timeout, interval).Should(Succeed(), "Should create workspace metadata configmap") |
| 208 | + |
| 209 | + // Check that metadata configmap is set up properly |
| 210 | + expectedOwnerReference := devworkspaceOwnerRef(devworkspace) |
| 211 | + Expect(metadataCM.OwnerReferences).Should(ContainElement(expectedOwnerReference), "Metadata configmap should be owned by DevWorkspace") |
| 212 | + Expect(metadataCM.Labels[constants.DevWorkspaceIDLabel]).Should(Equal(workspaceID), "Object should be labelled with DevWorkspace ID") |
| 213 | + |
| 214 | + originalDevfileYaml, originalPresent := metadataCM.Data["original.devworkspace.yaml"] |
| 215 | + Expect(originalPresent).Should(BeTrue(), "Metadata configmap should contain original devfile spec") |
| 216 | + originalDevfile := &dw.DevWorkspaceTemplateSpec{} |
| 217 | + Expect(yaml.Unmarshal([]byte(originalDevfileYaml), originalDevfile)).Should(Succeed()) |
| 218 | + Expect(originalDevfile).Should(Equal(&devworkspace.Spec.Template)) |
| 219 | + _, flattenedPresent := metadataCM.Data["flattened.devworkspace.yaml"] |
| 220 | + Expect(flattenedPresent).Should(BeTrue(), "Metadata configmap should contain flattened devfile spec") |
| 221 | + }) |
| 222 | + |
| 223 | + It("Syncs the DevWorkspace ServiceAccount", func() { |
| 224 | + By("Getting existing DevWorkspace from cluster") |
| 225 | + devworkspace := &dw.DevWorkspace{} |
| 226 | + Expect(k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace)).Should(Succeed()) |
| 227 | + workspaceID := devworkspace.Status.DevWorkspaceId |
| 228 | + Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set") |
| 229 | + |
| 230 | + By("Manually making Routing ready to continue") |
| 231 | + markRoutingReady("test-url", common.DevWorkspaceRoutingName(workspaceID)) |
| 232 | + |
| 233 | + sa := &corev1.ServiceAccount{} |
| 234 | + Eventually(func() error { |
| 235 | + saNN := types.NamespacedName{ |
| 236 | + Name: common.ServiceAccountName(workspaceID), |
| 237 | + Namespace: testNamespace, |
| 238 | + } |
| 239 | + return k8sClient.Get(ctx, saNN, sa) |
| 240 | + }, timeout, interval).Should(Succeed(), "Should create DevWorkspace ServiceAccount") |
| 241 | + |
| 242 | + // Check that SA is set up properly |
| 243 | + expectedOwnerReference := devworkspaceOwnerRef(devworkspace) |
| 244 | + Expect(sa.OwnerReferences).Should(ContainElement(expectedOwnerReference), "DevWorkspace ServiceAccount should be owned by DevWorkspace") |
| 245 | + Expect(sa.Labels[constants.DevWorkspaceIDLabel]).Should(Equal(workspaceID), "Object should be labelled with DevWorkspace ID") |
| 246 | + }) |
| 247 | + |
| 248 | + It("Syncs DevWorkspace Deployment", func() { |
| 249 | + By("Getting existing DevWorkspace from cluster") |
| 250 | + devworkspace := &dw.DevWorkspace{} |
| 251 | + Expect(k8sClient.Get(ctx, types.NamespacedName{Name: devWorkspaceName, Namespace: testNamespace}, devworkspace)).Should(Succeed()) |
| 252 | + workspaceID := devworkspace.Status.DevWorkspaceId |
| 253 | + Expect(workspaceID).ShouldNot(BeEmpty(), "DevWorkspaceID not set") |
| 254 | + |
| 255 | + By("Manually making Routing ready to continue") |
| 256 | + markRoutingReady("test-url", common.DevWorkspaceRoutingName(workspaceID)) |
| 257 | + |
| 258 | + deploy := &appsv1.Deployment{} |
| 259 | + Eventually(func() error { |
| 260 | + deployNN := types.NamespacedName{ |
| 261 | + Name: common.DeploymentName(workspaceID), |
| 262 | + Namespace: testNamespace, |
| 263 | + } |
| 264 | + return k8sClient.Get(ctx, deployNN, deploy) |
| 265 | + }, timeout, interval).Should(Succeed(), "Should create DevWorkspace Deployment") |
| 266 | + |
| 267 | + // Check that Deployment is set up properly |
| 268 | + expectedOwnerReference := devworkspaceOwnerRef(devworkspace) |
| 269 | + Expect(deploy.OwnerReferences).Should(ContainElement(expectedOwnerReference), "DevWorkspace Deployment should be owned by DevWorkspace") |
| 270 | + Expect(deploy.Labels[constants.DevWorkspaceIDLabel]).Should(Equal(workspaceID), "Object should be labelled with DevWorkspace ID") |
| 271 | + }) |
94 | 272 |
|
95 | 273 | }) |
96 | 274 | }) |
0 commit comments