Search in sources :

Example 16 with TestContext

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

the class BaseVSphereAdapterTest method shutdownGuestOS.

protected void shutdownGuestOS(ComputeState computeState) {
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest shutdownGuestRequest = getResourceOperationRequest("Shutdown", 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 shutdownOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterD2PowerOpsService.SELF_LINK)).setBody(shutdownGuestRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
        if (e != null) {
            ctx2.failIteration(e);
            return;
        }
        ctx2.completeIteration();
    });
    this.host.send(shutdownOp);
    ctx2.await();
    ComputeState[] cState = new ComputeState[1];
    this.host.waitFor("Guest shutdown request failed", () -> {
        cState[0] = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeState.documentSelfLink));
        if (cState[0].powerState.equals(ComputeService.PowerState.OFF)) {
            assertTrue(cState[0].address.isEmpty());
            return true;
        } else {
            return false;
        }
    });
    assertEquals(ComputeService.PowerState.OFF, 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)

Example 17 with TestContext

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

the class AWSComputeDiskDay2ServiceTest method setUp.

@Before
public void setUp() throws Exception {
    CommandLineArgumentParser.parseFromProperties(this);
    setAwsClientMockInfo(this.isAwsClientMock, this.awsMockEndpointReference);
    AuthCredentialsService.AuthCredentialsServiceState creds = new AuthCredentialsService.AuthCredentialsServiceState();
    creds.privateKey = this.secretKey;
    creds.privateKeyId = this.accessKey;
    TestContext ec2WaitContext = new TestContext(1, Duration.ofSeconds(30L));
    AWSUtils.getEc2AsyncClient(creds, TestAWSSetupUtils.regionId, getExecutor()).exceptionally(t -> {
        ec2WaitContext.fail(t);
        throw new CompletionException(t);
    }).thenAccept(ec2Client -> {
        this.client = ec2Client;
        ec2WaitContext.complete();
    });
    ec2WaitContext.await();
    this.awsTestContext = new HashMap<>();
    setUpTestVpc(this.client, this.awsTestContext, this.isMock, this.zoneId);
    this.singleNicSpec = (TestAWSSetupUtils.AwsNicSpecs) this.awsTestContext.get(TestAWSSetupUtils.NIC_SPECS_KEY);
    this.host = VerificationHost.create(0);
    try {
        this.host.setMaintenanceIntervalMicros(TimeUnit.MILLISECONDS.toMicros(250));
        this.host.start();
        PhotonModelServices.startServices(this.host);
        PhotonModelAdaptersRegistryAdapters.startServices(this.host);
        PhotonModelMetricServices.startServices(this.host);
        PhotonModelTaskServices.startServices(this.host);
        ServiceMetadata[] serviceMetadata = { factoryService(DiskTaskService.class, DiskTaskService::createFactory) };
        StartServicesHelper.startServices(this.host, serviceMetadata);
        AWSAdaptersTestUtils.startServicesSynchronously(this.host);
        this.host.setTimeoutSeconds(1200);
        this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
        this.host.waitForServiceAvailable(PhotonModelMetricServices.LINKS);
        this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
        initResourcePoolAndComputeHost(this.zoneId);
    } catch (Throwable e) {
        throw new Exception(e);
    }
}
Also used : Arrays(java.util.Arrays) ProvisionComputeTaskService(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService) TestAWSSetupUtils.createAWSComputeHost(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSComputeHost) ServiceMetadata(com.vmware.photon.controller.model.util.StartServicesHelper.ServiceMetadata) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) VerificationHost(com.vmware.xenon.common.test.VerificationHost) TestAWSSetupUtils.createAWSResourcePool(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSResourcePool) TestAWSSetupUtils.tearDownTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.tearDownTestVpc) DEVICE_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_NAME) TestAWSSetupUtils.setUpTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setUpTestVpc) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) Utils(com.vmware.xenon.common.Utils) TaskFactoryService(com.vmware.xenon.services.common.TaskFactoryService) StartServicesHelper(com.vmware.photon.controller.model.util.StartServicesHelper) Gson(com.google.gson.Gson) AWSBlockDeviceNameMapper(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSBlockDeviceNameMapper) TestAWSSetupUtils.verifyRemovalOfResourceState(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.verifyRemovalOfResourceState) Duration(java.time.Duration) Map(java.util.Map) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) After(org.junit.After) ResourcePoolService(com.vmware.photon.controller.model.resources.ResourcePoolService) TestUtils.getExecutor(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor) ProvisionDiskTaskService(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) Collector(java.util.stream.Collector) ProvisioningUtils(com.vmware.photon.controller.model.tasks.ProvisioningUtils) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) TestRequestSender(com.vmware.xenon.common.test.TestRequestSender) AWS_VM_REQUEST_TIMEOUT_MINUTES(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AWS_VM_REQUEST_TIMEOUT_MINUTES) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) CompletionException(java.util.concurrent.CompletionException) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Collectors(java.util.stream.Collectors) TestAWSSetupUtils.getResourceState(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getResourceState) TestAWSSetupUtils.avalabilityZoneIdentifier(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.avalabilityZoneIdentifier) TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client) List(java.util.List) TestAWSSetupUtils.getAwsDisksByIds(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getAwsDisksByIds) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) AWSSupportedOS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedOS) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) FactoryService(com.vmware.xenon.common.FactoryService) DiskService(com.vmware.photon.controller.model.resources.DiskService) TaskService(com.vmware.xenon.services.common.TaskService) ServiceMetadata.factoryService(com.vmware.photon.controller.model.util.StartServicesHelper.ServiceMetadata.factoryService) TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) TestAWSSetupUtils.getAwsInstancesByIds(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getAwsInstancesByIds) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) UriPaths(com.vmware.photon.controller.model.UriPaths) TestProvisionAWSDisk.createAWSDiskState(com.vmware.photon.controller.model.adapters.awsadapter.TestProvisionAWSDisk.createAWSDiskState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TestName(org.junit.rules.TestName) Volume(com.amazonaws.services.ec2.model.Volume) PLACEMENT_LINK(com.vmware.photon.controller.model.ComputeProperties.PLACEMENT_LINK) DEVICE_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_TYPE) Instance(com.amazonaws.services.ec2.model.Instance) Before(org.junit.Before) TestAWSSetupUtils.createAWSAuthentication(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSAuthentication) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Assert.assertNotNull(org.junit.Assert.assertNotNull) AWSStorageType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSStorageType) Operation(com.vmware.xenon.common.Operation) SecurityGroupService(com.vmware.photon.controller.model.resources.SecurityGroupService) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AWSSupportedVirtualizationTypes(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedVirtualizationTypes) TimeUnit(java.util.concurrent.TimeUnit) TestAWSSetupUtils.createAWSVMResource(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSVMResource) Rule(org.junit.Rule) TestContext(com.vmware.xenon.common.test.TestContext) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) TestAWSSetupUtils.regionId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.regionId) TestAWSSetupUtils.setAwsClientMockInfo(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setAwsClientMockInfo) IpPermission(com.amazonaws.services.ec2.model.IpPermission) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) ProvisionDiskTaskService(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) CompletionException(java.util.concurrent.CompletionException) ServiceMetadata(com.vmware.photon.controller.model.util.StartServicesHelper.ServiceMetadata) Before(org.junit.Before)

