Search in sources :

Example 6 with TestRequestSender

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

the class BaseVSphereAdapterTest method rebootVSphereVMAndWait.

protected void rebootVSphereVMAndWait(ComputeState computeState) {
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest rebootVMRequest = getResourceOperationRequest("Reboot", computeState.documentSelfLink, taskLink);
    Operation rebootOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterD2PowerOpsService.SELF_LINK)).setBody(rebootVMRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
        Assert.assertNull(e);
    });
    TestRequestSender sender = new TestRequestSender(this.host);
    sender.sendRequest(rebootOp);
    this.host.log("Waiting for the machine to be rebooted");
    ComputeState[] cState = new ComputeState[1];
    this.host.waitFor("Reboot request failed", () -> {
        cState[0] = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeState.documentSelfLink));
        if (cState[0].powerState.equals(ComputeService.PowerState.ON)) {
            return true;
        } else {
            return false;
        }
    });
    assertEquals(ComputeService.PowerState.ON, cState[0].powerState);
    assertNotNull(cState[0].address);
    this.host.log("Reboot operation completed successfully");
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TestRequestSender(com.vmware.xenon.common.test.TestRequestSender) 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 7 with TestRequestSender

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

the class BaseVSphereAdapterTest method querySnapshotState.

private SnapshotState querySnapshotState(String computeReferenceLink, Boolean isCurrent) {
    List<SnapshotState> snapshotStates = new ArrayList<>();
    QueryTask.Query querySnapshot = QueryTask.Query.Builder.create().addKindFieldClause(SnapshotState.class).addFieldClause(SnapshotState.FIELD_NAME_COMPUTE_LINK, computeReferenceLink).addFieldClause(SnapshotState.FIELD_NAME_IS_CURRENT, isCurrent).build();
    QueryTask qTask = QueryTask.Builder.createDirectTask().setQuery(querySnapshot).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).build();
    Operation postOperation = QueryUtils.createQueryTaskOperation(this.host, qTask, ServiceTypeCluster.INVENTORY_SERVICE);
    TestRequestSender sender = new TestRequestSender(this.host);
    Operation responseOp = sender.sendAndWait(postOperation);
    QueryResultsProcessor rp = QueryResultsProcessor.create(responseOp);
    if (rp.hasResults()) {
        snapshotStates.addAll(rp.streamDocuments(SnapshotState.class).collect(Collectors.toList()));
    }
    if (!snapshotStates.isEmpty()) {
        return snapshotStates.get(0);
    } else {
        return null;
    }
}
Also used : QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) ArrayList(java.util.ArrayList) TestRequestSender(com.vmware.xenon.common.test.TestRequestSender) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) Operation(com.vmware.xenon.common.Operation)

Example 8 with TestRequestSender

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

the class BaseVSphereAdapterTest method createSnapshotAndWait.

protected void createSnapshotAndWait(ComputeState vm, Boolean isSnapshottedAgain) throws Throwable {
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest snapshotRequest = getCreateSnapshotRequest(ResourceOperation.CREATE_SNAPSHOT.operation, vm.documentSelfLink, taskLink);
    Operation createSnapshotOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterSnapshotService.SELF_LINK)).setBody(snapshotRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> Assert.assertNull(e));
    TestRequestSender sender = new TestRequestSender(this.host);
    sender.sendRequest(createSnapshotOp);
    this.host.log("Waiting for the snapshot to be created");
    this.host.waitFor("Create snapshot request failed", () -> {
        ComputeState cState = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, vm.documentSelfLink));
        SnapshotState snapshotState = querySnapshotState(vm.documentSelfLink, true);
        String hasSnapshot = cState.customProperties.get(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS);
        if (!isSnapshottedAgain) {
            if (hasSnapshot != null && Boolean.parseBoolean(hasSnapshot) && snapshotState.parentLink == null) {
                return true;
            } else {
                return false;
            }
        } else {
            if (hasSnapshot != null && Boolean.parseBoolean(hasSnapshot) && snapshotState.parentLink != null) {
                return true;
            } else {
                return false;
            }
        }
    });
    this.host.log("Create snapshot operation completed successfully");
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) TestRequestSender(com.vmware.xenon.common.test.TestRequestSender) 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 9 with TestRequestSender

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

the class AWSComputeDiskDay2ServiceTest method performDiskOperationAndVerify.

