/** * Finds a component by name from another component. * @param component the component used to build a partial graph (should not be null) * @param componentName the component name (not null) * @return a component (can be null) */ public static Component findComponentFrom( Component component, String componentName ) { Graphs partialGraph = new Graphs(); if( component != null ) partialGraph.getRootComponents().add( component ); return findComponent( partialGraph, componentName ); }
private Graphs buildFinalGraphs() { Graphs result = new Graphs(); for( ComponentData cd : this.componentNameToComponentData.values()) { if( ComponentHelpers.findAllAncestors( cd.object ).isEmpty()) result.getRootComponents().add( cd.object ); } for( FacetData data : this.facetNameToFacetData.values()) result.getFacetNameToFacet().put( data.object.getName(), data.object ); return result; }
@Test public void testInstancesWithWrongImport() throws Exception { Graphs graphs = new Graphs(); graphs.getRootComponents().add( new Component( "VM" ).installerName( "target" )); File f = TestUtils.findTestFile( "/configurations/invalid/instances-with-inexisting-import.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); fromDef.buildInstances( graphs, f ); Iterator<ParsingError> iterator = fromDef.getErrors().iterator(); Assert.assertEquals( ErrorCode.CO_UNREACHABLE_FILE, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
@Test public void testGraphs_notRoot() { Component comp1 = new Component( "comp1" ).installerName( Constants.TARGET_INSTALLER ); Component comp2 = new Component( "comp2" ).installerName( Constants.TARGET_INSTALLER ); comp1.addChild( comp2 ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( comp2 ); Iterator<ModelError> iterator = RuntimeModelValidator.validate( graphs ).iterator(); Assert.assertEquals( ErrorCode.RM_NOT_A_ROOT_COMPONENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
@Test public void testRuntimeData() throws Exception { // The graph Graphs graphs = new Graphs(); Component vmComponent = new Component( "vm" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( vmComponent ); // The file to read File f = TestUtils.findTestFile( "/configurations/valid/single-runtime-instance.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); Collection<Instance> rootInstances = fromDef.buildInstances( graphs, f ); Assert.assertEquals( 0, fromDef.getErrors().size()); // The assertions Assert.assertEquals( 1, rootInstances.size()); Instance instance = rootInstances.iterator().next(); Assert.assertEquals( "vm 1", instance.getName()); Assert.assertEquals( vmComponent, instance.getComponent()); Assert.assertEquals( InstanceStatus.DEPLOYED_STARTED, instance.getStatus()); Assert.assertEquals( 3, instance.data.size()); Assert.assertEquals( "127.0.0.1", instance.data.get( "ip" )); Assert.assertEquals( "mach-ID", instance.data.get( "machine-id" )); Assert.assertEquals( "something different", instance.data.get( "whatever" )); }
@Test public void testOverriddenExports() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "Tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "Tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); File f = TestUtils.findTestFile( "/configurations/valid/instance-overridden-exports.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); Collection<Instance> rootInstances = fromDef.buildInstances( graphs, f ); Assert.assertEquals( 0, fromDef.getErrors().size()); Assert.assertEquals( 1, rootInstances.size()); Instance rootInstance = rootInstances.iterator().next(); Collection<Instance> instances = InstanceHelpers.buildHierarchicalList( rootInstance ); Assert.assertEquals( 2, instances.size()); Collection<ModelError> errors = RuntimeModelValidator.validate( instances ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.RM_MAGIC_INSTANCE_VARIABLE, errors.iterator().next().getErrorCode()); }
@Test public void testInstanceWithExtraData() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); File f = TestUtils.findTestFile( "/configurations/valid/instance-with-extra-data.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); Collection<Instance> rootInstances = fromDef.buildInstances( graphs, f ); Assert.assertEquals( 0, fromDef.getErrors().size()); Assert.assertEquals( 1, rootInstances.size()); Instance vmInstance = rootInstances.iterator().next(); Assert.assertEquals( 1, vmInstance.getChildren().size()); Assert.assertEquals( "VM1", vmInstance.getName()); Assert.assertEquals( 1, vmInstance.data.size()); Assert.assertEquals( "192.168.1.10", vmInstance.data.get( "ec2.elastic.ip" )); }
@Test public void testComponentResolutionWhenSurroundingSpaces() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component aComponent = new Component( "A" ).installerName( "whatever" ); vmComponent.addChild( aComponent ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); File f = TestUtils.findTestFile( "/configurations/valid/instance-with-space-after.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); Collection<Instance> rootInstances = fromDef.buildInstances( graphs, f ); Iterator<ParsingError> iterator = fromDef.getErrors().iterator(); Assert.assertFalse( iterator.hasNext()); Assert.assertEquals( 2, rootInstances.size()); for( Instance rootInstance : rootInstances ) { Assert.assertEquals( 1, rootInstance.getChildren().size()); Instance instance = rootInstance.getChildren().iterator().next(); Assert.assertEquals( "A", instance.getComponent().getName()); Assert.assertEquals( "A ", instance.getName()); } }
@Test public void testFindAllComponents_simple() { ApplicationTemplate app = new ApplicationTemplate(); Assert.assertEquals( 0, ComponentHelpers.findAllComponents( app ).size()); Graphs graphs = new Graphs(); app.setGraphs( graphs ); Component comp1 = new Component( "comp1" ); graphs.getRootComponents().add( comp1 ); Assert.assertEquals( 1, ComponentHelpers.findAllComponents( app ).size()); comp1.addChild( new Component( "comp-2" )); Assert.assertEquals( 2, ComponentHelpers.findAllComponents( app ).size()); Component comp3 = new Component( "comp_3" ); graphs.getRootComponents().add( comp3 ); comp3.addChild( new Component( "comp-2" )); Assert.assertEquals( 3, ComponentHelpers.findAllComponents( app ).size()); }
@Test public void testExtraData() throws Exception { // Parse Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); File f = TestUtils.findTestFile( "/configurations/valid/instance-with-extra-data.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); Collection<Instance> rootInstances = fromDef.buildInstances( graphs, f ); Assert.assertEquals( 0, fromDef.getErrors().size()); Assert.assertEquals( 1, rootInstances.size()); Instance vmInstance = rootInstances.iterator().next(); Assert.assertEquals( 1, vmInstance.getChildren().size()); Assert.assertEquals( "VM1", vmInstance.getName()); Assert.assertEquals( 1, vmInstance.data.size()); Assert.assertEquals( "192.168.1.10", vmInstance.data.get( "ec2.elastic.ip" )); // Write compareInstances( graphs, Arrays.asList( vmInstance ), true, true ); }
@Test public void testInexistingComponent() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); File f = TestUtils.findTestFile( "/configurations/invalid/inexisting-component.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); fromDef.buildInstances( graphs, f ); Iterator<ParsingError> iterator = fromDef.getErrors().iterator(); Assert.assertEquals( ErrorCode.CO_INEXISTING_COMPONENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
@Test public void testDuplicateInstance() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); File f = TestUtils.findTestFile( "/configurations/invalid/duplicate-instance.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); fromDef.buildInstances( graphs, f ); Iterator<ParsingError> iterator = fromDef.getErrors().iterator(); Assert.assertEquals( ErrorCode.CO_ALREADY_DEFINED_INSTANCE, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.CO_ALREADY_DEFINED_INSTANCE, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
@Test public void testInvalidChildInstance_2() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); // We cannot instantiate a VM under a VM Instance vmInstance = new Instance("vm" ).component( vmComponent ); Instance tomcatInstance = new Instance("tomcat" ).component( tomcatComponent ); InstanceHelpers.insertChild( vmInstance, tomcatInstance ); ApplicationTemplate app = new ApplicationTemplate( "app" ).version( "2.4" ).graphs( graphs ); app.getRootInstances().add( vmInstance ); Iterator<ModelError> iterator = RuntimeModelValidator.validate( app ).iterator(); Assert.assertEquals( ErrorCode.RM_INVALID_INSTANCE_PARENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
private Graphs buildGraphs() { Graphs graphs = new Graphs(); Component vmComponent = new Component( "VM" ).installerName( "target" ); graphs.getRootComponents().add( vmComponent ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "Tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "Tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Component warComponent = new Component( "WAR" ).installerName( "script" ); tomcatComponent.addChild( warComponent ); Assert.assertEquals( 0, RuntimeModelValidator.validate( graphs ).size()); return graphs; }
@Test public void testFromGraphs_withInheritance() throws Exception { Graphs graphs = new Graphs(); Component cA = new Component( "A" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cA ); cA.addExportedVariable( new ExportedVariable( "A.port", "9000" )); cA.addExportedVariable( new ExportedVariable( "A.ip", null )); Component cB = new Component( "B" ).installerName( Constants.TARGET_INSTALLER ); cB.extendComponent( cA ); graphs.getRootComponents().add( cB ); cB.addExportedVariable( new ExportedVariable( "A.port", "9000" )); cB.addExportedVariable( new ExportedVariable( "A.ip", null )); compareGraphs( graphs, false ); }
@Test public void testFromGraphs_noFacet() throws Exception { // Build a model Graphs graphs = new Graphs(); Component cA = new Component( "A" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cA ); cA.addExportedVariable( new ExportedVariable( "A.port", "9000" )); cA.addExportedVariable( new ExportedVariable( "A.ip", null )); cA.addImportedVariable( new ImportedVariable( "B.port", false, false )); cA.addImportedVariable( new ImportedVariable( "B.ip", true, false )); Component cB = new Component( "B" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cB ); cB.addExportedVariable( new ExportedVariable( "port", "9000" )); cB.addExportedVariable( new ExportedVariable( "ip", null )); // Write it into a file, load a new graph from this file and compare it with the current one compareGraphs( graphs, false ); // Verify exported variables are written correctly FileDefinition defToWrite = new FromGraphs().buildFileDefinition( graphs, null, false ); String s = ParsingModelIo.writeConfigurationFile( defToWrite, false, System.getProperty( "line.separator" )); Assert.assertTrue( s.contains( "port=\"9000\"" )); Assert.assertTrue( s.contains( "exports: ip, port=\"9000\";" )); }
@Test public void testFromGraphs_withExternal() throws Exception { Graphs graphs = new Graphs(); Component cA = new Component( "A" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cA ); cA.addExportedVariable( new ExportedVariable( "A.port", "9000" )); cA.addExportedVariable( new ExportedVariable( "A.ip", null )); cA.addImportedVariable( new ImportedVariable( "B.port", false, true )); cA.addImportedVariable( new ImportedVariable( "B.ip", true, false )); Component cB = new Component( "B" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cB ); cB.addExportedVariable( new ExportedVariable( "B.port", "9000" )); cB.addExportedVariable( new ExportedVariable( "B.ip", null )); compareGraphs( graphs, false ); }
@Test public void testFindComponent() { Graphs g = new Graphs(); Assert.assertNull( ComponentHelpers.findComponent( g, "c" )); Component c1 = new Component( "c1" ); g.getRootComponents().add( c1 ); Assert.assertEquals( c1, ComponentHelpers.findComponent( g, "c1" )); Component c2 = new Component( "c2" ); g.getRootComponents().add( c2 ); Assert.assertEquals( c2, ComponentHelpers.findComponent( g, "c2" )); Component c21 = new Component( "c21" ); c2.addChild( c21 ); Assert.assertEquals( c21, ComponentHelpers.findComponent( g, "c21" )); Component duplicateC1 = new Component( "c1" ); g.getRootComponents().add( duplicateC1 ); Assert.assertNotNull( ComponentHelpers.findComponent( g, "c1" )); TestApplication app = new TestApplication(); Assert.assertNotNull( ComponentHelpers.findComponent( app, "tomcat" )); Assert.assertNull( ComponentHelpers.findComponent( app, "tommy" )); }
@Test public void testChain() { ApplicationTemplate app = new ApplicationTemplate().name( "ins" ).description( "desc" ).version( "snapshot" ).graphs( new Graphs()); Assert.assertEquals( "ins", app.getName()); Assert.assertEquals( "desc", app.getDescription()); Assert.assertEquals( "snapshot", app.getVersion()); Assert.assertNotNull( app.getGraphs()); }
@Test public void testFromGraphs_oneFacet() throws Exception { Graphs graphs = new Graphs(); Component cA = new Component( "A" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cA ); cA.addImportedVariable( new ImportedVariable( "facetF.props", false, false )); cA.addExportedVariable( new ExportedVariable( "A.port", "9000" )); cA.addExportedVariable( new ExportedVariable( "A.ip", null )); cA.addImportedVariable( new ImportedVariable( "B.port", true, false )); cA.addImportedVariable( new ImportedVariable( "B.ip", true, false )); Component cB = new Component( "B" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cB ); Facet facetF = new Facet( "facetF" ); facetF.addExportedVariable( new ExportedVariable( "facetF.props", "something" )); cB.associateFacet( facetF ); cB.addExportedVariable( new ExportedVariable( "B.port", "9000" )); cB.addExportedVariable( new ExportedVariable( "B.ip", null )); compareGraphs( graphs, false ); }