Search in sources :

Example 1 with RunningJobsResponse

use of com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse in project cloudbreak by hortonworks.

the class SaltJobIdTrackerTest method callWithNotStartedWithAlreadyRunning.

@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
@Test
public void callWithNotStartedWithAlreadyRunning() throws Exception {
    String jobId = "1";
    SaltConnector saltConnector = Mockito.mock(SaltConnector.class);
    SaltJobRunner saltJobRunner = Mockito.mock(SaltJobRunner.class);
    PowerMockito.mockStatic(SaltStates.class);
    PowerMockito.when(SaltStates.jobIsRunning(any(), any())).thenReturn(true);
    RunningJobsResponse jobsResponse = new RunningJobsResponse();
    jobsResponse.setResult(List.of(Map.of("runningJob", Map.of())));
    PowerMockito.when(SaltStates.getRunningJobs(saltConnector)).thenReturn(jobsResponse);
    Set<String> targets = new HashSet<>();
    targets.add("10.0.0.1");
    targets.add("10.0.0.2");
    targets.add("10.0.0.3");
    when(saltJobRunner.getTargetHostnames()).thenReturn(targets);
    when(saltJobRunner.getJid()).thenReturn(JobId.jobId(jobId));
    when(saltJobRunner.getJobState()).thenReturn(JobState.NOT_STARTED);
    when(saltJobRunner.submit(any(SaltConnector.class))).thenReturn(jobId);
    SaltJobIdTracker saltJobIdTracker = new SaltJobIdTracker(saltConnector, saltJobRunner);
    try {
        saltJobIdTracker.call();
        fail("should throw exception");
    } catch (CloudbreakOrchestratorInProgressException e) {
        assertThat(e.getMessage(), allOf(containsString("There are running job(s) with id:"), containsString("runningJob")));
    }
    verify(saltJobRunner, times(1)).getJobState();
}
Also used : CloudbreakOrchestratorInProgressException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorInProgressException) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RunningJobsResponse(com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse) SaltConnector(com.sequenceiq.cloudbreak.orchestrator.salt.client.SaltConnector) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with RunningJobsResponse

use of com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse in project cloudbreak by hortonworks.

the class SaltJobIdTrackerTest method callWithNotStarted.

@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
@Test
public void callWithNotStarted() throws Exception {
    String jobId = "1";
    SaltConnector saltConnector = Mockito.mock(SaltConnector.class);
    SaltJobRunner saltJobRunner = Mockito.mock(SaltJobRunner.class);
    PowerMockito.mockStatic(SaltStates.class);
    PowerMockito.when(SaltStates.jobIsRunning(any(), any())).thenReturn(true);
    RunningJobsResponse jobsResponse = new RunningJobsResponse();
    jobsResponse.setResult(List.of());
    PowerMockito.when(SaltStates.getRunningJobs(saltConnector)).thenReturn(jobsResponse);
    Set<String> targets = new HashSet<>();
    targets.add("10.0.0.1");
    targets.add("10.0.0.2");
    targets.add("10.0.0.3");
    when(saltJobRunner.getTargetHostnames()).thenReturn(targets);
    when(saltJobRunner.getJid()).thenReturn(JobId.jobId(jobId));
    when(saltJobRunner.getJobState()).thenReturn(JobState.NOT_STARTED, JobState.IN_PROGRESS);
    when(saltJobRunner.submit(any(SaltConnector.class))).thenReturn(jobId);
    SaltJobIdTracker saltJobIdTracker = new SaltJobIdTracker(saltConnector, saltJobRunner);
    try {
        saltJobIdTracker.call();
        fail("should throw exception");
    } catch (CloudbreakOrchestratorInProgressException e) {
        assertThat(e.getMessage(), allOf(containsString("Target:"), containsString("10.0.0.1"), containsString("10.0.0.2"), containsString("10.0.0.3")));
    }
    PowerMockito.verifyStatic(SaltStates.class);
    SaltStates.jobIsRunning(any(), eq(jobId));
    checkTargets(targets, targetCaptor.getAllValues());
    verify(saltJobRunner, times(2)).getJobState();
}
Also used : CloudbreakOrchestratorInProgressException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorInProgressException) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RunningJobsResponse(com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse) SaltConnector(com.sequenceiq.cloudbreak.orchestrator.salt.client.SaltConnector) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with RunningJobsResponse

use of com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse in project cloudbreak by hortonworks.

the class SaltJobIdTrackerTest method callWithNotStartedAndSlsWithError.

@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())).thenThrow(new RuntimeException("Salt execution went wrong: saltErrorDetails"));
        RunningJobsResponse jobsResponse = new RunningJobsResponse();
        jobsResponse.setResult(List.of());
        PowerMockito.when(SaltStates.getRunningJobs(saltConnector)).thenReturn(jobsResponse);
        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());
    }
}
Also used : CloudbreakOrchestratorFailedException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorFailedException) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RunningJobsResponse(com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse) SaltConnector(com.sequenceiq.cloudbreak.orchestrator.salt.client.SaltConnector) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with RunningJobsResponse

use of com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse in project cloudbreak by hortonworks.

the class SaltStates method jobIsRunning.

public static boolean jobIsRunning(SaltConnector sc, String jid) {
    RunningJobsResponse runningInfo = sc.run("jobs.active", RUNNER, RunningJobsResponse.class);
    LOGGER.info("Active salt jobs: {}", runningInfo);
    for (Map<String, Map<String, Object>> results : runningInfo.getResult()) {
        for (Entry<String, Map<String, Object>> stringMapEntry : results.entrySet()) {
            if (stringMapEntry.getKey().equals(jid)) {
                return true;
            }
        }
    }
    return false;
}
Also used : RunningJobsResponse(com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse) Map(java.util.Map)

Example 5 with RunningJobsResponse

use of com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse in project cloudbreak by hortonworks.

the class SaltStatesTest method jobIsRunningTest.

@Test
public void jobIsRunningTest() throws CloudbreakOrchestratorFailedException {
    String jid = "3";
    RunningJobsResponse runningJobsResponse = new RunningJobsResponse();
    List<Map<String, Map<String, Object>>> result = new ArrayList<>();
    Map<String, Map<String, Object>> resultMap = new HashMap<>();
    resultMap.put(jid, new HashMap<>());
    result.add(resultMap);
    runningJobsResponse.setResult(result);
    when(saltConnector.run(eq("jobs.active"), any(), eq(RunningJobsResponse.class))).thenReturn(runningJobsResponse);
    boolean running = SaltStates.jobIsRunning(saltConnector, jid);
    assertTrue(running);
    resultMap.clear();
    running = SaltStates.jobIsRunning(saltConnector, jid);
    assertFalse(running);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RunningJobsResponse(com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

RunningJobsResponse (com.sequenceiq.cloudbreak.orchestrator.salt.domain.RunningJobsResponse)8 Test (org.junit.Test)5 CloudbreakOrchestratorInProgressException (com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorInProgressException)3 SaltConnector (com.sequenceiq.cloudbreak.orchestrator.salt.client.SaltConnector)3 StringContains.containsString (org.hamcrest.core.StringContains.containsString)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 CloudbreakOrchestratorFailedException (com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorFailedException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1