github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/builder/builder_base_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  	"k8s.io/apimachinery/pkg/types"
    26  
    27  	appsv1 "k8s.io/api/apps/v1"
    28  )
    29  
    30  var _ = Describe("base builder", func() {
    31  	It("should work well", func() {
    32  		const (
    33  			name                             = "foo"
    34  			ns                               = "default"
    35  			uid                              = types.UID("foo-bar")
    36  			labelKey1, labelValue1           = "foo-1", "bar-1"
    37  			labelKey2, labelValue2           = "foo-2", "bar-2"
    38  			labelKey3, labelValue3           = "foo-3", "bar-3"
    39  			annotationKey1, annotationValue1 = "foo-1", "bar-1"
    40  			annotationKey2, annotationValue2 = "foo-2", "bar-2"
    41  			annotationKey3, annotationValue3 = "foo-3", "bar-3"
    42  		)
    43  		labels := map[string]string{labelKey3: labelValue3}
    44  		annotations := map[string]string{annotationKey3: annotationValue3}
    45  		controllerRevision := "wer-23e23-sedfwe--34r23"
    46  		finalizer := "foo-bar"
    47  		owner := NewReplicatedStateMachineBuilder(ns, name).GetObject()
    48  		owner.UID = "sdfwsedqw-swed-sdswe"
    49  		ownerAPIVersion := "workloads.kubeblocks.io/v1alpha1"
    50  		ownerKind := "ReplicatedStateMachine"
    51  		obj := NewConfigMapBuilder(ns, name).
    52  			SetUID(uid).
    53  			AddLabels(labelKey1, labelValue1, labelKey2, labelValue2).
    54  			AddLabelsInMap(labels).
    55  			AddAnnotations(annotationKey1, annotationValue1, annotationKey2, annotationValue2).
    56  			AddAnnotationsInMap(annotations).
    57  			AddControllerRevisionHashLabel(controllerRevision).
    58  			AddFinalizers([]string{finalizer}).
    59  			SetOwnerReferences(ownerAPIVersion, ownerKind, owner).
    60  			GetObject()
    61  
    62  		Expect(obj.Name).Should(Equal(name))
    63  		Expect(obj.Namespace).Should(Equal(ns))
    64  		Expect(obj.UID).Should(Equal(uid))
    65  		Expect(obj.Labels).Should(HaveLen(4))
    66  		Expect(obj.Labels[labelKey1]).Should(Equal(labelValue1))
    67  		Expect(obj.Labels[labelKey2]).Should(Equal(labelValue2))
    68  		Expect(obj.Labels[labelKey3]).Should(Equal(labelValue3))
    69  		Expect(obj.Labels[appsv1.ControllerRevisionHashLabelKey]).Should(Equal(controllerRevision))
    70  		Expect(obj.Annotations).Should(HaveLen(3))
    71  		Expect(obj.Annotations[annotationKey1]).Should(Equal(annotationValue1))
    72  		Expect(obj.Annotations[annotationKey2]).Should(Equal(annotationValue2))
    73  		Expect(obj.Annotations[annotationKey3]).Should(Equal(annotationValue3))
    74  		Expect(obj.Finalizers).Should(HaveLen(1))
    75  		Expect(obj.Finalizers[0]).Should(Equal(finalizer))
    76  		Expect(obj.OwnerReferences).Should(HaveLen(1))
    77  		Expect(obj.OwnerReferences[0].APIVersion).Should(Equal(ownerAPIVersion))
    78  		Expect(obj.OwnerReferences[0].Kind).Should(Equal(ownerKind))
    79  		Expect(obj.OwnerReferences[0].Name).Should(Equal(owner.Name))
    80  		Expect(obj.OwnerReferences[0].UID).Should(Equal(owner.UID))
    81  	})
    82  })