@Test
  public void testIscsiLunDiskWithNoPortCantBeAdded() {
    LunDisk disk = createISCSILunDisk();
    AddDiskParameters parameters = createParameters();
    parameters.setDiskInfo(disk);
    initializeCommand(Guid.newGuid(), parameters);
    disk.getLun().getLunConnections().get(0).setPort(null);
    assertFalse(
        "checkIfLunDiskCanBeAdded() succeded for ISCSI lun which LUNs has storage_server_connection with a null port",
        command.checkIfLunDiskCanBeAdded(spyDiskValidator(disk)));
    assertTrue(
        "checkIfLunDiskCanBeAdded() failed but correct can do action hasn't been added to the return response",
        verifyCanDoActionMessagesContainMessage(
            EngineMessage.ACTION_TYPE_FAILED_DISK_LUN_ISCSI_MISSING_CONNECTION_PARAMS));

    clearCanDoActionMessages();

    disk.getLun().getLunConnections().get(0).setPort("");
    assertFalse(
        "checkIfLunDiskCanBeAdded() succeded for ISCSI lun which LUNs has storage_server_connection with a empty port",
        command.checkIfLunDiskCanBeAdded(spyDiskValidator(disk)));
    assertTrue(
        "checkIfLunDiskCanBeAdded() failed but correct can do action hasn't been added to the return response",
        verifyCanDoActionMessagesContainMessage(
            EngineMessage.ACTION_TYPE_FAILED_DISK_LUN_ISCSI_MISSING_CONNECTION_PARAMS));
  }
 private LunDisk createISCSILunDisk(
     ScsiGenericIO sgio, boolean isUsingScsiReservation, DiskInterface diskInterface) {
   LunDisk disk = createISCSILunDisk();
   disk.setSgio(sgio);
   disk.setUsingScsiReservation(isUsingScsiReservation);
   disk.setDiskInterface(diskInterface);
   return disk;
 }
 @Test
 public void testIscsiLunCanBeAdded() {
   LunDisk disk = createISCSILunDisk();
   AddDiskParameters parameters = createParameters();
   parameters.setDiskInfo(disk);
   initializeCommand(Guid.newGuid(), parameters);
   when(diskLunMapDao.getDiskIdByLunId(disk.getLun().getLUNId())).thenReturn(null);
   assertTrue(
       "checkIfLunDiskCanBeAdded() failed for valid iscsi lun",
       command.checkIfLunDiskCanBeAdded(spyDiskValidator(disk)));
 }
  @Test
  public void testGetLunDiskFails() {
    VDS vds = mockVds();
    LunDisk disk = createISCSILunDisk();
    List<LUNs> luns = Collections.emptyList();
    initializeCommand(Guid.newGuid());

    doReturn(luns)
        .when(command)
        .executeGetDeviceList(any(Guid.class), any(StorageType.class), any(String.class));
    assertNull(command.getLunDisk(disk.getLun(), vds));
  }
 @Test
 public void testUnknownTypeLunCantBeAdded() {
   LunDisk disk = createISCSILunDisk();
   AddDiskParameters parameters = createParameters();
   parameters.setDiskInfo(disk);
   initializeCommand(Guid.newGuid(), parameters);
   disk.getLun().setLunType(StorageType.UNKNOWN);
   assertFalse(
       "checkIfLunDiskCanBeAdded() succeded for LUN with UNKNOWN type",
       command.checkIfLunDiskCanBeAdded(spyDiskValidator(disk)));
   assertTrue(
       "checkIfLunDiskCanBeAdded() failed but correct can do action hasn't been added to the return response",
       verifyCanDoActionMessagesContainMessage(
           EngineMessage.ACTION_TYPE_FAILED_DISK_LUN_HAS_NO_VALID_TYPE));
 }
 private static LunDisk createISCSILunDisk() {
   LunDisk disk = new LunDisk();
   LUNs lun = new LUNs();
   lun.setLUNId("lunid");
   lun.setLunType(StorageType.ISCSI);
   StorageServerConnections connection = new StorageServerConnections();
   connection.setIqn("a");
   connection.setConnection("0.0.0.0");
   connection.setPort("1234");
   ArrayList<StorageServerConnections> connections = new ArrayList<>();
   connections.add(connection);
   lun.setLunConnections(connections);
   disk.setLun(lun);
   return disk;
 }
  public void flush() {
    switch (getDiskStorageType().getEntity()) {
      case LUN:
        LunDisk lunDisk = getLunDisk();
        DiskInterface diskInterface = getDiskInterface().getSelectedItem();
        if (DiskInterface.VirtIO_SCSI.equals(diskInterface)) {
          lunDisk.setSgio(
              !getIsScsiPassthrough().getEntity()
                  ? null
                  : getIsSgIoUnfiltered().getEntity()
                      ? ScsiGenericIO.UNFILTERED
                      : ScsiGenericIO.FILTERED);
          lunDisk.setUsingScsiReservation(getIsUsingScsiReservation().getEntity());
        } else {
          getIsScsiPassthrough().setEntity(false);
          lunDisk.setSgio(null);
          lunDisk.setUsingScsiReservation(false);
        }
        setDisk(lunDisk);
        break;
      case CINDER:
        CinderDisk cinderDisk = getCinderDisk();
        updateQuota(cinderDisk);
        updateDiskSize(cinderDisk);
        setDisk(cinderDisk);
        break;
      case IMAGE:
        DiskImage diskImage = getDiskImage();
        updateQuota(diskImage);
        updateDiskSize(diskImage);
        setDisk(diskImage);
        break;
    }

    getDisk().setDiskAlias(getAlias().getEntity());
    getDisk().setDiskDescription(getDescription().getEntity());
    getDisk().setDiskInterface(getDiskInterface().getSelectedItem());
    getDisk().setWipeAfterDelete(getIsWipeAfterDelete().getEntity());
    getDisk().setBoot(getIsBootable().getEntity());
    getDisk().setShareable(getIsShareable().getEntity());
    getDisk().setPlugged(getIsPlugged().getEntity());
    getDisk().setPropagateErrors(PropagateErrors.Off);
    getDisk().setReadOnly(getIsReadOnly().getIsAvailable() ? getIsReadOnly().getEntity() : null);
  }
  @Test
  public void testAddingPCILunExceedsSlotLimit() {
    mockInterfaceList();
    LunDisk disk = createISCSILunDisk();
    disk.setDiskInterface(DiskInterface.VirtIO);
    AddDiskParameters parameters = createParameters();
    parameters.setDiskInfo(disk);
    initializeCommand(Guid.newGuid(), parameters);
    when(diskLunMapDao.getDiskIdByLunId(disk.getLun().getLUNId())).thenReturn(null);
    VM vm = mockVm();
    mockMaxPciSlots();

    // use maximum slots for PCI. canDo expected to succeed.
    fillDiskMap(disk, vm, MAX_PCI_SLOTS - 2);
    CanDoActionTestUtils.runAndAssertCanDoActionSuccess(command);

    vm.getDiskMap().put(Guid.newGuid(), disk);
    CanDoActionTestUtils.runAndAssertCanDoActionFailure(
        command, EngineMessage.ACTION_TYPE_FAILED_EXCEEDED_MAX_PCI_SLOTS);
  }
  public static void removeLunDisk(LunDisk lunDisk) {
    DbFacade.getInstance().getVmDeviceDao().remove(new VmDeviceId(lunDisk.getId(), null));
    LUNs lun = lunDisk.getLun();
    DbFacade.getInstance()
        .getDiskLunMapDao()
        .remove(new DiskLunMapId(lunDisk.getId(), lun.getLUNId()));
    DbFacade.getInstance().getBaseDiskDao().remove(lunDisk.getId());

    lun.setLunConnections(
        new ArrayList<>(
            DbFacade.getInstance().getStorageServerConnectionDao().getAllForLun(lun.getLUNId())));

    if (!lun.getLunConnections().isEmpty()) {
      StorageHelperDirector.getInstance()
          .getItem(lun.getLunConnections().get(0).getStorageType())
          .removeLun(lun);
    } else {
      // if there are no connections then the lun is fcp.
      StorageHelperDirector.getInstance().getItem(StorageType.FCP).removeLun(lun);
    }
  }
  @Test
  public void testLunDiskValid() {
    VDS vds = mockVds();
    LunDisk disk = createISCSILunDisk();
    disk.setDiskInterface(DiskInterface.VirtIO);

    AddDiskParameters parameters = createParameters();
    parameters.setDiskInfo(disk);
    parameters.setVdsId(vds.getId());
    initializeCommand(Guid.newGuid(), parameters);
    command.setVds(vds);

    mockVm();
    mockMaxPciSlots();
    mockInterfaceList();

    List<LUNs> luns = Collections.singletonList(disk.getLun());
    doReturn(luns)
        .when(command)
        .executeGetDeviceList(any(Guid.class), any(StorageType.class), (any(String.class)));
    CanDoActionTestUtils.runAndAssertCanDoActionSuccess(command);
  }
  @Test
  public void testLunDiskInvalid() {
    VDS vds = mockVds();
    LunDisk disk = createISCSILunDisk();
    disk.setDiskInterface(DiskInterface.VirtIO);

    AddDiskParameters parameters = createParameters();
    parameters.setDiskInfo(disk);
    parameters.setVdsId(vds.getId());
    initializeCommand(Guid.newGuid(), parameters);
    command.setVds(vds);

    mockVm();
    mockMaxPciSlots();
    mockInterfaceList();

    List<LUNs> luns = Collections.emptyList();
    doReturn(luns)
        .when(command)
        .executeGetDeviceList(any(Guid.class), any(StorageType.class), any(String.class));
    CanDoActionTestUtils.runAndAssertCanDoActionFailure(
        command, EngineMessage.ACTION_TYPE_FAILED_DISK_LUN_INVALID);
  }
  @Test
  public void testLunDiskWithSgioCanBeAdded() {
    LunDisk disk = createISCSILunDisk();
    disk.setDiskInterface(DiskInterface.VirtIO_SCSI);
    disk.setSgio(ScsiGenericIO.UNFILTERED);

    AddDiskParameters parameters = createParameters();
    parameters.setDiskInfo(disk);
    initializeCommand(Guid.newGuid(), parameters);

    VM vm = mockVm();
    vm.setVdsGroupCompatibilityVersion(Version.v3_3);
    mockMaxPciSlots();

    when(osRepository.getDiskInterfaces(any(Integer.class), any(Version.class)))
        .thenReturn(new ArrayList<>(Arrays.asList("VirtIO_SCSI")));

    DiskValidator diskValidator = spyDiskValidator(disk);
    doReturn(true).when(diskValidator).isVirtioScsiControllerAttached(any(Guid.class));

    mockInterfaceList();

    CanDoActionTestUtils.runAndAssertCanDoActionSuccess(command);
  }