|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package api |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "net/http" |
| 23 | + "net/http/httptest" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/julienschmidt/httprouter" |
| 27 | + kubefloworgv1beta1 "github.com/kubeflow/notebooks/workspaces/controller/api/v1beta1" |
| 28 | + . "github.com/onsi/ginkgo/v2" |
| 29 | + . "github.com/onsi/gomega" |
| 30 | + corev1 "k8s.io/api/core/v1" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | + "k8s.io/utils/ptr" |
| 34 | +) |
| 35 | + |
| 36 | +var _ = Describe("Workspace Actions Handler", func() { |
| 37 | + |
| 38 | + // NOTE: the tests in this context work on the same resources, they must be run in order. |
| 39 | + // also, they assume a specific state of the cluster, so cannot be run in parallel with other tests. |
| 40 | + // therefore, we run them using the `Ordered` and `Serial` Ginkgo decorators. |
| 41 | + Context("with existing Workspaces", Serial, Ordered, func() { |
| 42 | + |
| 43 | + const namespaceName1 = "ws-ops-ns1" |
| 44 | + |
| 45 | + var ( |
| 46 | + workspaceName1 string |
| 47 | + workspaceKey1 types.NamespacedName |
| 48 | + workspaceKindName string |
| 49 | + ) |
| 50 | + |
| 51 | + BeforeAll(func() { |
| 52 | + uniqueName := "ws-ops-test" |
| 53 | + workspaceName1 = fmt.Sprintf("workspace-1-%s", uniqueName) |
| 54 | + workspaceKey1 = types.NamespacedName{Name: workspaceName1, Namespace: namespaceName1} |
| 55 | + workspaceKindName = fmt.Sprintf("workspacekind-%s", uniqueName) |
| 56 | + |
| 57 | + By("creating Namespace 1") |
| 58 | + namespace1 := &corev1.Namespace{ |
| 59 | + ObjectMeta: metav1.ObjectMeta{ |
| 60 | + Name: namespaceName1, |
| 61 | + }, |
| 62 | + } |
| 63 | + Expect(k8sClient.Create(ctx, namespace1)).To(Succeed()) |
| 64 | + |
| 65 | + By("creating a WorkspaceKind") |
| 66 | + workspaceKind := NewExampleWorkspaceKind(workspaceKindName) |
| 67 | + Expect(k8sClient.Create(ctx, workspaceKind)).To(Succeed()) |
| 68 | + |
| 69 | + By("creating Workspace 1 in Namespace 1") |
| 70 | + workspace1 := NewExampleWorkspace(workspaceName1, namespaceName1, workspaceKindName) |
| 71 | + Expect(k8sClient.Create(ctx, workspace1)).To(Succeed()) |
| 72 | + }) |
| 73 | + |
| 74 | + AfterAll(func() { |
| 75 | + By("deleting Workspace 1 from Namespace 1") |
| 76 | + workspace1 := &kubefloworgv1beta1.Workspace{ |
| 77 | + ObjectMeta: metav1.ObjectMeta{ |
| 78 | + Name: workspaceName1, |
| 79 | + Namespace: namespaceName1, |
| 80 | + }, |
| 81 | + } |
| 82 | + Expect(k8sClient.Delete(ctx, workspace1)).To(Succeed()) |
| 83 | + |
| 84 | + By("deleting WorkspaceKind") |
| 85 | + workspaceKind := &kubefloworgv1beta1.WorkspaceKind{ |
| 86 | + ObjectMeta: metav1.ObjectMeta{ |
| 87 | + Name: workspaceKindName, |
| 88 | + }, |
| 89 | + } |
| 90 | + Expect(k8sClient.Delete(ctx, workspaceKind)).To(Succeed()) |
| 91 | + |
| 92 | + By("deleting Namespace 1") |
| 93 | + namespace1 := &corev1.Namespace{ |
| 94 | + ObjectMeta: metav1.ObjectMeta{ |
| 95 | + Name: namespaceName1, |
| 96 | + }, |
| 97 | + } |
| 98 | + Expect(k8sClient.Delete(ctx, namespace1)).To(Succeed()) |
| 99 | + }) |
| 100 | + |
| 101 | + It("should pause a workspace successfully", func() { |
| 102 | + By("creating the HTTP request") |
| 103 | + path := strings.Replace(PauseWorkspacePath, ":"+NamespacePathParam, namespaceName1, 1) |
| 104 | + path = strings.Replace(path, ":"+ResourceNamePathParam, workspaceName1, 1) |
| 105 | + req, err := http.NewRequest(http.MethodPost, path, http.NoBody) |
| 106 | + Expect(err).NotTo(HaveOccurred()) |
| 107 | + |
| 108 | + By("setting the auth headers") |
| 109 | + req.Header.Set(userIdHeader, adminUser) |
| 110 | + req.Header.Set("Content-Type", "application/merge-patch+json") |
| 111 | + |
| 112 | + By("executing PauseWorkspaceHandler") |
| 113 | + ps := httprouter.Params{ |
| 114 | + httprouter.Param{Key: NamespacePathParam, Value: namespaceName1}, |
| 115 | + httprouter.Param{Key: ResourceNamePathParam, Value: workspaceName1}, |
| 116 | + } |
| 117 | + rr := httptest.NewRecorder() |
| 118 | + a.PauseWorkspaceHandler(rr, req, ps) |
| 119 | + rs := rr.Result() |
| 120 | + defer rs.Body.Close() |
| 121 | + |
| 122 | + By("verifying the HTTP response status code") |
| 123 | + Expect(rs.StatusCode).To(Equal(http.StatusOK), descUnexpectedHTTPStatus, rr.Body.String()) |
| 124 | + |
| 125 | + By("reading the HTTP response body") |
| 126 | + body, err := io.ReadAll(rs.Body) |
| 127 | + Expect(err).NotTo(HaveOccurred()) |
| 128 | + |
| 129 | + By("verifying the response is an empty JSON object") |
| 130 | + Expect(string(body)).To(Equal("{}\n")) |
| 131 | + |
| 132 | + By("getting the Workspace from the Kubernetes API") |
| 133 | + workspace := &kubefloworgv1beta1.Workspace{} |
| 134 | + Expect(k8sClient.Get(ctx, workspaceKey1, workspace)).To(Succeed()) |
| 135 | + |
| 136 | + By("ensuring the workspace is paused") |
| 137 | + Expect(workspace.Spec.Paused).To(Equal(ptr.To(true))) |
| 138 | + }) |
| 139 | + |
| 140 | + It("should return 404 for a non-existent workspace", func() { |
| 141 | + missingWorkspaceName := "non-existent-workspace" |
| 142 | + |
| 143 | + By("creating the HTTP request") |
| 144 | + path := strings.Replace(PauseWorkspacePath, ":"+NamespacePathParam, namespaceName1, 1) |
| 145 | + path = strings.Replace(path, ":"+ResourceNamePathParam, missingWorkspaceName, 1) |
| 146 | + req, err := http.NewRequest(http.MethodPost, path, http.NoBody) |
| 147 | + Expect(err).NotTo(HaveOccurred()) |
| 148 | + |
| 149 | + By("setting the auth headers") |
| 150 | + req.Header.Set(userIdHeader, adminUser) |
| 151 | + |
| 152 | + By("executing PauseWorkspaceHandler") |
| 153 | + ps := httprouter.Params{ |
| 154 | + httprouter.Param{Key: NamespacePathParam, Value: namespaceName1}, |
| 155 | + httprouter.Param{Key: ResourceNamePathParam, Value: missingWorkspaceName}, |
| 156 | + } |
| 157 | + rr := httptest.NewRecorder() |
| 158 | + a.PauseWorkspaceHandler(rr, req, ps) |
| 159 | + rs := rr.Result() |
| 160 | + defer rs.Body.Close() |
| 161 | + |
| 162 | + By("verifying the HTTP response status code") |
| 163 | + Expect(rs.StatusCode).To(Equal(http.StatusNotFound), descUnexpectedHTTPStatus, rr.Body.String()) |
| 164 | + }) |
| 165 | + }) |
| 166 | +}) |
0 commit comments