Added a unit test for RunningInstanceToStorageMappingUnix.

Added DI for it in compute service.
Minor fixes for tests
This commit is contained in:
Alex Yarmula 2010-04-25 12:36:26 -07:00
parent 8a3280467c
commit 950776c0e9
6 changed files with 268 additions and 183 deletions

View File

@ -34,6 +34,8 @@ import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import org.jclouds.aws.domain.Region; import org.jclouds.aws.domain.Region;
import org.jclouds.aws.ec2.EC2; import org.jclouds.aws.ec2.EC2;
import org.jclouds.aws.ec2.EC2AsyncClient; import org.jclouds.aws.ec2.EC2AsyncClient;
@ -52,6 +54,7 @@ import org.jclouds.aws.ec2.config.EC2ContextModule;
import org.jclouds.aws.ec2.domain.AvailabilityZone; import org.jclouds.aws.ec2.domain.AvailabilityZone;
import org.jclouds.aws.ec2.domain.KeyPair; import org.jclouds.aws.ec2.domain.KeyPair;
import org.jclouds.aws.ec2.domain.RunningInstance; import org.jclouds.aws.ec2.domain.RunningInstance;
import org.jclouds.aws.ec2.functions.RunningInstanceToStorageMappingUnix;
import org.jclouds.aws.ec2.services.InstanceClient; import org.jclouds.aws.ec2.services.InstanceClient;
import org.jclouds.compute.ComputeService; import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
@ -88,6 +91,7 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.inject.Provides; import com.google.inject.Provides;
import org.jclouds.util.Jsr330;
/** /**
* Configures the {@link ComputeServiceContext}; requires {@link EC2ComputeService} bound. * Configures the {@link ComputeServiceContext}; requires {@link EC2ComputeService} bound.
@ -105,6 +109,9 @@ public class EC2ComputeServiceContextModule extends EC2ContextModule {
bind(GetNodeMetadataStrategy.class).to(EC2GetNodeMetadataStrategy.class); bind(GetNodeMetadataStrategy.class).to(EC2GetNodeMetadataStrategy.class);
bind(RebootNodeStrategy.class).to(EC2RebootNodeStrategy.class); bind(RebootNodeStrategy.class).to(EC2RebootNodeStrategy.class);
bind(DestroyNodeStrategy.class).to(EC2DestroyNodeStrategy.class); bind(DestroyNodeStrategy.class).to(EC2DestroyNodeStrategy.class);
bind(new TypeLiteral<Function<RunningInstance, Map<String, String>>>(){}).
annotatedWith(Jsr330.named("volumeMapping")).
to(RunningInstanceToStorageMappingUnix.class).in(Scopes.SINGLETON);
} }
@Provides @Provides

View File

@ -27,13 +27,13 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.aws.ec2.compute.domain.RegionTag; import org.jclouds.aws.ec2.compute.domain.RegionTag;
import org.jclouds.aws.ec2.domain.InstanceState; import org.jclouds.aws.ec2.domain.InstanceState;
import org.jclouds.aws.ec2.domain.KeyPair; import org.jclouds.aws.ec2.domain.KeyPair;
import org.jclouds.aws.ec2.domain.RunningInstance; import org.jclouds.aws.ec2.domain.RunningInstance;
import org.jclouds.aws.ec2.functions.InstanceTypeToStorageMappingUnix;
import org.jclouds.aws.ec2.options.DescribeImagesOptions; import org.jclouds.aws.ec2.options.DescribeImagesOptions;
import org.jclouds.aws.ec2.services.AMIClient; import org.jclouds.aws.ec2.services.AMIClient;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
@ -65,16 +65,19 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
private final PopulateDefaultLoginCredentialsForImageStrategy credentialProvider; private final PopulateDefaultLoginCredentialsForImageStrategy credentialProvider;
private final Map<String, ? extends Image> images; private final Map<String, ? extends Image> images;
private final Map<String, ? extends Location> locations; private final Map<String, ? extends Location> locations;
private final Function<RunningInstance, Map<String, String>> instanceToStorageMapping;
@Inject @Inject
RunningInstanceToNodeMetadata(AMIClient amiClient, Map<RegionTag, KeyPair> credentialsMap, RunningInstanceToNodeMetadata(AMIClient amiClient, Map<RegionTag, KeyPair> credentialsMap,
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider, PopulateDefaultLoginCredentialsForImageStrategy credentialProvider,
Map<String, ? extends Image> images, Map<String, ? extends Location> locations) { Map<String, ? extends Image> images, Map<String, ? extends Location> locations,
@Named("volumeMapping") Function<RunningInstance, Map<String, String>> instanceToStorageMapping) {
this.amiClient = checkNotNull(amiClient, "amiClient"); this.amiClient = checkNotNull(amiClient, "amiClient");
this.credentialsMap = checkNotNull(credentialsMap, "credentialsMap"); this.credentialsMap = checkNotNull(credentialsMap, "credentialsMap");
this.credentialProvider = checkNotNull(credentialProvider, "credentialProvider"); this.credentialProvider = checkNotNull(credentialProvider, "credentialProvider");
this.images = checkNotNull(images, "images"); this.images = checkNotNull(images, "images");
this.locations = checkNotNull(locations, "locations"); this.locations = checkNotNull(locations, "locations");
this.instanceToStorageMapping = checkNotNull(instanceToStorageMapping);
} }
@Override @Override
@ -123,8 +126,7 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
// put storage info // put storage info
/* TODO: only valid for UNIX */ /* TODO: only valid for UNIX */
InstanceTypeToStorageMappingUnix instanceToStorageMapping = new InstanceTypeToStorageMappingUnix(); extra.putAll(instanceToStorageMapping.apply(instance));
extra.putAll(instanceToStorageMapping.apply(instance.getInstanceType()));
return extra; return extra;
} }

