|
| 1 | +// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package server_test |
| 5 | + |
| 6 | +import ( |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + corev1alpha1 "github.com/ironcore-dev/ironcore/api/core/v1alpha1" |
| 11 | + storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1" |
| 12 | + "github.com/ironcore-dev/ironcore/broker/machinebroker/apiutils" |
| 13 | + volumebrokerv1alpha1 "github.com/ironcore-dev/ironcore/broker/volumebroker/api/v1alpha1" |
| 14 | + irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1" |
| 15 | + iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1" |
| 16 | + poolletutils "github.com/ironcore-dev/ironcore/poollet/common/utils" |
| 17 | + volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1" |
| 18 | + corev1 "k8s.io/api/core/v1" |
| 19 | + "k8s.io/apimachinery/pkg/api/resource" |
| 20 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = Describe("CreateVolumeSnapshot", func() { |
| 25 | + ns, srv := SetupTest() |
| 26 | + volumeClass := SetupVolumeClass() |
| 27 | + |
| 28 | + It("should create a volume snapshot", func(ctx SpecContext) { |
| 29 | + By("creating a volume") |
| 30 | + volume := &storagev1alpha1.Volume{ |
| 31 | + ObjectMeta: metav1.ObjectMeta{ |
| 32 | + Namespace: ns.Name, |
| 33 | + Name: "test-volume", |
| 34 | + Labels: map[string]string{ |
| 35 | + volumebrokerv1alpha1.ManagerLabel: volumebrokerv1alpha1.VolumeBrokerManager, |
| 36 | + volumebrokerv1alpha1.CreatedLabel: "true", |
| 37 | + }, |
| 38 | + }, |
| 39 | + Spec: storagev1alpha1.VolumeSpec{ |
| 40 | + VolumeClassRef: &corev1.LocalObjectReference{ |
| 41 | + Name: volumeClass.Name, |
| 42 | + }, |
| 43 | + Resources: corev1alpha1.ResourceList{ |
| 44 | + corev1alpha1.ResourceStorage: resource.MustParse("1Gi"), |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | + Expect(k8sClient.Create(ctx, volume)).To(Succeed()) |
| 49 | + By("creating a volume snapshot") |
| 50 | + req := &iri.CreateVolumeSnapshotRequest{ |
| 51 | + VolumeSnapshot: &iri.VolumeSnapshot{ |
| 52 | + Metadata: &irimeta.ObjectMetadata{ |
| 53 | + Labels: map[string]string{ |
| 54 | + volumepoolletv1alpha1.VolumeSnapshotUIDLabel: "foobar", |
| 55 | + }, |
| 56 | + }, |
| 57 | + Spec: &iri.VolumeSnapshotSpec{ |
| 58 | + VolumeId: volume.Name, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + res, err := srv.CreateVolumeSnapshot(ctx, req) |
| 64 | + Expect(err).NotTo(HaveOccurred()) |
| 65 | + Expect(res).NotTo(BeNil()) |
| 66 | + Expect(res.VolumeSnapshot).NotTo(BeNil()) |
| 67 | + |
| 68 | + By("getting the ironcore volume snapshot") |
| 69 | + ironcoreVolumeSnapshot := &storagev1alpha1.VolumeSnapshot{} |
| 70 | + ironcoreVolumeSnapshotKey := client.ObjectKey{Namespace: ns.Name, Name: res.VolumeSnapshot.Metadata.Id} |
| 71 | + Expect(k8sClient.Get(ctx, ironcoreVolumeSnapshotKey, ironcoreVolumeSnapshot)).To(Succeed()) |
| 72 | + |
| 73 | + By("inspecting the ironcore volume snapshot") |
| 74 | + Expect(ironcoreVolumeSnapshot.Labels).To(Equal(map[string]string{ |
| 75 | + poolletutils.DownwardAPILabel(volumepoolletv1alpha1.VolumeSnapshotDownwardAPIPrefix, "root-volume-snapshot-uid"): "foobar", |
| 76 | + volumebrokerv1alpha1.CreatedLabel: "true", |
| 77 | + volumebrokerv1alpha1.ManagerLabel: volumebrokerv1alpha1.VolumeBrokerManager, |
| 78 | + })) |
| 79 | + encodedIRIAnnotations, err := apiutils.EncodeAnnotationsAnnotation(nil) |
| 80 | + Expect(err).NotTo(HaveOccurred()) |
| 81 | + encodedIRILabels, err := apiutils.EncodeLabelsAnnotation(map[string]string{ |
| 82 | + volumepoolletv1alpha1.VolumeSnapshotUIDLabel: "foobar", |
| 83 | + }) |
| 84 | + Expect(err).NotTo(HaveOccurred()) |
| 85 | + Expect(ironcoreVolumeSnapshot.Annotations).To(Equal(map[string]string{ |
| 86 | + volumebrokerv1alpha1.AnnotationsAnnotation: encodedIRIAnnotations, |
| 87 | + volumebrokerv1alpha1.LabelsAnnotation: encodedIRILabels, |
| 88 | + })) |
| 89 | + Expect(ironcoreVolumeSnapshot.Spec.VolumeRef.Name).To(Equal(volume.Name)) |
| 90 | + }) |
| 91 | + |
| 92 | +}) |
0 commit comments