Example 18 with TestContext

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

the class AWSLoadBalancerServiceTest method setUp.

@Override
@Before
public void setUp() throws Throwable {
    CommandLineArgumentParser.parseFromProperties(this);
    try {
        PhotonModelServices.startServices(this.host);
        PhotonModelMetricServices.startServices(this.host);
        PhotonModelTaskServices.startServices(this.host);
        PhotonModelAdaptersRegistryAdapters.startServices(this.host);
        AWSAdaptersTestUtils.startServicesSynchronously(this.host);
        AuthCredentialsServiceState creds = new AuthCredentialsServiceState();
        creds.privateKey = this.secretKey;
        creds.privateKeyId = this.accessKey;
        TestContext lbWaitContext = new TestContext(1, Duration.ofSeconds(30L));
        AWSUtils.getAwsLoadBalancingAsyncClient(creds, this.regionId, getExecutor()).exceptionally(t -> {
            lbWaitContext.fail(t);
            throw new CompletionException(t);
        }).thenAccept(ec2Client -> {
            this.client = ec2Client;
            lbWaitContext.complete();
        });
        lbWaitContext.await();
        TestContext ec2WaitContext = new TestContext(1, Duration.ofSeconds(30L));
        AWSUtils.getEc2AsyncClient(creds, this.regionId, getExecutor()).exceptionally(t -> {
            ec2WaitContext.fail(t);
            throw new CompletionException(t);
        }).thenAccept(ec2Client -> {
            this.ec2client = ec2Client;
            ec2WaitContext.complete();
        });
        ec2WaitContext.await();
        TestContext secGroupWaitContext = new TestContext(1, Duration.ofSeconds(30L));
        AWSUtils.getEc2AsyncClient(creds, this.regionId, getExecutor()).exceptionally(t -> {
            secGroupWaitContext.fail(t);
            throw new CompletionException(t);
        }).thenAccept(ec2Client -> {
            this.securityGroupClient = new AWSSecurityGroupClient(ec2Client);
            secGroupWaitContext.complete();
        });
        secGroupWaitContext.await();
        this.host.setTimeoutSeconds(this.timeoutSeconds);
        this.endpointState = createEndpointState();
        String vm1 = "vm1";
        String vm2 = "vm2";
        if (!this.isMock) {
            vm1 = provisionAWSVMWithEC2Client(this.host, this.ec2client, EC2_LINUX_AMI, this.subnetId, null);
            this.instancesToCleanUp.add(vm1);
            vm2 = provisionAWSVMWithEC2Client(this.host, this.ec2client, EC2_LINUX_AMI, this.subnetId, null);
            this.instancesToCleanUp.add(vm2);
        }
        this.cs1 = createComputeState(vm1);
        this.cs2 = createComputeState(vm2);
    } catch (Throwable e) {
        this.host.log("Error starting up services for the test %s", e.getMessage());
        throw new Exception(e);
    }
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) EC2_LINUX_AMI(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.EC2_LINUX_AMI) Arrays(java.util.Arrays) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) TestAWSSetupUtils.deleteVMsUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteVMsUsingEC2Client) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) TestAWSSetupUtils.provisionAWSVMWithEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.provisionAWSVMWithEC2Client) LoadBalancerDescription(com.amazonaws.services.elasticloadbalancing.model.LoadBalancerDescription) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) LoadBalancerState(com.vmware.photon.controller.model.resources.LoadBalancerService.LoadBalancerState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) Duration(java.time.Duration) After(org.junit.After) TestUtils.getExecutor(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) LoadBalancerService(com.vmware.photon.controller.model.resources.LoadBalancerService) HealthCheckConfiguration(com.vmware.photon.controller.model.resources.LoadBalancerDescriptionService.LoadBalancerDescription.HealthCheckConfiguration) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) DeleteLoadBalancerRequest(com.amazonaws.services.elasticloadbalancing.model.DeleteLoadBalancerRequest) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) AmazonElasticLoadBalancingAsyncClient(com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingAsyncClient) List(java.util.List) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) InstanceRequestType(com.vmware.photon.controller.model.adapterapi.LoadBalancerInstanceRequest.InstanceRequestType) Protocol(com.vmware.photon.controller.model.resources.LoadBalancerDescriptionService.LoadBalancerDescription.Protocol) ProvisionLoadBalancerTaskService(com.vmware.photon.controller.model.tasks.ProvisionLoadBalancerTaskService) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) RouteConfiguration(com.vmware.photon.controller.model.resources.LoadBalancerDescriptionService.LoadBalancerDescription.RouteConfiguration) ProvisionLoadBalancerTaskState(com.vmware.photon.controller.model.tasks.ProvisionLoadBalancerTaskService.ProvisionLoadBalancerTaskState) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) DescribeLoadBalancersRequest(com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersRequest) HealthCheck(com.amazonaws.services.elasticloadbalancing.model.HealthCheck) ArrayList(java.util.ArrayList) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) HashSet(java.util.HashSet) AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) ListenerDescription(com.amazonaws.services.elasticloadbalancing.model.ListenerDescription) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) Listener(com.amazonaws.services.elasticloadbalancing.model.Listener) Before(org.junit.Before) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) Assert.assertNull(org.junit.Assert.assertNull) TestContext(com.vmware.xenon.common.test.TestContext) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersResult) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) CompletionException(java.util.concurrent.CompletionException) Before(org.junit.Before)

