Search in sources :

Example 21 with TestContext

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;
            }
        });
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) Instance(com.amazonaws.services.ec2.model.Instance) TestContext(com.vmware.xenon.common.test.TestContext) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) Operation(com.vmware.xenon.common.Operation) Test(org.junit.Test)

Example 22 with TestContext

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));
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) BeforeClass(org.junit.BeforeClass) HashMap(java.util.HashMap) AWSClientManagerFactory.returnClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory.returnClientManager) AWSClientManagerFactory.getClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory.getClientManager) AWSClientManagerFactory.getClientReferenceCount(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory.getClientReferenceCount) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) AWSSecurityTokenServiceException(com.amazonaws.services.securitytoken.model.AWSSecurityTokenServiceException) Duration(java.time.Duration) After(org.junit.After) EXTERNAL_ID_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.EXTERNAL_ID_KEY) AWS_ARN_DEFAULT_SESSION_DURATION_SECONDS_PROPERTY(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.AWS_ARN_DEFAULT_SESSION_DURATION_SECONDS_PROPERTY) AwsClientType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AwsClientType) BasicReusableHostTestCase(com.vmware.xenon.common.BasicReusableHostTestCase) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) Before(org.junit.Before) StatelessService(com.vmware.xenon.common.StatelessService) AWS_MASTER_ACCOUNT_ACCESS_KEY_PROPERTY(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.AWS_MASTER_ACCOUNT_ACCESS_KEY_PROPERTY) Operation(com.vmware.xenon.common.Operation) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AWS_MASTER_ACCOUNT_SECRET_KEY_PROPERTY(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.AWS_MASTER_ACCOUNT_SECRET_KEY_PROPERTY) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) TestContext(com.vmware.xenon.common.test.TestContext) Ignore(org.junit.Ignore) ARN_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.ARN_KEY) UriUtils(com.vmware.xenon.common.UriUtils) Assume.assumeTrue(org.junit.Assume.assumeTrue) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Example 23 with TestContext

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());
}
Also used : AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) AWSSecurityTokenServiceException(com.amazonaws.services.securitytoken.model.AWSSecurityTokenServiceException) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Example 24 with TestContext

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());
}
Also used : AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) AWSSecurityTokenServiceException(com.amazonaws.services.securitytoken.model.AWSSecurityTokenServiceException) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Example 25 with TestContext

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);
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestContext(com.vmware.xenon.common.test.TestContext) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) Operation(com.vmware.xenon.common.Operation)

Aggregations

TestContext (com.vmware.xenon.common.test.TestContext)43 Operation (com.vmware.xenon.common.Operation)37 Test (org.junit.Test)18 ResourceOperationResponse (com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse)14 UriUtils (com.vmware.xenon.common.UriUtils)13 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)13 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)12 After (org.junit.After)12 Assert.assertEquals (org.junit.Assert.assertEquals)12 Before (org.junit.Before)12 CompletionException (java.util.concurrent.CompletionException)11 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)10 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)10 ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)10 CommandLineArgumentParser (com.vmware.xenon.common.CommandLineArgumentParser)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Duration (java.time.Duration)9 HashMap (java.util.HashMap)9 Level (java.util.logging.Level)9