View File

@ -1,177 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.aws.ec2.functions;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import org.jclouds.aws.ec2.domain.InstanceType;
import java.util.Map;
import static org.jclouds.compute.reference.ComputeServiceConstants.LOCAL_PARTITION_GB_PATTERN;
/**
* Map the instance type to storage information known about it. This information is statically set as described by Amazon.
*
* Note that having the partitions available doesn't mean they're formatted/mounted by default. The links below
* describe what partitions are formatted initially. To format/mount an available device, refer to
* <a href="http://meinit.nl/howto-use-amazon-elastic-compute-cloud-ec2">this article</a>.
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/2009-11-30/UserGuide/index.html?instance-storage-concepts.html" />
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html" />
*
* @author Oleksiy Yarmula
*/
public class InstanceTypeToStorageMappingUnix implements Function<InstanceType, Map<String, String>> {
public final static String ROOT_PARTITION_NAME_UNIX = "/dev/sda1";
@Override
public Map<String, String> apply(InstanceType instanceType) {
Map<String, String> mapping = Maps.newHashMap();
//root partition
mapping.put(String.format(LOCAL_PARTITION_GB_PATTERN, ROOT_PARTITION_NAME_UNIX),
getRootPartitionSizeForInstanceType(instanceType) + "");
//primary partition (always formatted/mounted)
mapping.put(String.format(LOCAL_PARTITION_GB_PATTERN, getPrimaryPartitionDeviceName(instanceType)),
getPrimaryPartitionSizeForInstanceType(instanceType) + "");
//additional partitions if any
for(Map.Entry<String, Integer> entry : getAdditionalPartitionsMapping(instanceType).entrySet()) {
mapping.put(String.format(LOCAL_PARTITION_GB_PATTERN, entry.getKey()),
entry.getValue() + "");
}
return mapping;
}
/**
* Retrieve the root partition size.
* Note, this is usually a rather small partition. Refer to
* {@link #getPrimaryPartitionSizeForInstanceType()} to determine the
* size of the primary (usually, biggest) partition. In some cases,
* for large instances there are several partitions of the size of primary partition.
*
* @param instanceType for which the root partition size is to be determined
* @return size in GB
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/2009-11-30/UserGuide/index.html?instance-storage-concepts.html" />
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html" />
*/
public int getRootPartitionSizeForInstanceType(InstanceType instanceType) {
/* per documentation at
http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html
M2 XLARGE doesn't have the root partition
TODO verify
*/
if(InstanceType.M2_XLARGE == instanceType) return 0;
//other types have 10 GB root partition
return 10;
}
public static String getPrimaryPartitionDeviceName(InstanceType instanceType) {
if(InstanceType.M1_SMALL == instanceType || InstanceType.C1_MEDIUM == instanceType)
return "/dev/sda2";
return "/dev/sdb";
}
/**
* Retrieve the primary partition size.
*
* This is usually the biggest partition. In some cases,
* for large instances there are several partitions of the size of primary partition.
*
* @param instanceType for which the primary partition size is to be determined
* @return size in GB
*/
public static int getPrimaryPartitionSizeForInstanceType(InstanceType instanceType) {
switch(instanceType) {
case M1_SMALL:
return 150;
case M1_LARGE:
return 420;
case M1_XLARGE:
return 420;
case C1_MEDIUM:
return 340;
case C1_XLARGE:
return 420;
case M2_XLARGE:
return 420;
case M2_2XLARGE:
return 840;
case M2_4XLARGE:
return 840;
}
throw new RuntimeException("Unknown instance type");
}
/**
* Retrieve additional devices mapping (non-root and non-primary) for the instance type.
*
* @param instanceType
* @return map with device name(s) and size(s) or empty map if the instance doesn't
* have any additional
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?concepts-amis-and-instances.html#instance-types" />
*/
public static Map<String, Integer> getAdditionalPartitionsMapping(InstanceType instanceType) {
Map<String, Integer> mapping = Maps.newHashMap();
int size = 0;
switch(instanceType) {
case M1_LARGE:
case M1_XLARGE:
case C1_XLARGE:
size = 420;
break;
case M2_4XLARGE:
size = 840;
break;
}
//m1.large, m1.xlarge, and c1.xlarge
if(InstanceType.M1_LARGE == instanceType || InstanceType.M1_XLARGE == instanceType ||
InstanceType.C1_XLARGE == instanceType || InstanceType.M2_4XLARGE == instanceType) {
mapping.put("/dev/sdc", size);
}
if(InstanceType.M1_XLARGE == instanceType || InstanceType.C1_XLARGE == instanceType) {
mapping.put("/dev/sdd", size);
}
if(InstanceType.M1_XLARGE == instanceType || InstanceType.C1_XLARGE == instanceType) {
mapping.put("/dev/sde", size);
}
return mapping;
}
}

