RecipesValidator { public static List<ModelError> validateComponentRecipes( File applicationFilesDirectory, Component component ) { List<ModelError> result; if( "puppet".equalsIgnoreCase( component.getInstallerName())) result = validatePuppetComponent( applicationFilesDirectory, component ); else if( "script".equalsIgnoreCase( component.getInstallerName())) result = validateScriptComponent( applicationFilesDirectory, component ); else result = Collections.emptyList(); return result; } private  RecipesValidator(); static List<ModelError> validateComponentRecipes( File applicationFilesDirectory, Component component ); static final String SCRIPTS_DIR_NAME; }
@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 testScriptValidation_success() 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()); Utils.writeStringInto( "\ntest", new File( directory, RecipesValidator.SCRIPTS_DIR_NAME + "/test.sh" )); Assert.assertEquals( 0, RecipesValidator.validateComponentRecipes( appDir, comp ).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 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 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_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 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 testPuppetValidation_invalidUpdatePp() 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/invalid-update.pp" ); Utils.copyStream( inputFile, targetFile ); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_PUPPET_MISSING_PARAM_IMPORT_DIFF, errors.get( 0 ).getErrorCode()); }
@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 testPuppetValidation_invalidInitPp() 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/init.pp" ); File inputFile = TestUtils.findTestFile( "/recipes/invalid-update.pp" ); Utils.copyStream( inputFile, targetFile ); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_PUPPET_MISSING_PARAM_IMPORT_DIFF, errors.get( 0 ).getErrorCode()); }
@Test public void testPuppetValidation_invalidUpdatePp_validInitPp() 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/init.pp" ); File inputFile = TestUtils.findTestFile( "/recipes/invalid-update.pp" ); Utils.copyStream( inputFile, targetFile ); targetFile = new File( directory, "roboconf_toto/manifests/update.pp" ); Utils.copyStream( inputFile, targetFile ); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_PUPPET_MISSING_PARAM_IMPORT_DIFF, errors.get( 0 ).getErrorCode()); }
@Test public void testPuppetValidation_onlyStartPp() 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/start.pp" ); File inputFile = TestUtils.findTestFile( "/recipes/invalid-update.pp" ); Utils.copyStream( inputFile, targetFile ); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 0, errors.size()); }
@Test public void testPuppetValidation_missingRunningState() 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/start.pp" ); File inputFile = TestUtils.findTestFile( "/recipes/missing-running-state.pp" ); Utils.copyStream( inputFile, targetFile ); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_PUPPET_MISSING_PARAM_RUNNING_STATE, errors.get( 0 ).getErrorCode()); }
@Test public void testPuppetValidation_missingParameterFromImports() throws Exception { File appDir = this.folder.newFolder(); Component comp = new Component( "toto" ).installerName( "puppet" ); comp.addImportedVariable( new ImportedVariable( "Test.ip", true, false )); comp.addImportedVariable( new ImportedVariable( "Test.port", true, false )); comp.addImportedVariable( new ImportedVariable( "Oops.ip", true, false )); File directory = ResourceUtils.findInstanceResourcesDirectory( appDir, comp ); Assert.assertTrue( new File( directory, "roboconf_toto/manifests" ).mkdirs()); File targetFile = new File( directory, "roboconf_toto/manifests/start.pp" ); File inputFile = TestUtils.findTestFile( "/recipes/update.pp" ); Utils.copyStream( inputFile, targetFile ); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 2, errors.size()); Assert.assertEquals( ErrorCode.REC_PUPPET_MISSING_PARAM_FROM_IMPORT, errors.get( 0 ).getErrorCode()); Assert.assertEquals( 3, errors.get( 0 ).getDetails().length ); Assert.assertEquals( ErrorDetailsKind.NAME, errors.get( 0 ).getDetails()[ 0 ].getErrorDetailsKind()); Assert.assertEquals( "toto", errors.get( 0 ).getDetails()[ 0 ].getElementName()); Assert.assertEquals( ErrorDetailsKind.FILE, errors.get( 0 ).getDetails()[ 1 ].getErrorDetailsKind()); Assert.assertEquals( ErrorDetailsKind.EXPECTED, errors.get( 0 ).getDetails()[ 2 ].getErrorDetailsKind()); Assert.assertEquals( "test", errors.get( 0 ).getDetails()[ 2 ].getElementName()); Assert.assertEquals( ErrorCode.REC_PUPPET_MISSING_PARAM_FROM_IMPORT, errors.get( 1 ).getErrorCode()); Assert.assertEquals( 3, errors.get( 1 ).getDetails().length ); Assert.assertEquals( ErrorDetailsKind.NAME, errors.get( 1 ).getDetails()[ 0 ].getErrorDetailsKind()); Assert.assertEquals( "toto", errors.get( 1 ).getDetails()[ 0 ].getElementName()); Assert.assertEquals( ErrorDetailsKind.FILE, errors.get( 1 ).getDetails()[ 1 ].getErrorDetailsKind()); Assert.assertEquals( ErrorDetailsKind.EXPECTED, errors.get( 1 ).getDetails()[ 2 ].getErrorDetailsKind()); Assert.assertEquals( "oops", errors.get( 1 ).getDetails()[ 2 ].getElementName()); }
@Test public void testPuppetValidation_invalidPuppetClass() throws Exception { Assume.assumeTrue(puppetIsInstalled()); File appDir = this.folder.newFolder(); Component comp = new Component( "toto" ).installerName( "puppet" ); comp.addImportedVariable( new ImportedVariable( "Test.ip", true, false )); comp.addImportedVariable( new ImportedVariable( "Test.port", true, false )); comp.addImportedVariable( new ImportedVariable( "Oops.ip", true, false )); File directory = ResourceUtils.findInstanceResourcesDirectory( appDir, comp ); Assert.assertTrue( new File( directory, "roboconf_toto/manifests" ).mkdirs()); File targetFile = new File( directory, "roboconf_toto/manifests/start.pp" ); Utils.writeStringInto( "Not a valid puppet class...", targetFile ); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_PUPPET_SYNTAX_ERROR, errors.get( 0 ).getErrorCode()); }
@Test public void testPuppetValidation_withMatchingImport() throws Exception { File appDir = this.folder.newFolder(); Component comp = new Component( "toto" ).installerName( "puppet" ); comp.addImportedVariable( new ImportedVariable( "WithInit.value", true, false )); File directory = ResourceUtils.findInstanceResourcesDirectory( appDir, comp ); Assert.assertTrue( new File( directory, "roboconf_toto/manifests" ).mkdirs()); File targetFile = new File( directory, "roboconf_toto/manifests/start.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 testPuppetValidation_invalidSyntax() throws Exception { Assume.assumeTrue(puppetIsInstalled()); 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/start.pp" ); File inputFile = TestUtils.findTestFile( "/recipes/invalid-syntax.pp" ); Utils.copyStream( inputFile, targetFile ); List<ModelError> errors = RecipesValidator.validateComponentRecipes( appDir, comp ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.REC_PUPPET_SYNTAX_ERROR, errors.get( 0 ).getErrorCode()); }