@Test public void callWithNotStartedAndSlsWithError() throws Exception { String jobId = "1"; try (SaltConnector saltConnector = Mockito.mock(SaltConnector.class)) { SaltJobRunner saltJobRunner = Mockito.mock(BaseSaltJobRunner.class); when(saltJobRunner.getJid()).thenReturn(JobId.jobId(jobId)); when(saltJobRunner.getJobState()).thenCallRealMethod(); doCallRealMethod().when(saltJobRunner).setJobState(any()); when(saltJobRunner.getNodesWithError()).thenCallRealMethod(); doCallRealMethod().when(saltJobRunner).setNodesWithError(any()); when(saltJobRunner.submit(any(SaltConnector.class))).thenReturn(jobId); saltJobRunner.setJobState(JobState.NOT_STARTED); Set<String> targets = Sets.newHashSet("10.0.0.1", "10.0.0.2", "10.0.0.3"); when(saltJobRunner.getTargetHostnames()).thenReturn(targets); PowerMockito.mockStatic(SaltStates.class); PowerMockito.when(SaltStates.jobIsRunning(any(), any())).thenReturn(false); PowerMockito.when(SaltStates.jidInfo(any(SaltConnector.class), anyString(), any(Target.class), any())) .thenThrow(new RuntimeException("Salt execution went wrong: saltErrorDetails")); try { new SaltJobIdTracker(saltConnector, saltJobRunner).call(); fail("should throw exception"); } catch (CloudbreakOrchestratorFailedException e) { assertThat(e.getMessage(), containsString("Salt execution went wrong: saltErrorDetails")); assertThat(e.getMessage(), not(containsString("Exception"))); } PowerMockito.verifyStatic(SaltStates.class); SaltStates.jobIsRunning(any(), eq(jobId)); checkTargets(targets, targetCaptor.getAllValues()); } }
@Override public Boolean call() throws Exception { if (JobState.NOT_STARTED.equals(saltJobRunner.getJobState())) { LOGGER.debug("Job has not started in the cluster. Starting for first time."); saltJobRunner.setJid(jobId(saltJobRunner.submit(saltConnector))); checkIsFinished(saltJobRunner.getJid().getJobId()); } else if (JobState.IN_PROGRESS.equals(saltJobRunner.getJobState())) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.debug("Job: {} is running currently checking the current state.", jobId); checkIsFinished(jobId); } else if (!retryOnFail && JobState.FAILED == saltJobRunner.getJobState()) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.info("Job: {} failed. Terminate execution on these targets: {}", jobId, saltJobRunner.getTargetHostnames()); throw new CloudbreakOrchestratorTerminateException(buildErrorMessage(), saltJobRunner.getNodesWithError()); } else if (JobState.FAILED == saltJobRunner.getJobState() || JobState.AMBIGUOUS == saltJobRunner.getJobState()) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.debug("Job: {} failed in the previous time. Trigger again with these targets: {}", jobId, saltJobRunner.getTargetHostnames()); saltJobRunner.setJid(jobId(saltJobRunner.submit(saltConnector))); saltJobRunner.setJobState(JobState.IN_PROGRESS); return call(); } if (JobState.IN_PROGRESS.equals(saltJobRunner.getJobState())) { String errorMessage = buildErrorMessage(); LOGGER.warn(String.format("Job: %s is running currently. Details: %s", saltJobRunner.getJid(), errorMessage)); throw new CloudbreakOrchestratorInProgressException(errorMessage, saltJobRunner.getNodesWithError()); } if (JobState.FAILED == saltJobRunner.getJobState() || JobState.AMBIGUOUS == saltJobRunner.getJobState()) { throw new CloudbreakOrchestratorFailedException(buildErrorMessage(), saltJobRunner.getNodesWithError()); } LOGGER.debug("Job (jid: {}) was finished. Triggering next salt event.", saltJobRunner.getJid().getJobId()); return true; }
SaltJobIdTracker implements OrchestratorBootstrap { @Override public Boolean call() throws Exception { if (JobState.NOT_STARTED.equals(saltJobRunner.getJobState())) { LOGGER.debug("Job has not started in the cluster. Starting for first time."); saltJobRunner.setJid(jobId(saltJobRunner.submit(saltConnector))); checkIsFinished(saltJobRunner.getJid().getJobId()); } else if (JobState.IN_PROGRESS.equals(saltJobRunner.getJobState())) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.debug("Job: {} is running currently checking the current state.", jobId); checkIsFinished(jobId); } else if (!retryOnFail && JobState.FAILED == saltJobRunner.getJobState()) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.info("Job: {} failed. Terminate execution on these targets: {}", jobId, saltJobRunner.getTargetHostnames()); throw new CloudbreakOrchestratorTerminateException(buildErrorMessage(), saltJobRunner.getNodesWithError()); } else if (JobState.FAILED == saltJobRunner.getJobState() || JobState.AMBIGUOUS == saltJobRunner.getJobState()) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.debug("Job: {} failed in the previous time. Trigger again with these targets: {}", jobId, saltJobRunner.getTargetHostnames()); saltJobRunner.setJid(jobId(saltJobRunner.submit(saltConnector))); saltJobRunner.setJobState(JobState.IN_PROGRESS); return call(); } if (JobState.IN_PROGRESS.equals(saltJobRunner.getJobState())) { String errorMessage = buildErrorMessage(); LOGGER.warn(String.format("Job: %s is running currently. Details: %s", saltJobRunner.getJid(), errorMessage)); throw new CloudbreakOrchestratorInProgressException(errorMessage, saltJobRunner.getNodesWithError()); } if (JobState.FAILED == saltJobRunner.getJobState() || JobState.AMBIGUOUS == saltJobRunner.getJobState()) { throw new CloudbreakOrchestratorFailedException(buildErrorMessage(), saltJobRunner.getNodesWithError()); } LOGGER.debug("Job (jid: {}) was finished. Triggering next salt event.", saltJobRunner.getJid().getJobId()); return true; } }
SaltJobIdTracker implements OrchestratorBootstrap { @Override public Boolean call() throws Exception { if (JobState.NOT_STARTED.equals(saltJobRunner.getJobState())) { LOGGER.debug("Job has not started in the cluster. Starting for first time."); saltJobRunner.setJid(jobId(saltJobRunner.submit(saltConnector))); checkIsFinished(saltJobRunner.getJid().getJobId()); } else if (JobState.IN_PROGRESS.equals(saltJobRunner.getJobState())) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.debug("Job: {} is running currently checking the current state.", jobId); checkIsFinished(jobId); } else if (!retryOnFail && JobState.FAILED == saltJobRunner.getJobState()) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.info("Job: {} failed. Terminate execution on these targets: {}", jobId, saltJobRunner.getTargetHostnames()); throw new CloudbreakOrchestratorTerminateException(buildErrorMessage(), saltJobRunner.getNodesWithError()); } else if (JobState.FAILED == saltJobRunner.getJobState() || JobState.AMBIGUOUS == saltJobRunner.getJobState()) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.debug("Job: {} failed in the previous time. Trigger again with these targets: {}", jobId, saltJobRunner.getTargetHostnames()); saltJobRunner.setJid(jobId(saltJobRunner.submit(saltConnector))); saltJobRunner.setJobState(JobState.IN_PROGRESS); return call(); } if (JobState.IN_PROGRESS.equals(saltJobRunner.getJobState())) { String errorMessage = buildErrorMessage(); LOGGER.warn(String.format("Job: %s is running currently. Details: %s", saltJobRunner.getJid(), errorMessage)); throw new CloudbreakOrchestratorInProgressException(errorMessage, saltJobRunner.getNodesWithError()); } if (JobState.FAILED == saltJobRunner.getJobState() || JobState.AMBIGUOUS == saltJobRunner.getJobState()) { throw new CloudbreakOrchestratorFailedException(buildErrorMessage(), saltJobRunner.getNodesWithError()); } LOGGER.debug("Job (jid: {}) was finished. Triggering next salt event.", saltJobRunner.getJid().getJobId()); return true; } SaltJobIdTracker(SaltConnector saltConnector, SaltJobRunner saltJobRunner);  SaltJobIdTracker(SaltConnector saltConnector, SaltJobRunner saltJobRunner, boolean retryOnFail); }
SaltJobIdTracker implements OrchestratorBootstrap { @Override public Boolean call() throws Exception { if (JobState.NOT_STARTED.equals(saltJobRunner.getJobState())) { LOGGER.debug("Job has not started in the cluster. Starting for first time."); saltJobRunner.setJid(jobId(saltJobRunner.submit(saltConnector))); checkIsFinished(saltJobRunner.getJid().getJobId()); } else if (JobState.IN_PROGRESS.equals(saltJobRunner.getJobState())) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.debug("Job: {} is running currently checking the current state.", jobId); checkIsFinished(jobId); } else if (!retryOnFail && JobState.FAILED == saltJobRunner.getJobState()) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.info("Job: {} failed. Terminate execution on these targets: {}", jobId, saltJobRunner.getTargetHostnames()); throw new CloudbreakOrchestratorTerminateException(buildErrorMessage(), saltJobRunner.getNodesWithError()); } else if (JobState.FAILED == saltJobRunner.getJobState() || JobState.AMBIGUOUS == saltJobRunner.getJobState()) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.debug("Job: {} failed in the previous time. Trigger again with these targets: {}", jobId, saltJobRunner.getTargetHostnames()); saltJobRunner.setJid(jobId(saltJobRunner.submit(saltConnector))); saltJobRunner.setJobState(JobState.IN_PROGRESS); return call(); } if (JobState.IN_PROGRESS.equals(saltJobRunner.getJobState())) { String errorMessage = buildErrorMessage(); LOGGER.warn(String.format("Job: %s is running currently. Details: %s", saltJobRunner.getJid(), errorMessage)); throw new CloudbreakOrchestratorInProgressException(errorMessage, saltJobRunner.getNodesWithError()); } if (JobState.FAILED == saltJobRunner.getJobState() || JobState.AMBIGUOUS == saltJobRunner.getJobState()) { throw new CloudbreakOrchestratorFailedException(buildErrorMessage(), saltJobRunner.getNodesWithError()); } LOGGER.debug("Job (jid: {}) was finished. Triggering next salt event.", saltJobRunner.getJid().getJobId()); return true; } SaltJobIdTracker(SaltConnector saltConnector, SaltJobRunner saltJobRunner);  SaltJobIdTracker(SaltConnector saltConnector, SaltJobRunner saltJobRunner, boolean retryOnFail); @Override Boolean call(); @Override String toString(); SaltJobRunner getSaltJobRunner(); }
SaltJobIdTracker implements OrchestratorBootstrap { @Override public Boolean call() throws Exception { if (JobState.NOT_STARTED.equals(saltJobRunner.getJobState())) { LOGGER.debug("Job has not started in the cluster. Starting for first time."); saltJobRunner.setJid(jobId(saltJobRunner.submit(saltConnector))); checkIsFinished(saltJobRunner.getJid().getJobId()); } else if (JobState.IN_PROGRESS.equals(saltJobRunner.getJobState())) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.debug("Job: {} is running currently checking the current state.", jobId); checkIsFinished(jobId); } else if (!retryOnFail && JobState.FAILED == saltJobRunner.getJobState()) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.info("Job: {} failed. Terminate execution on these targets: {}", jobId, saltJobRunner.getTargetHostnames()); throw new CloudbreakOrchestratorTerminateException(buildErrorMessage(), saltJobRunner.getNodesWithError()); } else if (JobState.FAILED == saltJobRunner.getJobState() || JobState.AMBIGUOUS == saltJobRunner.getJobState()) { String jobId = saltJobRunner.getJid().getJobId(); LOGGER.debug("Job: {} failed in the previous time. Trigger again with these targets: {}", jobId, saltJobRunner.getTargetHostnames()); saltJobRunner.setJid(jobId(saltJobRunner.submit(saltConnector))); saltJobRunner.setJobState(JobState.IN_PROGRESS); return call(); } if (JobState.IN_PROGRESS.equals(saltJobRunner.getJobState())) { String errorMessage = buildErrorMessage(); LOGGER.warn(String.format("Job: %s is running currently. Details: %s", saltJobRunner.getJid(), errorMessage)); throw new CloudbreakOrchestratorInProgressException(errorMessage, saltJobRunner.getNodesWithError()); } if (JobState.FAILED == saltJobRunner.getJobState() || JobState.AMBIGUOUS == saltJobRunner.getJobState()) { throw new CloudbreakOrchestratorFailedException(buildErrorMessage(), saltJobRunner.getNodesWithError()); } LOGGER.debug("Job (jid: {}) was finished. Triggering next salt event.", saltJobRunner.getJid().getJobId()); return true; } SaltJobIdTracker(SaltConnector saltConnector, SaltJobRunner saltJobRunner);  SaltJobIdTracker(SaltConnector saltConnector, SaltJobRunner saltJobRunner, boolean retryOnFail); @Override Boolean call(); @Override String toString(); SaltJobRunner getSaltJobRunner();  }