github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/builder/builder_container_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/api/resource" 28 "k8s.io/apimachinery/pkg/util/intstr" 29 ) 30 31 var _ = Describe("container builder", func() { 32 It("should work well", func() { 33 const name = "foo" 34 commands := []string{ 35 name, 36 "--bar", 37 } 38 args := []string{ 39 "arg1", 40 "arg2", 41 } 42 env := []corev1.EnvVar{ 43 { 44 Name: name, 45 Value: "bar", 46 }, 47 { 48 Name: "hello", 49 ValueFrom: &corev1.EnvVarSource{ 50 FieldRef: &corev1.ObjectFieldSelector{ 51 FieldPath: "metadata.name", 52 }, 53 }, 54 }, 55 } 56 image := "foo:latest" 57 policy := corev1.PullAlways 58 mounts := []corev1.VolumeMount{ 59 { 60 Name: name, 61 MountPath: "/data/foo", 62 }, 63 { 64 Name: "bar", 65 ReadOnly: true, 66 MountPath: "/log/bar", 67 }, 68 } 69 user := int64(0) 70 ctx := corev1.SecurityContext{ 71 RunAsUser: &user, 72 } 73 74 resourceQuantityValue := func(value string) resource.Quantity { 75 quantity, _ := resource.ParseQuantity(value) 76 return quantity 77 } 78 resources := corev1.ResourceRequirements{ 79 Limits: map[corev1.ResourceName]resource.Quantity{ 80 corev1.ResourceCPU: resourceQuantityValue("0.5"), 81 corev1.ResourceMemory: resourceQuantityValue("500m"), 82 }, 83 Requests: map[corev1.ResourceName]resource.Quantity{ 84 corev1.ResourceCPU: resourceQuantityValue("0.5"), 85 corev1.ResourceMemory: resourceQuantityValue("500m"), 86 }, 87 } 88 ports := []corev1.ContainerPort{ 89 { 90 Name: name, 91 ContainerPort: 12345, 92 Protocol: corev1.ProtocolTCP, 93 }, 94 { 95 Name: "bar", 96 ContainerPort: 54321, 97 Protocol: corev1.ProtocolUDP, 98 }, 99 } 100 readinessProbe := corev1.Probe{ 101 ProbeHandler: corev1.ProbeHandler{ 102 Exec: &corev1.ExecAction{ 103 Command: []string{}, 104 }, 105 }, 106 } 107 startupProbe := corev1.Probe{ 108 ProbeHandler: corev1.ProbeHandler{ 109 TCPSocket: &corev1.TCPSocketAction{ 110 Port: intstr.FromInt(12345), 111 }, 112 }, 113 } 114 container := NewContainerBuilder(name). 115 AddCommands(commands...). 116 AddArgs(args...). 117 AddEnv(env...). 118 SetImage(image). 119 SetImagePullPolicy(policy). 120 AddVolumeMounts(mounts...). 121 SetSecurityContext(ctx). 122 SetResources(resources). 123 AddPorts(ports...). 124 SetReadinessProbe(readinessProbe). 125 SetStartupProbe(startupProbe). 126 GetObject() 127 128 Expect(container.Name).Should(Equal(name)) 129 Expect(container.Command).Should(Equal(commands)) 130 Expect(container.Args).Should(Equal(args)) 131 Expect(container.Env).Should(Equal(env)) 132 Expect(container.Image).Should(Equal(image)) 133 Expect(container.ImagePullPolicy).Should(Equal(policy)) 134 Expect(container.VolumeMounts).Should(Equal(mounts)) 135 Expect(container.SecurityContext).ShouldNot(BeNil()) 136 Expect(*container.SecurityContext).Should(Equal(ctx)) 137 Expect(container.Resources).Should(Equal(resources)) 138 Expect(container.Ports).Should(Equal(ports)) 139 Expect(container.ReadinessProbe).ShouldNot(BeNil()) 140 Expect(*container.ReadinessProbe).Should(Equal(readinessProbe)) 141 Expect(container.StartupProbe).ShouldNot(BeNil()) 142 Expect(*container.StartupProbe).Should(Equal(startupProbe)) 143 }) 144 })