BlueprintValidatorImpl implements BlueprintValidator { @Override public void validateTopology() throws InvalidTopologyException { LOGGER.info("Validating topology for blueprint: [{}]", blueprint.getName()); Collection<HostGroup> hostGroups = blueprint.getHostGroups().values(); Map<String, Map<String, Collection<DependencyInfo>>> missingDependencies = new HashMap<String, Map<String, Collection<DependencyInfo>>>(); for (HostGroup group : hostGroups) { Map<String, Collection<DependencyInfo>> missingGroupDependencies = validateHostGroup(group); if (! missingGroupDependencies.isEmpty()) { missingDependencies.put(group.getName(), missingGroupDependencies); } } Collection<String> cardinalityFailures = new HashSet<String>(); Collection<String> services = blueprint.getServices(); for (String service : services) { for (String component : stack.getComponents(service)) { Cardinality cardinality = stack.getCardinality(component); AutoDeployInfo autoDeploy = stack.getAutoDeployInfo(component); if (cardinality.isAll()) { cardinalityFailures.addAll(verifyComponentInAllHostGroups(component, autoDeploy)); } else { cardinalityFailures.addAll(verifyComponentCardinalityCount( component, cardinality, autoDeploy)); } } } if (! missingDependencies.isEmpty() || ! cardinalityFailures.isEmpty()) { generateInvalidTopologyException(missingDependencies, cardinalityFailures); } } BlueprintValidatorImpl(Blueprint blueprint); @Override void validateTopology(); @Override void validateRequiredProperties(); Collection<String> verifyComponentCardinalityCount(String component,
                                                            Cardinality cardinality,
                                                            AutoDeployInfo autoDeploy);  }
