OpenstackIaasHandler extends AbstractThreadedTargetHandler { static void validateAll( Map<String,String> targetProperties, String appName, String instanceName ) throws TargetException { validate( targetProperties ); Set<String> mountPoints = new HashSet<> (); Set<String> volumeNames = new HashSet<> (); for( String s : findStorageIds( targetProperties )) { String mountPoint = findStorageProperty( targetProperties, s, VOLUME_MOUNT_POINT_PREFIX ); if( mountPoints.contains( mountPoint )) throw new TargetException( "Mount point '" + mountPoint + "' is already used by another volume for this VM." ); mountPoints.add( mountPoint ); String volumeName = findStorageProperty( targetProperties, s, VOLUME_NAME_PREFIX ); volumeName = expandVolumeName( volumeName, appName, instanceName ); if( volumeNames.contains( volumeName )) throw new TargetException( "Volume name '" + volumeName + "' is already used by another volume for this VM." ); volumeNames.add( volumeName ); String volumesize = findStorageProperty( targetProperties, s, VOLUME_SIZE_GB_PREFIX ); try { Integer.valueOf( volumesize ); } catch( NumberFormatException e ) { throw new TargetException( "The volume size must be a valid integer.", e ); } } }  @Override String getTargetId(); @Override String createMachine( TargetHandlerParameters parameters ); @Override boolean isMachineRunning( TargetHandlerParameters parameters, String machineId ); @Override MachineConfigurator machineConfigurator( TargetHandlerParameters parameters, String machineId ); @Override void terminateMachine( TargetHandlerParameters parameters, String machineId ); @Override String retrievePublicIpAddress( TargetHandlerParameters parameters, String machineId ); static final String TARGET_ID; }
@Test public void testValidateAll_success() throws Exception { Map<String,String> targetProperties = new HashMap<>(); targetProperties.put( FLAVOR_NAME, UUID.randomUUID().toString()); targetProperties.put( TENANT_NAME, UUID.randomUUID().toString()); targetProperties.put( IMAGE_NAME, UUID.randomUUID().toString()); targetProperties.put( SECURITY_GROUP, UUID.randomUUID().toString()); targetProperties.put( USER, UUID.randomUUID().toString()); targetProperties.put( PASSWORD, UUID.randomUUID().toString()); targetProperties.put( KEY_PAIR, UUID.randomUUID().toString()); targetProperties.put( API_URL, UUID.randomUUID().toString()); validateAll( targetProperties, null, null ); targetProperties.put( USE_BLOCK_STORAGE, "a, b, ,," ); targetProperties.put( VOLUME_NAME_PREFIX + "a", "name" ); targetProperties.put( VOLUME_SIZE_GB_PREFIX + "a", "20" ); targetProperties.put( VOLUME_MOUNT_POINT_PREFIX + "a", "/dev/sdf" ); validateAll( targetProperties, "app", "inst" ); }
@Test( expected = TargetException.class ) public void testValidateAll_duplicateVolumeName_allUseDefaults() throws Exception { Map<String,String> targetProperties = new HashMap<>(); targetProperties.put( FLAVOR_NAME, UUID.randomUUID().toString()); targetProperties.put( TENANT_NAME, UUID.randomUUID().toString()); targetProperties.put( IMAGE_NAME, UUID.randomUUID().toString()); targetProperties.put( SECURITY_GROUP, UUID.randomUUID().toString()); targetProperties.put( USER, UUID.randomUUID().toString()); targetProperties.put( PASSWORD, UUID.randomUUID().toString()); targetProperties.put( KEY_PAIR, UUID.randomUUID().toString()); targetProperties.put( API_URL, UUID.randomUUID().toString()); targetProperties.put( USE_BLOCK_STORAGE, "a, b, ,," ); targetProperties.put( VOLUME_MOUNT_POINT_PREFIX + "a", "/dev/sdc" ); targetProperties.put( VOLUME_MOUNT_POINT_PREFIX + "b", "/dev/sdd" ); validateAll( targetProperties, "app", "inst" ); }
@Test( expected = TargetException.class ) public void testValidateAll_duplicateVolumeName_emptyNameShouldResultInDefault() throws Exception { Map<String,String> targetProperties = new HashMap<>(); targetProperties.put( FLAVOR_NAME, UUID.randomUUID().toString()); targetProperties.put( TENANT_NAME, UUID.randomUUID().toString()); targetProperties.put( IMAGE_NAME, UUID.randomUUID().toString()); targetProperties.put( SECURITY_GROUP, UUID.randomUUID().toString()); targetProperties.put( USER, UUID.randomUUID().toString()); targetProperties.put( PASSWORD, UUID.randomUUID().toString()); targetProperties.put( KEY_PAIR, UUID.randomUUID().toString()); targetProperties.put( API_URL, UUID.randomUUID().toString()); targetProperties.put( USE_BLOCK_STORAGE, "a, b, ,," ); targetProperties.put( VOLUME_NAME_PREFIX + "a", "" ); targetProperties.put( VOLUME_MOUNT_POINT_PREFIX + "a", "/dev/sdc" ); targetProperties.put( VOLUME_MOUNT_POINT_PREFIX + "b", "/dev/sdd" ); validateAll( targetProperties, "app", "inst" ); }
@Test( expected = TargetException.class ) public void testValidateAll_duplicateMountPoint() throws Exception { Map<String,String> targetProperties = new HashMap<>(); targetProperties.put( FLAVOR_NAME, UUID.randomUUID().toString()); targetProperties.put( TENANT_NAME, UUID.randomUUID().toString()); targetProperties.put( IMAGE_NAME, UUID.randomUUID().toString()); targetProperties.put( SECURITY_GROUP, UUID.randomUUID().toString()); targetProperties.put( USER, UUID.randomUUID().toString()); targetProperties.put( PASSWORD, UUID.randomUUID().toString()); targetProperties.put( KEY_PAIR, UUID.randomUUID().toString()); targetProperties.put( API_URL, UUID.randomUUID().toString()); targetProperties.put( USE_BLOCK_STORAGE, "a, b" ); targetProperties.put( VOLUME_NAME_PREFIX + "a", "vol-a" ); targetProperties.put( VOLUME_NAME_PREFIX + "b", "vol-b" ); validateAll( targetProperties, "app", "inst" ); }
@Test( expected = TargetException.class ) public void testValidateAll_invalidVolumeSize() throws Exception { Map<String,String> targetProperties = new HashMap<>(); targetProperties.put( FLAVOR_NAME, UUID.randomUUID().toString()); targetProperties.put( TENANT_NAME, UUID.randomUUID().toString()); targetProperties.put( IMAGE_NAME, UUID.randomUUID().toString()); targetProperties.put( SECURITY_GROUP, UUID.randomUUID().toString()); targetProperties.put( USER, UUID.randomUUID().toString()); targetProperties.put( PASSWORD, UUID.randomUUID().toString()); targetProperties.put( KEY_PAIR, UUID.randomUUID().toString()); targetProperties.put( API_URL, UUID.randomUUID().toString()); targetProperties.put( USE_BLOCK_STORAGE, "a, b" ); targetProperties.put( VOLUME_NAME_PREFIX + "a", "vol-a" ); targetProperties.put( VOLUME_NAME_PREFIX + "b", "vol-b" ); targetProperties.put( VOLUME_MOUNT_POINT_PREFIX + "a", "/dev/sdc" ); targetProperties.put( VOLUME_MOUNT_POINT_PREFIX + "b", "/dev/sdd" ); targetProperties.put( VOLUME_SIZE_GB_PREFIX + "b", "five" ); validateAll( targetProperties, "app", "inst" ); }
@Test public void testValidateAll_success2() throws Exception { Map<String,String> targetProperties = new HashMap<>(); targetProperties.put( FLAVOR_NAME, UUID.randomUUID().toString()); targetProperties.put( TENANT_NAME, UUID.randomUUID().toString()); targetProperties.put( IMAGE_NAME, UUID.randomUUID().toString()); targetProperties.put( SECURITY_GROUP, UUID.randomUUID().toString()); targetProperties.put( USER, UUID.randomUUID().toString()); targetProperties.put( PASSWORD, UUID.randomUUID().toString()); targetProperties.put( KEY_PAIR, UUID.randomUUID().toString()); targetProperties.put( API_URL, UUID.randomUUID().toString()); targetProperties.put( USE_BLOCK_STORAGE, "a, b" ); targetProperties.put( VOLUME_NAME_PREFIX + "a", "vol-a" ); targetProperties.put( VOLUME_NAME_PREFIX + "b", "vol-b" ); targetProperties.put( VOLUME_MOUNT_POINT_PREFIX + "a", "/dev/sdc" ); targetProperties.put( VOLUME_MOUNT_POINT_PREFIX + "b", "/dev/sdd" ); targetProperties.put( VOLUME_SIZE_GB_PREFIX + "a", "40" ); targetProperties.put( VOLUME_SIZE_GB_PREFIX + "b", "4" ); validateAll( targetProperties, "app", "inst" ); }