github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/workloads/replicatedstatemachine_controller_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 workloads
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  	corev1 "k8s.io/api/core/v1"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	"github.com/1aal/kubeblocks/pkg/constant"
    29  
    30  	workloads "github.com/1aal/kubeblocks/apis/workloads/v1alpha1"
    31  	"github.com/1aal/kubeblocks/pkg/controller/builder"
    32  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    33  )
    34  
    35  var _ = Describe("ReplicatedStateMachine Controller", func() {
    36  	Context("reconciliation", func() {
    37  		It("should reconcile well", func() {
    38  			name := "test-stateful-replica-set"
    39  			port := int32(12345)
    40  			service := &corev1.Service{
    41  				Spec: corev1.ServiceSpec{
    42  					Ports: []corev1.ServicePort{
    43  						{
    44  							Name:     "foo",
    45  							Protocol: corev1.ProtocolTCP,
    46  							Port:     port,
    47  						},
    48  					},
    49  				},
    50  			}
    51  			commonLabels := map[string]string{
    52  				constant.AppManagedByLabelKey:   constant.AppName,
    53  				constant.AppNameLabelKey:        "ClusterDefName",
    54  				constant.AppComponentLabelKey:   "CompDefName",
    55  				constant.AppInstanceLabelKey:    "clusterName",
    56  				constant.KBAppComponentLabelKey: "componentName",
    57  			}
    58  			pod := builder.NewPodBuilder(testCtx.DefaultNamespace, "foo").
    59  				AddLabelsInMap(commonLabels).
    60  				AddContainer(corev1.Container{
    61  					Name:  "foo",
    62  					Image: "bar",
    63  					Ports: []corev1.ContainerPort{
    64  						{
    65  							Name:          "foo",
    66  							Protocol:      corev1.ProtocolTCP,
    67  							ContainerPort: port,
    68  						},
    69  					},
    70  				}).GetObject()
    71  			template := corev1.PodTemplateSpec{
    72  				ObjectMeta: pod.ObjectMeta,
    73  				Spec:       pod.Spec,
    74  			}
    75  			action := workloads.Action{
    76  				Image:   "foo",
    77  				Command: []string{"bar"},
    78  			}
    79  			rsm := builder.NewReplicatedStateMachineBuilder(testCtx.DefaultNamespace, name).
    80  				AddMatchLabelsInMap(commonLabels).
    81  				SetService(service).
    82  				SetTemplate(template).
    83  				AddCustomHandler(action).
    84  				GetObject()
    85  			Expect(k8sClient.Create(ctx, rsm)).Should(Succeed())
    86  			Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(rsm),
    87  				func(g Gomega, set *workloads.ReplicatedStateMachine) {
    88  					g.Expect(set.Status.ObservedGeneration).Should(BeEquivalentTo(1))
    89  				}),
    90  			).Should(Succeed())
    91  			Expect(k8sClient.Delete(ctx, rsm)).Should(Succeed())
    92  			Eventually(testapps.CheckObjExists(&testCtx, client.ObjectKeyFromObject(rsm), &workloads.ReplicatedStateMachine{}, false)).
    93  				Should(Succeed())
    94  		})
    95  	})
    96  })