Daniel Erez has uploaded a new change for review. Change subject: core: fix virtio-scsi validation on update VM ......................................................................
core: fix virtio-scsi validation on update VM Fix VirtIO-SCSI validation when updating a running VM: * Added a new signature for vmDeviceChanged method which gets also a specific VmDeviceType. ** Needed for using getVmDeviceByVmIdTypeAndDevice method from VmDeviceDao. I.e. instead of fetching all devices of VmDeviceGeneralType.CONTROLLER, filter also by VmDeviceType.VIRTIOSCSI specifically. * Added new test-cases for updating a running VM. Change-Id: I6f2247319b014f59d5c19c0054ee9e63d1dfe204 Bug-Url: https://bugzilla.redhat.com/1049272 Signed-off-by: Daniel Erez <de...@redhat.com> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVmCommand.java M backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/UpdateVmCommandTest.java 2 files changed, 64 insertions(+), 2 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/52/23052/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVmCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVmCommand.java index 23f903a..2446bc9 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVmCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVmCommand.java @@ -42,6 +42,7 @@ import org.ovirt.engine.core.common.queries.VdcQueryReturnValue; import org.ovirt.engine.core.common.queries.VdcQueryType; import org.ovirt.engine.core.common.utils.Pair; +import org.ovirt.engine.core.common.utils.VmDeviceType; import org.ovirt.engine.core.common.validation.group.UpdateEntity; import org.ovirt.engine.core.compat.DateTime; import org.ovirt.engine.core.compat.Guid; @@ -381,8 +382,11 @@ } } - if (getParameters().isVirtioScsiEnabled() != null && !getVm().isDown() - && vmDeviceChanged(VmDeviceGeneralType.CONTROLLER, getParameters().isVirtioScsiEnabled())) { + if (getParameters().isVirtioScsiEnabled() != null + && !getVm().isDown() + && vmDeviceChanged(VmDeviceGeneralType.CONTROLLER, + VmDeviceType.VIRTIOSCSI.getName(), + getParameters().isVirtioScsiEnabled())) { addCanDoActionMessage("$device VirtIO-SCSI"); return failCanDoAction(VdcBllMessages.VM_CANNOT_UPDATE_DEVICE_VM_NOT_DOWN); } @@ -411,6 +415,20 @@ return deviceEnabled == vmDevices.isEmpty(); } + /** + * Determines whether a VM device change has been request by the user. + * @param deviceType VmDeviceGeneralType. + * @param device VmDeviceType name. + * @param deviceEnabled indicates whether the user asked to enable the device. + * @return true if a change has been requested; otherwise, false + */ + private boolean vmDeviceChanged(VmDeviceGeneralType deviceType, String device, boolean deviceEnabled) { + List<VmDevice> vmDevices = getVmDeviceDao().getVmDeviceByVmIdTypeAndDevice(getParameters().getVmId(), + deviceType, device); + + return deviceEnabled == vmDevices.isEmpty(); + } + private boolean isVmExist() { return getParameters().getVmStaticData() != null && getVm() != null; } diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/UpdateVmCommandTest.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/UpdateVmCommandTest.java index 0ec2350..75d0958 100644 --- a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/UpdateVmCommandTest.java +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/UpdateVmCommandTest.java @@ -30,16 +30,21 @@ import org.ovirt.engine.core.common.businessentities.VDS; import org.ovirt.engine.core.common.businessentities.VDSGroup; import org.ovirt.engine.core.common.businessentities.VM; +import org.ovirt.engine.core.common.businessentities.VMStatus; +import org.ovirt.engine.core.common.businessentities.VmDevice; +import org.ovirt.engine.core.common.businessentities.VmDeviceGeneralType; import org.ovirt.engine.core.common.businessentities.VmStatic; import org.ovirt.engine.core.common.config.ConfigValues; import org.ovirt.engine.core.common.errors.VdcBllMessages; import org.ovirt.engine.core.common.osinfo.OsRepository; import org.ovirt.engine.core.common.utils.SimpleDependecyInjector; +import org.ovirt.engine.core.common.utils.VmDeviceType; import org.ovirt.engine.core.compat.Guid; import org.ovirt.engine.core.compat.Version; import org.ovirt.engine.core.dao.DiskDao; import org.ovirt.engine.core.dao.VdsDAO; import org.ovirt.engine.core.dao.VmDAO; +import org.ovirt.engine.core.dao.VmDeviceDAO; import org.ovirt.engine.core.utils.MockConfigRule; import org.ovirt.engine.core.utils.customprop.ValidationError; @@ -58,6 +63,8 @@ private VdsDAO vdsDAO; @Mock private DiskDao diskDAO; + @Mock + private VmDeviceDAO vmDeviceDAO; @Mock OsRepository osRepository; @@ -240,6 +247,43 @@ VdcBllMessages.CANNOT_DISABLE_VIRTIO_SCSI_PLUGGED_DISKS); } + @Test + public void testCannotDisableVirtioScsiForRunningVM() { + prepareVmToPassCanDoAction(); + command.getParameters().setVirtioScsiEnabled(false); + vm.setStatus(VMStatus.Up); + mockDiskDaoGetAllForVm(Collections.<Disk> emptyList(), true); + + doReturn(vmDeviceDAO).when(command).getVmDeviceDao(); + doReturn(true).when(command).areUpdatedFieldsLegal(); + + doReturn(Collections.emptyList()).when(vmDeviceDAO).getVmDeviceByVmIdAndType( + any(Guid.class), any(VmDeviceGeneralType.class)); + doReturn(Collections.singletonList(new VmDevice())).when(vmDeviceDAO).getVmDeviceByVmIdTypeAndDevice( + vm.getId(), VmDeviceGeneralType.CONTROLLER, VmDeviceType.VIRTIOSCSI.getName()); + + CanDoActionTestUtils.runAndAssertCanDoActionFailure(command, + VdcBllMessages.VM_CANNOT_UPDATE_DEVICE_VM_NOT_DOWN); + } + + @Test + public void testCanEditARunningVM() { + prepareVmToPassCanDoAction(); + vm.setStatus(VMStatus.Up); + mockDiskDaoGetAllForVm(Collections.<Disk> emptyList(), true); + + doReturn(vmDeviceDAO).when(command).getVmDeviceDao(); + doReturn(true).when(command).areUpdatedFieldsLegal(); + + doReturn(Collections.emptyList()).when(vmDeviceDAO).getVmDeviceByVmIdAndType( + any(Guid.class), any(VmDeviceGeneralType.class)); + doReturn(Collections.emptyList()).when(vmDeviceDAO).getVmDeviceByVmIdTypeAndDevice( + any(Guid.class), any(VmDeviceGeneralType.class), any(String.class)); + + CanDoActionTestUtils.runAndAssertCanDoActionSuccess(command); + } + + private void prepareVmToPassCanDoAction() { vmStatic.setName("vm1"); vmStatic.setMemSizeMb(256); -- To view, visit http://gerrit.ovirt.org/23052 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6f2247319b014f59d5c19c0054ee9e63d1dfe204 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Daniel Erez <de...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches