private void mockToUpdateDiskVm(List<VM> vms) {
   for (VM vm : vms) {
     if (vm.getId().equals(command.getParameters().getVmId())) {
       when(vmDao.get(command.getParameters().getVmId())).thenReturn(vm);
       break;
     }
   }
 }
  @Test
  public void testDoNotUpdateDeviceWhenReadOnlyIsNotChanged() {
    final UpdateVmDiskParameters parameters = createParameters();
    parameters.getDiskInfo().setReadOnly(false);

    when(diskDao.get(diskImageGuid)).thenReturn(createDiskImage());
    initializeCommand(parameters);
    mockVdsCommandSetVolumeDescription();
    command.executeVmCommand();

    verify(command, atLeast(1)).updateReadOnlyRequested();
    assertFalse(command.updateReadOnlyRequested());
    verify(vmDeviceDao, never()).update(any(VmDevice.class));
  }
  @Test
  public void testExtendingDiskWithQuota() {
    Guid quotaId = Guid.newGuid();

    DiskImage oldDiskImage = createDiskImage();
    oldDiskImage.setQuotaId(quotaId);
    oldDiskImage.setSize(
        SizeConverter.convert(3, SizeConverter.SizeUnit.GiB, SizeConverter.SizeUnit.BYTES)
            .longValue());

    DiskImage newDiskImage = createDiskImage();
    newDiskImage.setQuotaId(quotaId);
    newDiskImage.setSize(
        SizeConverter.convert(5, SizeConverter.SizeUnit.GiB, SizeConverter.SizeUnit.BYTES)
            .longValue());

    UpdateVmDiskParameters parameters =
        new UpdateVmDiskParameters(vmId, diskImageGuid, newDiskImage);
    long diskExtendingDiffInGB =
        newDiskImage.getSizeInGigabytes() - oldDiskImage.getSizeInGigabytes();

    when(diskDao.get(diskImageGuid)).thenReturn(oldDiskImage);
    initializeCommand(parameters);

    QuotaStorageConsumptionParameter consumptionParameter =
        (QuotaStorageConsumptionParameter) command.getQuotaStorageConsumptionParameters().get(0);
    assertEquals(consumptionParameter.getRequestedStorageGB().longValue(), diskExtendingDiffInGB);
  }
  @Test
  public void testUpdateDiskInterfaceUnsupported() {
    final UpdateVmDiskParameters parameters = createParameters();
    parameters.getDiskInfo().setDiskInterface(DiskInterface.IDE);
    when(diskDao.get(diskImageGuid))
        .thenAnswer(
            new Answer<Object>() {
              @Override
              public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                final DiskImage oldDisk = createDiskImage();
                oldDisk.setDiskInterface(DiskInterface.VirtIO);
                assertNotSame(
                    oldDisk.getDiskInterface(), parameters.getDiskInfo().getDiskInterface());
                return oldDisk;
              }
            });

    initializeCommand(parameters);
    doReturn(true).when(command).validatePciAndIdeLimit(anyListOf(VM.class));
    mockVdsCommandSetVolumeDescription();

    when(diskValidator.isReadOnlyPropertyCompatibleWithInterface())
        .thenReturn(ValidationResult.VALID);
    when(diskValidator.isDiskInterfaceSupported(any(VM.class)))
        .thenReturn(new ValidationResult(EngineMessage.ACTION_TYPE_DISK_INTERFACE_UNSUPPORTED));
    when(command.getDiskValidator(parameters.getDiskInfo())).thenReturn(diskValidator);
    CanDoActionTestUtils.runAndAssertCanDoActionFailure(
        command, EngineMessage.ACTION_TYPE_DISK_INTERFACE_UNSUPPORTED);
  }
  @Test
  public void testUpdateReadOnlyPropertyOnChange() {
    // Disk should be updated as Read Only
    final UpdateVmDiskParameters parameters = createParameters();
    parameters.getDiskInfo().setReadOnly(true);

    when(diskDao.get(diskImageGuid)).thenReturn(createDiskImage());
    initializeCommand(parameters);
    stubVmDevice(diskImageGuid, vmId);
    mockVdsCommandSetVolumeDescription();
    command.executeVmCommand();

    verify(command, atLeast(1)).updateReadOnlyRequested();
    assertTrue(command.updateReadOnlyRequested());
    verify(vmDeviceDao).update(any(VmDevice.class));
  }
  @Test
  public void clearAddressOnInterfaceChange() {
    final UpdateVmDiskParameters parameters = createParameters();
    // update new disk interface so it will be different than the old one
    parameters.getDiskInfo().setDiskInterface(DiskInterface.VirtIO_SCSI);

    // creating old disk with interface different than interface of disk from parameters
    // have to return original disk on each request to dao,
    // since the command updates retrieved instance of disk
    when(diskDao.get(diskImageGuid))
        .thenAnswer(
            new Answer<Object>() {
              @Override
              public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                final DiskImage oldDisk = createDiskImage();
                oldDisk.setDiskInterface(DiskInterface.VirtIO);
                assertNotSame(
                    oldDisk.getDiskInterface(), parameters.getDiskInfo().getDiskInterface());
                return oldDisk;
              }
            });
    initializeCommand(parameters);
    mockVdsCommandSetVolumeDescription();
    command.executeVmCommand();

    // verify that device address was cleared exactly once
    verify(vmDeviceDao).clearDeviceAddress(diskImageGuid);
  }
  private void canDoActionMakeDiskBootableOnOtherVm(boolean boot) {
    UpdateVmDiskParameters parameters = createParameters();
    Disk newDisk = parameters.getDiskInfo();
    newDisk.setBoot(true);

    Guid otherVmId = Guid.newGuid();
    VM otherVm = new VM();
    otherVm.setId(otherVmId);

    DiskImage otherDisk = new DiskImage();
    otherDisk.setId(Guid.newGuid());
    otherDisk.setActive(true);
    otherDisk.setBoot(boot);
    if (boot) {
      when(diskDao.getVmBootActiveDisk(otherVmId)).thenReturn(otherDisk);
    }
    when(diskDao.get(diskImageGuid)).thenReturn(createDiskImage());

    initializeCommand(parameters, Arrays.asList(createVmStatusDown(), otherVm));

    mockInterfaceList();

    // The command should only succeed if there is no other bootable disk
    assertEquals(!boot, command.canDoAction());
  }
 /** Mock VDS */
 protected void mockVds() {
   VDS vds = new VDS();
   vds.setVdsGroupCompatibilityVersion(new Version("3.1"));
   command.setVdsId(Guid.Empty);
   doReturn(vdsDao).when(command).getVdsDao();
   when(vdsDao.get(Guid.Empty)).thenReturn(vds);
 }
  @Test
  public void testSucceedInterfaceCanUpdateReadOnly() {
    initializeCommand(new UpdateVmDiskParameters(vmId, diskImageGuid, createDiskImage()));
    doReturn(true).when(command).updateReadOnlyRequested();
    doReturn(ValidationResult.VALID)
        .when(diskValidator)
        .isReadOnlyPropertyCompatibleWithInterface();

    assertTrue(command.validateCanUpdateReadOnly(diskValidator));
  }
