github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/dataprotection/action/action_create_vs_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 action_test
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  	corev1 "k8s.io/api/core/v1"
    26  
    27  	vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  
    31  	dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1"
    32  	"github.com/1aal/kubeblocks/pkg/constant"
    33  	"github.com/1aal/kubeblocks/pkg/dataprotection/action"
    34  	dputils "github.com/1aal/kubeblocks/pkg/dataprotection/utils"
    35  	"github.com/1aal/kubeblocks/pkg/generics"
    36  	"github.com/1aal/kubeblocks/pkg/testutil"
    37  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    38  	testdp "github.com/1aal/kubeblocks/pkg/testutil/dataprotection"
    39  	viper "github.com/1aal/kubeblocks/pkg/viperx"
    40  )
    41  
    42  var _ = Describe("CreateVolumeSnapshotAction Test", func() {
    43  	const (
    44  		actionName = "test-create-vs-action"
    45  		pvcName    = "test-pvc"
    46  		volumeName = "test-volume"
    47  	)
    48  
    49  	cleanEnv := func() {
    50  		By("clean resources")
    51  		inNS := client.InNamespace(testCtx.DefaultNamespace)
    52  		testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.BackupSignature, true, inNS)
    53  		testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.PersistentVolumeClaimSignature, true, inNS)
    54  		testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.VolumeSnapshotSignature, true, inNS)
    55  	}
    56  
    57  	BeforeEach(func() {
    58  		cleanEnv()
    59  		viper.Set(constant.KBToolsImage, testdp.KBToolImage)
    60  	})
    61  
    62  	AfterEach(func() {
    63  		cleanEnv()
    64  		viper.Set(constant.KBToolsImage, "")
    65  	})
    66  
    67  	Context("create action that create volume snapshot", func() {
    68  		It("should return error when PVC is empty", func() {
    69  			act := &action.CreateVolumeSnapshotAction{}
    70  			status, err := act.Execute(buildActionCtx())
    71  			Expect(err).To(HaveOccurred())
    72  			Expect(status.Phase).Should(Equal(dpv1alpha1.ActionPhaseFailed))
    73  		})
    74  
    75  		It("should success to execute action", func() {
    76  			pvc := testdp.NewFakePVC(&testCtx, pvcName)
    77  			Expect(testapps.ChangeObj(&testCtx, pvc, func(claim *corev1.PersistentVolumeClaim) {
    78  				claim.Spec.VolumeName = volumeName
    79  			})).Should(Succeed())
    80  			act := &action.CreateVolumeSnapshotAction{
    81  				Name:  actionName,
    82  				Owner: testdp.NewFakeBackup(&testCtx, nil),
    83  				ObjectMeta: metav1.ObjectMeta{
    84  					Namespace: testCtx.DefaultNamespace,
    85  					Name:      actionName,
    86  				},
    87  				PersistentVolumeClaimWrappers: []action.PersistentVolumeClaimWrapper{
    88  					{
    89  						PersistentVolumeClaim: *pvc,
    90  						VolumeName:            volumeName,
    91  					},
    92  				},
    93  			}
    94  
    95  			// mock pv
    96  			testapps.NewPersistentVolumeFactory(testCtx.DefaultNamespace, volumeName, pvcName).
    97  				SetCSIDriver(testutil.DefaultCSIDriver).SetStorage("1Gi").Create(&testCtx)
    98  
    99  			By("execute action, its status should be running")
   100  			status, err := act.Execute(buildActionCtx())
   101  			Expect(err).ShouldNot(HaveOccurred())
   102  			Expect(status.Phase).Should(Equal(dpv1alpha1.ActionPhaseRunning))
   103  
   104  			By("check volume snapshot be created")
   105  			key := client.ObjectKey{
   106  				Namespace: testCtx.DefaultNamespace,
   107  				Name:      dputils.GetBackupVolumeSnapshotName(actionName, volumeName),
   108  			}
   109  			Eventually(testapps.CheckObjExists(&testCtx, key, &vsv1.VolumeSnapshot{}, true)).Should(Succeed())
   110  		})
   111  	})
   112  })