View File

@ -0,0 +1,180 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.aws.ec2.functions;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import org.jclouds.aws.ec2.domain.InstanceType;
import org.jclouds.aws.ec2.domain.RunningInstance;
import java.util.Map;
import static org.jclouds.compute.reference.ComputeServiceConstants.LOCAL_PARTITION_GB_PATTERN;
/**
* Map the instance to storage information known about it. This information is statically set as described by Amazon.
*
* Note that having the partitions available doesn't mean they're formatted/mounted by default. The links below
* describe what partitions are formatted initially. To format/mount an available device, refer to
* <a href="http://meinit.nl/howto-use-amazon-elastic-compute-cloud-ec2">this article</a>.
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/2009-11-30/UserGuide/index.html?instance-storage-concepts.html" />
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html" />
*
* @author Oleksiy Yarmula
*/
public class RunningInstanceToStorageMappingUnix implements Function<RunningInstance, Map<String, String>> {
public final static String ROOT_PARTITION_NAME_UNIX = "/dev/sda1";
@Override
public Map<String, String> apply(RunningInstance instance) {
final InstanceType instanceType = instance.getInstanceType();
Map<String, String> mapping = Maps.newHashMap();
//root partition
mapping.put(String.format(LOCAL_PARTITION_GB_PATTERN, ROOT_PARTITION_NAME_UNIX),
getRootPartitionSizeForInstanceType(instanceType) + "");
//primary partition (always formatted/mounted)
mapping.put(String.format(LOCAL_PARTITION_GB_PATTERN, getPrimaryPartitionDeviceName(instanceType)),
getPrimaryPartitionSizeForInstanceType(instanceType) + "");
//additional partitions if any
for(Map.Entry<String, Integer> entry : getAdditionalPartitionsMapping(instanceType).entrySet()) {
mapping.put(String.format(LOCAL_PARTITION_GB_PATTERN, entry.getKey()),
entry.getValue() + "");
}
return mapping;
}
/**
* Retrieve the root partition size.
* Note, this is usually a rather small partition. Refer to
* {@link #getPrimaryPartitionSizeForInstanceType} to determine the
* size of the primary (usually, biggest) partition. In some cases,
* for large instances there are several partitions of the size of primary partition.
*
* @param instanceType for which the root partition size is to be determined
* @return size in GB
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/2009-11-30/UserGuide/index.html?instance-storage-concepts.html" />
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html" />
*/
public int getRootPartitionSizeForInstanceType(InstanceType instanceType) {
/* per documentation at
http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html
M2 XLARGE doesn't have the root partition
TODO verify
*/
if(InstanceType.M2_XLARGE == instanceType) return 0;
//other types have 10 GB root partition
return 10;
}
public static String getPrimaryPartitionDeviceName(InstanceType instanceType) {
if(InstanceType.M1_SMALL == instanceType || InstanceType.C1_MEDIUM == instanceType)
return "/dev/sda2";
return "/dev/sdb";
}
/**
* Retrieve the primary partition size.
*
* This is usually the biggest partition. In some cases,
* for large instances there are several partitions of the size of primary partition.
*
* @param instanceType for which the primary partition size is to be determined
* @return size in GB
*/
public static int getPrimaryPartitionSizeForInstanceType(InstanceType instanceType) {
switch(instanceType) {
case M1_SMALL:
return 150;
case M1_LARGE:
return 420;
case M1_XLARGE:
return 420;
case C1_MEDIUM:
return 340;
case C1_XLARGE:
return 420;
case M2_XLARGE:
return 420;
case M2_2XLARGE:
return 840;
case M2_4XLARGE:
return 840;
}
throw new RuntimeException("Unknown instance type");
}
/**
* Retrieve additional devices mapping (non-root and non-primary) for the instance type.
*
* @param instanceType
* @return map with device name(s) and size(s) or empty map if the instance doesn't
* have any additional
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?concepts-amis-and-instances.html#instance-types" />
*/
public static Map<String, Integer> getAdditionalPartitionsMapping(InstanceType instanceType) {
Map<String, Integer> mapping = Maps.newHashMap();
int size = 0;
switch(instanceType) {
case M1_LARGE:
case M1_XLARGE:
case C1_XLARGE:
size = 420;
break;
case M2_4XLARGE:
size = 840;
break;
}
//m1.large, m1.xlarge, and c1.xlarge
if(InstanceType.M1_LARGE == instanceType || InstanceType.M1_XLARGE == instanceType ||
InstanceType.C1_XLARGE == instanceType || InstanceType.M2_4XLARGE == instanceType) {
mapping.put("/dev/sdc", size);
}
if(InstanceType.M1_XLARGE == instanceType || InstanceType.C1_XLARGE == instanceType) {
mapping.put("/dev/sdd", size);
}
if(InstanceType.M1_XLARGE == instanceType || InstanceType.C1_XLARGE == instanceType) {
mapping.put("/dev/sde", size);
}
return mapping;
}
}