Exemplo n.º 10
0
  @Test
  public void testDiskAliasAdnDescriptionMetaDataShouldNotBeUpdated() {
    // Disk should be updated as Read Only
    final UpdateVmDiskParameters parameters = createParameters();
    when(diskDao.get(diskImageGuid)).thenReturn(createDiskImage());

    initializeCommand(parameters);
    mockVdsCommandSetVolumeDescription();
    command.executeVmCommand();
  }
Exemplo n.º 11
0
  @Test
  public void testResize() {
    DiskImage oldDisk = createDiskImage();
    when(diskDao.get(diskImageGuid)).thenReturn(oldDisk);

    UpdateVmDiskParameters parameters = createParameters();
    ((DiskImage) parameters.getDiskInfo()).setSize(oldDisk.getSize() * 2);
    initializeCommand(parameters);

    assertTrue(command.validateCanResizeDisk());
  }
Exemplo n.º 12
0
  @Test
  public void testFailInterfaceCanUpdateReadOnly() {
    initializeCommand(new UpdateVmDiskParameters(vmId, diskImageGuid, createDiskImage()));
    doReturn(true).when(command).updateReadOnlyRequested();
    doReturn(
            new ValidationResult(
                EngineMessage.ACTION_TYPE_FAILED_INTERFACE_DOES_NOT_SUPPORT_READ_ONLY_ATTR))
        .when(diskValidator)
        .isReadOnlyPropertyCompatibleWithInterface();

    assertFalse(command.validateCanUpdateReadOnly(diskValidator));
  }
