IsoAttachStepCmd extends StepCommand { @Override protected void execute() throws ApiFeException, InterruptedException, RpcException { List<BaseEntity> vmList = step.getTransientResourceEntities(Vm.KIND); Preconditions.checkArgument(vmList.size() == 1, "There should be only 1 VM referenced by step %s", step.getId()); VmEntity vmEntity = (VmEntity) vmList.get(0); List<IsoEntity> isoList = step.getTransientResourceEntities(IsoEntity.KIND); Preconditions.checkArgument(isoList.size() == 1, "There should be only 1 ISO referenced by step %s", step.getId()); IsoEntity isoEntity = isoList.get(0); try { try { taskCommand.getHostClient(vmEntity).attachISO(vmEntity.getId(), String.format("%s/%s", vmEntity.buildVmFolderPath(), isoEntity.getName())); } catch (VmNotFoundException ex) { taskCommand.getHostClient(vmEntity, false).attachISO(vmEntity.getId(), String.format("%s/%s", vmEntity.buildVmFolderPath(), isoEntity.getName())); } } catch (RuntimeException ex) { vmBackend.tombstoneIsoEntity(isoEntity); throw new ApiFeException(ex); } vmBackend.addIso(isoEntity, vmEntity); vmEntity.addIso(isoEntity); } IsoAttachStepCmd(TaskCommand taskCommand, StepBackend stepBackend, StepEntity step,
                          VmBackend vmBackend, EntityLockBackend entityLockBackend);   }
@Test public void testSuccessfulAttachIso() throws Exception { when(hostClient.attachISO(vmEntity.getId(), String.format("%s/%s", vmEntity.buildVmFolderPath(), isoEntity.getName()))).thenReturn(attachISOResponse); doNothing().when(vmBackend).addIso(isoEntity, vmEntity); command.execute(); InOrder inOrder = inOrder(hostClient, vmBackend); inOrder.verify(hostClient).attachISO(vmEntity.getId(), String.format("%s/%s", vmEntity.buildVmFolderPath(), isoEntity.getName())); inOrder.verify(vmBackend).addIso(isoEntity, vmEntity); verifyNoMoreInteractions(hostClient, vmBackend); assertThat(vmEntity.getIsos().get(0), is(isoEntity)); }
@Test public void testFailedAttachIso() throws Exception { doThrow(new RuntimeException("Runtime error")).when(hostClient).attachISO(anyString(), anyString()); doNothing().when(vmBackend).tombstoneIsoEntity(isoEntity); try { command.execute(); fail("should have failed due to RuntimeException"); } catch (ApiFeException e) { assertEquals(e.getCause().getMessage(), "Runtime error"); } InOrder inOrder = inOrder(hostClient, vmBackend); inOrder.verify(hostClient).attachISO(vmEntity.getId(), String.format("%s/%s", vmEntity.buildVmFolderPath(), isoEntity.getName())); inOrder.verify(vmBackend).tombstoneIsoEntity(isoEntity); verifyNoMoreInteractions(hostClient, vmBackend); }
@Test public void testStaleAgent() throws Exception { when(hostClient.attachISO(anyString(), anyString())) .thenThrow(new VmNotFoundException("Error")).thenReturn(attachISOResponse); doNothing().when(vmBackend).addIso(isoEntity, vmEntity); command.execute(); InOrder inOrder = inOrder(hostClient, vmBackend); inOrder.verify(hostClient, times(2)).attachISO(vmEntity.getId(), String.format("%s/%s", vmEntity.buildVmFolderPath(), isoEntity.getName())); inOrder.verify(vmBackend).addIso(isoEntity, vmEntity); verifyNoMoreInteractions(hostClient, vmBackend); assertThat(vmEntity.getIsos().get(0), is(isoEntity)); }