Search in sources :

Example 1 with TestContext

use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.

the class TestAWSClientManagement method testAWSClientManagementArnRefresh.

/**
 * This test requires a minimum of 15 minutes waiting to ensure the ARN credentials refresh
 * occurs.
 */
@Ignore
@Test
public void testAWSClientManagementArnRefresh() throws Throwable {
    this.ec2ClientReferenceCount = getClientReferenceCount(AwsClientType.EC2);
    this.host.setTimeoutSeconds(1200);
    // 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, this.externalId);
    TestContext waitContext = new TestContext(1, Duration.ofSeconds(30L));
    ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
        waitContext.fail(t);
        throw new CompletionException(t);
    }).thenAccept(ec2Client -> {
        this.client = ec2Client;
        waitContext.complete();
    });
    waitContext.await();
    this.clientCacheCount = ec2ClientManager.getCacheCount();
    host.log(Level.INFO, "Waiting 16 minutes for the current set of credentials to expire.");
    Thread.sleep(TimeUnit.MINUTES.toMillis(16));
    host.log(Level.INFO, "Retrieving the ec2 client with a refreshed set of credentials.");
    // Requesting the EC2 client will generate a new client as the original client's credentials
    // are now expired.
    AmazonEC2AsyncClient oldClient = this.client;
    TestContext nextContext = new TestContext(1, Duration.ofSeconds(30L));
    ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
        nextContext.fail(t);
        throw new CompletionException(t);
    }).thenAccept(ec2Client -> {
        this.client = ec2Client;
        this.clientCacheCount++;
        nextContext.complete();
    });
    nextContext.await();
    assertNotEquals(oldClient, this.client);
    assertEquals(this.clientCacheCount, ec2ClientManager.getCacheCount());
}
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) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with TestContext

use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.

the class TestAWSClientManagement method testAWSClientManagementArn.

@Test
public void testAWSClientManagementArn() 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, this.externalId);
    TestContext waitContext = new TestContext(1, Duration.ofSeconds(30L));
    ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
        waitContext.fail(t);
        throw new CompletionException(t);
    }).thenAccept(ec2Client -> {
        this.client = ec2Client;
        waitContext.complete();
    });
    waitContext.await();
    Assert.assertNotNull(this.client);
    this.clientCacheCount = ec2ClientManager.getCacheCount();
    // Requesting another AWS client with the same set of credentials will not
    // create a new entry in the cache
    AmazonEC2AsyncClient oldClient = this.client;
    TestContext nextContext = new TestContext(1, Duration.ofSeconds(30L));
    ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
        nextContext.fail(t);
        throw new CompletionException(t);
    }).thenAccept(ec2Client -> {
        this.client = ec2Client;
        nextContext.complete();
    });
    nextContext.await();
    Assert.assertNotNull(this.client);
    Assert.assertEquals(oldClient, this.client);
    assertEquals(this.clientCacheCount, ec2ClientManager.getCacheCount());
}
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) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Example 3 with TestContext

use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.

the class TestAWSClientManagement method testAWSClientManagementArnDr.

@Test
public void testAWSClientManagementArnDr() 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, this.externalId);
    TestContext waitContext = new TestContext(1, Duration.ofSeconds(30L));
    ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
        waitContext.fail(t);
        throw new CompletionException(t);
    }).thenAccept(ec2Client -> {
        this.client = ec2Client;
        waitContext.complete();
    });
    waitContext.await();
    Assert.assertNotNull(this.client);
    this.clientCacheCount = ec2ClientManager.getCacheCount();
    // Requesting another AWS client with the same set of credentials will not
    // create a new entry in the cache
    AmazonEC2AsyncClient oldClient = this.client;
    TestContext nextContext = new TestContext(1, Duration.ofSeconds(30L));
    ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
        nextContext.fail(t);
        throw new CompletionException(t);
    }).thenAccept(ec2Client -> {
        this.client = ec2Client;
        nextContext.complete();
    });
    nextContext.await();
    Assert.assertNotNull(this.client);
    Assert.assertEquals(oldClient, this.client);
    assertEquals(this.clientCacheCount, ec2ClientManager.getCacheCount());
}
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) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Example 4 with TestContext

use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.

the class TestAWSClientManagement method testAWSClientManagement.

