HostEnterMaintenanceModeStepCmd extends StepCommand { @Override protected void execute() throws ApiFeException, InterruptedException, RpcException { List<BaseEntity> entityList = step.getTransientResourceEntities(); hostEntity = (HostEntity) Iterables.getOnlyElement(entityList); int vmCount = vmBackend.countVmsOnHost(hostEntity); if (vmCount > 0) { throw new HostHasVmsException(hostEntity.getId(), vmCount); } logger.info("Calling deployer to enter host to maintenance mode {}", hostEntity); ChangeHostModeTaskService.State serviceDocument = taskCommand.getDeployerXenonClient() .enterMaintenanceMode(hostEntity.getId()); for (StepEntity nextStep : taskCommand.getTask().getSteps()) { nextStep.createOrUpdateTransientResource(XenonTaskStatusStepCmd.REMOTE_TASK_LINK_RESOURCE_KEY, serviceDocument.documentSelfLink); } } HostEnterMaintenanceModeStepCmd(TaskCommand taskCommand,
                                         StepBackend stepBackend,
                                         StepEntity step,
                                         HostBackend hostBackend,
                                         VmBackend vmBackend);   }
@Test(expectedExceptions = HostStateChangeException.class) public void testFailure() throws Throwable { when(deployerClient.enterMaintenanceMode(anyString())).thenReturn(serviceDocument); when(deployerClient.getHostChangeModeStatus(any(String.class))).thenThrow(HostStateChangeException.class); command.execute(); deployerClient.getHostChangeModeStatus(serviceDocument.documentSelfLink); fail("Should have failed"); }
@Test(expectedExceptions = HostStateChangeException.class) public void testHostStatusChangeFailed() throws Throwable{ ChangeHostModeTaskService.State failedState = new ChangeHostModeTaskService.State(); failedState.hostMode = HostMode.ENTERING_MAINTENANCE; failedState.hostServiceLink = this.hostEntity.getId(); failedState.taskState = new TaskState(); failedState.taskState.stage = TaskState.TaskStage.FAILED; failedState.taskState.failure = new ServiceErrorResponse(); failedState.taskState.failure.message = "failed"; when(deployerClient.getHostChangeModeStatus(anyString())).thenReturn(failedState); commandStatus.execute(); Fail.fail("should have failed"); }