github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/builder/builder_pod_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  )
    28  
    29  var _ = Describe("pod builder", func() {
    30  	It("should work well", func() {
    31  		name := "foo"
    32  		ns := "default"
    33  		port := int32(12345)
    34  		container := *NewContainerBuilder("foo-1").
    35  			SetImage("bar-1").
    36  			AddPorts(corev1.ContainerPort{
    37  				Name:          "foo-1",
    38  				Protocol:      corev1.ProtocolTCP,
    39  				ContainerPort: port,
    40  			}).GetObject()
    41  		containers := []corev1.Container{
    42  			*NewContainerBuilder("foo-2").SetImage("bar-2").
    43  				AddPorts(corev1.ContainerPort{
    44  					Name:          "foo-2",
    45  					Protocol:      corev1.ProtocolTCP,
    46  					ContainerPort: port,
    47  				}).GetObject(),
    48  		}
    49  		volumes := []corev1.Volume{
    50  			{
    51  				Name: "data",
    52  				VolumeSource: corev1.VolumeSource{
    53  					EmptyDir: &corev1.EmptyDirVolumeSource{},
    54  				},
    55  			},
    56  		}
    57  		restartPolicy := corev1.RestartPolicyOnFailure
    58  		user := int64(0)
    59  		ctx := corev1.PodSecurityContext{
    60  			RunAsUser: &user,
    61  		}
    62  		tolerations := []corev1.Toleration{
    63  			{
    64  				Key:      "node",
    65  				Operator: corev1.TolerationOpEqual,
    66  				Value:    "node-0",
    67  			},
    68  		}
    69  		pod := NewPodBuilder(ns, name).
    70  			SetContainers(containers).
    71  			AddContainer(container).
    72  			AddVolumes(volumes...).
    73  			SetRestartPolicy(restartPolicy).
    74  			SetSecurityContext(ctx).
    75  			AddTolerations(tolerations...).
    76  			GetObject()
    77  
    78  		Expect(pod.Name).Should(Equal(name))
    79  		Expect(pod.Namespace).Should(Equal(ns))
    80  		Expect(pod.Spec.Containers).Should(HaveLen(2))
    81  		Expect(pod.Spec.Containers[0]).Should(Equal(containers[0]))
    82  		Expect(pod.Spec.Containers[1]).Should(Equal(container))
    83  		Expect(pod.Spec.Volumes).Should(HaveLen(1))
    84  		Expect(pod.Spec.Volumes[0]).Should(Equal(volumes[0]))
    85  		Expect(pod.Spec.RestartPolicy).Should(Equal(restartPolicy))
    86  		Expect(pod.Spec.SecurityContext).ShouldNot(BeNil())
    87  		Expect(*pod.Spec.SecurityContext).Should(Equal(ctx))
    88  		Expect(pod.Spec.Tolerations).Should(HaveLen(1))
    89  		Expect(pod.Spec.Tolerations[0]).Should(Equal(tolerations[0]))
    90  	})
    91  })