|
| 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 workspacekinds_test |
| 18 | + |
| 19 | +import ( |
| 20 | + kubefloworgv1beta1 "github.com/kubeflow/notebooks/workspaces/controller/api/v1beta1" |
| 21 | + . "github.com/onsi/ginkgo/v2" |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/utils/ptr" |
| 25 | + |
| 26 | + workspacekinds "github.com/kubeflow/notebooks/workspaces/backend/internal/models/workspacekinds" |
| 27 | +) |
| 28 | + |
| 29 | +var _ = Describe("WorkspaceKind Types", func() { |
| 30 | + Describe("NewWorkspaceKindModelFromWorkspaceKind", func() { |
| 31 | + Context("with complete WorkspaceKind", func() { |
| 32 | + It("should create a complete WorkspaceKind model with all fields", func() { |
| 33 | + wsk := &kubefloworgv1beta1.WorkspaceKind{ |
| 34 | + ObjectMeta: metav1.ObjectMeta{ |
| 35 | + Name: "test-workspacekind", |
| 36 | + Namespace: "kubeflow", |
| 37 | + Labels: map[string]string{ |
| 38 | + "app": "workspacekind", |
| 39 | + "version": "v1", |
| 40 | + }, |
| 41 | + Annotations: map[string]string{ |
| 42 | + "description": "Complete workspacekind", |
| 43 | + }, |
| 44 | + }, |
| 45 | + Spec: kubefloworgv1beta1.WorkspaceKindSpec{ |
| 46 | + Spawner: kubefloworgv1beta1.WorkspaceKindSpawner{ |
| 47 | + DisplayName: "Complete Workspacekind", |
| 48 | + Description: "A complete test workspacekind with all features", |
| 49 | + Deprecated: ptr.To(false), |
| 50 | + Hidden: ptr.To(false), |
| 51 | + Icon: kubefloworgv1beta1.WorkspaceKindIcon{ |
| 52 | + Url: ptr.To("/workspaces/backend/api/v1/workspacekinds/test-workspacekind/assets/icon"), |
| 53 | + }, |
| 54 | + Logo: kubefloworgv1beta1.WorkspaceKindIcon{ |
| 55 | + Url: ptr.To("/workspaces/backend/api/v1/workspacekinds/test-workspacekind/assets/logo"), |
| 56 | + }, |
| 57 | + }, |
| 58 | + PodTemplate: kubefloworgv1beta1.WorkspaceKindPodTemplate{ |
| 59 | + PodMetadata: &kubefloworgv1beta1.WorkspaceKindPodMetadata{ |
| 60 | + Labels: map[string]string{ |
| 61 | + "app": "test-workspacekind", |
| 62 | + }, |
| 63 | + Annotations: map[string]string{ |
| 64 | + "annotation-key": "annotation-value", |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + result := workspacekinds.NewWorkspaceKindModelFromWorkspaceKind(wsk) |
| 72 | + |
| 73 | + Expect(result).ToNot(BeNil()) |
| 74 | + Expect(result.Name).To(Equal("test-workspacekind")) |
| 75 | + Expect(result.DisplayName).To(Equal("Complete Workspacekind")) |
| 76 | + Expect(result.Description).To(Equal("A complete test workspacekind with all features")) |
| 77 | + Expect(result.Deprecated).To(BeFalse()) |
| 78 | + Expect(result.DeprecationMessage).To(Equal("")) |
| 79 | + Expect(result.Hidden).To(BeFalse()) |
| 80 | + Expect(result.Icon.URL).To(Equal("/workspaces/backend/api/v1/workspacekinds/test-workspacekind/assets/icon")) |
| 81 | + Expect(result.Logo.URL).To(Equal("/workspaces/backend/api/v1/workspacekinds/test-workspacekind/assets/logo")) |
| 82 | + Expect(result.PodTemplate.PodMetadata.Labels).To(HaveKeyWithValue("app", "test-workspacekind")) |
| 83 | + Expect(result.PodTemplate.PodMetadata.Annotations).To(HaveKeyWithValue("annotation-key", "annotation-value")) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + Context("with empty strings in required fields", func() { |
| 88 | + It("should handle empty display name and description", func() { |
| 89 | + wsk := &kubefloworgv1beta1.WorkspaceKind{ |
| 90 | + ObjectMeta: metav1.ObjectMeta{ |
| 91 | + Name: "empty-fields-workspace", |
| 92 | + }, |
| 93 | + Spec: kubefloworgv1beta1.WorkspaceKindSpec{ |
| 94 | + Spawner: kubefloworgv1beta1.WorkspaceKindSpawner{ |
| 95 | + DisplayName: "", |
| 96 | + Description: "", |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + result := workspacekinds.NewWorkspaceKindModelFromWorkspaceKind(wsk) |
| 102 | + |
| 103 | + Expect(result).ToNot(BeNil()) |
| 104 | + Expect(result.Name).To(Equal("empty-fields-workspace")) |
| 105 | + Expect(result.DisplayName).To(Equal("")) |
| 106 | + Expect(result.Description).To(Equal("")) |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + Context("with special characters in names", func() { |
| 111 | + It("should handle names with hyphens and underscores", func() { |
| 112 | + wsk := &kubefloworgv1beta1.WorkspaceKind{ |
| 113 | + ObjectMeta: metav1.ObjectMeta{ |
| 114 | + Name: "special-chars_workspace-123", |
| 115 | + }, |
| 116 | + Spec: kubefloworgv1beta1.WorkspaceKindSpec{ |
| 117 | + Spawner: kubefloworgv1beta1.WorkspaceKindSpawner{ |
| 118 | + DisplayName: "Special-Chars_Workspace 123!", |
| 119 | + Description: "Workspace with special characters in name", |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + result := workspacekinds.NewWorkspaceKindModelFromWorkspaceKind(wsk) |
| 125 | + |
| 126 | + Expect(result).ToNot(BeNil()) |
| 127 | + Expect(result.Name).To(Equal("special-chars_workspace-123")) |
| 128 | + Expect(result.DisplayName).To(Equal("Special-Chars_Workspace 123!")) |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + Context("with long descriptions", func() { |
| 133 | + It("should handle very long descriptions", func() { |
| 134 | + longDescription := "This is a very long description that contains multiple sentences. " + |
| 135 | + "It describes a workspace kind that has many features and capabilities. " + |
| 136 | + "The description should be preserved exactly as provided without truncation. " + |
| 137 | + "This tests the ability to handle larger text fields in the workspace kind model." |
| 138 | + |
| 139 | + wsk := &kubefloworgv1beta1.WorkspaceKind{ |
| 140 | + ObjectMeta: metav1.ObjectMeta{ |
| 141 | + Name: "long-description-workspace", |
| 142 | + }, |
| 143 | + Spec: kubefloworgv1beta1.WorkspaceKindSpec{ |
| 144 | + Spawner: kubefloworgv1beta1.WorkspaceKindSpawner{ |
| 145 | + DisplayName: "Long Description Workspace", |
| 146 | + Description: longDescription, |
| 147 | + }, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + result := workspacekinds.NewWorkspaceKindModelFromWorkspaceKind(wsk) |
| 152 | + |
| 153 | + Expect(result).ToNot(BeNil()) |
| 154 | + Expect(result.Description).To(Equal(longDescription)) |
| 155 | + Expect(len(result.Description)).To(BeNumerically(">", 200)) |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + }) |
| 160 | +}) |
0 commit comments