@Override
@Test
public void testExtendedOptionsAndLogin() throws Exception {
String region = "us-west-2";
AWSSecurityGroupClient securityGroupClient = AWSEC2Client.class.cast(
view.unwrap(AWSEC2ApiMetadata.CONTEXT_TOKEN).getApi()).getSecurityGroupServices();
KeyPairClient keyPairClient = EC2Client.class.cast(view.unwrap(AWSEC2ApiMetadata.CONTEXT_TOKEN).getApi())
.getKeyPairServices();
InstanceClient instanceClient = EC2Client.class.cast(view.unwrap(AWSEC2ApiMetadata.CONTEXT_TOKEN).getApi())
.getInstanceServices();
String group = this.group + "o";
Date before = new Date();
ImmutableMap<String, String> userMetadata = ImmutableMap.<String, String> of("test", group);
ImmutableSet<String> tags = ImmutableSet.of(group);
// note that if you change the location, you must also specify image parameters
Template template = client.templateBuilder().locationId(region).osFamily(AMZN_LINUX).os64Bit(true).build();
template.getOptions().tags(tags);
template.getOptions().userMetadata(userMetadata);
template.getOptions().tags(tags);
template.getOptions().as(AWSEC2TemplateOptions.class).enableMonitoring();
template.getOptions().as(AWSEC2TemplateOptions.class).spotPrice(0.3f);
String startedId = null;
try {
cleanupExtendedStuffInRegion(region, securityGroupClient, keyPairClient, group);
Thread.sleep(3000);// eventual consistency if deletes actually occurred.
// create a security group that allows ssh in so that our scripts later
// will work
String groupId = securityGroupClient.createSecurityGroupInRegionAndReturnId(region, group, group);
securityGroupClient.authorizeSecurityGroupIngressInRegion(region, groupId, permit(IpProtocol.TCP).port(22));
template.getOptions().as(AWSEC2TemplateOptions.class).securityGroupIds(groupId);
// create a keypair to pass in as well
KeyPair result = keyPairClient.createKeyPairInRegion(region, group);
template.getOptions().as(AWSEC2TemplateOptions.class).keyPair(result.getKeyName());
// pass in the private key, so that we can run a script with it
assert result.getKeyMaterial() != null : result;
template.getOptions().overrideLoginPrivateKey(result.getKeyMaterial());
Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, template);
NodeMetadata first = getOnlyElement(nodes);
checkUserMetadataContains(first, userMetadata);
checkTagsInNodeEquals(first, tags);
assert first.getCredentials() != null : first;
assert first.getCredentials().identity != null : first;
startedId = first.getProviderId();
AWSRunningInstance instance = AWSRunningInstance.class.cast(getOnlyElement(getOnlyElement(instanceClient
.describeInstancesInRegion(region, startedId))));
assertEquals(instance.getKeyName(), group);
assert instance.getSpotInstanceRequestId() != null;
assertEquals(instance.getMonitoringState(), MonitoringState.ENABLED);
// generate some load
ListenableFuture<ExecResponse> future = client.submitScriptOnNode(first.getId(), Statements
.exec("while true; do true; done"), runAsRoot(false).nameTask("cpuSpinner"));
// monitoring granularity for free tier is 5 minutes, so lets make sure we have data.
Thread.sleep(TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES));
// stop the spinner
future.cancel(true);
CloudWatchApi monitoringApi = ContextBuilder.newBuilder(new AWSCloudWatchProviderMetadata())
.credentials(identity, credential)
.modules(setupModules())
.buildApi(CloudWatchApi.class);
try {
GetMetricStatisticsResponse datapoints = monitoringApi.getMetricApiForRegion(instance.getRegion())
.getMetricStatistics(GetMetricStatistics.builder()
.dimension(new Dimension(EC2Constants.Dimension.INSTANCE_ID, instance.getId()))
.unit(Unit.PERCENT)
.namespace("AWS/EC2")
.metricName("CPUUtilization")
.startTime(before)
.endTime(new Date())
.period(60)
.statistic(Statistics.AVERAGE)
.build());
assert (datapoints.size() > 0) : instance;
} finally {
monitoringApi.close();
}
// make sure we made our dummy group and also let in the user's group
assertEquals(newTreeSet(instance.getGroupNames()), ImmutableSortedSet.<String> of("jclouds#" + group, group));
// make sure our dummy group has no rules
SecurityGroup secgroup = getOnlyElement(securityGroupClient.describeSecurityGroupsInRegion(instance
.getRegion(), "jclouds#" + group));
assert secgroup.size() == 0 : secgroup;
// try to run a script with the original keyPair
runScriptWithCreds(group, first.getOperatingSystem(), LoginCredentials.builder().user(
first.getCredentials().identity).privateKey(result.getKeyMaterial()).build());
} finally {
client.destroyNodesMatching(NodePredicates.inGroup(group));
if (startedId != null) {
// ensure we didn't delete these resources!
assertEquals(keyPairClient.describeKeyPairsInRegion(region, group).size(), 1);
assertEquals(securityGroupClient.describeSecurityGroupsInRegion(region, group).size(), 1);
}
cleanupExtendedStuffInRegion(region, securityGroupClient, keyPairClient, group);
}
}