Skip to content

Commit

Permalink
unit tests: Use libdv
Browse files Browse the repository at this point in the history
Instead reimplmentation of DataVolume defining,
we can use libdv.

Add WithName decorator, as we need a predefined name.
Also drop the functions and just inline it, because it is used just once.

Signed-off-by: Or Shoval <oshoval@redhat.com>
  • Loading branch information
oshoval committed Jan 25, 2024
1 parent 61d4a97 commit 9469e76
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/virtctl/vm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ go_test(
"//staging/src/kubevirt.io/client-go/kubecli:go_default_library",
"//staging/src/kubevirt.io/client-go/testutils:go_default_library",
"//tests/clientcmd:go_default_library",
"//tests/libdv:go_default_library",
"//vendor/github.com/golang/mock/gomock:go_default_library",
"//vendor/github.com/onsi/ginkgo/v2:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
Expand All @@ -61,6 +62,5 @@ go_test(
"//vendor/k8s.io/apimachinery/pkg/util/yaml:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/fake:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
"//vendor/kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1:go_default_library",
],
)
20 changes: 9 additions & 11 deletions pkg/virtctl/vm/add_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import (
v1 "kubevirt.io/api/core/v1"
cdifake "kubevirt.io/client-go/generated/containerized-data-importer/clientset/versioned/fake"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"

"kubevirt.io/kubevirt/tests/clientcmd"
"kubevirt.io/kubevirt/tests/libdv"
)

var _ = Describe("Add volume command", func() {
Expand Down Expand Up @@ -133,10 +133,16 @@ var _ = Describe("Add volume command", func() {
if commandName == "addvolume" {
kubecli.MockKubevirtClientInstance.EXPECT().CdiClient().Return(cdiClient)
if useDv {
cdiClient.CdiV1beta1().DataVolumes(k8smetav1.NamespaceDefault).Create(context.Background(), createTestDataVolume(), k8smetav1.CreateOptions{})
cdiClient.CdiV1beta1().DataVolumes(k8smetav1.NamespaceDefault).Create(
context.Background(),
libdv.NewDataVolume(libdv.WithName("testvolume")),
k8smetav1.CreateOptions{})
} else {
kubecli.MockKubevirtClientInstance.EXPECT().CoreV1().Return(coreClient.CoreV1())
coreClient.CoreV1().PersistentVolumeClaims(k8smetav1.NamespaceDefault).Create(context.Background(), createTestPVC(), k8smetav1.CreateOptions{})
coreClient.CoreV1().PersistentVolumeClaims(k8smetav1.NamespaceDefault).Create(
context.Background(),
createTestPVC(),
k8smetav1.CreateOptions{})
}
}
expectFunc(vmiName, volumeName, useDv)
Expand All @@ -162,14 +168,6 @@ var _ = Describe("Add volume command", func() {
)
})

func createTestDataVolume() *v1beta1.DataVolume {
return &v1beta1.DataVolume{
ObjectMeta: k8smetav1.ObjectMeta{
Name: "testvolume",
},
}
}

func createTestPVC() *k8sv1.PersistentVolumeClaim {
return &k8sv1.PersistentVolumeClaim{
ObjectMeta: k8smetav1.ObjectMeta{
Expand Down
6 changes: 6 additions & 0 deletions tests/libdv/dv.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func WithNamespace(namespace string) dvOption {
}
}

func WithName(name string) dvOption {
return func(dv *v1beta1.DataVolume) {
dv.ObjectMeta.Name = name
}
}

type pvcOption func(*corev1.PersistentVolumeClaimSpec)

// WithPVC is a dvOption to add a PVCOption spec to the DataVolume
Expand Down

0 comments on commit 9469e76

Please sign in to comment.