github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/start_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 operations
    21  
    22  import (
    23  	"encoding/json"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  
    30  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    31  	opsutil "github.com/1aal/kubeblocks/controllers/apps/operations/util"
    32  	"github.com/1aal/kubeblocks/pkg/constant"
    33  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    34  	"github.com/1aal/kubeblocks/pkg/generics"
    35  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    36  )
    37  
    38  var _ = Describe("Start OpsRequest", func() {
    39  
    40  	var (
    41  		randomStr             = testCtx.GetRandomStr()
    42  		clusterDefinitionName = "cluster-definition-for-ops-" + randomStr
    43  		clusterVersionName    = "clusterversion-for-ops-" + randomStr
    44  		clusterName           = "cluster-for-ops-" + randomStr
    45  	)
    46  
    47  	cleanEnv := func() {
    48  		// must wait till resources deleted and no longer existed before the testcases start,
    49  		// otherwise if later it needs to create some new resource objects with the same name,
    50  		// in race conditions, it will find the existence of old objects, resulting failure to
    51  		// create the new objects.
    52  		By("clean resources")
    53  
    54  		// delete cluster(and all dependent sub-resources), clusterversion and clusterdef
    55  		testapps.ClearClusterResources(&testCtx)
    56  
    57  		// delete rest resources
    58  		inNS := client.InNamespace(testCtx.DefaultNamespace)
    59  		ml := client.HasLabels{testCtx.TestObjLabelKey}
    60  		// namespaced
    61  		testapps.ClearResources(&testCtx, generics.OpsRequestSignature, inNS, ml)
    62  	}
    63  
    64  	BeforeEach(cleanEnv)
    65  
    66  	AfterEach(cleanEnv)
    67  
    68  	Context("Test OpsRequest", func() {
    69  		It("Test start OpsRequest", func() {
    70  			By("init operations resources ")
    71  			reqCtx := intctrlutil.RequestCtx{Ctx: ctx}
    72  			opsRes, _, _ := initOperationsResources(clusterDefinitionName, clusterVersionName, clusterName)
    73  
    74  			By("mock cluster annotations for start opsRequest")
    75  			// mock snapshot annotation for cluster
    76  			componentReplicasMap := map[string]int32{}
    77  			for _, v := range opsRes.Cluster.Spec.ComponentSpecs {
    78  				componentReplicasMap[v.Name] = v.Replicas
    79  			}
    80  			componentReplicasSnapshot, _ := json.Marshal(componentReplicasMap)
    81  			opsRes.Cluster.Annotations = map[string]string{
    82  				constant.SnapShotForStartAnnotationKey: string(componentReplicasSnapshot),
    83  			}
    84  			By("create Start opsRequest")
    85  			ops := testapps.NewOpsRequestObj("start-ops-"+randomStr, testCtx.DefaultNamespace,
    86  				clusterName, appsv1alpha1.StartType)
    87  			opsRes.OpsRequest = testapps.CreateOpsRequest(ctx, testCtx, ops)
    88  
    89  			By("test start action and reconcile function")
    90  			startHandler := StartOpsHandler{}
    91  			oldComponentReplicasMap, _ := startHandler.getComponentReplicasSnapshot(opsRes.Cluster.Annotations)
    92  			Expect(opsutil.PatchClusterOpsAnnotations(ctx, k8sClient, opsRes.Cluster, nil)).Should(Succeed())
    93  			// mock cluster phase to stopped
    94  			Expect(testapps.ChangeObjStatus(&testCtx, opsRes.Cluster, func() {
    95  				opsRes.Cluster.Status.Phase = appsv1alpha1.StoppedClusterPhase
    96  			})).ShouldNot(HaveOccurred())
    97  
    98  			_, err := GetOpsManager().Do(reqCtx, k8sClient, opsRes)
    99  			Expect(err).ShouldNot(HaveOccurred())
   100  			Eventually(testapps.GetOpsRequestPhase(&testCtx, client.ObjectKeyFromObject(opsRes.OpsRequest))).Should(Equal(appsv1alpha1.OpsCreatingPhase))
   101  			// do start action
   102  			_, err = GetOpsManager().Do(reqCtx, k8sClient, opsRes)
   103  			Expect(err).ShouldNot(HaveOccurred())
   104  			for _, v := range opsRes.Cluster.Spec.ComponentSpecs {
   105  				oldReplicas := oldComponentReplicasMap[v.Name]
   106  				Expect(oldReplicas == v.Replicas).Should(BeTrue())
   107  			}
   108  			_, err = GetOpsManager().Reconcile(reqCtx, k8sClient, opsRes)
   109  			Expect(err == nil).Should(BeTrue())
   110  		})
   111  
   112  	})
   113  })