@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 testFindAllExportedVariables_simple() { Component comp = new Component( "comp" ); Map<String,String> exports = ComponentHelpers.findAllExportedVariables( comp ); Assert.assertEquals( 0, exports.size()); comp.addExportedVariable( new ExportedVariable( "test", "ok" )); exports = ComponentHelpers.findAllExportedVariables( comp ); Assert.assertEquals( 1, exports.size()); Assert.assertEquals( "ok", exports.get( "comp.test" )); }
@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 testFindPrefixesForExportedVariables_withComponentVariables() { Component component = new Component( "comp" ); component.addExportedVariable( new ExportedVariable( "comp.ip", "" )); component.addExportedVariable( new ExportedVariable( "comp.split.property", "" )); component.addExportedVariable( new ExportedVariable( "comp.port", "8000" )); Instance instance = new Instance( "inst" ).component( component ); Set<String> prefixes = VariableHelpers.findPrefixesForExportedVariables( instance ); Assert.assertEquals( 1, prefixes.size()); Assert.assertTrue( prefixes.contains( "comp" )); Facet facet = new Facet( "facet" ); facet.addExportedVariable( new ExportedVariable( "something", "value" )); component.associateFacet( facet ); prefixes = VariableHelpers.findPrefixesForExportedVariables( instance ); Assert.assertEquals( 2, prefixes.size()); Assert.assertTrue( prefixes.contains( "comp" )); Assert.assertTrue( prefixes.contains( "facet" )); }
comp.addExportedVariable( new ExportedVariable( "comp.export1", "c1" )); comp.addExportedVariable( new ExportedVariable( "comp.export2", "c2" )); comp.addExportedVariable( new ExportedVariable( "comp.export3", "c3" ));
@Test public void testFindAllExportedVariables() { Instance instance = new Instance( "inst 1" ).component( new Component( "c" )); Assert.assertEquals( 0, InstanceHelpers.findAllExportedVariables( instance ).size()); instance.overriddenExports.put( "var1", "value1" ); Map<String,String> map = InstanceHelpers.findAllExportedVariables( instance ); Assert.assertEquals( 1, map.size()); Assert.assertEquals( "value1", map.get( "var1" )); Component component = new Component( "comp 1" ); component.addExportedVariable( new ExportedVariable( "var1", "another value" )); component.addExportedVariable( new ExportedVariable( "var2", "value2" )); instance.setComponent( component ); map = InstanceHelpers.findAllExportedVariables( instance ); Assert.assertEquals( 2, map.size()); Assert.assertEquals( "value1", map.get( "comp 1.var1" )); Assert.assertEquals( "value2", map.get( "comp 1.var2" )); instance.overriddenExports.clear(); map = InstanceHelpers.findAllExportedVariables( instance ); Assert.assertEquals( 2, map.size()); Assert.assertEquals( "another value", map.get( "comp 1.var1" )); Assert.assertEquals( "value2", map.get( "comp 1.var2" )); }
@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 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 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 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()); }
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 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 testHasAllRequiredImports_withWildcard() throws Exception { // Same test than "testHasAllRequiredImports_required" // but here, variable imports use a wild card. Component dbComponent = new Component( "database" ).installerName( "whatever" ); dbComponent.addExportedVariable( new ExportedVariable( "database.ip", null )); dbComponent.addExportedVariable( new ExportedVariable( "database.port", "3009" )); dbComponent.addExportedVariable( new ExportedVariable( "database.collection", "whatever" )); Component appServerComponent = new Component( "app-server" ).installerName( "whatever" ); appServerComponent.addExportedVariable( new ExportedVariable( "app-server.ip", null )); appServerComponent.addExportedVariable( new ExportedVariable( "app-server.port", "8009" )); appServerComponent.addImportedVariable( new ImportedVariable( "database.*", false, false )); Instance appServer = new Instance( "app server" ).component( appServerComponent ); appServer.overriddenExports.put( "app-server.ip", "192.168.1.15" ); appServer.setStatus( InstanceStatus.STARTING ); Instance database = new Instance( "database" ).component( dbComponent ); database.overriddenExports.put( "database.ip", "192.168.1.28" ); // The application server does not know about the database Assert.assertFalse( ImportHelpers.hasAllRequiredImports( appServer, Logger.getAnonymousLogger())); // The application server is now aware of the database ImportHelpers.addImport( appServer, "database", new Import( database )); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( appServer, null )); appServer.getImports().clear(); Assert.assertFalse( ImportHelpers.hasAllRequiredImports( appServer, null )); }
@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 ); }
@Test public void testHasAllRequiredImports_required() throws Exception { Component dbComponent = new Component( "database" ).installerName( "whatever" ); dbComponent.addExportedVariable( new ExportedVariable( "database.ip", null )); dbComponent.addExportedVariable( new ExportedVariable( "database.port", "3009" )); dbComponent.addExportedVariable( new ExportedVariable( "database.collection", "whatever" )); Component appServerComponent = new Component( "app-server" ).installerName( "whatever" ); appServerComponent.addExportedVariable( new ExportedVariable( "app-server.ip", null )); appServerComponent.addExportedVariable( new ExportedVariable( "app-server.port", "8009" )); appServerComponent.addImportedVariable( new ImportedVariable( "database.ip", false, false )); appServerComponent.addImportedVariable( new ImportedVariable( "database.port", false, false )); appServerComponent.addImportedVariable( new ImportedVariable( "database.collection", true, false )); Instance appServer = new Instance( "app server" ).component( appServerComponent ); appServer.overriddenExports.put( "app-server.ip", "192.168.1.15" ); appServer.setStatus( InstanceStatus.STARTING ); Instance database = new Instance( "database" ).component( dbComponent ); database.overriddenExports.put( "database.ip", "192.168.1.28" ); // The application server does not know about the database Assert.assertFalse( ImportHelpers.hasAllRequiredImports( appServer, Logger.getAnonymousLogger())); // The application server is now aware of the database ImportHelpers.addImport( appServer, "database", new Import( database )); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( appServer, null )); appServer.getImports().clear(); Assert.assertFalse( ImportHelpers.hasAllRequiredImports( appServer, null )); }
@Test public void test_N_InstantiationsWithConflict() throws Exception { // The graph 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 ); // The file to read File f = TestUtils.findTestFile( "/configurations/invalid/instanceof-name-conflict-with-count.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); Collection<Instance> rootInstances = fromDef.buildInstances( graphs, f ); Assert.assertEquals( 1, fromDef.getErrors().size()); Assert.assertEquals( ErrorCode.CO_CONFLICTING_INFERRED_INSTANCE, fromDef.getErrors().iterator().next().getErrorCode()); // The assertions Application app = new Application( new ApplicationTemplate()); app.getRootInstances().addAll( rootInstances ); Assert.assertEquals( 5, rootInstances.size()); // Should be 6, but there two have the same path. // So, one is overriding the other. }
@Test public void testFromGraphs_threeFacets() throws Exception { Graphs graphs = new Graphs(); Component cA = new Component( "A" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cA ); Facet facet = new Facet( "my-facet-1" ); facet.addExportedVariable( new ExportedVariable( "data", "coucou" )); cA.associateFacet( facet ); 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 )); cA.addImportedVariable( new ImportedVariable( "facetF.props", false, false )); Component cB = new Component( "B" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cB ); facet = new Facet( "facetF" ); facet.addExportedVariable( new ExportedVariable( "facetF.props", "some value" )); cB.associateFacet( facet ); facet = new Facet( "my-facet-2" ); facet.addExportedVariable( new ExportedVariable( "my-facet-2.woo", "woo" )); cB.associateFacet( facet ); cB.addExportedVariable( new ExportedVariable( "B.port", "9000" )); cB.addExportedVariable( new ExportedVariable( "B.ip", null )); compareGraphs( graphs, false ); }
@Test public void testHasAllRequiredImports_optional() throws Exception { Component clusterNodeComponent = new Component( "cluster" ).installerName( "whatever" ); clusterNodeComponent.addImportedVariable( new ImportedVariable( "cluster.ip", true, false )); clusterNodeComponent.addImportedVariable( new ImportedVariable( "cluster.port", true, false )); clusterNodeComponent.addExportedVariable( new ExportedVariable( "cluster.ip", null )); clusterNodeComponent.addExportedVariable( new ExportedVariable( "cluster.port", "9007" )); Instance i1 = new Instance( "inst 1" ).component( clusterNodeComponent ); i1.overriddenExports.put( "cluster.ip", "192.168.1.15" ); i1.setStatus( InstanceStatus.STARTING ); Instance i2 = new Instance( "inst 2" ).component( clusterNodeComponent ); i2.overriddenExports.put( "cluster.ip", "192.168.1.28" ); // The cluster node does not know about the other node Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i1, null )); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i2, null )); // The node is now aware of another node ImportHelpers.addImport( i1, "cluster", new Import( i2 )); i1.setStatus( InstanceStatus.STARTING ); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i1, null )); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i2, null )); i1.getImports().clear(); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i1, null )); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i2, null )); }
tomcatComponent.addExportedVariable( new ExportedVariable( "Tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "Tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent );