TargetsMngrImpl implements ITargetsMngr { @Override public Map<String,byte[]> findScriptResourcesForAgent( String targetId ) throws IOException { Map<String,byte[]> result = new HashMap<>( 0 ); File targetDir = new File( findTargetDirectory( targetId ), Constants.PROJECT_SUB_DIR_SCRIPTS ); if( targetDir.isDirectory()){ List<String> exclusionPatterns = Arrays.asList( "(?i).*" + Pattern.quote( Constants.LOCAL_RESOURCE_PREFIX ) + ".*" ); result.putAll( Utils.storeDirectoryResourcesAsBytes( targetDir, exclusionPatterns )); } return result; } TargetsMngrImpl( IConfigurationMngr configurationMngr ); @Override String createTarget( String targetContent ); @Override String createTarget( File targetPropertiesFile, ApplicationTemplate creator ); @Override void updateTarget( String targetId, String newTargetContent ); @Override void deleteTarget( String targetId ); @Override void associateTargetWith( String targetId, AbstractApplication app, String instancePathOrComponentName ); @Override void dissociateTargetFrom( AbstractApplication app, String instancePathOrComponentName ); @Override void copyOriginalMapping( Application app ); @Override void applicationWasDeleted( AbstractApplication app ); @Override TargetProperties findTargetProperties( AbstractApplication app, String instancePath ); @Override TargetProperties findTargetProperties( String targetId ); @Override String findTargetId( AbstractApplication app, String instancePath, boolean strict ); @Override String findTargetId( AbstractApplication app, String instancePath ); @Override Map<String,byte[]> findScriptResourcesForAgent( String targetId ); @Override Map<String,byte[]> findScriptResourcesForAgent( AbstractApplication app, Instance scopedInstance ); @Override File findScriptForDm( AbstractApplication app, Instance scopedInstance ); @Override List<TargetWrapperDescriptor> listAllTargets(); @Override TargetWrapperDescriptor findTargetById( String targetId ); @Override List<TargetWrapperDescriptor> listPossibleTargets( AbstractApplication app ); @Override void addHint( String targetId, AbstractApplication app ); @Override void removeHint( String targetId, AbstractApplication app ); @Override TargetProperties lockAndGetTarget( Application app, Instance scopedInstance ); @Override void unlockTarget( Application app, Instance scopedInstance ); @Override List<TargetUsageItem> findUsageStatistics( String targetId );  }
@Test public void testFindScriptResourcesForAgent() throws Exception { Properties props = new Properties(); props.setProperty( "id", "tid1" ); props.setProperty( "handler", "h" ); File propsFile = this.folder.newFile( "my-target.properties" ); Utils.writePropertiesFile( props, propsFile ); TestApplication app = new TestApplication(); String targetId1 = this.mngr.createTarget( propsFile, app.getTemplate()); String targetId2 = this.mngr.createTarget( "id: tid2\nhandler: h" ); File dir1 = new File( this.configurationMngr.getWorkingDirectory(), ConfigurationUtils.TARGETS + "/" + targetId1 + "/" + Constants.PROJECT_SUB_DIR_SCRIPTS ); File dir2 = new File( this.configurationMngr.getWorkingDirectory(), ConfigurationUtils.TARGETS + "/" + targetId2 + "/" + Constants.PROJECT_SUB_DIR_SCRIPTS ); Assert.assertTrue( new File( dir1, "sub" ).mkdirs()); Assert.assertTrue( new File( dir2, "sub" ).mkdirs()); Utils.writeStringInto( "#!/bin/bash\necho Bonjour le monde cruel > toto.txt", new File( dir1, Constants.SCOPED_SCRIPT_AT_AGENT_SUFFIX + "sh" )); Utils.writeStringInto( "#!/bin/bash\necho Bonjour le monde cruel > toto.txt", new File( dir1, "sub/whatever.sh" )); Utils.writeStringInto( "#!/bin/bash\necho Bonjour le monde cruel > toto.txt", new File( dir1, "sub/" + Constants.LOCAL_RESOURCE_PREFIX + "whatever.sh" )); Utils.writeStringInto( "#!/bin/bash\necho Bonjour le monde cruel > toto.txt", new File( dir1, "some-properties-for-whatever.properties" )); Utils.writeStringInto( "#!/bin/bash\necho Bonjour le monde cruel > toto.txt", new File( dir1, "sub/properties-for-whatever.properties" )); Utils.writeStringInto( "#!/bin/bash\necho touch toto.txt", new File( dir2, "toto-script.py" )); this.mngr.associateTargetWith( targetId1, app, null ); Map<String,byte[]> resources = this.mngr.findScriptResourcesForAgent( targetId1 ); Assert.assertEquals( 4, resources.size()); Assert.assertTrue( resources.containsKey( Constants.SCOPED_SCRIPT_AT_AGENT_SUFFIX + "sh" )); Assert.assertTrue( resources.containsKey( "sub/whatever.sh" )); Assert.assertTrue( resources.containsKey( "some-properties-for-whatever.properties" )); Assert.assertTrue( resources.containsKey( "sub/properties-for-whatever.properties" )); Map<String,byte[]> resourcesByInst = this.mngr.findScriptResourcesForAgent( app, app.getMySqlVm()); Assert.assertEquals( resources.keySet(), resourcesByInst.keySet()); this.mngr.associateTargetWith( targetId2, app, InstanceHelpers.computeInstancePath( app.getMySqlVm())); resources = this.mngr.findScriptResourcesForAgent( targetId1 ); Assert.assertEquals( 4, resources.size()); Assert.assertTrue( resources.containsKey( Constants.SCOPED_SCRIPT_AT_AGENT_SUFFIX + "sh" )); Assert.assertTrue( resources.containsKey( "sub/whatever.sh" )); Assert.assertTrue( resources.containsKey( "some-properties-for-whatever.properties" )); Assert.assertTrue( resources.containsKey( "sub/properties-for-whatever.properties" )); resourcesByInst = this.mngr.findScriptResourcesForAgent( app, app.getMySqlVm()); Assert.assertEquals( 1, resourcesByInst.size()); Assert.assertTrue( resourcesByInst.containsKey( "toto-script.py" )); }