github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/component/component_fieldref_util_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 component 21 22 import ( 23 "fmt" 24 "strings" 25 26 . "github.com/onsi/ginkgo/v2" 27 . "github.com/onsi/gomega" 28 29 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 30 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 31 ) 32 33 var _ = Describe("ComponentRef Fields Tests", func() { 34 const clusterDefName = "test-clusterdef" 35 const clusterName = "test-cluster" 36 const clusterVersionName = "test-clusterversion" 37 const clusterNamespace = "test-compref" 38 const mysqlCompDefName = "mysql-def" 39 const referredCompDefName = "maxscale-def" 40 41 const mysqlCompName = "mysql" 42 const referredCompName = "maxscale" 43 44 Context("test fieldRef", func() { 45 var clusterDefBuilder *testapps.MockClusterDefFactory 46 var clusterBuilder *testapps.MockClusterFactory 47 48 BeforeEach(func() { 49 By("create cluster definition builder") 50 clusterDefBuilder = testapps.NewClusterDefFactory(clusterDefName). 51 AddComponentDef(testapps.StatefulMySQLComponent, mysqlCompDefName). 52 AddComponentDef(testapps.StatefulMySQLComponent, referredCompDefName) 53 54 // add one mysql component 55 clusterDefBuilder = clusterDefBuilder.AddComponentDef(testapps.StatefulMySQLComponent, mysqlCompDefName) 56 57 By("create cluste builder") 58 clusterBuilder = testapps.NewClusterFactory(clusterNamespace, clusterName, clusterDefName, clusterVersionName) 59 }) 60 61 It("test fieldref", func() { 62 clusterDef := clusterDefBuilder.GetObject() 63 64 By("add one component to cluster") 65 clusterBuilder = clusterBuilder.AddComponent(mysqlCompName, mysqlCompDefName).AddComponent(referredCompName, referredCompDefName) 66 cluster := clusterBuilder.GetObject() 67 68 componentDef := clusterDef.GetComponentDefByName(referredCompDefName) 69 Expect(componentDef).NotTo(BeNil()) 70 components := cluster.Spec.GetDefNameMappingComponents()[referredCompDefName] 71 Expect(len(components)).To(Equal(1)) 72 73 By("lookup component name, should success") 74 valueFrom := &appsv1alpha1.ComponentValueFrom{ 75 Type: appsv1alpha1.FromFieldRef, 76 FieldPath: "$.components[0].name", 77 } 78 value, err := resolveFieldRef(valueFrom, components, componentDef) 79 Expect(err).To(BeNil()) 80 Expect(value).To(Equal(referredCompName)) 81 82 By("lookup componentSpec name, should success") 83 valueFrom = &appsv1alpha1.ComponentValueFrom{ 84 Type: appsv1alpha1.FromFieldRef, 85 FieldPath: "$.componentDef.name", 86 } 87 value, err = resolveFieldRef(valueFrom, components, componentDef) 88 Expect(err).To(BeNil()) 89 Expect(value).To(Equal(referredCompDefName)) 90 91 By("invalid json path, should fail") 92 valueFrom = &appsv1alpha1.ComponentValueFrom{ 93 Type: appsv1alpha1.FromFieldRef, 94 FieldPath: "$.invalidField.name", 95 } 96 _, err = resolveFieldRef(valueFrom, components, componentDef) 97 Expect(err).ShouldNot(BeNil()) 98 }) 99 100 It("test invalid serviceRef with no service", func() { 101 clusterDef := clusterDefBuilder.GetObject() 102 103 By("add one component to cluster") 104 clusterBuilder = clusterBuilder.AddComponent(mysqlCompName, mysqlCompDefName).AddComponent(referredCompName, referredCompDefName) 105 cluster := clusterBuilder.GetObject() 106 107 componentDef := clusterDef.GetComponentDefByName(referredCompDefName) 108 Expect(componentDef).NotTo(BeNil()) 109 components := cluster.Spec.GetDefNameMappingComponents()[referredCompDefName] 110 Expect(len(components)).To(Equal(1)) 111 112 By("lookup service name, should fail") 113 _, err := resolveServiceRef(cluster.Name, components, componentDef) 114 if componentDef.Service != nil { 115 Expect(err).To(BeNil()) 116 } else { 117 Expect(err).NotTo(BeNil()) 118 Expect(err.Error()).To(ContainSubstring("does not have service")) 119 } 120 }) 121 122 It("test invalid serviceRef with multiple components", func() { 123 clusterDef := clusterDefBuilder.GetObject() 124 125 By("add one component to cluster") 126 clusterBuilder = clusterBuilder. 127 AddComponent(mysqlCompName, mysqlCompDefName). 128 AddComponent(referredCompName, referredCompDefName). 129 // add component one more time 130 AddComponent("oneMoreComp", referredCompDefName) 131 cluster := clusterBuilder.GetObject() 132 133 componentDef := clusterDef.GetComponentDefByName(referredCompDefName) 134 Expect(componentDef).NotTo(BeNil()) 135 components := cluster.Spec.GetDefNameMappingComponents()[referredCompDefName] 136 Expect(len(components)).To(Equal(2)) 137 138 By("lookup service name, should fail") 139 _, err := resolveServiceRef(cluster.Name, components, componentDef) 140 Expect(err).NotTo(BeNil()) 141 Expect(err.Error()).To(ContainSubstring("expect one component but got")) 142 }) 143 144 It("test serviceRef with correct setting", func() { 145 clusterDef := clusterDefBuilder.AddNamedServicePort("mysql", 3306).GetObject() 146 147 By("add one component to cluster") 148 clusterBuilder = clusterBuilder. 149 AddComponent(mysqlCompName, mysqlCompDefName). 150 AddComponent(referredCompName, referredCompDefName) 151 cluster := clusterBuilder.GetObject() 152 153 componentDef := clusterDef.GetComponentDefByName(referredCompDefName) 154 Expect(componentDef).NotTo(BeNil()) 155 components := cluster.Spec.GetDefNameMappingComponents()[referredCompDefName] 156 Expect(len(components)).To(Equal(1)) 157 158 By("lookup service name, should fail") 159 value, err := resolveServiceRef(cluster.Name, components, componentDef) 160 Expect(err).To(BeNil()) 161 Expect(value).To(Equal(fmt.Sprintf("%s-%s", cluster.Name, referredCompName))) 162 }) 163 164 It("test headlessServiceSvc", func() { 165 clusterDef := clusterDefBuilder.GetObject() 166 167 By("add one component to cluster") 168 var replicas int32 = 3 169 clusterBuilder = clusterBuilder. 170 AddComponent(mysqlCompName, mysqlCompDefName). 171 AddComponent(referredCompName, referredCompDefName).SetReplicas(replicas) 172 cluster := clusterBuilder.GetObject() 173 174 componentDef := clusterDef.GetComponentDefByName(referredCompDefName) 175 Expect(componentDef).NotTo(BeNil()) 176 177 components := cluster.Spec.GetDefNameMappingComponents()[referredCompDefName] 178 Expect(len(components)).To(Equal(1)) 179 180 By("construct headless service name") 181 valueFrom := &appsv1alpha1.ComponentValueFrom{ 182 Type: appsv1alpha1.FromHeadlessServiceRef, 183 Format: "", 184 JoinWith: "", 185 } 186 187 value := resolveHeadlessServiceFieldRef(valueFrom, cluster, components) 188 addrs := strings.Split(value, ",") 189 Expect(len(addrs)).To(Equal(int(replicas))) 190 for i, addr := range addrs { 191 Expect(addr).To(Equal(fmt.Sprintf("%s-%s-%d.%s-%s-headless.%s.svc", cluster.Name, referredCompName, i, cluster.Name, referredCompName, cluster.Namespace))) 192 } 193 }) 194 }) 195 })