github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/apis/apps/v1alpha1/servicedescriptor_webhook_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package v1alpha1 21 22 import ( 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 corev1 "k8s.io/api/core/v1" 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 ) 29 30 var _ = Describe("ServiceDescriptor Webhook", func() { 31 Context("spec validation", func() { 32 var ( 33 name = "test-service-descriptor" 34 namespace = "default" 35 ) 36 var sd *ServiceDescriptor 37 BeforeEach(func() { 38 sd = &ServiceDescriptor{ 39 ObjectMeta: metav1.ObjectMeta{ 40 Name: name, 41 Namespace: namespace, 42 }, 43 Spec: ServiceDescriptorSpec{ 44 ServiceKind: "mock-kind", 45 ServiceVersion: "mock-version", 46 Endpoint: &CredentialVar{ 47 Value: "mock-endpoint", 48 }, 49 Port: &CredentialVar{ 50 Value: "mock-port", 51 }, 52 Auth: &ConnectionCredentialAuth{ 53 Username: &CredentialVar{ 54 Value: "mock-username", 55 }, 56 Password: &CredentialVar{ 57 Value: "mock-password", 58 }, 59 }, 60 }, 61 } 62 }) 63 64 It("should succeed if spec is well defined", func() { 65 Expect(k8sClient.Create(ctx, sd)).Should(Succeed()) 66 Expect(k8sClient.Delete(ctx, sd)).Should(Succeed()) 67 }) 68 69 It("should return an error if endpoint value and valueFrom are not empty", func() { 70 sd.Spec.Endpoint.ValueFrom = &corev1.EnvVarSource{ 71 SecretKeyRef: &corev1.SecretKeySelector{ 72 LocalObjectReference: corev1.LocalObjectReference{ 73 Name: "mock-secret", 74 }, 75 Key: "mock-key", 76 }, 77 } 78 err := k8sClient.Create(ctx, sd) 79 Expect(err).ShouldNot(BeNil()) 80 Expect(err.Error()).Should(ContainSubstring("value and valueFrom cannot be specified at the same time")) 81 }) 82 83 It("should return an error if port value and valueFrom are not empty", func() { 84 sd.Spec.Port.ValueFrom = &corev1.EnvVarSource{ 85 SecretKeyRef: &corev1.SecretKeySelector{ 86 LocalObjectReference: corev1.LocalObjectReference{ 87 Name: "mock-secret", 88 }, 89 Key: "mock-key", 90 }, 91 } 92 err := k8sClient.Create(ctx, sd) 93 Expect(err).ShouldNot(BeNil()) 94 Expect(err.Error()).Should(ContainSubstring("value and valueFrom cannot be specified at the same time")) 95 }) 96 97 It("should return an error if auth username value and valueFrom are not empty", func() { 98 sd.Spec.Auth.Username.ValueFrom = &corev1.EnvVarSource{ 99 SecretKeyRef: &corev1.SecretKeySelector{ 100 LocalObjectReference: corev1.LocalObjectReference{ 101 Name: "mock-secret", 102 }, 103 Key: "mock-key", 104 }, 105 } 106 sd.Spec.Auth.Password.ValueFrom = &corev1.EnvVarSource{ 107 SecretKeyRef: &corev1.SecretKeySelector{ 108 LocalObjectReference: corev1.LocalObjectReference{ 109 Name: "mock-secret", 110 }, 111 Key: "mock-key", 112 }, 113 } 114 err := k8sClient.Create(ctx, sd) 115 Expect(err).ShouldNot(BeNil()) 116 Expect(err.Error()).Should(ContainSubstring("value and valueFrom cannot be specified at the same time")) 117 }) 118 }) 119 })