use of com.vmware.photon.controller.model.ComputeProperties.PLACEMENT_LINK in project photon-model by vmware.
the class TestAWSProvisionTask method testProvision.
// Creates a AWS instance via a provision task.
@Test
public void testProvision() throws Throwable {
initResourcePoolAndComputeHost();
// create a AWS VM compute resoruce
boolean addNonExistingSecurityGroup = true;
this.vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, this.getClass(), this.currentTestName.getMethodName() + "_vm1", zoneId, regionId, null, /* tagLinks */
this.singleNicSpec, addNonExistingSecurityGroup, this.awsTestContext);
// set placement link
String zoneId = TestAWSSetupUtils.zoneId + avalabilityZoneIdentifier;
ComputeState zoneComputeState = createAWSComputeHost(this.host, this.endpointState, zoneId, regionId, this.isAwsClientMock, this.awsMockEndpointReference, null);
zoneComputeState.id = zoneId;
zoneComputeState = TestUtils.doPatch(this.host, zoneComputeState, ComputeState.class, UriUtils.buildUri(this.host, zoneComputeState.documentSelfLink));
if (this.vmState.customProperties == null) {
this.vmState.customProperties = new HashMap<>();
}
this.vmState.customProperties.put(PLACEMENT_LINK, zoneComputeState.documentSelfLink);
TestUtils.doPatch(this.host, this.vmState, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
// kick off a provision task to do the actual VM creation
ProvisionComputeTaskState provisionTask = new ProvisionComputeTaskService.ProvisionComputeTaskState();
provisionTask.computeLink = this.vmState.documentSelfLink;
provisionTask.isMockRequest = this.isMock;
provisionTask.taskSubStage = ProvisionComputeTaskState.SubStage.CREATING_HOST;
// Wait for default request timeout in minutes for the machine to be powered ON before
// reporting failure to the parent task.
provisionTask.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + TimeUnit.MINUTES.toMicros(AWS_VM_REQUEST_TIMEOUT_MINUTES);
provisionTask.tenantLinks = this.endpointState.tenantLinks;
provisionTask = TestUtils.doPost(this.host, provisionTask, ProvisionComputeTaskState.class, UriUtils.buildUri(this.host, ProvisionComputeTaskService.FACTORY_LINK));
this.host.waitForFinishedTask(ProvisionComputeTaskState.class, provisionTask.documentSelfLink);
// check that the VM has been created
ProvisioningUtils.queryComputeInstances(this.host, 3);
if (!this.isMock) {
ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
Instance instance = instances.get(0);
assertTags(Collections.emptySet(), instance, this.vmState.name);
assertVmNetworksConfiguration(instance);
assertStorageConfiguration(this.client, instance, compute);
assertEquals(zoneId, instance.getPlacement().getAvailabilityZone());
}
this.host.setTimeoutSeconds(600);
this.host.waitFor("Error waiting for stats with default collection windows", () -> {
try {
this.host.log(Level.INFO, "Issuing stats request for VM with default collection window.");
issueStatsRequest(this.vmState, null);
} catch (Throwable t) {
return false;
}
return true;
});
// store the network links and disk links for removal check later
List<String> resourcesToDelete = new ArrayList<>();
if (this.vmState.diskLinks != null) {
resourcesToDelete.addAll(this.vmState.diskLinks);
}
if (this.vmState.networkInterfaceLinks != null) {
resourcesToDelete.addAll(this.vmState.networkInterfaceLinks);
}
// delete vm
TestAWSSetupUtils.deleteVMs(this.vmState.documentSelfLink, this.isMock, this.host);
// validates the local documents of network links and disk links have been removed
verifyRemovalOfResourceState(this.host, resourcesToDelete);
// create another AWS VM
List<String> instanceIdList = new ArrayList<>();
Set<TagState> tags = createTags(null, "testProvisionKey1", "testProvisionValue1", "testProvisionKey2", "testProvisionValue2");
Set<String> tagLinks = tags.stream().map(t -> t.documentSelfLink).collect(Collectors.toSet());
addNonExistingSecurityGroup = false;
this.vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, this.getClass(), this.currentTestName.getMethodName() + "_vm2", TestAWSSetupUtils.zoneId, regionId, tagLinks, this.singleNicSpec, addNonExistingSecurityGroup, this.awsTestContext);
TestAWSSetupUtils.provisionMachine(this.host, this.vmState, this.isMock, instanceIdList);
if (!this.isMock) {
ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
assertTags(tags, instances.get(0), this.vmState.name);
assertVmNetworksConfiguration(instances.get(0));
assertStorageConfiguration(this.client, instances.get(0), compute);
// reach out to AWS and get the current state
TestAWSSetupUtils.getBaseLineInstanceCount(this.host, this.client, null);
}
// delete just the local representation of the resource
TestAWSSetupUtils.deleteVMs(this.vmState.documentSelfLink, this.isMock, this.host, true);
if (!this.isMock) {
try {
TestAWSSetupUtils.getBaseLineInstanceCount(this.host, this.client, null);
} finally {
TestAWSSetupUtils.deleteVMsUsingEC2Client(this.client, this.host, instanceIdList);
deleteSecurityGroupUsingEC2Client(this.client, this.host, this.sgToCleanUp);
}
}
this.vmState = null;
this.sgToCleanUp = null;
}
Aggregations