github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/apis/apps/v1alpha1/clusterversion_webhook_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 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 v1alpha1 18 19 import ( 20 . "github.com/onsi/ginkgo/v2" 21 . "github.com/onsi/gomega" 22 23 corev1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 ) 27 28 var _ = Describe("clusterVersion webhook", func() { 29 var ( 30 randomStr = testCtx.GetRandomStr() 31 clusterDefinitionName = "webhook-mysql-definition-" + randomStr 32 clusterVersionName = "webhook-mysql-clusterversion-" + randomStr 33 ) 34 cleanupObjects := func() { 35 // Add any setup steps that needs to be executed before each test 36 err := k8sClient.DeleteAllOf(ctx, &ClusterVersion{}, client.HasLabels{testCtx.TestObjLabelKey}) 37 Expect(err).NotTo(HaveOccurred()) 38 err = k8sClient.DeleteAllOf(ctx, &ClusterDefinition{}, client.HasLabels{testCtx.TestObjLabelKey}) 39 Expect(err).NotTo(HaveOccurred()) 40 } 41 BeforeEach(func() { 42 // Add any setup steps that needs to be executed before each test 43 cleanupObjects() 44 }) 45 46 AfterEach(func() { 47 // Add any teardown steps that needs to be executed after each test 48 cleanupObjects() 49 }) 50 Context("When clusterVersion create and update", func() { 51 It("Should webhook validate passed", func() { 52 By("By testing create a new clusterVersion when clusterDefinition not exist") 53 clusterVersion := createTestClusterVersionObj(clusterDefinitionName, clusterVersionName) 54 Expect(testCtx.CreateObj(ctx, clusterVersion)).ShouldNot(Succeed()) 55 56 By("By creating a new clusterDefinition") 57 clusterDef, _ := createTestClusterDefinitionObj(clusterDefinitionName) 58 Expect(testCtx.CreateObj(ctx, clusterDef)).Should(Succeed()) 59 60 By("By testing component name is not found in clusterDefinition") 61 clusterVersion.Spec.ComponentVersions[1].ComponentDefRef = "proxy1" 62 Expect(testCtx.CheckedCreateObj(ctx, clusterVersion)).ShouldNot(Succeed()) 63 64 By("By creating an clusterVersion") 65 clusterVersion.Spec.ComponentVersions[1].ComponentDefRef = "proxy" 66 Expect(testCtx.CheckedCreateObj(ctx, clusterVersion)).Should(Succeed()) 67 68 By("By testing create a new clusterVersion with invalid config template") 69 clusterVersionDup := createTestClusterVersionObj(clusterDefinitionName, clusterVersionName+"-for-config") 70 clusterVersionDup.Spec.ComponentVersions[0].ConfigSpecs = []ComponentConfigSpec{ 71 { 72 ComponentTemplateSpec: ComponentTemplateSpec{ 73 Name: "tpl1", 74 TemplateRef: "cm1", 75 VolumeName: "volume1", 76 }, 77 ConfigConstraintRef: "constraint1", 78 }, 79 { 80 ComponentTemplateSpec: ComponentTemplateSpec{ 81 Name: "tpl2", 82 TemplateRef: "cm2", 83 VolumeName: "volume1", 84 }, 85 ConfigConstraintRef: "constraint2", 86 }, 87 } 88 err := testCtx.CreateObj(ctx, clusterVersionDup) 89 Expect(err).ShouldNot(Succeed()) 90 Expect(err.Error()).Should(ContainSubstring("volume[volume1] already existed.")) 91 92 By("By testing update clusterVersion.status") 93 patch := client.MergeFrom(clusterVersion.DeepCopy()) 94 clusterVersion.Status.Message = "Hello, kubeblocks!" 95 Expect(k8sClient.Status().Patch(ctx, clusterVersion, patch)).Should(Succeed()) 96 97 By("By testing update clusterVersion.spec") 98 patch = client.MergeFrom(clusterVersion.DeepCopy()) 99 clusterVersion.Spec.ClusterDefinitionRef = "test1" 100 Expect(k8sClient.Patch(ctx, clusterVersion, patch)).ShouldNot(Succeed()) 101 }) 102 }) 103 }) 104 105 func createTestClusterVersionObj(clusterDefinitionName, clusterVersionName string) *ClusterVersion { 106 clusterVersion := &ClusterVersion{ 107 ObjectMeta: metav1.ObjectMeta{ 108 Name: clusterVersionName, 109 Namespace: "default", 110 }, 111 Spec: ClusterVersionSpec{ 112 ClusterDefinitionRef: clusterDefinitionName, 113 ComponentVersions: []ClusterComponentVersion{ 114 { 115 ComponentDefRef: "replicasets", 116 VersionsCtx: VersionsContext{Containers: []corev1.Container{ 117 {Name: "main"}, 118 }}}, 119 { 120 ComponentDefRef: "proxy", 121 VersionsCtx: VersionsContext{Containers: []corev1.Container{ 122 {Name: "main"}, 123 }}}, 124 }, 125 }, 126 Status: ClusterVersionStatus{}, 127 } 128 return clusterVersion 129 }