Skip to content

Commit

Permalink
openstack,ovirt: add destionationClient tests
Browse files Browse the repository at this point in the history
Signed-off-by: Benny Zlotnik <[email protected]>
  • Loading branch information
bennyz authored and ahadas committed Mar 19, 2024
1 parent 5b8e2ad commit 3f1d432
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/controller/plan/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ go_library(
go_test(
name = "plan_test",
srcs = [
"kubevirt_test.go",
"plan_suite_test.go",
"validation_test.go",
"vm_name_handler_test.go",
Expand All @@ -85,7 +86,9 @@ go_test(
deps = [
"//pkg/apis/forklift/v1beta1",
"//pkg/apis/forklift/v1beta1/provider",
"//pkg/apis/forklift/v1beta1/ref",
"//pkg/controller/base",
"//pkg/controller/plan/context",
"//pkg/lib/condition",
"//pkg/lib/logging",
"//vendor/github.com/onsi/ginkgo/v2:ginkgo",
Expand Down
10 changes: 10 additions & 0 deletions pkg/controller/plan/adapter/openstack/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,20 @@ go_test(
srcs = [
"adapter_suite_test.go",
"builder_test.go",
"destionationclient_test.go",
],
embed = [":openstack"],
deps = [
"//pkg/apis/forklift/v1beta1",
"//pkg/apis/forklift/v1beta1/plan",
"//pkg/controller/plan/context",
"//pkg/lib/logging",
"//vendor/github.com/onsi/ginkgo/v2:ginkgo",
"//vendor/github.com/onsi/gomega",
"//vendor/k8s.io/api/core/v1:core",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta",
"//vendor/k8s.io/apimachinery/pkg/runtime",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client/fake",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func forkliftFailHandler(message string, callerSkip ...int) {
func TestTests(t *testing.T) {
defer GinkgoRecover()
RegisterFailHandler(forkliftFailHandler)
RunSpecs(t, "openstack builder")
RunSpecs(t, "OpenStack Suite")
}
8 changes: 8 additions & 0 deletions pkg/controller/plan/adapter/openstack/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@ func (r *Builder) getVolumePopulatorCR(imageID string) (populatorCr api.Openstac
}),
})
if err != nil {
err = liberr.Wrap(err)
return
}
if len(populatorCrList.Items) == 0 {
Expand All @@ -1171,6 +1172,7 @@ func (r *Builder) getVolumePopulatorCR(imageID string) (populatorCr api.Openstac
}
if len(populatorCrList.Items) > 1 {
err = liberr.New("multiple OpenstackVolumePopulator CRs found for image", "imageID", imageID)
return
}

populatorCr = populatorCrList.Items[0]
Expand All @@ -1188,6 +1190,7 @@ func (r *Builder) getVolumePopulatorPVC(imageID string) (populatorPvc *core.Pers
}),
})
if err != nil {
err = liberr.Wrap(err)
return
}

Expand All @@ -1197,6 +1200,7 @@ func (r *Builder) getVolumePopulatorPVC(imageID string) (populatorPvc *core.Pers
}
if len(populatorPvcList.Items) > 1 {
err = liberr.New("multiple PersistentVolumeClaims found for image", "imageID", imageID)
return
}

populatorPvc = &populatorPvcList.Items[0]
Expand Down Expand Up @@ -1272,10 +1276,12 @@ func (r *Builder) persistentVolumeClaimWithSourceRef(image model.Image,
func (r *Builder) PopulatorTransferredBytes(persistentVolumeClaim *core.PersistentVolumeClaim) (transferredBytes int64, err error) {
image, err := r.getImageFromPVC(persistentVolumeClaim)
if err != nil {
err = liberr.Wrap(err)
return
}
populatorCr, err := r.getVolumePopulatorCR(image.ID)
if err != nil {
err = liberr.Wrap(err)
return
}
progressPercentage, err := strconv.ParseInt(populatorCr.Status.Progress, 10, 64)
Expand Down Expand Up @@ -1306,13 +1312,15 @@ func (r *Builder) SetPopulatorDataSourceLabels(vmRef ref.Ref, pvcs []*core.Persi
workload := &model.Workload{}
err = r.Source.Inventory.Find(workload, vmRef)
if err != nil {
err = liberr.Wrap(err)
return
}
var images []*model.Image
for _, volume := range workload.Volumes {
lookupName := getImageFromVolumeName(r.Context, vmRef.ID, volume.ID)
image, err := r.getImageByName(lookupName)
if err != nil {
r.Log.Error(err, "Couldn't find the image from the volume.", "volume", volume.ID, "vmRef", vmRef)
continue
}
images = append(images, image)
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/plan/adapter/openstack/destinationclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (r *DestinationClient) findPVCByCR(cr *v1beta1.OpenstackVolumePopulator) (p
}
if len(pvcList.Items) > 1 {
err = liberr.New("Multiple PVCs found", "imageID", cr.Spec.ImageID)
return
}

pvc = &pvcList.Items[0]
Expand Down
143 changes: 143 additions & 0 deletions pkg/controller/plan/adapter/openstack/destionationclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//nolint:errcheck
package openstack

import (
"context"

v1beta1 "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
"github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/plan"
plancontext "github.com/konveyor/forklift-controller/pkg/controller/plan/context"
"github.com/konveyor/forklift-controller/pkg/lib/logging"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

var destinationClientLog = logging.WithName("openstack-destinationclient-test")

var _ = Describe("openstack destinationclient tests", func() {
destinationClient := createDestinationClient()
openstackVolPopCr := &v1beta1.OpenstackVolumePopulator{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
Labels: map[string]string{
"migration": "migration1",
"imageID": "image1",
},
},
Spec: v1beta1.OpenstackVolumePopulatorSpec{
ImageID: "image1",
},
}

pvc1 := &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "testPVC",
Namespace: "test",
Labels: map[string]string{
"migration": "migration1",
"imageID": "image1",
},
},
}

Describe("findPvcByCr", func() {
It("should return an error when PVC is not found", func() {
pvc, err := destinationClient.findPVCByCR(openstackVolPopCr)
Expect(pvc).To(BeNil())
Expect(err).To(MatchError("PVC not found"))
})

It("should return the PVC when it is found", func() {
destinationClient = createDestinationClient(pvc1)
pvc, err := destinationClient.findPVCByCR(openstackVolPopCr)
Expect(pvc).NotTo(BeNil())
Expect(pvc.Name).To(Equal("testPVC"))
Expect(err).ToNot(HaveOccurred())
})

It("should return an error when multiple PVCs are found", func() {
pvc2 := &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "testPVC2",
Namespace: "test",
Labels: map[string]string{
"migration": "migration1",
"imageID": "image1",
},
},
}
destinationClient = createDestinationClient(pvc1, pvc2)
pvc, err := destinationClient.findPVCByCR(openstackVolPopCr)
Expect(pvc).To(BeNil())
Expect(err).To(MatchError("Multiple PVCs found"))
})
})

Describe("SetPopulatorCrOwnership", func() {
It("should set the owner reference for the populator CR", func() {
destinationClient = createDestinationClient(openstackVolPopCr, pvc1)
destinationClient.SetPopulatorCrOwnership()

patchedOpenstackVolPopCr := &v1beta1.OpenstackVolumePopulator{}
err := destinationClient.Client.Get(context.TODO(), client.ObjectKey{
Name: "test",
Namespace: "test",
}, patchedOpenstackVolPopCr)
Expect(err).ToNot(HaveOccurred())
Expect(patchedOpenstackVolPopCr.GetOwnerReferences()).To(HaveLen(1))
Expect(patchedOpenstackVolPopCr.GetOwnerReferences()[0].Kind).To(Equal("PersistentVolumeClaim"))
Expect(patchedOpenstackVolPopCr.GetOwnerReferences()[0].Name).To(Equal("testPVC"))
})
})
})

func createDestinationClient(objs ...runtime.Object) *DestinationClient {
scheme := runtime.NewScheme()
_ = v1.AddToScheme(scheme)
v1beta1.SchemeBuilder.AddToScheme(scheme)
client := fake.NewClientBuilder().
WithScheme(scheme).
WithRuntimeObjects(objs...).
Build()
return &DestinationClient{
Context: &plancontext.Context{
Destination: plancontext.Destination{
Client: client,
},
Plan: createPlan(),
Log: destinationClientLog,

// To make sure r.Scheme is not nil
Client: client,
},
}
}

func createPlan() *v1beta1.Plan {
return &v1beta1.Plan{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: v1beta1.PlanSpec{
TargetNamespace: "test",
},
Status: v1beta1.PlanStatus{
Migration: plan.MigrationStatus{
History: []plan.Snapshot{
{
Migration: plan.SnapshotRef{
UID: "migration1",
},
},
},
},
},
}
}
24 changes: 23 additions & 1 deletion pkg/controller/plan/adapter/ovirt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "ovirt",
Expand Down Expand Up @@ -40,3 +40,25 @@ go_library(
"//vendor/sigs.k8s.io/controller-runtime/pkg/controller/controllerutil",
],
)

go_test(
name = "ovirt_test",
srcs = [
"destinationclient_test.go",
"ovirt_suite_test.go",
],
embed = [":ovirt"],
deps = [
"//pkg/apis/forklift/v1beta1",
"//pkg/apis/forklift/v1beta1/plan",
"//pkg/controller/plan/context",
"//pkg/lib/logging",
"//vendor/github.com/onsi/ginkgo/v2:ginkgo",
"//vendor/github.com/onsi/gomega",
"//vendor/k8s.io/api/core/v1:core",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta",
"//vendor/k8s.io/apimachinery/pkg/runtime",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client/fake",
],
)
4 changes: 3 additions & 1 deletion pkg/controller/plan/adapter/ovirt/destinationclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,19 @@ func (r *DestinationClient) findPVCByCR(cr *v1beta1.OvirtVolumePopulator) (pvc *
"diskID": cr.Spec.DiskID,
}),
})

if err != nil {
err = liberr.Wrap(err)
return
}

if len(pvcList.Items) == 0 {
err = liberr.New("PVC not found", "diskID", cr.Spec.DiskID)
return
}

if len(pvcList.Items) > 1 {
err = liberr.New("Multiple PVCs found", "diskID", cr.Spec.DiskID)
return
}

pvc = &pvcList.Items[0]
Expand Down
Loading

0 comments on commit 3f1d432

Please sign in to comment.