github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controllerutil/volumesnapshot_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 controllerutil
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	snapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
    27  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	rtclient "sigs.k8s.io/controller-runtime/pkg/client"
    30  
    31  	viper "github.com/1aal/kubeblocks/pkg/viperx"
    32  )
    33  
    34  var _ = Describe("VolumeSnapshot compat client", func() {
    35  	const snapName = "test-volumesnapshot-name"
    36  
    37  	var (
    38  		pvcName       = "test-pvc-name"
    39  		snapClassName = "test-vsc-name"
    40  	)
    41  
    42  	viper.SetDefault("VOLUMESNAPSHOT_API_BETA", "true")
    43  
    44  	It("test create/get/list/patch/delete", func() {
    45  		compatClient := VolumeSnapshotCompatClient{Client: k8sClient, Ctx: ctx}
    46  		snap := &snapshotv1.VolumeSnapshot{
    47  			ObjectMeta: metav1.ObjectMeta{
    48  				Name:      snapName,
    49  				Namespace: "default",
    50  			},
    51  			Spec: snapshotv1.VolumeSnapshotSpec{
    52  				Source: snapshotv1.VolumeSnapshotSource{
    53  					PersistentVolumeClaimName: &pvcName,
    54  				},
    55  			},
    56  		}
    57  		snapKey := rtclient.ObjectKeyFromObject(snap)
    58  		snapGet := &snapshotv1.VolumeSnapshot{}
    59  
    60  		By("create volumesnapshot")
    61  		// check object not found
    62  		exists, err := compatClient.CheckResourceExists(snapKey, snapGet)
    63  		Expect(err).Should(BeNil())
    64  		Expect(exists).Should(BeFalse())
    65  		// create
    66  		Expect(compatClient.Create(snap)).Should(Succeed())
    67  		// check object exists
    68  		exists, err = compatClient.CheckResourceExists(snapKey, snapGet)
    69  		Expect(err).Should(BeNil())
    70  		Expect(exists).Should(BeTrue())
    71  
    72  		By("get volumesnapshot")
    73  		Expect(compatClient.Get(snapKey, snapGet)).Should(Succeed())
    74  		Expect(snapKey.Name).Should(Equal(snapName))
    75  
    76  		By("list volumesnapshots")
    77  		snapList := &snapshotv1.VolumeSnapshotList{}
    78  		Expect(compatClient.List(snapList)).Should(Succeed())
    79  		Expect(snapList.Items).ShouldNot(BeEmpty())
    80  
    81  		By("patch volumesnapshot")
    82  		snapPatch := snap.DeepCopy()
    83  		snap.Spec.VolumeSnapshotClassName = &snapClassName
    84  		Expect(compatClient.Patch(snap, snapPatch)).Should(Succeed())
    85  		Expect(compatClient.Get(snapKey, snapGet)).Should(Succeed())
    86  		Expect(*snapGet.Spec.VolumeSnapshotClassName).Should(Equal(snapClassName))
    87  
    88  		By("delete volumesnapshot")
    89  		Expect(compatClient.Delete(snap)).Should(Succeed())
    90  		Eventually(func() error {
    91  			return compatClient.Get(snapKey, snapGet)
    92  		}).Should(Satisfy(apierrors.IsNotFound))
    93  	})
    94  })