use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.
the class AWSResetServiceTest method testReset.
// Test the reset operation.
@Test
public void testReset() throws Throwable {
provisionSingleAWS();
// check that the VM has been created
ProvisioningUtils.queryComputeInstances(this.host, 2);
ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
if (!this.isMock) {
List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
Instance instance = instances.get(0);
ComputeState vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
setVMSecurityGroupsToBeDeleted(instance, vm);
}
String taskLink = UUID.randomUUID().toString();
ResourceOperationRequest request = new ResourceOperationRequest();
request.isMockRequest = this.isMock;
request.operation = ResourceOperation.RESET.operation;
request.payload = new HashMap<>();
request.resourceReference = UriUtils.buildUri(this.host, compute.documentSelfLink);
request.taskReference = UriUtils.buildUri(this.host, taskLink);
TestContext ctx = this.host.testCreate(1);
createTaskResultListener(this.host, taskLink, (u) -> {
if (u.getAction() != Service.Action.PATCH) {
return false;
}
ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
if (TaskState.isFailed(response.taskInfo)) {
ctx.failIteration(new IllegalStateException(response.taskInfo.failure.message));
} else if (TaskState.isFinished(response.taskInfo)) {
ctx.completeIteration();
}
return true;
});
TestContext ctx2 = this.host.testCreate(1);
Operation resetOp = Operation.createPatch(UriUtils.buildUri(this.host, AWSResetService.SELF_LINK)).setBody(request).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
if (e != null) {
ctx2.failIteration(e);
return;
}
ctx2.completeIteration();
});
this.host.send(resetOp);
ctx2.await();
// in EC2 documentation
if (!this.isMock) {
// Waiting for power off
this.host.waitFor("Timed out waiting for EC2 to power off", () -> {
String state = getVMState(this.client, compute.id);
if (("stopping".equals(state) || "stopped".equals(state))) {
this.host.log(Level.INFO, "EC2 is being powered off");
return true;
} else {
return false;
}
});
// Waiting for power on
this.host.waitFor("Timed out waiting for EC2 to power on", () -> {
String state = getVMState(this.client, compute.id);
if ("running".equals(state)) {
this.host.log(Level.INFO, "EC2 is being powered on");
return true;
} else {
return false;
}
});
}
}
use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.
the class TestAWSClientManagement method testAwsS3ClientManagement.
@Test
public void testAwsS3ClientManagement() throws Throwable {
// Ensure that we start with a clean state.
AWSClientManagerFactory.cleanUp(AwsClientType.S3_TRANSFER_MANAGER);
// Get a reference to the client manager in the test
AWSClientManager s3ClientManager = getClientManager(AwsClientType.S3_TRANSFER_MANAGER);
assertEquals(1, getClientReferenceCount(AwsClientType.S3_TRANSFER_MANAGER));
AuthCredentialsServiceState testCreds = new AuthCredentialsServiceState();
testCreds.privateKey = this.accessKey;
testCreds.privateKeyId = this.secretKey;
final AmazonS3Client[] s3Client = new AmazonS3Client[1];
TestContext waitContext = new TestContext(1, Duration.ofSeconds(30L));
s3ClientManager.getOrCreateS3TransferManagerAsync(testCreds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
waitContext.fail(t);
throw new CompletionException(t);
}).thenAccept(i -> waitContext.complete());
waitContext.await();
assertEquals(1, s3ClientManager.getCacheCount());
// Return the references from the test
returnClientManager(s3ClientManager, AwsClientType.S3_TRANSFER_MANAGER);
assertEquals(0, getClientReferenceCount(AwsClientType.S3_TRANSFER_MANAGER));
}
use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.
the class TestAWSClientManagement method testEc2ClientInvalidArnKey.
@Test
public void testEc2ClientInvalidArnKey() throws Throwable {
this.ec2ClientReferenceCount = getClientReferenceCount(AwsClientType.EC2);
this.host.setTimeoutSeconds(60);
// Getting a reference to client managers in the test
AWSClientManager ec2ClientManager = getClientManager(AwsClientType.EC2);
ec2ClientManager.cleanUpArnCache();
assertEquals(this.ec2ClientReferenceCount + 1, getClientReferenceCount(AwsClientType.EC2));
this.creds = new AuthCredentialsServiceState();
this.creds.customProperties = new HashMap<>();
this.creds.customProperties.put(ARN_KEY, this.arn + "-invalid");
this.creds.customProperties.put(EXTERNAL_ID_KEY, this.externalId);
AWSSecurityTokenServiceException[] expectedException = new AWSSecurityTokenServiceException[1];
TestContext waitContext = new TestContext(1, Duration.ofSeconds(60L));
ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
expectedException[0] = (AWSSecurityTokenServiceException) t.getCause();
waitContext.complete();
throw new CompletionException(t);
});
waitContext.await();
Assert.assertNull(this.client);
Assert.assertEquals(Operation.STATUS_CODE_FORBIDDEN, expectedException[0].getStatusCode());
Assert.assertEquals("AccessDenied", expectedException[0].getErrorCode());
}
use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.
the class TestAWSClientManagement method testEc2ClientInvalidExternalId.
@Test
public void testEc2ClientInvalidExternalId() throws Throwable {
this.ec2ClientReferenceCount = getClientReferenceCount(AwsClientType.EC2);
this.host.setTimeoutSeconds(60);
// Getting a reference to client managers in the test
AWSClientManager ec2ClientManager = getClientManager(AwsClientType.EC2);
ec2ClientManager.cleanUpArnCache();
assertEquals(this.ec2ClientReferenceCount + 1, getClientReferenceCount(AwsClientType.EC2));
this.creds = new AuthCredentialsServiceState();
this.creds.customProperties = new HashMap<>();
this.creds.customProperties.put(ARN_KEY, this.arn);
this.creds.customProperties.put(EXTERNAL_ID_KEY, "invalid");
AWSSecurityTokenServiceException[] expectedException = new AWSSecurityTokenServiceException[1];
TestContext waitContext = new TestContext(1, Duration.ofSeconds(60L));
ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
expectedException[0] = (AWSSecurityTokenServiceException) t.getCause();
waitContext.complete();
throw new CompletionException(t);
});
waitContext.await();
Assert.assertNull(this.client);
Assert.assertEquals(Operation.STATUS_CODE_FORBIDDEN, expectedException[0].getStatusCode());
Assert.assertEquals("AccessDenied", expectedException[0].getErrorCode());
}
use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.
the class BaseVSphereAdapterTest method suspendVSphereVM.
protected void suspendVSphereVM(ComputeState computeState) {
String taskLink = UUID.randomUUID().toString();
ResourceOperationRequest suspendVMRequest = getResourceOperationRequest("Suspend", computeState.documentSelfLink, taskLink);
TestContext ctx = this.host.testCreate(1);
createTaskResultListener(this.host, taskLink, (u) -> {
if (u.getAction() != Service.Action.PATCH) {
return false;
}
ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
if (TaskState.isFailed(response.taskInfo)) {
ctx.failIteration(new IllegalStateException(response.taskInfo.failure.message));
} else {
ctx.completeIteration();
}
return true;
});
TestContext ctx2 = this.host.testCreate(1);
Operation suspendOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterD2PowerOpsService.SELF_LINK)).setBody(suspendVMRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
if (e != null) {
ctx2.failIteration(e);
return;
}
ctx2.completeIteration();
});
this.host.send(suspendOp);
ctx2.await();
ComputeState[] cstate = new ComputeState[1];
this.host.waitFor("Suspend request failed", () -> {
cstate[0] = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeState.documentSelfLink));
if (cstate[0].powerState.equals(ComputeService.PowerState.SUSPEND)) {
assertTrue(cstate[0].address.isEmpty());
return true;
} else {
return false;
}
});
assertEquals(ComputeService.PowerState.SUSPEND, cstate[0].powerState);
}
Aggregations