github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/rsm/transformer_deletion_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 rsm
    21  
    22  import (
    23  	"context"
    24  	"time"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  
    29  	"github.com/golang/mock/gomock"
    30  	apps "k8s.io/api/apps/v1"
    31  	batchv1 "k8s.io/api/batch/v1"
    32  	corev1 "k8s.io/api/core/v1"
    33  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    34  	"sigs.k8s.io/controller-runtime/pkg/client"
    35  
    36  	"github.com/1aal/kubeblocks/pkg/controller/builder"
    37  	"github.com/1aal/kubeblocks/pkg/controller/graph"
    38  )
    39  
    40  var _ = Describe("object deletion transformer test.", func() {
    41  	BeforeEach(func() {
    42  		rsm = builder.NewReplicatedStateMachineBuilder(namespace, name).
    43  			SetUID(uid).
    44  			AddMatchLabelsInMap(selectors).
    45  			SetServiceName(headlessSvcName).
    46  			SetReplicas(3).
    47  			SetRoles(roles).
    48  			SetMembershipReconfiguration(&reconfiguration).
    49  			SetService(service).
    50  			GetObject()
    51  
    52  		transCtx = &rsmTransformContext{
    53  			Context:       ctx,
    54  			Client:        graphCli,
    55  			EventRecorder: nil,
    56  			Logger:        logger,
    57  			rsmOrig:       rsm.DeepCopy(),
    58  			rsm:           rsm,
    59  		}
    60  
    61  		dag = mockDAG()
    62  		transformer = &ObjectDeletionTransformer{}
    63  	})
    64  
    65  	Context("rsm deletion", func() {
    66  		It("should work well", func() {
    67  			ts := metav1.NewTime(time.Now())
    68  			transCtx.rsmOrig.DeletionTimestamp = &ts
    69  			transCtx.rsm.DeletionTimestamp = &ts
    70  			sts := mockUnderlyingSts(*rsm, rsm.Generation)
    71  			headLessSvc := buildHeadlessSvc(*rsm)
    72  			envConfig := buildEnvConfigMap(*rsm)
    73  			envConfigShouldNotBeDeleted := buildEnvConfigMap(*rsm)
    74  			envConfigShouldNotBeDeleted.Name = "env-cm-should-not-be-deleted"
    75  			actionName := getActionName(rsm.Name, int(rsm.Generation), 1, jobTypeSwitchover)
    76  			action := buildAction(rsm, actionName, jobTypeSwitchover, jobScenarioMembership, "", "")
    77  			k8sMock.EXPECT().
    78  				List(gomock.Any(), &apps.StatefulSetList{}, gomock.Any()).
    79  				DoAndReturn(func(_ context.Context, list *apps.StatefulSetList, _ ...client.ListOption) error {
    80  					Expect(list).ShouldNot(BeNil())
    81  					list.Items = []apps.StatefulSet{*sts}
    82  					return nil
    83  				}).Times(1)
    84  			k8sMock.EXPECT().
    85  				List(gomock.Any(), &corev1.ServiceList{}, gomock.Any()).
    86  				DoAndReturn(func(_ context.Context, list *corev1.ServiceList, _ ...client.ListOption) error {
    87  					Expect(list).ShouldNot(BeNil())
    88  					list.Items = []corev1.Service{*headLessSvc}
    89  					return nil
    90  				}).Times(1)
    91  			k8sMock.EXPECT().
    92  				List(gomock.Any(), &corev1.ConfigMapList{}, gomock.Any()).
    93  				DoAndReturn(func(_ context.Context, list *corev1.ConfigMapList, _ ...client.ListOption) error {
    94  					Expect(list).ShouldNot(BeNil())
    95  					list.Items = []corev1.ConfigMap{*envConfig, *envConfigShouldNotBeDeleted}
    96  					return nil
    97  				}).Times(1)
    98  			k8sMock.EXPECT().
    99  				List(gomock.Any(), &batchv1.JobList{}, gomock.Any()).
   100  				DoAndReturn(func(_ context.Context, list *batchv1.JobList, _ ...client.ListOption) error {
   101  					Expect(list).ShouldNot(BeNil())
   102  					list.Items = []batchv1.Job{*action}
   103  					return nil
   104  				}).Times(1)
   105  
   106  			Expect(transformer.Transform(transCtx, dag)).Should(Equal(graph.ErrPrematureStop))
   107  			dagExpected := mockDAG()
   108  			graphCli.Delete(dagExpected, transCtx.rsm)
   109  			graphCli.Delete(dagExpected, action)
   110  			graphCli.Delete(dagExpected, envConfig)
   111  			graphCli.Delete(dagExpected, headLessSvc)
   112  			graphCli.Delete(dagExpected, sts)
   113  			Expect(dag.Equals(dagExpected, less)).Should(BeTrue())
   114  		})
   115  	})
   116  })