@Test public void testFindInstanceDirectoryOnAgent() { File f = InstanceHelpers.findInstanceDirectoryOnAgent( new Instance( "inst" ) .component( new Component( "c" ).installerName( "my-plugin" ))); File tempDir = new File( System.getProperty( "java.io.tmpdir" )); Assert.assertTrue( f.getAbsolutePath().startsWith( tempDir.getAbsolutePath())); Assert.assertTrue( f.getAbsolutePath().contains( "inst" )); Assert.assertTrue( f.getAbsolutePath().contains( "my-plugin" )); }
@Test public void testFindPrefixesForExportedVariables_withInstanceVariablesOnly() { Instance instance = new Instance( "inst" ).component( new Component( "comp" )); instance.overriddenExports.put( "comp.ip", "" ); instance.overriddenExports.put( "comp.split.property", "" ); instance.overriddenExports.put( "comp.port", "8000" ); instance.overriddenExports.put( "facet.desc", "some description" ); Set<String> prefixes = VariableHelpers.findPrefixesForExportedVariables( instance ); Assert.assertEquals( 2, prefixes.size()); Assert.assertTrue( prefixes.contains( "comp" )); Assert.assertTrue( prefixes.contains( "facet" )); }
@Test public void testInstances() { List<Instance> instances = new ArrayList<> (); for( int i=0; i<10; i++ ) { Instance inst = new Instance( "inst-" + i ).component( new Component( "comp" )); instances.add( inst ); } Assert.assertEquals( 0, RuntimeModelValidator.validate( instances ).size()); }
@Test public void testStoreInstanceResources_inexistingDirectory() throws Exception { Instance instance = new Instance( "whatever" ).component( new Component( "comp" )); Map<?,?> map = ResourceUtils.storeInstanceResources( new File( "file/does/not/exist" ), instance ); Assert.assertEquals( 0, map.size()); }
@Test public void testStoreInstanceResources_notADirectory() throws Exception { final File appDir = this.folder.newFolder(); final String componentName = "my-component"; final File componentDirectory = new File( appDir, Constants.PROJECT_DIR_GRAPH + File.separator + componentName ); Assert.assertTrue( componentDirectory.getParentFile().mkdirs()); Assert.assertTrue( componentDirectory.createNewFile()); Instance instance = new Instance( "whatever" ).component( new Component( componentName )); Map<?,?> map = ResourceUtils.storeInstanceResources( appDir, instance ); Assert.assertEquals( 0, map.size()); }
@Test public void testWriteInstances_notEmpty() throws Exception { Component component = new Component( "comp" ); Instance instance = new Instance( "inst" ).component( component ).status( InstanceStatus.DEPLOYING ).channel( "c" ); instance.overriddenExports.put( "check", "true" ); instance.data.put( "something", "else" ); File targetFile = this.folder.newFile(); RuntimeModelIo.writeInstances( targetFile, Arrays.asList( instance )); Assert.assertTrue( targetFile.exists()); Assert.assertTrue( 0 < targetFile.length()); }
@Test public void testFindPrefixesForExportedVariables_withVariable() { Instance instance = new Instance( "inst" ).component( new Component( "comp" )); Set<String> prefixes = VariableHelpers.findPrefixesForExportedVariables( instance ); Assert.assertEquals( 0, prefixes.size()); }
@Test public void testOneInstance() throws Exception { Graphs graphs = buildGraphs(); Component vmComponent = ComponentHelpers.findComponent( graphs, "VM" ); Assert.assertNotNull( vmComponent ); Instance inst = new Instance( "inst" ).component( vmComponent ); compareInstances( graphs, Arrays.asList( inst ), false, false ); }
@Test public void testOneInstanceWithComments() throws Exception { Graphs graphs = buildGraphs(); Component vmComponent = ComponentHelpers.findComponent( graphs, "VM" ); Assert.assertNotNull( vmComponent ); Instance inst = new Instance( "inst" ).component( vmComponent ); compareInstances( graphs, Arrays.asList( inst ), true, false ); }
@Test public void testRemoveOffScopeInstances_zero() { Instance root = new Instance( "root" ).component( new Component( "Root" ).installerName( Constants.TARGET_INSTALLER )); Instance server = new Instance( "server" ).component( new Component( "Server" ).installerName( "whatever" )); Instance app1 = new Instance( "app1" ).component( new Component( "Application" ).installerName( "whatever" )); Instance app2 = new Instance( "app2" ).component( new Component( "Application" ).installerName( "whatever" )); Instance server2 = new Instance( "server2" ).component( new Component( "Server" ).installerName( "whatever" )); InstanceHelpers.insertChild( root, server ); InstanceHelpers.insertChild( root, server2 ); InstanceHelpers.insertChild( server, app1 ); InstanceHelpers.insertChild( server, app2 ); Assert.assertEquals( 5, InstanceHelpers.buildHierarchicalList( root ).size()); InstanceHelpers.removeOffScopeInstances( root ); Assert.assertEquals( 5, InstanceHelpers.buildHierarchicalList( root ).size()); }
@Test public void testRemoveOffScopeInstances_oneTarget() { Instance root = new Instance( "root" ).component( new Component( "Root" ).installerName( Constants.TARGET_INSTALLER )); Instance server = new Instance( "server" ).component( new Component( "Server" ).installerName( Constants.TARGET_INSTALLER )); Instance app1 = new Instance( "app1" ).component( new Component( "Application" ).installerName( "whatever" )); Instance app2 = new Instance( "app2" ).component( new Component( "Application" ).installerName( "whatever" )); Instance server2 = new Instance( "server2" ).component( new Component( "Server" ).installerName( "whatever" )); InstanceHelpers.insertChild( root, server ); InstanceHelpers.insertChild( root, server2 ); InstanceHelpers.insertChild( server, app1 ); InstanceHelpers.insertChild( server2, app2 ); Assert.assertEquals( 5, InstanceHelpers.buildHierarchicalList( root ).size()); InstanceHelpers.removeOffScopeInstances( root ); List<Instance> instances = InstanceHelpers.buildHierarchicalList( root ); Assert.assertEquals( 3, instances.size()); Assert.assertTrue( instances.contains( root )); Assert.assertTrue( instances.contains( server2 )); Assert.assertTrue( instances.contains( app2 )); }
@Test public void testFindAllScopedInstances() { Instance root = new Instance( "root" ).component( new Component( "Root" ).installerName( Constants.TARGET_INSTALLER )); Instance server = new Instance( "server" ).component( new Component( "Server" ).installerName( Constants.TARGET_INSTALLER )); Instance app1 = new Instance( "app1" ).component( new Component( "Application" ).installerName( "whatever" )); Instance app2 = new Instance( "app2" ).component( new Component( "Application" ).installerName( Constants.TARGET_INSTALLER )); Instance server2 = new Instance( "server2" ).component( new Component( "Server" ).installerName( "whatever" )); InstanceHelpers.insertChild( root, server ); InstanceHelpers.insertChild( root, server2 ); InstanceHelpers.insertChild( server, app1 ); InstanceHelpers.insertChild( server2, app2 ); Application app = new Application( "test", new ApplicationTemplate()); app.getRootInstances().add( root ); List<Instance> instances = InstanceHelpers.findAllScopedInstances( app ); Assert.assertEquals( 3, instances.size()); Assert.assertTrue( instances.contains( root )); Assert.assertTrue( instances.contains( server )); Assert.assertTrue( instances.contains( app2 )); }
@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()); }
@Test public void testFindScopedInstance() { Instance root = new Instance( "root" ).component( new Component( "Root" ).installerName( Constants.TARGET_INSTALLER )); Instance server = new Instance( "server" ).component( new Component( "Server" ).installerName( Constants.TARGET_INSTALLER )); Instance app1 = new Instance( "app1" ).component( new Component( "Application" ).installerName( "whatever" )); Instance app2 = new Instance( "app2" ).component( new Component( "Application" ).installerName( "whatever" )); Instance server2 = new Instance( "server2" ).component( new Component( "Server" ).installerName( "whatever" )); InstanceHelpers.insertChild( root, server ); InstanceHelpers.insertChild( root, server2 ); InstanceHelpers.insertChild( server, app1 ); InstanceHelpers.insertChild( server2, app2 ); Assert.assertEquals( root, InstanceHelpers.findScopedInstance( root )); Assert.assertEquals( server, InstanceHelpers.findScopedInstance( server )); Assert.assertEquals( server, InstanceHelpers.findScopedInstance( app1 )); Assert.assertEquals( root, InstanceHelpers.findScopedInstance( app2 )); }
@Override public void execute() throws CommandException { // Resolve runtime structures Instance parentInstance = resolveInstance( this.instr, this.instr.getParentInstancePath(), true ); ManagedApplication ma = resolveManagedApplication( this.manager, this.instr ); // Verify we can create new VMs in the model verify( this.executionContext, this.instr.getComponent()); // Execute the command try { Instance instance = new Instance( this.instr.getInstanceName()).component( this.instr.getComponent()); this.manager.instancesMngr().addInstance( ma, parentInstance, instance ); update( this.executionContext, instance ); } catch( Exception e ) { throw new CommandException( e ); } }
@Test public void testFindPrefixesForImportedVariables() { Component component = new Component( "comp" ); component.addImportedVariable( new ImportedVariable( "comp.ip", false, false )); component.addImportedVariable( new ImportedVariable( "comp.split.property", false, false )); component.addImportedVariable( new ImportedVariable( "comp.port", false, false )); component.addImportedVariable( new ImportedVariable( "facet.desc", false, false )); Instance instance = new Instance( "inst" ).component( component ); Set<String> prefixes = VariableHelpers.findPrefixesForImportedVariables( instance ); Assert.assertEquals( 2, prefixes.size()); Assert.assertTrue( prefixes.contains( "comp" )); Assert.assertTrue( prefixes.contains( "facet" )); component.importedVariables.clear(); prefixes = VariableHelpers.findPrefixesForImportedVariables( instance ); Assert.assertEquals( 0, prefixes.size()); }
@Test public void testFindInstanceResourcesDirectory_withCycle() throws Exception { final File appDir = this.folder.newFolder(); final Component c1 = new Component( "c1" ); final Component c2 = new Component( "c2" ); final Component c3 = new Component( "c3" ); c1.extendComponent( c2 ); c2.extendComponent( c3 ); c3.extendComponent( c1 ); Assert.assertFalse( ResourceUtils.findInstanceResourcesDirectory( appDir, c1 ).exists()); Assert.assertFalse( ResourceUtils.findInstanceResourcesDirectory( appDir, c2 ).exists()); Assert.assertFalse( ResourceUtils.findInstanceResourcesDirectory( appDir, c3 ).exists()); Map<?,?> map = ResourceUtils.storeInstanceResources( appDir, new Instance( "i" ).component( c3 )); Assert.assertEquals( 0, map.size()); }
@Test public void testChain() { Instance inst = new Instance().name( "ins" ).status( InstanceStatus.DEPLOYING ).component( null ).parent( null ); Assert.assertEquals( 0, inst.channels.size()); Assert.assertEquals( "ins", inst.getName()); Assert.assertEquals( InstanceStatus.DEPLOYING, inst.getStatus()); Assert.assertNull( inst.getComponent()); Assert.assertNull( inst.getParent()); Assert.assertEquals( 1, inst.channel( "woo" ).channels.size()); Assert.assertEquals( 2, inst.channel( "yeah" ).channels.size()); Assert.assertEquals( 2, inst.channel( "woo" ).channels.size()); }
@Test public void testFindPrefixesForMandatoryImportedVariables() { Component component = new Component( "comp" ); component.addImportedVariable( new ImportedVariable( "comp.ip", false, false )); component.addImportedVariable( new ImportedVariable( "comp.split.property", false, false )); component.addImportedVariable( new ImportedVariable( "comp.port", false, false )); component.addImportedVariable( new ImportedVariable( "facet.desc", true, false )); component.addImportedVariable( new ImportedVariable( "facet-n.prop1", true, false )); component.addImportedVariable( new ImportedVariable( "facet-n.prop2", false, false )); Instance instance = new Instance( "inst" ).component( component ); Set<String> prefixes = VariableHelpers.findPrefixesForMandatoryImportedVariables( instance ); Assert.assertEquals( 2, prefixes.size()); Assert.assertTrue( prefixes.contains( "comp" )); Assert.assertTrue( prefixes.contains( "facet-n" )); component.importedVariables.clear(); prefixes = VariableHelpers.findPrefixesForMandatoryImportedVariables( instance ); Assert.assertEquals( 0, prefixes.size()); }
@Test public void testMessage_addInstance() throws Exception { Instance child = new Instance( "child" ).channel( "channel 4" ).status( InstanceStatus.DEPLOYED_STOPPED ); child.component( new Component( "comp_child" ).installerName( "whatever" )); MsgCmdAddInstance msg = new MsgCmdAddInstance( child ); checkBasics( msg, MsgCmdAddInstance.class ); Instance root = new Instance( "root" ).status( InstanceStatus.DEPLOYED_STARTED ).channel( "channel1" ).channel( "channel2" ); root.component( new Component( "comp_root" ).installerName( "whatever" )); InstanceHelpers.insertChild( root, child ); msg = new MsgCmdAddInstance( child ); checkBasics( msg, MsgCmdAddInstance.class ); msg = new MsgCmdAddInstance( new Instance( "instance without component" )); checkBasics( msg, MsgCmdAddInstance.class ); }