github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/backup_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 "sigs.k8s.io/controller-runtime/pkg/client" 26 27 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 28 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 29 "github.com/1aal/kubeblocks/pkg/generics" 30 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 31 ) 32 33 var _ = Describe("Backup OpsRequest", func() { 34 35 var ( 36 randomStr = testCtx.GetRandomStr() 37 clusterDefinitionName = "cluster-definition-for-ops-" + randomStr 38 clusterVersionName = "clusterversion-for-ops-" + randomStr 39 clusterName = "cluster-for-ops-" + randomStr 40 ) 41 42 cleanEnv := func() { 43 // must wait till resources deleted and no longer existed before the testcases start, 44 // otherwise if later it needs to create some new resource objects with the same name, 45 // in race conditions, it will find the existence of old objects, resulting failure to 46 // create the new objects. 47 By("clean resources") 48 49 // delete cluster(and all dependent sub-resources), clusterversion and clusterdef 50 testapps.ClearClusterResources(&testCtx) 51 52 // delete rest resources 53 inNS := client.InNamespace(testCtx.DefaultNamespace) 54 ml := client.HasLabels{testCtx.TestObjLabelKey} 55 // namespaced 56 testapps.ClearResources(&testCtx, generics.OpsRequestSignature, inNS, ml) 57 } 58 59 BeforeEach(cleanEnv) 60 61 AfterEach(cleanEnv) 62 63 Context("Test OpsRequest for backup", func() { 64 var ( 65 opsRes *OpsResource 66 reqCtx intctrlutil.RequestCtx 67 ) 68 BeforeEach(func() { 69 By("init operations resources ") 70 opsRes, _, _ = initOperationsResources(clusterDefinitionName, clusterVersionName, clusterName) 71 reqCtx = intctrlutil.RequestCtx{Ctx: testCtx.Ctx} 72 }) 73 74 It("should create a backup resource for cluster", func() { 75 By("create Backup OpsRequest") 76 opsRes.OpsRequest = createBackupOpsObj(clusterName, "backup-ops-"+randomStr) 77 78 By("mock backup OpsRequest is Running") 79 _, err := GetOpsManager().Do(reqCtx, k8sClient, opsRes) 80 Expect(err).ShouldNot(HaveOccurred()) 81 Eventually(testapps.GetOpsRequestPhase(&testCtx, client.ObjectKeyFromObject(opsRes.OpsRequest))).Should(Equal(appsv1alpha1.OpsCreatingPhase)) 82 83 By("test backup action and reconcile function") 84 testapps.MockConsensusComponentStatefulSet(&testCtx, clusterName, consensusComp) 85 testapps.MockStatelessComponentDeploy(&testCtx, clusterName, statelessComp) 86 bHandler := BackupOpsHandler{} 87 _ = bHandler.Action(reqCtx, k8sClient, opsRes) 88 89 By("test backup reconcile action") 90 _, err = GetOpsManager().Reconcile(reqCtx, k8sClient, opsRes) 91 Expect(err).ShouldNot(HaveOccurred()) 92 }) 93 }) 94 }) 95 96 func createBackupOpsObj(clusterName, backupOpsName string) *appsv1alpha1.OpsRequest { 97 ops := testapps.NewOpsRequestObj(backupOpsName, testCtx.DefaultNamespace, 98 clusterName, appsv1alpha1.BackupType) 99 return testapps.CreateOpsRequest(ctx, testCtx, ops) 100 }