use of com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase in project cloudbreak by hortonworks.
the class SaltOrchestratorTest method testStopClusterManagerAgent.
@Test
public void testStopClusterManagerAgent() throws Exception {
Set<Node> downscaleTargets = new HashSet<>();
downscaleTargets.add(new Node("10.0.0.2", "1.1.1.2", "10-0-0-2.example.com", "hg", "fqdn2", null));
downscaleTargets.add(new Node("10.0.0.3", "1.1.1.3", "10-0-0-3.example.com", "hg", "fqdn3", null));
PowerMockito.mockStatic(SaltStates.class);
Set<String> responsiveAddresses = new HashSet<>();
responsiveAddresses.add("10.0.0.1");
responsiveAddresses.add("10.0.0.2");
responsiveAddresses.add("10.0.0.3");
PowerMockito.when(SaltStates.collectMinionIpAddresses(any(), any())).thenReturn(responsiveAddresses);
Set<OrchestrationNode> allNodes = new HashSet<>();
allNodes.add(() -> new Node("10.0.0.1", "1.1.1.1", "10-0-0-1.example.com", "hg", "fqdn3", null));
allNodes.addAll(downscaleTargets.stream().map(node -> (OrchestrationNode) () -> node).collect(Collectors.toSet()));
Callable pillarSaveCallable = mock(Callable.class);
when(saltRunner.runner(any(), any(), any())).thenReturn(pillarSaveCallable);
when(saltRunner.runner(any(), any(), any(), anyInt(), anyBoolean())).thenReturn(mock(Callable.class));
saltOrchestrator.stopClusterManagerAgent(() -> allNodes, gatewayConfig, targets, downscaleTargets, exitCriteriaModel, new CmAgentStopFlags(false, false, false));
ArgumentCaptor<ModifyGrainBase> modifyGrainBaseArgumentCaptor = ArgumentCaptor.forClass(ModifyGrainBase.class);
ArgumentCaptor<PillarSave> pillarSaveArgumentCaptor = ArgumentCaptor.forClass(PillarSave.class);
verify(pillarSaveCallable, times(1)).call();
InOrder inOrder = inOrder(saltCommandRunner, saltRunner);
inOrder.verify(saltCommandRunner).runModifyGrainCommand(any(), modifyGrainBaseArgumentCaptor.capture(), any(), any());
ModifyGrainBase modifyGrainBase = modifyGrainBaseArgumentCaptor.getValue();
assertThat(modifyGrainBase, instanceOf(GrainAddRunner.class));
assertEquals("roles", modifyGrainBase.getKey());
assertEquals("cloudera_manager_agent_stop", modifyGrainBase.getValue());
inOrder.verify(saltRunner).runner(pillarSaveArgumentCaptor.capture(), any(), any());
PillarSave capturedPillarSave = pillarSaveArgumentCaptor.getValue();
ArgumentCaptor<SaltJobIdTracker> saltJobIdCaptor = ArgumentCaptor.forClass(SaltJobIdTracker.class);
inOrder.verify(saltRunner).runner(saltJobIdCaptor.capture(), any(), any(), anyInt(), anyBoolean());
inOrder.verify(saltCommandRunner).runModifyGrainCommand(any(), modifyGrainBaseArgumentCaptor.capture(), any(), any());
inOrder.verifyNoMoreInteractions();
modifyGrainBase = modifyGrainBaseArgumentCaptor.getValue();
assertThat(modifyGrainBase, instanceOf(GrainRemoveRunner.class));
assertEquals("roles", modifyGrainBase.getKey());
assertEquals("cloudera_manager_agent_stop", modifyGrainBase.getValue());
Set<String> targets = Whitebox.getInternalState(capturedPillarSave, "targets");
assertTrue(targets.contains("10.0.0.1"));
Pillar pillar = Whitebox.getInternalState(capturedPillarSave, "pillar");
Map<String, Map> hosts = (Map) ((Map) pillar.getJson()).get("hosts");
assertTrue(hosts.keySet().contains("10.0.0.1"));
assertTrue(hosts.keySet().contains("10.0.0.2"));
assertTrue(hosts.keySet().contains("10.0.0.3"));
}
use of com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase in project cloudbreak by hortonworks.
the class SaltOrchestratorTest method testExistingIsPrimaryReplacement.
@Test
public void testExistingIsPrimaryReplacement() throws Exception {
ObjectMapper mapper = new ObjectMapper();
GatewayConfig replica1 = mock(GatewayConfig.class);
GatewayConfig replica2 = mock(GatewayConfig.class);
Node replicaNode1 = mock(Node.class);
Node replicaNode2 = mock(Node.class);
when(replica1.getHostname()).thenReturn("replica1.domain");
when(replica2.getHostname()).thenReturn("replica2.domain");
when(replicaNode1.getHostname()).thenReturn("replica1.domain");
when(replicaNode2.getHostname()).thenReturn("replica2.domain");
PowerMockito.mockStatic(SaltStates.class);
HostAndRoleTarget replicaTarget = new HostAndRoleTarget("freeipa_replica", Set.of("replica1.domain", "replica2.domain"));
ArrayNode replicaRole = mapper.createArrayNode();
replicaRole.add("freeipa_replica");
PowerMockito.when(SaltStates.getGrains(eq(saltConnector), eq(replicaTarget), eq("roles"))).thenReturn(Map.of("replica1.domain", replicaRole, "replica2.domain", replicaRole));
saltOrchestrator.installFreeIpa(replica1, List.of(replica1, replica2), Set.of(replicaNode1, replicaNode2), exitCriteriaModel);
ArgumentCaptor<ModifyGrainBase> modifyGrainCaptor = ArgumentCaptor.forClass(ModifyGrainBase.class);
verify(saltCommandRunner, times(2)).runModifyGrainCommand(eq(saltConnector), modifyGrainCaptor.capture(), eq(exitCriteriaModel), eq(exitCriteria));
List<ModifyGrainBase> modifyGrains = modifyGrainCaptor.getAllValues();
GrainAddRunner addReplacementRole = (GrainAddRunner) modifyGrains.get(0);
assertEquals("freeipa_primary_replacement", addReplacementRole.getValue());
assertEquals(Set.of("replica1.domain"), addReplacementRole.getTargetHostnames());
GrainRemoveRunner removeReplicaRole = (GrainRemoveRunner) modifyGrains.get(1);
assertEquals("freeipa_replica", removeReplicaRole.getValue());
assertEquals(Set.of("replica1.domain"), removeReplicaRole.getTargetHostnames());
ArgumentCaptor<SaltJobIdTracker> saltJobIdTrackerCaptor = ArgumentCaptor.forClass(SaltJobIdTracker.class);
verify(saltRunner, times(3)).runner(saltJobIdTrackerCaptor.capture(), eq(exitCriteria), eq(exitCriteriaModel), anyInt(), anyBoolean());
List<SaltJobIdTracker> saltJobIdTrackers = saltJobIdTrackerCaptor.getAllValues();
HighStateRunner highStateRunner = (HighStateRunner) saltJobIdTrackers.get(0).getSaltJobRunner();
assertTrue(highStateRunner.getTargetHostnames().contains("replica1.domain") || highStateRunner.getTargetHostnames().contains("replica2.domain"));
assertEquals(1, highStateRunner.getTargetHostnames().size());
HighStateRunner highStateRunner2 = (HighStateRunner) saltJobIdTrackers.get(1).getSaltJobRunner();
assertTrue(highStateRunner2.getTargetHostnames().contains("replica2.domain") || highStateRunner2.getTargetHostnames().contains("replica1.domain"));
assertEquals(1, highStateRunner2.getTargetHostnames().size());
assertFalse(highStateRunner.getTargetHostnames().containsAll(highStateRunner2.getTargetHostnames()));
HighStateRunner highStateRunner3 = (HighStateRunner) saltJobIdTrackers.get(2).getSaltJobRunner();
assertTrue(highStateRunner3.getTargetHostnames().contains("replica1.domain"));
assertEquals(1, highStateRunner3.getTargetHostnames().size());
}
use of com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase in project cloudbreak by hortonworks.
the class SaltOrchestratorTest method testNewInstanceIsPrimaryReplacement.
@Test
public void testNewInstanceIsPrimaryReplacement() throws Exception {
ObjectMapper mapper = new ObjectMapper();
GatewayConfig newInstance = mock(GatewayConfig.class);
GatewayConfig replica2 = mock(GatewayConfig.class);
Node newInstanceNode = mock(Node.class);
Node replicaNode2 = mock(Node.class);
when(newInstance.getHostname()).thenReturn("newInstance.domain");
when(replica2.getHostname()).thenReturn("replica2.domain");
when(newInstanceNode.getHostname()).thenReturn("newInstance.domain");
when(replicaNode2.getHostname()).thenReturn("replica2.domain");
PowerMockito.mockStatic(SaltStates.class);
HostAndRoleTarget replicaTarget = new HostAndRoleTarget("freeipa_replica", Set.of("newInstance.domain", "replica2.domain"));
ArrayNode replicaRole = mapper.createArrayNode();
replicaRole.add("freeipa_replica");
PowerMockito.when(SaltStates.getGrains(eq(saltConnector), eq(replicaTarget), eq("roles"))).thenReturn(Map.of("replica2.domain", replicaRole));
saltOrchestrator.installFreeIpa(newInstance, List.of(newInstance, replica2), Set.of(newInstanceNode, replicaNode2), exitCriteriaModel);
ArgumentCaptor<ModifyGrainBase> modifyGrainCaptor = ArgumentCaptor.forClass(ModifyGrainBase.class);
verify(saltCommandRunner, times(2)).runModifyGrainCommand(eq(saltConnector), modifyGrainCaptor.capture(), eq(exitCriteriaModel), eq(exitCriteria));
List<ModifyGrainBase> modifyGrains = modifyGrainCaptor.getAllValues();
GrainAddRunner addReplacementRole = (GrainAddRunner) modifyGrains.get(0);
assertEquals("freeipa_primary_replacement", addReplacementRole.getValue());
assertEquals(Set.of("newInstance.domain"), addReplacementRole.getTargetHostnames());
GrainRemoveRunner removeReplicaRole = (GrainRemoveRunner) modifyGrains.get(1);
assertEquals("freeipa_replica", removeReplicaRole.getValue());
assertEquals(Set.of("newInstance.domain"), removeReplicaRole.getTargetHostnames());
ArgumentCaptor<SaltJobIdTracker> saltJobIdTrackerCaptor = ArgumentCaptor.forClass(SaltJobIdTracker.class);
verify(saltRunner, times(2)).runner(saltJobIdTrackerCaptor.capture(), eq(exitCriteria), eq(exitCriteriaModel), anyInt(), anyBoolean());
List<SaltJobIdTracker> saltJobIdTrackers = saltJobIdTrackerCaptor.getAllValues();
HighStateRunner highStateRunner = (HighStateRunner) saltJobIdTrackers.get(0).getSaltJobRunner();
assertTrue(highStateRunner.getTargetHostnames().contains("replica2.domain"));
assertEquals(1, highStateRunner.getTargetHostnames().size());
HighStateRunner highStateRunner2 = (HighStateRunner) saltJobIdTrackers.get(1).getSaltJobRunner();
assertTrue(highStateRunner2.getTargetHostnames().contains("newInstance.domain"));
assertEquals(1, highStateRunner2.getTargetHostnames().size());
}
use of com.sequenceiq.cloudbreak.orchestrator.salt.poller.checker.ModifyGrainBase in project cloudbreak by hortonworks.
the class SaltOrchestrator method runOrchestratorGrainRunner.
@Override
public void runOrchestratorGrainRunner(OrchestratorGrainRunnerParams grainRunnerParams) throws CloudbreakOrchestratorFailedException {
try (SaltConnector sc = saltService.createSaltConnector(grainRunnerParams.getPrimaryGatewayConfig())) {
Set<String> targetHostNames = grainRunnerParams.getTargetHostNames();
GrainOperation grainOperation = grainRunnerParams.getGrainOperation();
Set<Node> allNodes = grainRunnerParams.getAllNodes();
String key = grainRunnerParams.getKey();
String value = grainRunnerParams.getValue();
ModifyGrainBase runner;
switch(grainOperation) {
case ADD:
runner = new GrainAddRunner(targetHostNames, allNodes, key, value);
break;
case REMOVE:
runner = new GrainRemoveRunner(targetHostNames, allNodes, key, value);
break;
default:
throw new IllegalStateException("Unexpected value: " + grainOperation);
}
saltCommandRunner.runModifyGrainCommand(sc, runner, grainRunnerParams.getExitCriteriaModel(), exitCriteria);
} catch (Exception e) {
LOGGER.error("Exception during grain runner", e);
throw new CloudbreakOrchestratorFailedException(e.getMessage(), e);
}
}
Aggregations