Search in sources :

Example 11 with CloudbreakTestDto

use of com.sequenceiq.it.cloudbreak.dto.CloudbreakTestDto in project cloudbreak by hortonworks.

the class TestContext method awaitWithClient.

private <T extends CloudbreakTestDto, E extends Enum<E>> T awaitWithClient(T entity, Map<String, E> desiredStatuses, RunningParameter runningParameter, MicroserviceClient client) {
    checkShutdown();
    String key = getKeyForAwait(entity, entity.getClass(), runningParameter);
    CloudbreakTestDto awaitEntity = get(key);
    if (awaitEntity == null) {
        awaitEntity = entity;
    }
    if (runningParameter.isWaitForFlow()) {
        awaitForFlow(awaitEntity, runningParameter);
    }
    try {
        resourceCrns.put(awaitEntity.getCrn(), awaitEntity);
        LOGGER.info("Resource Crn: '{}' by '{}' has been put to resource Crns map.", awaitEntity.getCrn(), awaitEntity.getName());
    } catch (IllegalStateException | NullPointerException e) {
        LOGGER.info("Resource Crn is not available for: {}", awaitEntity.getName());
    }
    if (!getExceptionMap().isEmpty() && runningParameter.isSkipOnFail()) {
        Log.await(LOGGER, String.format("Cloudbreak await should be skipped because of previous error. await [%s]", desiredStatuses));
    } else {
        resourceAwait.await(awaitEntity, desiredStatuses, getTestContext(), runningParameter, flowUtilSingleStatus.getPollingDurationOrTheDefault(runningParameter), maxRetry, maxRetryCount, client);
    }
    return entity;
}
Also used : CloudbreakTestDto(com.sequenceiq.it.cloudbreak.dto.CloudbreakTestDto)

Example 12 with CloudbreakTestDto

use of com.sequenceiq.it.cloudbreak.dto.CloudbreakTestDto in project cloudbreak by hortonworks.

the class TestInvocationListener method afterInvocation.

/**
 * Collects all the available test resources to a JSON file based on the:
 * - Related test DTO: provides a resource name type (eg.: environmentName),
 * - Test Context: provides the resource's unique name (eg.: aws-test-039df4d8aec04a61965).
 *
 * Resource files can be saved based on the test cases (eg.: resource_names_testCreateNewEnvironmentWithNewNetworkAndNoInternet);
 * because of each test has it's own Test Context, that has been built by the Given test steps for that thread of test execution.
 */
@Override
public void afterInvocation(IInvokedMethod invokedMethod, ITestResult testResult) {
    TestContext testContext;
    JSONObject jsonObject = new JSONObject();
    Object[] parameters = testResult.getParameters();
    if (parameters == null || parameters.length == 0) {
        LOGGER.warn("Test context could not be found because parameters array is empty in test result.");
        return;
    }
    try {
        testContext = (TestContext) parameters[0];
    } catch (ClassCastException e) {
        LOGGER.warn("Test context could not be casted from test result parameters.");
        return;
    }
    List<CloudbreakTestDto> testDtos = new ArrayList<>(testContext.getResourceNames().values());
    List<CloudbreakTestDto> orderedTestDtos = testDtos.stream().sorted(new CompareByOrder()).collect(Collectors.toList());
    for (CloudbreakTestDto testDto : orderedTestDtos) {
        try {
            String resourceNameType = Optional.ofNullable(testDto.getResourceNameType()).orElse("");
            if (!resourceNameType.trim().isEmpty()) {
                if (jsonObject.has(resourceNameType)) {
                    List<String> resourceNames = new ArrayList<>();
                    try {
                        JSONArray resources = jsonObject.getJSONArray(resourceNameType);
                        for (int i = 0; i < resources.length(); i++) {
                            String resource = resources.getString(i);
                            resourceNames.add(resource);
                        }
                    } catch (JSONException e) {
                        String resource = jsonObject.getString(resourceNameType);
                        resourceNames.add(resource);
                    }
                    resourceNames.add(testDto.getName());
                    LOGGER.info("Created resource name array: '{}'.", resourceNames);
                    JSONArray resourceNameArray = new JSONArray(resourceNames);
                    jsonObject.put(resourceNameType, resourceNameArray);
                } else {
                    jsonObject.put(resourceNameType, testDto.getName());
                }
                LOGGER.info("Put Resource Name: '{}' to JSON Object.", testDto.getName());
            }
        } catch (Exception e) {
            LOGGER.info("Appending JSON object is failing, because of: {}", e.getMessage(), e);
        }
    }
    if (jsonObject.length() != 0) {
        String fileName = "resource_names_" + testContext.getTestMethodName().orElseGet(this::getDefaultFileNameTag) + ".json";
        try {
            Files.writeString(Paths.get(fileName), jsonObject.toString());
            LOGGER.info("Resource file have been created with name: {} and content: {}.", fileName, jsonObject);
        } catch (IOException e) {
            LOGGER.info("Creating/Appending resource file throws exception: {}", e.getMessage(), e);
        } catch (Exception e) {
            LOGGER.info("Creating/Appending resource file is failing, because of: {}", e.getMessage(), e);
        }
    } else {
        LOGGER.info("No resources found, no output file needs to be created.");
    }
}
Also used : TestContext(com.sequenceiq.it.cloudbreak.context.TestContext) CompareByOrder(com.sequenceiq.it.cloudbreak.context.CompareByOrder) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) IOException(java.io.IOException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) CloudbreakTestDto(com.sequenceiq.it.cloudbreak.dto.CloudbreakTestDto) JSONObject(org.json.JSONObject)

Aggregations

CloudbreakTestDto (com.sequenceiq.it.cloudbreak.dto.CloudbreakTestDto)12 Test (org.testng.annotations.Test)5 TestFailException (com.sequenceiq.it.cloudbreak.exception.TestFailException)4 BeansException (org.springframework.beans.BeansException)3 CloudbreakUser (com.sequenceiq.it.cloudbreak.actor.CloudbreakUser)2 TestContext (com.sequenceiq.it.cloudbreak.context.TestContext)2 ArrayList (java.util.ArrayList)2 UsedImageStacksV4Response (com.sequenceiq.cloudbreak.api.endpoint.v4.util.responses.UsedImageStacksV4Response)1 EnvironmentStatus (com.sequenceiq.environment.api.v1.environment.model.response.EnvironmentStatus)1 Status (com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.Status)1 UsedImageStacksV1Response (com.sequenceiq.freeipa.api.v1.util.model.UsedImageStacksV1Response)1 MicroserviceClient (com.sequenceiq.it.cloudbreak.MicroserviceClient)1 EnvironmentTestClient (com.sequenceiq.it.cloudbreak.client.EnvironmentTestClient)1 FreeIpaTestClient (com.sequenceiq.it.cloudbreak.client.FreeIpaTestClient)1 ImageCatalogTestClient (com.sequenceiq.it.cloudbreak.client.ImageCatalogTestClient)1 SdxTestClient (com.sequenceiq.it.cloudbreak.client.SdxTestClient)1 UtilTestClient (com.sequenceiq.it.cloudbreak.client.UtilTestClient)1 CompareByOrder (com.sequenceiq.it.cloudbreak.context.CompareByOrder)1 Description (com.sequenceiq.it.cloudbreak.context.Description)1 MockedTestContext (com.sequenceiq.it.cloudbreak.context.MockedTestContext)1