private void performDiskOperationAndVerify(String computeStateLink, List<String> diskLinks, String requestType, DiskTaskService.DiskTaskState.SubStage expectedTerminalState) throws Throwable {
    List<String> taskServiceLinks = new ArrayList<>();
    diskLinks.forEach(diskLink -> {
        DiskTaskService.DiskTaskState diskOpTask = new DiskTaskService.DiskTaskState();
        diskOpTask.taskSubStage = DiskTaskService.DiskTaskState.SubStage.STARTED;
        try {
            diskOpTask = TestUtils.doPost(this.host, diskOpTask, DiskTaskService.DiskTaskState.class, UriUtils.buildUri(this.host, DiskTaskService.FACTORY_LINK));
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        ResourceOperationRequest request = new ResourceOperationRequest();
        request.isMockRequest = this.isMock;
        request.operation = requestType;
        request.payload = new HashMap<>();
        request.payload.put(PhotonModelConstants.DISK_LINK, diskLink);
        request.resourceReference = UriUtils.buildUri(this.host, computeStateLink);
        request.taskReference = UriUtils.buildUri(this.host, diskOpTask.documentSelfLink);
        Operation diskOp = Operation.createPatch(UriUtils.buildUri(this.host, AWSComputeDiskDay2Service.SELF_LINK)).setBody(request).setReferer(this.host.getReferer());
        TestRequestSender sender = new TestRequestSender(this.host);
        sender.sendRequest(diskOp);
        this.host.log("Waiting for disk operation to complete");
        taskServiceLinks.add(diskOpTask.documentSelfLink);
    });
    taskServiceLinks.forEach(taskServiceLink -> {
        this.host.waitFor("disk Operation failed.", () -> {
            DiskTaskService.DiskTaskState diskTaskState = this.host.getServiceState(null, DiskTaskService.DiskTaskState.class, UriUtils.buildUri(this.host, taskServiceLink));
            // Check if the disk operation is successful or not.
            if (diskTaskState.taskSubStage == DiskTaskService.DiskTaskState.SubStage.FINISHED || diskTaskState.taskSubStage == DiskTaskService.DiskTaskState.SubStage.FAILED) {
                assertEquals("diskOperation Task did not terminate as expected.", expectedTerminalState, diskTaskState.taskSubStage);
                return true;
            } else {
                return false;
            }
        });
    });
}
Also used : ProvisionDiskTaskService(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService) ArrayList(java.util.ArrayList) TestRequestSender(com.vmware.xenon.common.test.TestRequestSender) 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 10 with TestRequestSender

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

the class ResourceOperationSpecServiceTest method testRegisterAndPatchExtensions.

@Test
public void testRegisterAndPatchExtensions() {
    ResourceOperationSpec[] states = registerResourceOperation(this.endpointType, ResourceType.COMPUTE, "testRegisterAndPatchExtensions");
    ResourceOperationSpec requestedState = states[0];
    ResourceOperationSpec persistedState = states[1];
    Assert.assertTrue(persistedState.documentSelfLink.endsWith(ResourceOperationSpecFactoryService.generateSelfLink(requestedState)));
    this.logger.info("Persisted: " + persistedState);
    String E_2 = "e2";
    ResourceOperationSpec specWithExtension = new ResourceOperationSpec();
    specWithExtension.documentSelfLink = ResourceOperationSpecFactoryService.generateSelfLink(requestedState.endpointType, requestedState.resourceType, requestedState.operation);
    specWithExtension.extensions = new HashMap<>();
    specWithExtension.extensions.put(E_2, "v2");
    Operation patchOne = Operation.createPatch(super.host, specWithExtension.documentSelfLink).setBodyNoCloning(specWithExtension);
    TestRequestSender sender = super.host.getTestRequestSender();
    sender.sendAndWait(patchOne);
    ResourceOperationSpec patchedSpec = sender.sendGetAndWait(UriUtils.buildUri(super.host, specWithExtension.documentSelfLink), ResourceOperationSpec.class);
    Assert.assertNotNull(patchedSpec);
    Assert.assertNotNull(patchedSpec.extensions);
    Assert.assertNotNull(patchedSpec.extensions.get(E_2));
    specWithExtension.name = "tryNewName";
    Operation patchTwo = Operation.createPatch(super.host, specWithExtension.documentSelfLink).setBodyNoCloning(specWithExtension);
    try {
        sender.sendAndWait(patchTwo);
    } catch (RuntimeException rte) {
        Throwable[] allSuppressed = rte.getSuppressed();
        Assert.assertNotNull(allSuppressed);
        Assert.assertEquals(1, allSuppressed.length);
        Throwable suppressed = allSuppressed[0];
        this.logger.info("Suppressed error: " + suppressed.getMessage());
        Assert.assertTrue(suppressed instanceof IllegalArgumentException);
    }
}
Also used : TestRequestSender(com.vmware.xenon.common.test.TestRequestSender) ResourceOperationSpec(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceOperationSpec) Operation(com.vmware.xenon.common.Operation) Test(org.junit.Test)

Aggregations

Operation (com.vmware.xenon.common.Operation)15 TestRequestSender (com.vmware.xenon.common.test.TestRequestSender)15 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)10 ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)7 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)6 SnapshotState (com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState)5 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 QueryResultsProcessor (com.vmware.xenon.common.QueryResultsProcessor)2 QueryTask (com.vmware.xenon.services.common.QueryTask)2 Query (com.vmware.xenon.services.common.QueryTask.Query)2 VolumeTypeList (com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSVolumeTypeDiscoveryService.VolumeTypeList)1 AzureSubscriptionEndpointCreationRequest (com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionEndpointCreationService.AzureSubscriptionEndpointCreationRequest)1 AzureSubscriptionEndpointsEnumerationRequest (com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionEndpointsEnumerationService.AzureSubscriptionEndpointsEnumerationRequest)1 AzureSubscriptionsEnumerationRequest (com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService.AzureSubscriptionsEnumerationRequest)1 ResourceOperationSpec (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceOperationSpec)1 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)1 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)1 ProvisionDiskTaskService (com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService)1