Exemplo n.º 13
0
  @Test
  public void testFaultyResize() {
    when(diskDao.get(diskImageGuid)).thenReturn(createDiskImage());

    UpdateVmDiskParameters parameters = createParameters();
    ((DiskImage) parameters.getDiskInfo()).setSize(parameters.getDiskInfo().getSize() / 2);
    initializeCommand(parameters);

    assertFalse(command.validateCanResizeDisk());
    CanDoActionTestUtils.assertCanDoActionMessages(
        "wrong failure",
        command,
        EngineMessage.ACTION_TYPE_FAILED_REQUESTED_DISK_SIZE_IS_TOO_SMALL);
  }
Exemplo n.º 14
0
  @Test
  public void testOnlyDiskAliasChangedMetaDataShouldBeUpdated() {
    // Disk should be updated as Read Only
    final UpdateVmDiskParameters parameters = createParameters();
    when(diskDao.get(diskImageGuid)).thenReturn(createDiskImage());

    parameters.getDiskInfo().setDiskAlias("New Disk Alias");
    initializeCommand(parameters);
    mockVdsCommandSetVolumeDescription();
    command.executeVmCommand();
    verify(command, times(1))
        .runVdsCommand(
            eq(VDSCommandType.SetVolumeDescription),
            any(SetVolumeDescriptionVDSCommandParameters.class));
  }
Exemplo n.º 15
0
  @Test
  public void getOtherVmDisks() {
    UpdateVmDiskParameters parameters = createParameters();

    DiskImage otherDisk = new DiskImage();
    otherDisk.setId(Guid.newGuid());
    otherDisk.setActive(true);
    when(diskDao.getAllForVm(vmId))
        .thenReturn(new LinkedList<>(Arrays.asList(parameters.getDiskInfo(), otherDisk)));
    when(diskDao.get(diskImageGuid)).thenReturn(createDiskImage());
    initializeCommand(parameters);

    VM vm = createVmStatusDown();
    mockCtorRelatedDaoCalls(Collections.singletonList(vm));
    List<Disk> otherDisks = command.getOtherVmDisks(vm.getId());
    assertEquals("Wrong number of other disks", 1, otherDisks.size());
    assertFalse("Wrong other disk", otherDisks.contains(parameters.getDiskInfo()));
  }
Exemplo n.º 16
0
  @Test
  public void testFailedRoDiskResize() {
    StorageDomain sd = new StorageDomain();
    sd.setAvailableDiskSize(Integer.MAX_VALUE);
    sd.setStatus(StorageDomainStatus.Active);
    when(storageDomainDao.getForStoragePool(any(Guid.class), any(Guid.class))).thenReturn(sd);

    UpdateVmDiskParameters parameters = createParameters();
    ((DiskImage) parameters.getDiskInfo()).setSize(parameters.getDiskInfo().getSize() * 2);
    initializeCommand(parameters);

    DiskImage oldDisk = createDiskImage();
    doReturn(oldDisk).when(command).getOldDisk();

    VmDevice vmDevice = stubVmDevice(diskImageGuid, vmId);
    vmDevice.setIsReadOnly(true);

    assertFalse(command.validateCanResizeDisk());
    CanDoActionTestUtils.assertCanDoActionMessages(
        "wrong failure", command, EngineMessage.ACTION_TYPE_FAILED_CANNOT_RESIZE_READ_ONLY_DISK);
  }
Exemplo n.º 17
0
  @Test
  public void nullifiedSnapshotOnUpdateDiskToShareable() {
    UpdateVmDiskParameters parameters = createParameters();
    DiskImage disk = createShareableDisk(VolumeFormat.RAW);
    parameters.setDiskInfo(disk);
    StorageDomain storage = addNewStorageDomainToDisk(disk, StorageType.NFS);
    parameters.setDiskInfo(disk);

    DiskImage oldDisk = createDiskImage();
    oldDisk.setVmSnapshotId(Guid.newGuid());

    when(diskDao.get(diskImageGuid)).thenReturn(oldDisk);
    when(storageDomainStaticDao.get(storage.getId())).thenReturn(storage.getStorageStaticData());

    initializeCommand(parameters);
    mockVdsCommandSetVolumeDescription();
    mockInterfaceList();

    CanDoActionTestUtils.runAndAssertCanDoActionSuccess(command);
    command.executeVmCommand();
    assertTrue(oldDisk.getVmSnapshotId() == null);
  }
Exemplo n.º 18
0
 private void mockNullVm() {
   mockGetForDisk((VM) null);
   mockGetVmsListForDisk(null);
   when(vmDao.get(command.getParameters().getVmId())).thenReturn(null);
 }