Issue 291: added test cases

This commit is contained in:
Adrian Cole 2010-06-23 15:43:39 -07:00
parent 66eed877b1
commit 5afc44b2b7
2 changed files with 195 additions and 99 deletions

View File

@ -59,34 +59,40 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
@Inject
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions(
Map<RegionAndName, KeyPair> credentialsMap,
Map<RegionAndName, String> securityGroupMap, CreateUniqueKeyPair createUniqueKeyPair,
CreateSecurityGroupIfNeeded createSecurityGroupIfNeeded) {
Map<RegionAndName, KeyPair> credentialsMap,
Map<RegionAndName, String> securityGroupMap,
CreateUniqueKeyPair createUniqueKeyPair,
CreateSecurityGroupIfNeeded createSecurityGroupIfNeeded) {
this.credentialsMap = credentialsMap;
this.securityGroupMap = securityGroupMap;
this.createUniqueKeyPair = createUniqueKeyPair;
this.createSecurityGroupIfNeeded = createSecurityGroupIfNeeded;
}
public RunInstancesOptions execute(String region, String tag, Template template) {
public RunInstancesOptions execute(String region, String tag,
Template template) {
checkArgument(template.getSize() instanceof EC2Size,
"unexpected image type. should be EC2Size, was: " + template.getSize().getClass());
"unexpected image type. should be EC2Size, was: "
+ template.getSize().getClass());
EC2Size ec2Size = EC2Size.class.cast(template.getSize());
RunInstancesOptions instanceOptions = asType(ec2Size.getInstanceType()).withAdditionalInfo(tag);
String keyPairName = createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, template
.getOptions());
String subnetId = template.getOptions().as(EC2TemplateOptions.class).getSubnetId();
if(subnetId != null)
RunInstancesOptions instanceOptions = asType(ec2Size.getInstanceType())
.withAdditionalInfo(tag);
String keyPairName = createNewKeyPairUnlessUserSpecifiedOtherwise(region,
tag, template.getOptions());
String subnetId = EC2TemplateOptions.class.cast(template.getOptions())
.getSubnetId();
if (subnetId != null) {
instanceOptions.withSubnetId(subnetId);
else {
Set<String> groups = getSecurityGroupsForTagAndOptions(region, tag, template.getOptions());
} else {
Set<String> groups = getSecurityGroupsForTagAndOptions(region, tag,
template.getOptions());
instanceOptions.withSecurityGroups(groups);
}
if (keyPairName != null)
instanceOptions.withKeyName(keyPairName);
@ -94,33 +100,35 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
}
@VisibleForTesting
String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String tag,
TemplateOptions options) {
String createNewKeyPairUnlessUserSpecifiedOtherwise(String region,
String tag, TemplateOptions options) {
String keyPairName = null;
boolean shouldAutomaticallyCreateKeyPair = true;
if (options instanceof EC2TemplateOptions) {
keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair();
if (keyPairName == null)
shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(options)
.shouldAutomaticallyCreateKeyPair();
shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(
options).shouldAutomaticallyCreateKeyPair();
}
if (keyPairName == null && shouldAutomaticallyCreateKeyPair) {
RegionAndName regionAndName = new RegionAndName(region, tag);
KeyPair keyPair = createUniqueKeyPair.apply(regionAndName);
// get or create incidental resources
// TODO race condition. we were using MapMaker, but it doesn't seem to refresh properly
// TODO race condition. we were using MapMaker, but it doesn't seem to
// refresh properly
// when
// another thread
// deletes a key
credentialsMap.put(new RegionAndName(region, keyPair.getKeyName()), keyPair);
credentialsMap.put(new RegionAndName(region, keyPair.getKeyName()),
keyPair);
keyPairName = keyPair.getKeyName();
}
return keyPairName;
}
@VisibleForTesting
Set<String> getSecurityGroupsForTagAndOptions(String region, @Nullable String tag,
TemplateOptions options) {
Set<String> getSecurityGroupsForTagAndOptions(String region,
@Nullable String tag, TemplateOptions options) {
Set<String> groups = Sets.newLinkedHashSet();
if (tag != null) {
@ -130,19 +138,21 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
RegionNameAndIngressRules regionNameAndIngessRulesForMarkerGroup;
if (options instanceof EC2TemplateOptions
&& EC2TemplateOptions.class.cast(options).getGroupIds().size() > 0) {
regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region,
markerGroup, new int[] {}, false);
&& EC2TemplateOptions.class.cast(options).getGroupIds().size() > 0) {
regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(
region, markerGroup, new int[] {}, false);
groups.addAll(EC2TemplateOptions.class.cast(options).getGroupIds());
} else {
regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region,
markerGroup, options.getInboundPorts(), true);
regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(
region, markerGroup, options.getInboundPorts(), true);
}
if (!securityGroupMap.containsKey(regionNameAndIngessRulesForMarkerGroup)) {
if (!securityGroupMap
.containsKey(regionNameAndIngessRulesForMarkerGroup)) {
securityGroupMap.put(regionNameAndIngessRulesForMarkerGroup,
createSecurityGroupIfNeeded.apply(regionNameAndIngessRulesForMarkerGroup));
createSecurityGroupIfNeeded
.apply(regionNameAndIngessRulesForMarkerGroup));
}
}
return groups;

View File

@ -50,7 +50,8 @@ import com.google.common.collect.ImmutableSet;
@Test(groups = "unit", testName = "ec2.CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest")
public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
public void testExecuteWithDefaultOptions() throws SecurityException, NoSuchMethodException {
public void testExecuteWithDefaultOptions() throws SecurityException,
NoSuchMethodException {
// setup constants
String region = Region.AP_SOUTHEAST_1;
String tag = "tag";
@ -61,24 +62,28 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
// create mocks
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions strategy = createMock(
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class, new Method[] {
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class
.getDeclaredMethod("createNewKeyPairUnlessUserSpecifiedOtherwise",
String.class, String.class, TemplateOptions.class),
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class
.getDeclaredMethod("getSecurityGroupsForTagAndOptions",
String.class, String.class, TemplateOptions.class) });
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class,
new Method[] {
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class
.getDeclaredMethod(
"createNewKeyPairUnlessUserSpecifiedOtherwise",
String.class, String.class, TemplateOptions.class),
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class
.getDeclaredMethod("getSecurityGroupsForTagAndOptions",
String.class, String.class, TemplateOptions.class) });
TemplateOptions options = createMock(TemplateOptions.class);
EC2TemplateOptions options = createMock(EC2TemplateOptions.class);
Template template = createMock(Template.class);
// setup expectations
expect(template.getSize()).andReturn(size).atLeastOnce();
expect(template.getOptions()).andReturn(options).atLeastOnce();
expect(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, options))
.andReturn(systemGeneratedKeyPairName);
expect(strategy.getSecurityGroupsForTagAndOptions(region, tag, options)).andReturn(
generatedGroups);
expect(
strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag,
options)).andReturn(systemGeneratedKeyPairName);
expect(strategy.getSecurityGroupsForTagAndOptions(region, tag, options))
.andReturn(generatedGroups);
expect(options.getSubnetId()).andReturn(null);
// replay mocks
replay(options);
@ -87,13 +92,73 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
// run
RunInstancesOptions runOptions = strategy.execute(region, tag, template);
assertEquals(runOptions.buildQueryParameters(), ImmutableMultimap.<String, String> of());
assertEquals(runOptions.buildFormParameters().entries(), ImmutableMultimap
.<String, String> of("InstanceType", size.getProviderId(), "SecurityGroup.1",
generatedGroup, "AdditionalInfo", tag, "KeyName",
systemGeneratedKeyPairName).entries());
assertEquals(runOptions.buildMatrixParameters(), ImmutableMultimap.<String, String> of());
assertEquals(runOptions.buildRequestHeaders(), ImmutableMultimap.<String, String> of());
assertEquals(runOptions.buildQueryParameters(), ImmutableMultimap
.<String, String> of());
assertEquals(runOptions.buildFormParameters().entries(),
ImmutableMultimap.<String, String> of("InstanceType",
size.getProviderId(), "AdditionalInfo", tag,
"SecurityGroup.1", generatedGroup, "KeyName",
systemGeneratedKeyPairName).entries());
assertEquals(runOptions.buildMatrixParameters(), ImmutableMultimap
.<String, String> of());
assertEquals(runOptions.buildRequestHeaders(), ImmutableMultimap
.<String, String> of());
assertEquals(runOptions.buildStringPayload(), null);
// verify mocks
verify(options);
verify(template);
verify(strategy);
}
public void testExecuteWithSubnet() throws SecurityException,
NoSuchMethodException {
// setup constants
String region = Region.AP_SOUTHEAST_1;
String tag = "tag";
EC2Size size = EC2Size.M1_SMALL;
String systemGeneratedKeyPairName = "systemGeneratedKeyPair";
// create mocks
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions strategy = createMock(
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class,
new Method[] {
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class
.getDeclaredMethod(
"createNewKeyPairUnlessUserSpecifiedOtherwise",
String.class, String.class, TemplateOptions.class),
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class
.getDeclaredMethod("getSecurityGroupsForTagAndOptions",
String.class, String.class, TemplateOptions.class) });
EC2TemplateOptions options = createMock(EC2TemplateOptions.class);
Template template = createMock(Template.class);
// setup expectations
expect(template.getSize()).andReturn(size).atLeastOnce();
expect(template.getOptions()).andReturn(options).atLeastOnce();
expect(
strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag,
options)).andReturn(systemGeneratedKeyPairName);
expect(options.getSubnetId()).andReturn("1");
// replay mocks
replay(options);
replay(template);
replay(strategy);
// run
RunInstancesOptions runOptions = strategy.execute(region, tag, template);
assertEquals(runOptions.buildQueryParameters(), ImmutableMultimap
.<String, String> of());
assertEquals(runOptions.buildFormParameters().entries(),
ImmutableMultimap.<String, String> of("InstanceType",
size.getProviderId(), "AdditionalInfo", tag, "SubnetId", "1",
"KeyName", systemGeneratedKeyPairName).entries());
assertEquals(runOptions.buildMatrixParameters(), ImmutableMultimap
.<String, String> of());
assertEquals(runOptions.buildRequestHeaders(), ImmutableMultimap
.<String, String> of());
assertEquals(runOptions.buildStringPayload(), null);
// verify mocks
@ -122,8 +187,8 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
replayStrategy(strategy);
// run
assertEquals(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, options),
userSuppliedKeyPair);
assertEquals(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(
region, tag, options), userSuppliedKeyPair);
// verify mocks
verify(options);
@ -146,13 +211,15 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
// setup expectations
expect(options.getKeyPair()).andReturn(userSuppliedKeyPair);
expect(options.shouldAutomaticallyCreateKeyPair())
.andReturn(shouldAutomaticallyCreateKeyPair);
expect(strategy.createUniqueKeyPair.apply(new RegionAndName(region, tag))).andReturn(keyPair);
expect(keyPair.getKeyName()).andReturn(systemGeneratedKeyPairName).atLeastOnce();
expect(options.shouldAutomaticallyCreateKeyPair()).andReturn(
shouldAutomaticallyCreateKeyPair);
expect(strategy.createUniqueKeyPair.apply(new RegionAndName(region, tag)))
.andReturn(keyPair);
expect(keyPair.getKeyName()).andReturn(systemGeneratedKeyPairName)
.atLeastOnce();
expect(
strategy.credentialsMap.put(new RegionAndName(region, systemGeneratedKeyPairName),
keyPair)).andReturn(null);
strategy.credentialsMap.put(new RegionAndName(region,
systemGeneratedKeyPairName), keyPair)).andReturn(null);
// replay mocks
replay(options);
@ -160,8 +227,8 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
replayStrategy(strategy);
// run
assertEquals(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, options),
systemGeneratedKeyPairName);
assertEquals(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(
region, tag, options), systemGeneratedKeyPairName);
// verify mocks
verify(options);
@ -174,7 +241,8 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
String region = Region.AP_SOUTHEAST_1;
String tag = "tag";
String userSuppliedKeyPair = null;
boolean shouldAutomaticallyCreateKeyPair = false; // here's the important part!
boolean shouldAutomaticallyCreateKeyPair = false; // here's the important
// part!
// create mocks
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions strategy = setupStrategy();
@ -183,8 +251,8 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
// setup expectations
expect(options.getKeyPair()).andReturn(userSuppliedKeyPair);
expect(options.shouldAutomaticallyCreateKeyPair())
.andReturn(shouldAutomaticallyCreateKeyPair);
expect(options.shouldAutomaticallyCreateKeyPair()).andReturn(
shouldAutomaticallyCreateKeyPair);
// replay mocks
replay(options);
@ -192,8 +260,8 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
replayStrategy(strategy);
// run
assertEquals(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, options),
null);
assertEquals(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(
region, tag, options), null);
// verify mocks
verify(options);
@ -219,21 +287,25 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
// setup expectations
expect(options.getGroupIds()).andReturn(groupIds).atLeastOnce();
expect(options.getInboundPorts()).andReturn(ports).atLeastOnce();
RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region,
generatedMarkerGroup, ports, shouldAuthorizeSelf);
expect(strategy.securityGroupMap.containsKey(regionNameAndIngressRules)).andReturn(
groupExisted);
expect(strategy.createSecurityGroupIfNeeded.apply(regionNameAndIngressRules)).andReturn(
generatedMarkerGroup);
expect(strategy.securityGroupMap.put(regionNameAndIngressRules, generatedMarkerGroup))
.andReturn(null);
RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(
region, generatedMarkerGroup, ports, shouldAuthorizeSelf);
expect(strategy.securityGroupMap.containsKey(regionNameAndIngressRules))
.andReturn(groupExisted);
expect(
strategy.createSecurityGroupIfNeeded
.apply(regionNameAndIngressRules)).andReturn(
generatedMarkerGroup);
expect(
strategy.securityGroupMap.put(regionNameAndIngressRules,
generatedMarkerGroup)).andReturn(null);
// replay mocks
replay(options);
replayStrategy(strategy);
// run
assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, tag, options), returnVal);
assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, tag,
options), returnVal);
// verify mocks
verify(options);
@ -258,21 +330,25 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
// setup expectations
expect(options.getGroupIds()).andReturn(groupIds).atLeastOnce();
expect(options.getInboundPorts()).andReturn(ports).atLeastOnce();
RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region,
generatedMarkerGroup, ports, shouldAuthorizeSelf);
expect(strategy.securityGroupMap.containsKey(regionNameAndIngressRules)).andReturn(
groupExisted);
expect(strategy.createSecurityGroupIfNeeded.apply(regionNameAndIngressRules)).andReturn(
generatedMarkerGroup);
expect(strategy.securityGroupMap.put(regionNameAndIngressRules, generatedMarkerGroup))
.andReturn(null);
RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(
region, generatedMarkerGroup, ports, shouldAuthorizeSelf);
expect(strategy.securityGroupMap.containsKey(regionNameAndIngressRules))
.andReturn(groupExisted);
expect(
strategy.createSecurityGroupIfNeeded
.apply(regionNameAndIngressRules)).andReturn(
generatedMarkerGroup);
expect(
strategy.securityGroupMap.put(regionNameAndIngressRules,
generatedMarkerGroup)).andReturn(null);
// replay mocks
replay(options);
replayStrategy(strategy);
// run
assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, tag, options), returnVal);
assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, tag,
options), returnVal);
// verify mocks
verify(options);
@ -297,17 +373,18 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
// setup expectations
expect(options.getGroupIds()).andReturn(groupIds).atLeastOnce();
expect(options.getInboundPorts()).andReturn(ports).atLeastOnce();
RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region,
generatedMarkerGroup, ports, shouldAuthorizeSelf);
expect(strategy.securityGroupMap.containsKey(regionNameAndIngressRules)).andReturn(
groupExisted);
RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(
region, generatedMarkerGroup, ports, shouldAuthorizeSelf);
expect(strategy.securityGroupMap.containsKey(regionNameAndIngressRules))
.andReturn(groupExisted);
// replay mocks
replay(options);
replayStrategy(strategy);
// run
assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, tag, options), returnVal);
assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, tag,
options), returnVal);
// verify mocks
verify(options);
@ -323,7 +400,8 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
int[] ports = new int[] {};
boolean shouldAuthorizeSelf = true;
boolean groupExisted = true;
Set<String> returnVal = ImmutableSet.<String> of(generatedMarkerGroup, "group1", "group2");
Set<String> returnVal = ImmutableSet.<String> of(generatedMarkerGroup,
"group1", "group2");
// create mocks
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions strategy = setupStrategy();
@ -331,25 +409,31 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
// setup expectations
expect(options.getGroupIds()).andReturn(groupIds).atLeastOnce();
RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region,
generatedMarkerGroup, ports, shouldAuthorizeSelf); // note this works since there's
RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(
region, generatedMarkerGroup, ports, shouldAuthorizeSelf); // note
// this
// works
// since
// there's
// no equals on portsq
expect(strategy.securityGroupMap.containsKey(regionNameAndIngressRules)).andReturn(
groupExisted);
expect(strategy.securityGroupMap.containsKey(regionNameAndIngressRules))
.andReturn(groupExisted);
// replay mocks
replay(options);
replayStrategy(strategy);
// run
assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, tag, options), returnVal);
assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, tag,
options), returnVal);
// verify mocks
verify(options);
verifyStrategy(strategy);
}
private void verifyStrategy(CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions strategy) {
private void verifyStrategy(
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions strategy) {
verify(strategy.credentialsMap);
verify(strategy.securityGroupMap);
verify(strategy.createUniqueKeyPair);
@ -363,11 +447,13 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
CreateUniqueKeyPair createUniqueKeyPair = createMock(CreateUniqueKeyPair.class);
CreateSecurityGroupIfNeeded createSecurityGroupIfNeeded = createMock(CreateSecurityGroupIfNeeded.class);
return new CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions(credentialsMap,
securityGroupMap, createUniqueKeyPair, createSecurityGroupIfNeeded);
return new CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions(
credentialsMap, securityGroupMap, createUniqueKeyPair,
createSecurityGroupIfNeeded);
}
private void replayStrategy(CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions strategy) {
private void replayStrategy(
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions strategy) {
replay(strategy.credentialsMap);
replay(strategy.securityGroupMap);
replay(strategy.createUniqueKeyPair);