@Test public void testValidateTopology_basic() throws Exception { group1Components.add("component1"); group1Components.add("component1"); services.addAll(Arrays.asList("service1", "service2")); expect(stack.getComponents("service1")).andReturn(Collections.singleton("component1")).anyTimes(); expect(stack.getComponents("service2")).andReturn(Collections.singleton("component2")).anyTimes(); expect(blueprint.getHostGroupsForComponent("component1")).andReturn(Collections.singleton(group1)).anyTimes(); expect(blueprint.getHostGroupsForComponent("component2")).andReturn(Arrays.asList(group1, group2)).anyTimes(); replay(blueprint, stack, group1, group2, dependency1); BlueprintValidator validator = new BlueprintValidatorImpl(blueprint); validator.validateTopology(); }
@Test(expected = InvalidTopologyException.class) public void testValidateTopology_basic_negative() throws Exception { group1Components.add("component2"); services.addAll(Collections.singleton("service1")); expect(stack.getComponents("service1")).andReturn(Arrays.asList("component1", "component2")).anyTimes(); expect(blueprint.getHostGroupsForComponent("component1")).andReturn(Collections.<HostGroup>emptyList()).anyTimes(); expect(blueprint.getHostGroupsForComponent("component2")).andReturn(Arrays.asList(group1, group2)).anyTimes(); replay(blueprint, stack, group1, group2, dependency1); BlueprintValidator validator = new BlueprintValidatorImpl(blueprint); validator.validateTopology(); }
@Test public void testValidateTopology_autoDeploy() throws Exception { group1Components.add("component2"); services.addAll(Collections.singleton("service1")); expect(blueprint.getHostGroupsForComponent("component1")).andReturn(Collections.<HostGroup>emptyList()).anyTimes(); expect(blueprint.getHostGroupsForComponent("component2")).andReturn(Arrays.asList(group1, group2)).anyTimes(); expect(stack.getComponents("service1")).andReturn(Arrays.asList("component1", "component2")).anyTimes(); expect(stack.getAutoDeployInfo("component1")).andReturn(autoDeploy).anyTimes(); expect(group1.addComponent("component1")).andReturn(true).once(); replay(blueprint, stack, group1, group2, dependency1); BlueprintValidator validator = new BlueprintValidatorImpl(blueprint); validator.validateTopology(); verify(group1); }
@Test public void testValidateTopology_autoDeploy_hasDependency() throws Exception { group1Components.add("component2"); dependencies1.add(dependency1); services.addAll(Collections.singleton("service1")); expect(blueprint.getHostGroupsForComponent("component1")).andReturn(Collections.<HostGroup>emptyList()).anyTimes(); expect(blueprint.getHostGroupsForComponent("component2")).andReturn(Arrays.asList(group1, group2)).anyTimes(); expect(blueprint.getHostGroupsForComponent("component3")).andReturn(Collections.<HostGroup>emptyList()).anyTimes(); expect(stack.getComponents("service1")).andReturn(Arrays.asList("component1", "component2")).anyTimes(); expect(stack.getComponents("service2")).andReturn(Collections.singleton("component3")).anyTimes(); expect(stack.getAutoDeployInfo("component1")).andReturn(autoDeploy).anyTimes(); AutoDeployInfo dependencyAutoDeploy = new AutoDeployInfo(); dependencyAutoDeploy.setEnabled(true); dependencyAutoDeploy.setCoLocate("service1/component1"); expect(dependency1.getScope()).andReturn("host").anyTimes(); expect(dependency1.getAutoDeploy()).andReturn(dependencyAutoDeploy).anyTimes(); expect(dependency1.getComponentName()).andReturn("component3").anyTimes(); expect(dependency1.getServiceName()).andReturn("service1").anyTimes(); expect(dependency1.getName()).andReturn("dependency1").anyTimes(); expect(dependencyComponentInfo.isClient()).andReturn(true).anyTimes(); expect(stack.getComponentInfo("component3")).andReturn(dependencyComponentInfo).anyTimes(); expect(group1.addComponent("component1")).andReturn(true).once(); expect(group1.addComponent("component3")).andReturn(true).once(); replay(blueprint, stack, group1, group2, dependency1, dependencyComponentInfo); BlueprintValidator validator = new BlueprintValidatorImpl(blueprint); validator.validateTopology(); verify(group1); }
@Test public void testShouldDependencyBeExcludedWenRelatedServiceIsNotInBlueprint() throws Exception { hostGroups.clear(); hostGroups.put("group1", group1); group1Components.add("component-1"); dependencies1.add(dependency1); services.addAll(Collections.singleton("service-1")); expect(blueprint.getHostGroupsForComponent("component-1")).andReturn(Arrays.asList(group1)).anyTimes(); expect(blueprint.getName()).andReturn("blueprint-1").anyTimes(); Cardinality cardinality = new Cardinality("1"); expect(stack.getComponents("service-1")).andReturn(Arrays.asList("component-1")).anyTimes(); expect(stack.getAutoDeployInfo("component-1")).andReturn(autoDeploy).anyTimes(); expect(stack.getDependenciesForComponent("component-1")).andReturn(dependencies1).anyTimes(); expect(stack.getCardinality("component-1")).andReturn(cardinality).anyTimes(); AutoDeployInfo dependencyAutoDeploy = new AutoDeployInfo(); dependencyAutoDeploy.setEnabled(true); dependencyAutoDeploy.setCoLocate("service1/component1"); expect(dependency1.getScope()).andReturn("host").anyTimes(); expect(dependency1.getAutoDeploy()).andReturn(dependencyAutoDeploy).anyTimes(); expect(dependency1.getComponentName()).andReturn("component-d").anyTimes(); expect(dependency1.getServiceName()).andReturn("service-d").anyTimes(); expect(dependency1.getName()).andReturn("dependency-1").anyTimes(); expect(dependencyComponentInfo.isClient()).andReturn(true).anyTimes(); expect(stack.getComponentInfo("component-d")).andReturn(dependencyComponentInfo).anyTimes(); replay(blueprint, stack, group1, group2, dependency1, dependencyComponentInfo); BlueprintValidator validator = new BlueprintValidatorImpl(blueprint); validator.validateTopology(); verify(group1); }
@Test(expected=InvalidTopologyException.class) public void testShouldThrowErrorWhenDependentComponentIsNotInBlueprint() throws Exception { hostGroups.clear(); hostGroups.put("group1", group1); group1Components.add("component-1"); dependencies1.add(dependency1); services.addAll(Collections.singleton("service-1")); expect(blueprint.getHostGroupsForComponent("component-1")).andReturn(Arrays.asList(group1)).anyTimes(); expect(blueprint.getName()).andReturn("blueprint-1").anyTimes(); Cardinality cardinality = new Cardinality("1"); expect(stack.getComponents("service-1")).andReturn(Arrays.asList("component-1")).anyTimes(); expect(stack.getAutoDeployInfo("component-1")).andReturn(autoDeploy).anyTimes(); expect(stack.getDependenciesForComponent("component-1")).andReturn(dependencies1).anyTimes(); expect(stack.getCardinality("component-1")).andReturn(cardinality).anyTimes(); AutoDeployInfo dependencyAutoDeploy = null; expect(dependency1.getScope()).andReturn("host").anyTimes(); expect(dependency1.getAutoDeploy()).andReturn(dependencyAutoDeploy).anyTimes(); expect(dependency1.getComponentName()).andReturn("component-d").anyTimes(); expect(dependency1.getServiceName()).andReturn("service-d").anyTimes(); expect(dependency1.getName()).andReturn("dependency-1").anyTimes(); expect(stack.getComponentInfo("component-d")).andReturn(dependencyComponentInfo).anyTimes(); replay(blueprint, stack, group1, group2, dependency1, dependencyComponentInfo); BlueprintValidator validator = new BlueprintValidatorImpl(blueprint); validator.validateTopology(); verify(group1); }
@Test(expected=InvalidTopologyException.class) public void testWhenComponentIsConditionallyDependentAndOnlyOneOfTheConditionsIsSatisfied() throws Exception { hostGroups.clear(); hostGroups.put("group1", group1); group1Components.add("component-1"); dependencies1.add(dependency1); dependencies1.add(dependency2); services.addAll(Collections.singleton("service-1")); expect(blueprint.getHostGroupsForComponent("component-1")).andReturn(Arrays.asList(group1)).anyTimes(); expect(blueprint.getName()).andReturn("blueprint-1").anyTimes(); Map<String, Map<String, String>> properties = new HashMap<String, Map<String, String>>(); Map<String, String> typeProps = new HashMap<String, String>(); typeProps.put("yarn.resourcemanager.hostname", "testhost"); properties.put("yarn-site", typeProps); Configuration clusterConfig = new Configuration(properties, Collections.<String, Map<String, Map<String, String>>>emptyMap()); Cardinality cardinality = new Cardinality("1"); expect(stack.getComponents("service-1")).andReturn(Arrays.asList("component-1")).anyTimes(); expect(stack.getAutoDeployInfo("component-1")).andReturn(autoDeploy).anyTimes(); expect(stack.getDependenciesForComponent("component-1")).andReturn(dependencies1).anyTimes(); expect(stack.getCardinality("component-1")).andReturn(cardinality).anyTimes(); AutoDeployInfo dependencyAutoDeploy = null; expect(dependency1.getScope()).andReturn("host").anyTimes(); expect(dependency1.getAutoDeploy()).andReturn(dependencyAutoDeploy).anyTimes(); expect(dependency1.getComponentName()).andReturn("component-d").anyTimes(); expect(dependency1.getServiceName()).andReturn("service-d").anyTimes(); expect(dependency1.getName()).andReturn("dependency-1").anyTimes(); expect(dependency1.hasDependencyConditions()).andReturn(true).anyTimes(); expect(dependency1.getDependencyConditions()).andReturn(dependenciesConditionInfos1).anyTimes(); expect(dependency2.getScope()).andReturn("host").anyTimes(); expect(dependency2.getAutoDeploy()).andReturn(dependencyAutoDeploy).anyTimes(); expect(dependency2.getComponentName()).andReturn("component-d").anyTimes(); expect(dependency2.getServiceName()).andReturn("service-d").anyTimes(); expect(dependency2.getName()).andReturn("dependency-2").anyTimes(); expect(dependency2.hasDependencyConditions()).andReturn(false).anyTimes(); expect(dependencyConditionInfo1.isResolved(EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(true).anyTimes(); expect(dependencyConditionInfo2.isResolved(EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(false).anyTimes(); expect(dependencyComponentInfo.isClient()).andReturn(false).anyTimes(); expect(stack.getComponentInfo("component-d")).andReturn(dependencyComponentInfo).anyTimes(); replay(blueprint, stack, group1, group2, dependency1, dependency2, dependencyComponentInfo,dependencyConditionInfo1,dependencyConditionInfo2); BlueprintValidator validator = new BlueprintValidatorImpl(blueprint); validator.validateTopology(); verify(group1); }