|
| 1 | +// Copyright (c) 2019-2025 Red Hat, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +package tests |
| 14 | + |
| 15 | +import ( |
| 16 | + "fmt" |
| 17 | + |
| 18 | + dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" |
| 19 | + "github.com/devfile/devworkspace-operator/test/e2e/pkg/config" |
| 20 | + "github.com/onsi/ginkgo/v2" |
| 21 | + "github.com/onsi/gomega" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = ginkgo.Describe("[Create DevWorkspace and ensure data is persisted during restarts]", func() { |
| 25 | + defer ginkgo.GinkgoRecover() |
| 26 | + |
| 27 | + ginkgo.It("Wait DevWorkspace Webhook Server Pod", func() { |
| 28 | + controllerLabel := "app.kubernetes.io/name=devworkspace-webhook-server" |
| 29 | + |
| 30 | + deploy, err := config.AdminK8sClient.WaitForPodRunningByLabel(config.OperatorNamespace, controllerLabel) |
| 31 | + if err != nil { |
| 32 | + ginkgo.Fail(fmt.Sprintf("cannot get the Pod status with label %s: %s", controllerLabel, err.Error())) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if !deploy { |
| 37 | + ginkgo.Fail("Devworkspace webhook didn't start properly") |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + ginkgo.It("Add DevWorkspace to cluster and wait running status", func() { |
| 42 | + commandResult, err := config.DevK8sClient.OcApplyWorkspace(config.DevWorkspaceNamespace, "test/resources/simple-devworkspace-with-project-clone.yaml") |
| 43 | + if err != nil { |
| 44 | + ginkgo.Fail(fmt.Sprintf("Failed to create DevWorkspace: %s %s", err.Error(), commandResult)) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + deploy, err := config.DevK8sClient.WaitDevWsStatus("code-latest", config.DevWorkspaceNamespace, dw.DevWorkspaceStatusRunning) |
| 49 | + if !deploy { |
| 50 | + ginkgo.Fail(fmt.Sprintf("DevWorkspace didn't start properly. Error: %s", err)) |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + var podName string |
| 55 | + ginkgo.It("Check that project-clone succeeded as expected", func() { |
| 56 | + podSelector := "controller.devfile.io/devworkspace_name=code-latest" |
| 57 | + var err error |
| 58 | + podName, err = config.AdminK8sClient.GetPodNameBySelector(podSelector, config.DevWorkspaceNamespace) |
| 59 | + if err != nil { |
| 60 | + ginkgo.Fail(fmt.Sprintf("Can get devworkspace pod by selector. Error: %s", err)) |
| 61 | + } |
| 62 | + resultOfExecCommand, err := config.AdminK8sClient.GetLogsForContainer(podName, config.DevWorkspaceNamespace, "project-clone") |
| 63 | + if err != nil { |
| 64 | + ginkgo.Fail(fmt.Sprintf("Cannot get logs for project-clone container. Error: `%s`. Exec output: `%s`", err, resultOfExecCommand)) |
| 65 | + } |
| 66 | + gomega.Expect(resultOfExecCommand).To(gomega.ContainSubstring("Cloning project web-nodejs-sample to /projects")) |
| 67 | + }) |
| 68 | + |
| 69 | + ginkgo.It("Make some changes in DevWorkspace dev container", func() { |
| 70 | + resultOfExecCommand, err := config.AdminK8sClient.ExecCommandInContainer(podName, config.DevWorkspaceNamespace, "dev", "bash -c 'echo \"## Modified via e2e test\" >> /projects/web-nodejs-sample/README.md'") |
| 71 | + if err != nil { |
| 72 | + ginkgo.Fail(fmt.Sprintf("failed to make changes to DevWorkspace container, returned: %s", resultOfExecCommand)) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + ginkgo.It("Stop DevWorkspace", func() { |
| 77 | + err := config.AdminK8sClient.UpdateDevWorkspaceStarted("code-latest", config.DevWorkspaceNamespace, false) |
| 78 | + if err != nil { |
| 79 | + ginkgo.Fail(fmt.Sprintf("failed to stop DevWorkspace container, returned: %s", err)) |
| 80 | + } |
| 81 | + deploy, err := config.DevK8sClient.WaitDevWsStatus("code-latest", config.DevWorkspaceNamespace, dw.DevWorkspaceStatusStopped) |
| 82 | + if !deploy { |
| 83 | + ginkgo.Fail(fmt.Sprintf("DevWorkspace didn't start properly. Error: %s", err)) |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + ginkgo.It("Start DevWorkspace", func() { |
| 88 | + err := config.AdminK8sClient.UpdateDevWorkspaceStarted("code-latest", config.DevWorkspaceNamespace, true) |
| 89 | + if err != nil { |
| 90 | + ginkgo.Fail(fmt.Sprintf("failed to start DevWorkspace container")) |
| 91 | + } |
| 92 | + deploy, err := config.DevK8sClient.WaitDevWsStatus("code-latest", config.DevWorkspaceNamespace, dw.DevWorkspaceStatusRunning) |
| 93 | + if !deploy { |
| 94 | + ginkgo.Fail(fmt.Sprintf("DevWorkspace didn't start properly. Error: %s", err)) |
| 95 | + } |
| 96 | + }) |
| 97 | + |
| 98 | + ginkgo.It("Verify changes persist after DevWorkspace restart", func() { |
| 99 | + podSelector := "controller.devfile.io/devworkspace_name=code-latest" |
| 100 | + podName, err := config.AdminK8sClient.GetPodNameBySelector(podSelector, config.DevWorkspaceNamespace) |
| 101 | + if err != nil { |
| 102 | + ginkgo.Fail(fmt.Sprintf("Can get devworkspace pod by selector. Error: %s", err)) |
| 103 | + } |
| 104 | + err = config.AdminK8sClient.WaitForPodContainerToReady(config.DevWorkspaceNamespace, podName, "dev") |
| 105 | + if err != nil { |
| 106 | + ginkgo.Fail(fmt.Sprintf("failed waiting for DevWorkspace container 'dev' to become ready. Error: %s", err)) |
| 107 | + } |
| 108 | + resultOfExecCommand, err := config.AdminK8sClient.ExecCommandInContainer(podName, config.DevWorkspaceNamespace, "dev", "bash -c 'cat /projects/web-nodejs-sample/README.md'") |
| 109 | + if err != nil { |
| 110 | + ginkgo.Fail(fmt.Sprintf("failed to verify to DevWorkspace container, returned: %s", resultOfExecCommand)) |
| 111 | + } |
| 112 | + gomega.Expect(resultOfExecCommand).To(gomega.ContainSubstring("## Modified via e2e test")) |
| 113 | + }) |
| 114 | + |
| 115 | + ginkgo.It("Check that project-clone logs mention project already cloned", func() { |
| 116 | + resultOfExecCommand, err := config.AdminK8sClient.GetLogsForContainer(podName, config.DevWorkspaceNamespace, "project-clone") |
| 117 | + if err != nil { |
| 118 | + ginkgo.Fail(fmt.Sprintf("Cannot get logs for project-clone container. Error: `%s`. Exec output: `%s`", err, resultOfExecCommand)) |
| 119 | + } |
| 120 | + gomega.Expect(resultOfExecCommand).To(gomega.ContainSubstring("Project 'web-nodejs-sample' is already cloned and has all remotes configured")) |
| 121 | + }) |
| 122 | +}) |
0 commit comments