github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/dataprotection/backup/request_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 backup 21 22 import ( 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 corev1 "k8s.io/api/core/v1" 27 "sigs.k8s.io/controller-runtime/pkg/client" 28 29 dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1" 30 "github.com/1aal/kubeblocks/pkg/constant" 31 ctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 32 "github.com/1aal/kubeblocks/pkg/dataprotection/utils/boolptr" 33 "github.com/1aal/kubeblocks/pkg/generics" 34 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 35 testdp "github.com/1aal/kubeblocks/pkg/testutil/dataprotection" 36 ) 37 38 var _ = Describe("Request Test", func() { 39 buildRequest := func() *Request { 40 return &Request{ 41 RequestCtx: ctrlutil.RequestCtx{ 42 Log: logger, 43 Ctx: testCtx.Ctx, 44 Recorder: recorder, 45 }, 46 Client: testCtx.Cli, 47 } 48 } 49 50 cleanEnv := func() { 51 // must wait till resources deleted and no longer existed before the testcases start, 52 // otherwise if later it needs to create some new resource objects with the same name, 53 // in race conditions, it will find the existence of old objects, resulting failure to 54 // create the new objects. 55 By("clean resources") 56 57 // delete rest mocked objects 58 inNS := client.InNamespace(testCtx.DefaultNamespace) 59 ml := client.HasLabels{testCtx.TestObjLabelKey} 60 61 // namespaced 62 testapps.ClearResources(&testCtx, generics.ClusterSignature, inNS, ml) 63 testapps.ClearResources(&testCtx, generics.PodSignature, inNS, ml) 64 testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.BackupSignature, true, inNS) 65 66 // wait all backup to be deleted, otherwise the controller maybe create 67 // job to delete the backup between the ClearResources function delete 68 // the job and get the job list, resulting the ClearResources panic. 69 Eventually(testapps.List(&testCtx, generics.BackupSignature, inNS)).Should(HaveLen(0)) 70 71 testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.BackupPolicySignature, true, inNS) 72 testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.JobSignature, true, inNS) 73 testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.PersistentVolumeClaimSignature, true, inNS) 74 75 // non-namespaced 76 testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.ActionSetSignature, true, ml) 77 testapps.ClearResources(&testCtx, generics.StorageClassSignature, ml) 78 testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.BackupRepoSignature, true, ml) 79 testapps.ClearResources(&testCtx, generics.StorageProviderSignature, ml) 80 testapps.ClearResources(&testCtx, generics.VolumeSnapshotClassSignature, ml) 81 testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.PersistentVolumeSignature, true, ml) 82 } 83 84 var clusterInfo *testdp.BackupClusterInfo 85 86 BeforeEach(func() { 87 cleanEnv() 88 clusterInfo = testdp.NewFakeCluster(&testCtx) 89 }) 90 91 AfterEach(func() { 92 cleanEnv() 93 }) 94 95 When("with default settings", func() { 96 var ( 97 backup *dpv1alpha1.Backup 98 actionSet *dpv1alpha1.ActionSet 99 backupPolicy *dpv1alpha1.BackupPolicy 100 backupRepo *dpv1alpha1.BackupRepo 101 102 targetPod *corev1.Pod 103 request *Request 104 ) 105 106 BeforeEach(func() { 107 actionSet = testapps.CreateCustomizedObj(&testCtx, "backup/actionset.yaml", 108 &dpv1alpha1.ActionSet{}, testapps.WithName(testdp.ActionSetName)) 109 backup = testdp.NewFakeBackup(&testCtx, nil) 110 backupRepo = testapps.CreateCustomizedObj(&testCtx, "backup/backuprepo.yaml", &dpv1alpha1.BackupRepo{}, nil) 111 backupPolicy = testdp.NewBackupPolicyFactory(testCtx.DefaultNamespace, testdp.BackupPolicyName). 112 SetBackupRepoName(testdp.BackupRepoName). 113 SetPathPrefix(testdp.BackupPathPrefix). 114 SetTarget(constant.AppInstanceLabelKey, testdp.ClusterName, 115 constant.KBAppComponentLabelKey, testdp.ComponentName, 116 constant.RoleLabelKey, constant.Leader). 117 AddBackupMethod(testdp.BackupMethodName, false, testdp.ActionSetName). 118 Create(&testCtx).GetObject() 119 120 targetPod = clusterInfo.TargetPod 121 request = buildRequest() 122 }) 123 124 Context("build action", func() { 125 It("should build action", func() { 126 request.Backup = backup 127 request.ActionSet = actionSet 128 request.TargetPods = []*corev1.Pod{targetPod} 129 request.BackupPolicy = backupPolicy 130 request.BackupMethod = &backupPolicy.Spec.BackupMethods[0] 131 request.BackupRepo = backupRepo 132 _, err := request.BuildActions() 133 Expect(err).NotTo(HaveOccurred()) 134 }) 135 136 It("build create volume snapshot action", func() { 137 request.TargetPods = []*corev1.Pod{targetPod} 138 request.BackupMethod = &dpv1alpha1.BackupMethod{ 139 Name: testdp.VSBackupMethodName, 140 SnapshotVolumes: boolptr.True(), 141 } 142 _, err := request.buildCreateVolumeSnapshotAction() 143 Expect(err).Should(HaveOccurred()) 144 }) 145 146 }) 147 }) 148 })