Example 19 with TestContext

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

the class AWSInstanceTypeServiceTest method testGetInstanceTypesPositive.

@Test
public void testGetInstanceTypesPositive() throws Throwable {
    EndpointState ep = createEndpointState();
    TestContext ctx = this.host.testCreate(1);
    Operation getOperation = Operation.createGet(UriUtils.buildUri(this.host, AWSInstanceTypeService.SELF_LINK + "?endpoint=" + ep.documentSelfLink)).setCompletion((operation, throwable) -> {
        try {
            if (throwable != null) {
                ctx.failIteration(throwable);
                return;
            }
            assertEquals(Operation.STATUS_CODE_OK, operation.getStatusCode());
            InstanceTypeList instanceTypes = operation.getBody(InstanceTypeList.class);
            assertNotNull("Tenant links should ne set.", instanceTypes.tenantLinks);
            assertEquals("Tenant links size equal to endpoint tenant links size is " + "expecteed.", ep.tenantLinks.size(), instanceTypes.tenantLinks.size());
            assertNotNull("Instance types should not be null.", instanceTypes.instanceTypes);
            instanceTypes.instanceTypes.stream().filter(instanceType -> instanceType.id == null || instanceType.name == null).findFirst().ifPresent(instanceType -> fail("Found instance type without id " + "or name."));
            // Validate that all types have cpu and memory extended data set.
            instanceTypes.instanceTypes.stream().filter(instanceType -> instanceType.cpuCount == null || instanceType.memoryInMB == null).findFirst().ifPresent(instanceType -> fail("Found instance type without extended data present: " + instanceType.id));
            // Check that one of the well known type is present.
            InstanceTypeList.InstanceType t2MicroType = instanceTypes.instanceTypes.stream().filter(instanceType -> InstanceType.T2Micro.toString().equals(instanceType.id)).findFirst().orElseThrow(() -> new AssertionError("Unable to " + "find t2.micro instance type."));
            assertEquals(1, t2MicroType.cpuCount.intValue());
            assertEquals(1024, t2MicroType.memoryInMB.intValue());
            assertNotNull(t2MicroType.storageType);
            assertNotNull(t2MicroType.networkType);
            ctx.completeIteration();
        } catch (AssertionError err) {
            ctx.failIteration(err);
        }
    });
    this.send(getOperation);
    this.testWait(ctx);
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation) InstanceTypeList(com.vmware.photon.controller.model.support.InstanceTypeList) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 20 with TestContext

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

the class AWSRebootServiceTest method testReboot.

// Test the reboot operation.
@Test
public void testReboot() 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));
        assertAndSetVMSecurityGroupsToBeDeleted(instance, vm);
    }
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest request = new ResourceOperationRequest();
    request.isMockRequest = this.isMock;
    request.operation = ResourceOperation.REBOOT.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(2);
    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;
    });
    Operation rebootOp = Operation.createPatch(UriUtils.buildUri(this.host, AWSRebootService.SELF_LINK)).setBody(request).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
        if (e != null) {
            ctx.failIteration(e);
            return;
        }
        ctx.completeIteration();
    });
    this.host.send(rebootOp);
    ctx.await();
    ComputeState vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
    assertEquals(ComputeService.PowerState.ON, vm.powerState);
}
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)

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