Search in sources :

Example 1 with SaltErrorResolver

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

the class SaltJobIdTrackerTest method callWithInProgressAndMissingNodes.

@Test
public void callWithInProgressAndMissingNodes() 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);
        SaltErrorResolver saltErrorResolver = Mockito.mock(SaltErrorResolver.class);
        when(saltConnector.getSaltErrorResolver()).thenReturn(saltErrorResolver);
        saltJobRunner.setJobState(JobState.IN_PROGRESS);
        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);
        Multimap<String, Map<String, String>> missingNodesWithReason = ArrayListMultimap.create();
        Multimap<String, String> missingNodesWithResolvedReason = ArrayListMultimap.create();
        String missingMachine = "10.0.0.1";
        missingNodesWithReason.put(missingMachine, Collections.singletonMap("Name", "some-script.sh"));
        missingNodesWithResolvedReason.put(missingMachine, "Failed to execute: {Name=some-script.sh}");
        when(saltErrorResolver.resolveErrorMessages(missingNodesWithReason)).thenReturn(missingNodesWithResolvedReason);
        PowerMockito.mockStatic(SaltStates.class);
        PowerMockito.when(SaltStates.jobIsRunning(any(), any())).thenReturn(false);
        PowerMockito.when(SaltStates.jidInfo(any(SaltConnector.class), anyString(), any())).thenReturn(missingNodesWithReason);
        try {
            new SaltJobIdTracker(saltConnector, saltJobRunner).call();
            fail("should throw exception");
        } catch (CloudbreakOrchestratorFailedException e) {
            assertThat(e.getMessage(), allOf(containsString("Node: 10.0.0.1 Error(s): Failed to execute: {Name=some-script.sh}"), 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());
    }
}
Also used : CloudbreakOrchestratorFailedException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorFailedException) SaltErrorResolver(com.sequenceiq.cloudbreak.orchestrator.salt.SaltErrorResolver) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Map(java.util.Map) SaltConnector(com.sequenceiq.cloudbreak.orchestrator.salt.client.SaltConnector) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with SaltErrorResolver

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

the class SaltJobIdTrackerTest method callWithInProgressAndJobIsFinished.

@Test
public void callWithInProgressAndJobIsFinished() throws Exception {
    String jobId = "1";
    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());
    SaltErrorResolver saltErrorResolver = Mockito.mock(SaltErrorResolver.class);
    when(saltConnector.getSaltErrorResolver()).thenReturn(saltErrorResolver);
    when(saltJobRunner.submit(any(SaltConnector.class))).thenReturn(jobId);
    saltJobRunner.setJobState(JobState.IN_PROGRESS);
    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);
    PowerMockito.mockStatic(SaltStates.class);
    PowerMockito.when(SaltStates.jobIsRunning(any(), any())).thenReturn(false);
    Multimap<String, Map<String, String>> missingNodesWithReason = ArrayListMultimap.create();
    Multimap<String, String> missingNodesWithResolvedReason = ArrayListMultimap.create();
    PowerMockito.when(SaltStates.jidInfo(any(), any(), any())).thenReturn(missingNodesWithReason);
    when(saltErrorResolver.resolveErrorMessages(missingNodesWithReason)).thenReturn(missingNodesWithResolvedReason);
    SaltJobIdTracker underTest = new SaltJobIdTracker(saltConnector, saltJobRunner);
    assertTrue(underTest.call());
    assertEquals(JobState.FINISHED, saltJobRunner.getJobState());
    PowerMockito.verifyStatic(SaltStates.class);
    SaltStates.jobIsRunning(any(), eq(jobId));
    checkTargets(targets, targetCaptor.getAllValues());
}
Also used : SaltErrorResolver(com.sequenceiq.cloudbreak.orchestrator.salt.SaltErrorResolver) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Map(java.util.Map) SaltConnector(com.sequenceiq.cloudbreak.orchestrator.salt.client.SaltConnector) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with SaltErrorResolver

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

the class SaltJobIdTrackerTest method callWithMissingNodesUsingStderrFailures.

@Test
public void callWithMissingNodesUsingStderrFailures() 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);
        SaltErrorResolver saltErrorResolver = Mockito.mock(SaltErrorResolver.class);
        when(saltConnector.getSaltErrorResolver()).thenReturn(saltErrorResolver);
        saltJobRunner.setJobState(JobState.IN_PROGRESS);
        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);
        Multimap<String, Map<String, String>> missingNodesWithReason = ArrayListMultimap.create();
        Multimap<String, String> missingNodesWithResolvedReason = ArrayListMultimap.create();
        String missingMachine = "10.0.0.1";
        missingNodesWithReason.put(missingMachine, Map.of("Name", "/opt/salt/scripts/backup_db.sh", "Stderr", "Could not create backup"));
        missingNodesWithResolvedReason.put(missingMachine, "Could not create backup");
        when(saltErrorResolver.resolveErrorMessages(missingNodesWithReason)).thenReturn(missingNodesWithResolvedReason);
        PowerMockito.mockStatic(SaltStates.class);
        PowerMockito.when(SaltStates.jobIsRunning(any(), any())).thenReturn(false);
        PowerMockito.when(SaltStates.jidInfo(any(SaltConnector.class), anyString(), any())).thenReturn(missingNodesWithReason);
        try {
            new SaltJobIdTracker(saltConnector, saltJobRunner).call();
            fail("should throw exception");
        } catch (CloudbreakOrchestratorFailedException e) {
            assertThat(e.getMessage(), allOf(containsString("Node: 10.0.0.1 Error(s): Could not create backup"), 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());
    }
}
Also used : CloudbreakOrchestratorFailedException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorFailedException) SaltErrorResolver(com.sequenceiq.cloudbreak.orchestrator.salt.SaltErrorResolver) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Map(java.util.Map) SaltConnector(com.sequenceiq.cloudbreak.orchestrator.salt.client.SaltConnector) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SaltErrorResolver (com.sequenceiq.cloudbreak.orchestrator.salt.SaltErrorResolver)3 SaltConnector (com.sequenceiq.cloudbreak.orchestrator.salt.client.SaltConnector)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 StringContains.containsString (org.hamcrest.core.StringContains.containsString)3 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 CloudbreakOrchestratorFailedException (com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorFailedException)2