@Test public void testExtractNames() { List<Component> components = new ArrayList<Component> (); components.add( new Component( "c1" )); components.add( new Component( "c2" )); List<String> names = ComponentHelpers.extractNames( components ); Assert.assertEquals( 2, names.size()); Assert.assertEquals( "c1", names.get( 0 )); Assert.assertEquals( "c2", names.get( 1 )); }
@Test public void testParseTargetProperties_inexistingDirectory() throws Exception { List<ModelError> errors = TargetValidator.parseTargetProperties( new File( "whatever" ), new Component( "c" )); Assert.assertEquals( 0, errors.size()); }
@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 testParseTargetProperties_ok() throws Exception { File dir = new File( this.folder.newFolder(), Constants.PROJECT_DIR_GRAPH + "/c" ); Utils.createDirectory( dir ); Utils.writeStringInto( "id: tid1\nhandler: in", new File( dir, "t1.properties" )); List<ModelError> errors = TargetValidator.parseTargetProperties( dir.getParentFile().getParentFile(), new Component( "c" )); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_TARGET_NO_NAME, errors.get( 0 ).getErrorCode()); }
@Test public void testParseTargetProperties_noProperties() throws Exception { File dir = new File( this.folder.newFolder(), Constants.PROJECT_DIR_GRAPH + "/c" ); Utils.createDirectory( dir ); Utils.writeStringInto( "id: tid1\nhandler: in", new File( dir, "t1.not-properties" )); List<ModelError> errors = TargetValidator.parseTargetProperties( dir, new Component( "c" )); Assert.assertEquals( 0, errors.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 testPuppetValidation_twoModules() throws Exception { File appDir = this.folder.newFolder(); Component comp = new Component( "toto" ).installerName( "puppet" ); File directory = ResourceUtils.findInstanceResourcesDirectory( appDir, comp ); Assert.assertTrue( new File( directory, "roboconf_toto" ).mkdirs()); Assert.assertTrue( new File( directory, "roBoconf_titi" ).mkdirs()); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_PUPPET_HAS_TOO_MANY_RBCF_MODULES, errors.get( 0 ).getErrorCode()); }
@Test public void testScriptValidation_noScriptAtAll() throws Exception { File appDir = this.folder.newFolder(); Component comp = new Component( "toto" ).installerName( "script" ); File directory = ResourceUtils.findInstanceResourcesDirectory( appDir, comp ); Assert.assertTrue( new File( directory, RecipesValidator.SCRIPTS_DIR_NAME ).mkdirs()); Assert.assertEquals( 0, RecipesValidator.validateComponentRecipes( appDir, comp ).size()); }
@Test public void testPuppetValidation_validUpdatePp() throws Exception { File appDir = this.folder.newFolder(); Component comp = new Component( "toto" ).installerName( "puppet" ); File directory = ResourceUtils.findInstanceResourcesDirectory( appDir, comp ); Assert.assertTrue( new File( directory, "roboconf_toto/manifests" ).mkdirs()); File targetFile = new File( directory, "roboconf_toto/manifests/update.pp" ); File inputFile = TestUtils.findTestFile( "/recipes/update.pp" ); Utils.copyStream( inputFile, targetFile ); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 0, errors.size()); }
@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 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 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 testEmptyRecipeDirectory() throws Exception { File appDir = this.folder.newFolder(); Component comp = new Component("toto").installerName("script"); File directory = ResourceUtils.findInstanceResourcesDirectory(appDir, comp); Assert.assertTrue(directory.mkdirs()); List<ModelError> errors = RecipesValidator.validateComponentRecipes(appDir, comp); Assert.assertEquals(0, errors.size()); }
@Test public void testScriptValidation_noScriptsDirectory() throws Exception { File appDir = this.folder.newFolder(); Component comp = new Component( "toto" ).installerName( "script" ); File directory = ResourceUtils.findInstanceResourcesDirectory( appDir, comp ); Assert.assertTrue( directory.mkdirs()); Utils.writeStringInto( "", new File( directory, "test.sh" )); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_SCRIPT_NO_SCRIPTS_DIR, errors.get( 0 ).getErrorCode()); }
@Test public void testPuppetValidation_noModule() throws Exception { File appDir = this.folder.newFolder(); Component comp = new Component( "toto" ).installerName( "puppet" ); File directory = ResourceUtils.findInstanceResourcesDirectory( appDir, comp ); Assert.assertTrue( directory.mkdirs()); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_PUPPET_HAS_NO_RBCF_MODULE, errors.get( 0 ).getErrorCode()); }
@Test public void testPuppetValidation_oneModuleButWildCardImports() throws Exception { File appDir = this.folder.newFolder(); Component comp = new Component( "toto" ).installerName( "puppet" ); comp.addImportedVariable( new ImportedVariable( "Other.*", true, false )); File directory = ResourceUtils.findInstanceResourcesDirectory( appDir, comp ); Assert.assertTrue( new File( directory, "roboconf_toto" ).mkdirs()); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_PUPPET_DISLIKES_WILDCARD_IMPORTS, errors.get( 0 ).getErrorCode()); }
@Test public void testFixVariableName() { Component comp = new Component( "coMp" ); Assert.assertEquals( "coMp.ip", ComponentHelpers.fixVariableName( comp, "ip" )); Assert.assertEquals( "coMp.ip", ComponentHelpers.fixVariableName( comp, "coMp.ip" )); Facet facet = new Facet( "f" ); Assert.assertEquals( "f.port", ComponentHelpers.fixVariableName( facet, "port" )); Assert.assertEquals( "f.ip", ComponentHelpers.fixVariableName( facet, "f.ip" )); }
@Test public void testIsTarget() { Instance inst = new Instance( "i" ); Assert.assertFalse( InstanceHelpers.isTarget( inst )); inst.setComponent( new Component( "comp" ).installerName( "whatever" )); Assert.assertFalse( InstanceHelpers.isTarget( inst )); inst.getComponent().setInstallerName( Constants.TARGET_INSTALLER ); Assert.assertTrue( InstanceHelpers.isTarget( inst )); }