github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/stop_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 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 "sigs.k8s.io/controller-runtime/pkg/client" 27 28 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 29 "github.com/1aal/kubeblocks/pkg/constant" 30 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 31 "github.com/1aal/kubeblocks/pkg/generics" 32 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 33 ) 34 35 var _ = Describe("Stop OpsRequest", func() { 36 37 var ( 38 randomStr = testCtx.GetRandomStr() 39 clusterDefinitionName = "cluster-definition-for-ops-" + randomStr 40 clusterVersionName = "clusterversion-for-ops-" + randomStr 41 clusterName = "cluster-for-ops-" + randomStr 42 ) 43 44 cleanEnv := func() { 45 // must wait till resources deleted and no longer existed before the testcases start, 46 // otherwise if later it needs to create some new resource objects with the same name, 47 // in race conditions, it will find the existence of old objects, resulting failure to 48 // create the new objects. 49 By("clean resources") 50 51 // delete cluster(and all dependent sub-resources), clusterversion and clusterdef 52 testapps.ClearClusterResources(&testCtx) 53 54 // delete rest resources 55 inNS := client.InNamespace(testCtx.DefaultNamespace) 56 ml := client.HasLabels{testCtx.TestObjLabelKey} 57 // namespaced 58 testapps.ClearResources(&testCtx, generics.OpsRequestSignature, inNS, ml) 59 } 60 61 BeforeEach(cleanEnv) 62 63 AfterEach(cleanEnv) 64 65 Context("Test OpsRequest", func() { 66 It("Test stop OpsRequest", func() { 67 reqCtx := intctrlutil.RequestCtx{Ctx: ctx} 68 opsRes, _, _ := initOperationsResources(clusterDefinitionName, clusterVersionName, clusterName) 69 By("create Stop opsRequest") 70 ops := testapps.NewOpsRequestObj("stop-ops-"+randomStr, testCtx.DefaultNamespace, 71 clusterName, appsv1alpha1.StopType) 72 opsRes.OpsRequest = testapps.CreateOpsRequest(ctx, testCtx, ops) 73 74 By("test stop action and reconcile function") 75 // update ops phase to running first 76 _, err := GetOpsManager().Do(reqCtx, k8sClient, opsRes) 77 Expect(err).ShouldNot(HaveOccurred()) 78 Eventually(testapps.GetOpsRequestPhase(&testCtx, client.ObjectKeyFromObject(opsRes.OpsRequest))).Should(Equal(appsv1alpha1.OpsCreatingPhase)) 79 // do stop cluster 80 _, err = GetOpsManager().Do(reqCtx, k8sClient, opsRes) 81 Expect(err).ShouldNot(HaveOccurred()) 82 Expect(len(opsRes.Cluster.Annotations[constant.SnapShotForStartAnnotationKey]) != 0).Should(BeTrue()) 83 for _, v := range opsRes.Cluster.Spec.ComponentSpecs { 84 Expect(v.Replicas).Should(BeEquivalentTo(0)) 85 } 86 _, err = GetOpsManager().Reconcile(reqCtx, k8sClient, opsRes) 87 Expect(err == nil).Should(BeTrue()) 88 }) 89 90 }) 91 })