@Test
public void testAWSClientManagement() throws Throwable {
    this.ec2ClientReferenceCount = getClientReferenceCount(AwsClientType.EC2);
    this.cloudWatchClientReferenceCount = getClientReferenceCount(AwsClientType.CLOUD_WATCH);
    this.host.setTimeoutSeconds(60);
    // Getting a reference to client managers in the test
    AWSClientManager ec2ClientManager = getClientManager(AwsClientType.EC2);
    ec2ClientManager.cleanUpArnCache();
    @SuppressWarnings("unused") AWSClientManager cloudWatchClientManager = getClientManager(AwsClientType.CLOUD_WATCH);
    cloudWatchClientManager.cleanUpArnCache();
    assertEquals(this.ec2ClientReferenceCount + 1, getClientReferenceCount(AwsClientType.EC2));
    assertEquals(this.cloudWatchClientReferenceCount + 1, getClientReferenceCount(AwsClientType.CLOUD_WATCH));
    // Getting an AWSclient from the client manager
    this.creds = new AuthCredentialsServiceState();
    this.creds.privateKey = this.accessKey;
    this.creds.privateKeyId = this.secretKey;
    TestContext waitContext = new TestContext(1, Duration.ofSeconds(30L));
    ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
        waitContext.fail(t);
        throw new CompletionException(t);
    }).thenAccept(ec2Client -> {
        this.client = ec2Client;
        waitContext.complete();
    });
    waitContext.await();
    this.clientCacheCount = ec2ClientManager.getCacheCount();
    // Requesting another AWS client with the same set of credentials will not
    // create a new entry in the cache
    TestContext nextContext = new TestContext(1, Duration.ofSeconds(30L));
    ec2ClientManager.getOrCreateEC2ClientAsync(this.creds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
        nextContext.fail(t);
        throw new CompletionException(t);
    }).thenAccept(ec2Client -> {
        this.client = ec2Client;
        nextContext.complete();
    });
    nextContext.await();
    assertEquals(this.clientCacheCount, ec2ClientManager.getCacheCount());
    // Emulating shutdown of individual services to check that the client resources are
    // cleaned up as expected.
    this.host.sendAndWaitExpectSuccess(Operation.createDelete(UriUtils.buildUri(this.host, AWSInstanceService.SELF_LINK)));
    assertEquals(this.ec2ClientReferenceCount, getClientReferenceCount(AwsClientType.EC2));
    this.host.sendAndWaitExpectSuccess(Operation.createDelete(UriUtils.buildUri(this.host, AWSStatsService.SELF_LINK)));
    assertEquals(this.cloudWatchClientReferenceCount, getClientReferenceCount(AwsClientType.CLOUD_WATCH));
    // Returning the references from the test
    returnClientManager(ec2ClientManager, AwsClientType.EC2);
    assertEquals(this.ec2ClientReferenceCount - 1, getClientReferenceCount(AwsClientType.EC2));
}
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) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Example 5 with TestContext

use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.

the class BaseTestCase method doOperation.

protected <T extends ServiceDocument> T doOperation(T inState, URI uri, Class<T> type, boolean expectFailure, Action action) throws Throwable {
    this.host.log("Executing operation %s for resource: %s ...", action.name(), uri);
    final List<T> doc = Arrays.asList((T) null);
    final Throwable[] error = { null };
    TestContext ctx = testCreate(1);
    Operation op;
    if (action == Action.POST) {
        op = createForcedPost(uri);
    } else {
        // createPost sets the proper authorization context for the operation
        op = Operation.createPost(uri);
        // replace POST with the provided action
        op.setAction(action);
    }
    op.addPragmaDirective(Operation.PRAGMA_DIRECTIVE_QUEUE_FOR_SERVICE_AVAILABILITY);
    op.setBody(inState).setCompletion((o, e) -> {
        if (e != null) {
            if (expectFailure) {
                error[0] = e;
                ctx.completeIteration();
            } else {
                ctx.failIteration(e);
            }
            return;
        }
        if (!o.hasBody()) {
            ctx.failIteration(new IllegalStateException("body was expected"));
            return;
        }
        doc.set(0, o.getBody(type));
        if (expectFailure) {
            ctx.failIteration(new IllegalStateException("ERROR: operation completed successfully but exception excepted."));
        } else {
            ctx.completeIteration();
        }
    });
    this.host.send(op);
    ctx.await();
    this.host.logThroughput();
    if (expectFailure) {
        Throwable ex = error[0];
        throw ex;
    }
    return doc.get(0);
}
Also used : TestContext(com.vmware.xenon.common.test.TestContext) 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