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

@ -60,7 +60,8 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
@Inject @Inject
CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions( CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions(
Map<RegionAndName, KeyPair> credentialsMap, Map<RegionAndName, KeyPair> credentialsMap,
Map<RegionAndName, String> securityGroupMap, CreateUniqueKeyPair createUniqueKeyPair, Map<RegionAndName, String> securityGroupMap,
CreateUniqueKeyPair createUniqueKeyPair,
CreateSecurityGroupIfNeeded createSecurityGroupIfNeeded) { CreateSecurityGroupIfNeeded createSecurityGroupIfNeeded) {
this.credentialsMap = credentialsMap; this.credentialsMap = credentialsMap;
this.securityGroupMap = securityGroupMap; this.securityGroupMap = securityGroupMap;
@ -68,22 +69,27 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
this.createSecurityGroupIfNeeded = createSecurityGroupIfNeeded; 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, 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()); EC2Size ec2Size = EC2Size.class.cast(template.getSize());
RunInstancesOptions instanceOptions = asType(ec2Size.getInstanceType()).withAdditionalInfo(tag); RunInstancesOptions instanceOptions = asType(ec2Size.getInstanceType())
.withAdditionalInfo(tag);
String keyPairName = createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, template String keyPairName = createNewKeyPairUnlessUserSpecifiedOtherwise(region,
.getOptions()); tag, template.getOptions());
String subnetId = template.getOptions().as(EC2TemplateOptions.class).getSubnetId(); String subnetId = EC2TemplateOptions.class.cast(template.getOptions())
if(subnetId != null) .getSubnetId();
if (subnetId != null) {
instanceOptions.withSubnetId(subnetId); instanceOptions.withSubnetId(subnetId);
} else {
else { Set<String> groups = getSecurityGroupsForTagAndOptions(region, tag,
Set<String> groups = getSecurityGroupsForTagAndOptions(region, tag, template.getOptions()); template.getOptions());
instanceOptions.withSecurityGroups(groups); instanceOptions.withSecurityGroups(groups);
} }
@ -94,33 +100,35 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
} }
@VisibleForTesting @VisibleForTesting
String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String tag, String createNewKeyPairUnlessUserSpecifiedOtherwise(String region,
TemplateOptions options) { String tag, TemplateOptions options) {
String keyPairName = null; String keyPairName = null;
boolean shouldAutomaticallyCreateKeyPair = true; boolean shouldAutomaticallyCreateKeyPair = true;
if (options instanceof EC2TemplateOptions) { if (options instanceof EC2TemplateOptions) {
keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair(); keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair();
if (keyPairName == null) if (keyPairName == null)
shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(options) shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(
.shouldAutomaticallyCreateKeyPair(); options).shouldAutomaticallyCreateKeyPair();
} }
if (keyPairName == null && shouldAutomaticallyCreateKeyPair) { if (keyPairName == null && shouldAutomaticallyCreateKeyPair) {
RegionAndName regionAndName = new RegionAndName(region, tag); RegionAndName regionAndName = new RegionAndName(region, tag);
KeyPair keyPair = createUniqueKeyPair.apply(regionAndName); KeyPair keyPair = createUniqueKeyPair.apply(regionAndName);
// get or create incidental resources // 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 // when
// another thread // another thread
// deletes a key // deletes a key
credentialsMap.put(new RegionAndName(region, keyPair.getKeyName()), keyPair); credentialsMap.put(new RegionAndName(region, keyPair.getKeyName()),
keyPair);
keyPairName = keyPair.getKeyName(); keyPairName = keyPair.getKeyName();
} }
return keyPairName; return keyPairName;
} }
@VisibleForTesting @VisibleForTesting
Set<String> getSecurityGroupsForTagAndOptions(String region, @Nullable String tag, Set<String> getSecurityGroupsForTagAndOptions(String region,
TemplateOptions options) { @Nullable String tag, TemplateOptions options) {
Set<String> groups = Sets.newLinkedHashSet(); Set<String> groups = Sets.newLinkedHashSet();
if (tag != null) { if (tag != null) {
@ -131,18 +139,20 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions {
if (options instanceof EC2TemplateOptions if (options instanceof EC2TemplateOptions
&& EC2TemplateOptions.class.cast(options).getGroupIds().size() > 0) { && EC2TemplateOptions.class.cast(options).getGroupIds().size() > 0) {
regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region, regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(
markerGroup, new int[] {}, false); region, markerGroup, new int[] {}, false);
groups.addAll(EC2TemplateOptions.class.cast(options).getGroupIds()); groups.addAll(EC2TemplateOptions.class.cast(options).getGroupIds());
} else { } else {
regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(region, regionNameAndIngessRulesForMarkerGroup = new RegionNameAndIngressRules(
markerGroup, options.getInboundPorts(), true); region, markerGroup, options.getInboundPorts(), true);
} }
if (!securityGroupMap.containsKey(regionNameAndIngessRulesForMarkerGroup)) { if (!securityGroupMap
.containsKey(regionNameAndIngessRulesForMarkerGroup)) {
securityGroupMap.put(regionNameAndIngessRulesForMarkerGroup, securityGroupMap.put(regionNameAndIngessRulesForMarkerGroup,
createSecurityGroupIfNeeded.apply(regionNameAndIngessRulesForMarkerGroup)); createSecurityGroupIfNeeded
.apply(regionNameAndIngessRulesForMarkerGroup));
} }
} }
return groups; return groups;

View File

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