EmbeddedHandler extends AbstractThreadedTargetHandler { @Override public String createMachine( TargetHandlerParameters parameters ) throws TargetException { String machineId = parameters.getApplicationName() + "_" + parameters.getScopedInstancePath(); this.logger.fine( "Creating machine with ID " + machineId ); if( parameters.getTargetProperties().containsKey( IP_ADDRESSES )) { String ip = acquireIpAddress( parameters, machineId ); if( ip == null ) { this.logger.fine( "No IP address is available from the pool for " + machineId ); throw new TargetException( "No IP address is available from the pool for " + machineId ); } this.logger.fine( "IP " + ip + " was assigned to " + machineId ); } else { this.machineIdToIp.put( machineId, "" ); } return machineId; }  @Override String getTargetId(); @Override void start(); @Override String createMachine( TargetHandlerParameters parameters ); @Override MachineConfigurator machineConfigurator( TargetHandlerParameters parameters, String machineId ); @Override void terminateMachine( TargetHandlerParameters parameters, String machineId ); @Override boolean isMachineRunning( TargetHandlerParameters parameters, String machineId ); @Override String retrievePublicIpAddress( TargetHandlerParameters parameters, String machineId ); static final String TARGET_ID; static final String IP_ADDRESSES; static final String SCP_USER; static final String SCP_KEY_FILE; static final String SCP_KNOWN_HOSTS_FILE; static final String SCP_AGENT_CONFIG_DIR; static final String SCP_HOST_KEY_PREFIX; static final String SCP_DISABLE_HOST_VALIDATION; }
@Test( expected = TargetException.class ) public void testIpList_noMoreIpAvailable() throws Exception { Map<String,String> targetProperties = new HashMap<> (); targetProperties.put( IP_ADDRESSES, "192.168.1.1, 192.168.1.2" ); EmbeddedHandler target = new EmbeddedHandler(); target.karafData = this.folder.newFolder().getAbsolutePath(); try { Assert.assertNotNull( target.createMachine( new TargetHandlerParameters() .applicationName( "app" ) .domain( "domain" ) .scopedInstancePath( "nothing1" ) .targetProperties( targetProperties ))); Assert.assertNotNull( target.createMachine( new TargetHandlerParameters() .applicationName( "app" ) .domain( "domain" ) .scopedInstancePath( "nothing2" ) .targetProperties( targetProperties ))); } catch( Exception e ) { Assert.fail( "No exception should have been thrown here." ); } Assert.assertNotNull( target.createMachine( new TargetHandlerParameters() .applicationName( "app" ) .domain( "domain" ) .scopedInstancePath( "nothing3" ) .targetProperties( targetProperties ))); }
@Test public void testIpList_verifyPersistedCache() throws Exception { Map<String,String> targetProperties = new HashMap<> (); targetProperties.put( IP_ADDRESSES, "192.168.1.1, 192.168.1.2" ); EmbeddedHandler target = new EmbeddedHandler(); target.karafData = this.folder.newFolder().getAbsolutePath(); Assert.assertNotNull( target.createMachine( new TargetHandlerParameters() .applicationName( "app" ) .domain( "domain" ) .scopedInstancePath( "nothing1" ) .targetProperties( targetProperties ))); Assert.assertNotNull( target.createMachine( new TargetHandlerParameters() .applicationName( "app" ) .domain( "domain" ) .scopedInstancePath( "nothing2" ) .targetProperties( targetProperties ))); final File backup = new File( target.karafData, PRIVATE_BACKUP_FILE ); Properties props = Utils.readPropertiesFile( backup ); Assert.assertTrue( Arrays.asList( "192.168.1.1, 192.168.1.2", "192.168.1.2, 192.168.1.1" ).contains( props.get( PRIVATE_BACKUP_USED_IPS ))); Assert.assertTrue( Arrays.asList( "app_nothing1=192.168.1.1, app_nothing2=192.168.1.2", "app_nothing2=192.168.1.2, app_nothing1=192.168.1.1" ).contains( props.get( PRIVATE_BACKUP_IDS_TO_IPS ))); }