View File

@ -38,6 +38,7 @@ import org.jclouds.aws.ec2.domain.InstanceState;
import org.jclouds.aws.ec2.domain.InstanceType; import org.jclouds.aws.ec2.domain.InstanceType;
import org.jclouds.aws.ec2.domain.KeyPair; import org.jclouds.aws.ec2.domain.KeyPair;
import org.jclouds.aws.ec2.domain.RunningInstance; import org.jclouds.aws.ec2.domain.RunningInstance;
import org.jclouds.aws.ec2.functions.RunningInstanceToStorageMappingUnix;
import org.jclouds.aws.ec2.services.AMIClient; import org.jclouds.aws.ec2.services.AMIClient;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy; import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
@ -94,7 +95,8 @@ public class RunningInstanceToNodeMetadataTest {
replay(locations); replay(locations);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(amiClient, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(amiClient,
credentialsMap, credentialProvider, images, locations); credentialsMap, credentialProvider, images, locations,
new RunningInstanceToStorageMappingUnix());
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getLocation(), location); assertEquals(metadata.getLocation(), location);
@ -162,7 +164,8 @@ public class RunningInstanceToNodeMetadataTest {
replay(locations); replay(locations);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(amiClient, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(amiClient,
credentialsMap, credentialProvider, images, locations); credentialsMap, credentialProvider, images, locations,
new RunningInstanceToStorageMappingUnix());
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);

View File

@ -0,0 +1,70 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.aws.ec2.functions;
import org.jclouds.aws.ec2.domain.InstanceType;
import org.jclouds.aws.ec2.domain.RunningInstance;
import org.testng.annotations.Test;
import java.util.Map;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
/**
* @author Oleksiy Yarmula
*/
public class RunningInstanceToStorageMappingUnixTest {
@Test
public void testMappingForM1LargeInstance() {
RunningInstanceToStorageMappingUnix volumeMapping = new RunningInstanceToStorageMappingUnix();
RunningInstance instance = createMock(RunningInstance.class);
expect(instance.getInstanceType()).andStubReturn(InstanceType.M1_LARGE);
replay(instance);
Map<String, String> mappingReturned = volumeMapping.apply(instance);
assert mappingReturned.size() == 3 : String.format(
"Expected size of mapping devices: %d. Found: %d", 3, mappingReturned.size());
assert mappingReturned.containsKey("disk_drive//dev/sda1/gb");
assert mappingReturned.containsKey("disk_drive//dev/sdb/gb");
assert mappingReturned.containsKey("disk_drive//dev/sdc/gb");
}
@Test
public void testMappingForM1XLargeInstance() {
RunningInstanceToStorageMappingUnix volumeMapping = new RunningInstanceToStorageMappingUnix();
RunningInstance instance = createMock(RunningInstance.class);
expect(instance.getInstanceType()).andStubReturn(InstanceType.M1_XLARGE);
replay(instance);
Map<String, String> mappingReturned = volumeMapping.apply(instance);
assert mappingReturned.size() == 5 : String.format(
"Expected size of mapping devices: %d. Found: %d", 5, mappingReturned.size());
assert mappingReturned.containsKey("disk_drive//dev/sda1/gb");
assert mappingReturned.containsKey("disk_drive//dev/sdb/gb");
assert mappingReturned.containsKey("disk_drive//dev/sdc/gb");
assert mappingReturned.containsKey("disk_drive//dev/sdd/gb");
assert mappingReturned.containsKey("disk_drive//dev/sde/gb");
}
}