github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/builder/builder_service_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 builder 21 22 import ( 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 corev1 "k8s.io/api/core/v1" 27 "k8s.io/apimachinery/pkg/util/intstr" 28 ) 29 30 var _ = Describe("service builder", func() { 31 It("should work well", func() { 32 const ( 33 name = "foo" 34 ns = "default" 35 selectorKey1, selectorValue1 = "foo-1", "bar-1" 36 selectorKey2, selectorValue2 = "foo-2", "bar-2" 37 selectorKey3, selectorValue3 = "foo-3", "bar-3" 38 selectorKey4, selectorValue4 = "foo-4", "bar-4" 39 port = int32(12345) 40 ) 41 selectors := map[string]string{selectorKey4: selectorValue4} 42 ports := []corev1.ServicePort{ 43 { 44 Name: "foo-1", 45 Protocol: corev1.ProtocolTCP, 46 Port: port, 47 }, 48 } 49 containerPorts := []corev1.ContainerPort{ 50 { 51 Name: "foo-2", 52 Protocol: corev1.ProtocolTCP, 53 ContainerPort: port, 54 }, 55 } 56 serviceType := corev1.ServiceTypeLoadBalancer 57 svc := NewHeadlessServiceBuilder(ns, name). 58 AddSelector(selectorKey1, selectorValue1). 59 AddSelectors(selectorKey2, selectorValue2, selectorKey3, selectorValue3). 60 AddSelectorsInMap(selectors). 61 AddPorts(ports...). 62 AddContainerPorts(containerPorts...). 63 SetType(serviceType). 64 GetObject() 65 66 Expect(svc.Name).Should(Equal(name)) 67 Expect(svc.Namespace).Should(Equal(ns)) 68 Expect(svc.Spec.Selector).ShouldNot(BeNil()) 69 Expect(svc.Spec.Selector).Should(HaveLen(4)) 70 Expect(svc.Spec.Selector[selectorKey1]).Should(Equal(selectorValue1)) 71 Expect(svc.Spec.Selector[selectorKey2]).Should(Equal(selectorValue2)) 72 Expect(svc.Spec.Selector[selectorKey3]).Should(Equal(selectorValue3)) 73 Expect(svc.Spec.Selector[selectorKey4]).Should(Equal(selectorValue4)) 74 Expect(svc.Spec.Ports).ShouldNot(BeNil()) 75 Expect(svc.Spec.Ports).Should(HaveLen(2)) 76 Expect(svc.Spec.Ports[0]).Should(Equal(ports[0])) 77 Expect(svc.Spec.Type).Should(Equal(serviceType)) 78 Expect(svc.Spec.ExternalTrafficPolicy).Should(Equal(corev1.ServiceExternalTrafficPolicyTypeLocal)) 79 hasPort := func(containerPort corev1.ContainerPort, servicePorts []corev1.ServicePort) bool { 80 for _, servicePort := range servicePorts { 81 if containerPort.Protocol == servicePort.Protocol && 82 intstr.FromString(containerPort.Name) == servicePort.TargetPort { 83 return true 84 } 85 } 86 return false 87 } 88 for _, containerPort := range containerPorts { 89 Expect(hasPort(containerPort, svc.Spec.Ports)).Should(BeTrue()) 90 } 91 }) 92 })