From 98f52f6c6edacb35687a5b6b3dac6ae2e3b4a702 Mon Sep 17 00:00:00 2001 From: Or Mergi Date: Mon, 29 Jul 2024 16:01:12 +0300 Subject: [PATCH] e2e,storage migration,UpdateVMWithDV: Fetch volume name using slices Signed-off-by: Or Mergi --- tests/storage/migration.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/storage/migration.go b/tests/storage/migration.go index ca1b21e49632..40d5ea3889e9 100644 --- a/tests/storage/migration.go +++ b/tests/storage/migration.go @@ -236,13 +236,11 @@ var _ = SIGDescribe("[Serial]Volumes update with migration", Serial, func() { // TODO: right now, for simplicity, this function assumes the DV in the first position in the datavolumes templata list. Otherwise, we need // to pass the old name of the DV to be replaces. updateVMWithDV := func(vm *virtv1.VirtualMachine, volName, name string) { - var replacedIndex int - for i, v := range vm.Spec.Template.Spec.Volumes { - if v.Name == volName { - replacedIndex = i - break - } - } + i := slices.IndexFunc(vm.Spec.Template.Spec.Volumes, func(volume virtv1.Volume) bool { + return volume.Name == volName + }) + Expect(i).To(BeNumerically(">", -1)) + By(fmt.Sprintf("Replacing volume %s with DV %s", volName, name)) updatedVolume := virtv1.Volume{ Name: volName, @@ -252,14 +250,14 @@ var _ = SIGDescribe("[Serial]Volumes update with migration", Serial, func() { p, err := patch.New( patch.WithReplace("/spec/dataVolumeTemplates/0/metadata/name", name), - patch.WithReplace(fmt.Sprintf("/spec/template/spec/volumes/%d", replacedIndex), updatedVolume), + patch.WithReplace(fmt.Sprintf("/spec/template/spec/volumes/%d", i), updatedVolume), patch.WithReplace("/spec/updateVolumesStrategy", virtv1.UpdateVolumesStrategyMigration), ).GeneratePayload() Expect(err).ToNot(HaveOccurred()) vm, err = virtClient.VirtualMachine(vm.Namespace).Patch(context.Background(), vm.Name, types.JSONPatchType, p, metav1.PatchOptions{}) Expect(err).ToNot(HaveOccurred()) - Expect(vm.Spec.Template.Spec.Volumes[replacedIndex].VolumeSource.DataVolume.Name).To(Equal(name)) + Expect(vm.Spec.Template.Spec.Volumes[i].VolumeSource.DataVolume.Name).To(Equal(name)) } BeforeEach(func() {