Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright 2026 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package source

import (
"context"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
"github.com/deckhouse/virtualization-controller/pkg/dvcr"
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/cvicondition"
)

var _ = Describe("ObjectRef VirtualImage on PVC", func() {
It("copies size from VirtualImage when provisioning pod is completed", func() {
pod := &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodSucceeded,
},
}

importer := &ImporterMock{
GetPodFunc: func(_ context.Context, _ supplements.Generator) (*corev1.Pod, error) {
return pod, nil
},
}
stat := &StatMock{
CheckPodFunc: func(_ *corev1.Pod) error {
return nil
},
GetDVCRImageNameFunc: func(_ *corev1.Pod) string {
return "registry.example.com/image:test"
},
}
recorder := &eventrecord.EventRecorderLoggerMock{
EventFunc: func(_ client.Object, _, _, _ string) {},
}

syncer := NewObjectRefVirtualImageOnPvc(recorder, importer, &dvcr.Settings{}, stat)

vi := &v1alpha2.VirtualImage{
ObjectMeta: metav1.ObjectMeta{
Name: "vi",
Namespace: "test-ns",
},
Status: v1alpha2.VirtualImageStatus{
Size: v1alpha2.ImageStatusSize{
Stored: "12Gi",
StoredBytes: "12884901888",
Unpacked: "10Gi",
UnpackedBytes: "10737418240",
},
CDROM: true,
Format: "raw",
Target: v1alpha2.VirtualImageStatusTarget{
PersistentVolumeClaim: "vi-pvc",
},
},
}

cvi := &v1alpha2.ClusterVirtualImage{
ObjectMeta: metav1.ObjectMeta{
Name: "cvi",
Generation: 1,
UID: "11111111-1111-1111-1111-111111111111",
},
}

cb := conditions.NewConditionBuilder(cvicondition.ReadyType).Generation(cvi.Generation)

res, err := syncer.Sync(context.Background(), cvi, vi, cb)

Expect(err).NotTo(HaveOccurred())
Expect(res.RequeueAfter).To(Equal(time.Second))
Expect(cvi.Status.Phase).To(Equal(v1alpha2.ImageReady))
Expect(cvi.Status.Size).To(Equal(vi.Status.Size))
Expect(cvi.Status.CDROM).To(Equal(vi.Status.CDROM))
Expect(cvi.Status.Format).To(Equal(vi.Status.Format))
Expect(cvi.Status.Target.RegistryURL).To(Equal("registry.example.com/image:test"))
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2026 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package source

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestSources(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "CVI Sources")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ package source
import (
"context"
"log/slog"
"strconv"

vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -167,9 +169,17 @@ var _ = Describe("ObjectRef VirtualImageSnapshot PersistentVolumeClaim", func()
},
Spec: corev1.PersistentVolumeClaimSpec{
StorageClassName: &sc.Name,
Resources: corev1.VolumeResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: resource.MustParse("10Gi"),
},
},
},
Status: corev1.PersistentVolumeClaimStatus{
Phase: corev1.ClaimBound,
Capacity: corev1.ResourceList{
corev1.ResourceStorage: resource.MustParse("12Gi"),
},
},
}
})
Expand Down Expand Up @@ -222,8 +232,15 @@ var _ = Describe("ObjectRef VirtualImageSnapshot PersistentVolumeClaim", func()
Expect(err).ToNot(HaveOccurred())
Expect(res.IsZero()).To(BeTrue())

storedSize := resource.MustParse("12Gi")
unpackedSize := resource.MustParse("12Gi")

ExpectCondition(vi, metav1.ConditionTrue, vicondition.Ready, false)
Expect(vi.Status.Phase).To(Equal(v1alpha2.ImageReady))
Expect(vi.Status.Size.Stored).To(Equal("12Gi"))
Expect(vi.Status.Size.StoredBytes).To(Equal(strconv.FormatInt(storedSize.Value(), 10)))
Expect(vi.Status.Size.Unpacked).To(Equal("12Gi"))
Expect(vi.Status.Size.UnpackedBytes).To(Equal(strconv.FormatInt(unpackedSize.Value(), 10)))
})
})

Expand Down
Loading
Loading