Search in sources :

Example 1 with Secret

use of io.github.microcks.domain.Secret in project microcks by microcks.

the class TestRunnerService method launchTestsInternal.

/**
 * Launch tests using asynchronous/completable future pattern.
 * @param testResult TestResults to aggregate results within
 * @param service Service to test
 * @param runnerType Type of runner for launching the tests
 * @return A Future wrapping test results
 */
@Async
public CompletableFuture<TestResult> launchTestsInternal(TestResult testResult, Service service, TestRunnerType runnerType) {
    // Found next build number for this test.
    List<TestResult> older = testResultRepository.findByServiceId(service.getId(), PageRequest.of(0, 2, Sort.Direction.DESC, "testNumber"));
    if (older != null && !older.isEmpty() && older.get(0).getTestNumber() != null) {
        testResult.setTestNumber(older.get(0).getTestNumber() + 1L);
    } else {
        testResult.setTestNumber(1L);
    }
    Secret secret = null;
    if (testResult.getSecretRef() != null) {
        secret = secretRepository.findById(testResult.getSecretRef().getSecretId()).orElse(null);
        log.debug("Using a secret to test endpoint? '{}'", secret != null ? secret.getName() : "none");
    }
    // Initialize runner once as it is shared for each test.
    AbstractTestRunner<HttpMethod> testRunner = retrieveRunner(runnerType, secret, testResult.getTimeout(), service.getId());
    if (testRunner == null) {
        // Set failure and stopped flags and save before exiting.
        testResult.setSuccess(false);
        testResult.setInProgress(false);
        testResult.setElapsedTime(0);
        testResultRepository.save(testResult);
        return CompletableFuture.completedFuture(testResult);
    }
    for (TestCaseResult testCaseResult : testResult.getTestCaseResults()) {
        // Retrieve operation corresponding to testCase.
        Operation operation = service.getOperations().stream().filter(o -> o.getName().equals(testCaseResult.getOperationName())).findFirst().get();
        String testCaseId = IdBuilder.buildTestCaseId(testResult, operation);
        // Prepare collection of requests to launch.
        List<Request> requests = requestRepository.findByOperationId(IdBuilder.buildOperationId(service, operation));
        requests = cloneRequestsForTestCase(requests, testCaseId);
        List<TestReturn> results = new ArrayList<>();
        try {
            HttpMethod method = testRunner.buildMethod(operation.getMethod());
            results = testRunner.runTest(service, operation, testResult, requests, testResult.getTestedEndpoint(), method);
        } catch (URISyntaxException use) {
            log.error("URISyntaxException on endpoint {}, aborting current tests", testResult.getTestedEndpoint(), use);
            // Set flags and add to results before exiting loop.
            testCaseResult.setSuccess(false);
            testCaseResult.setElapsedTime(0);
            testResultRepository.save(testResult);
            break;
        } catch (Throwable t) {
            log.error("Throwable while testing operation {}", operation.getName(), t);
        }
        // sample request for that operation -> mark it as failed.
        if (results == null) {
            testCaseResult.setSuccess(false);
            testCaseResult.setElapsedTime(0);
            testResultRepository.save(testResult);
        } else if (!results.isEmpty()) {
            updateTestCaseResultWithReturns(testCaseResult, results, testCaseId);
            testResultRepository.save(testResult);
        }
    }
    // Update success, progress indicators and total time before saving and returning.
    updateTestResult(testResult);
    return CompletableFuture.completedFuture(testResult);
}
Also used : TestCaseResult(io.github.microcks.domain.TestCaseResult) TestReturn(io.github.microcks.domain.TestReturn) PageRequest(org.springframework.data.domain.PageRequest) Request(io.github.microcks.domain.Request) ArrayList(java.util.ArrayList) TestResult(io.github.microcks.domain.TestResult) Operation(io.github.microcks.domain.Operation) URISyntaxException(java.net.URISyntaxException) Secret(io.github.microcks.domain.Secret) HttpMethod(org.springframework.http.HttpMethod) Async(org.springframework.scheduling.annotation.Async)

Example 2 with Secret

use of io.github.microcks.domain.Secret in project microcks by microcks.

the class ImportServiceDefinitionTask method importServiceDefinition.

@Scheduled(cron = "${services.update.interval}")
public void importServiceDefinition() {
    // Prepare some flags.
    int updated = 0;
    long startTime = System.currentTimeMillis();
    log.info("Starting scan of Service definitions update scheduled task...");
    long numJobs = jobRepository.count();
    log.debug("Found {} jobs to check. Splitting in {} chunks.", numJobs, (numJobs / CHUNK_SIZE + 1));
    for (int i = 0; i < numJobs / CHUNK_SIZE + 1; i++) {
        List<ImportJob> jobs = jobRepository.findAll(PageRequest.of(i, CHUNK_SIZE)).getContent();
        log.debug("Found {} jobs into chunk {}", jobs.size(), i);
        for (ImportJob job : jobs) {
            log.debug("Dealing with job " + job.getName());
            if (job.isActive()) {
                // Retrieve associated secret if any.
                Secret jobSecret = null;
                if (job.getSecretRef() != null) {
                    log.debug("Retrieving secret {} for job {}", job.getSecretRef().getName(), job.getName());
                    jobSecret = secretRepository.findById(job.getSecretRef().getSecretId()).orElse(null);
                }
                // Get older and fresh Etag if any.
                String etag = job.getEtag();
                String freshEtag = null;
                try {
                    freshEtag = HTTPDownloader.getURLEtag(job.getRepositoryUrl(), jobSecret, job.isRepositoryDisableSSLValidation());
                } catch (IOException ioe) {
                    log.error("Got an IOException while checking ETag for {}, pursuing...", job.getRepositoryUrl());
                }
                // Test if we must update this service definition.
                if (freshEtag == null || (freshEtag != null && !freshEtag.equals(etag))) {
                    log.debug("No Etag or fresher one found, updating service definition for " + job.getName());
                    job.setEtag(freshEtag);
                    jobService.doImportJob(job);
                    updated++;
                }
            }
        }
    }
    long duration = System.currentTimeMillis() - startTime;
    log.info("Task end in " + duration + " ms, updating " + updated + " job services definitions");
}
Also used : Secret(io.github.microcks.domain.Secret) ImportJob(io.github.microcks.domain.ImportJob) IOException(java.io.IOException) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 3 with Secret

use of io.github.microcks.domain.Secret in project microcks by microcks.

the class JobService method doImportJob.

/**
 * Realize the import of a repository defined into an import job.
 * @param job The job containing information onto the repository.
 */
public void doImportJob(ImportJob job) {
    log.info("Starting import for job '{}'", job.getName());
    // Retrieve associated secret if any.
    Secret jobSecret = null;
    if (job.getSecretRef() != null) {
        log.debug("Retrieving secret {} for job {}", job.getSecretRef().getName(), job.getName());
        jobSecret = secretRepository.findById(job.getSecretRef().getSecretId()).orElse(null);
    }
    // Reinitialize service references and import errors before new import.
    job.setServiceRefs(null);
    job.setLastImportError(null);
    List<Service> services = null;
    try {
        services = serviceService.importServiceDefinition(job.getRepositoryUrl(), jobSecret, job.isRepositoryDisableSSLValidation(), job.isMainArtifact());
    } catch (MockRepositoryImportException mrie) {
        log.warn("MockRepositoryImportException while importing job '{}' : {}", job.getName(), mrie.getMessage());
        job.setLastImportError(mrie.getMessage());
    }
    // Add service references if any.
    if (services != null) {
        for (Service service : services) {
            job.addServiceRef(new ServiceRef(service.getId(), service.getName(), service.getVersion()));
        }
    }
    job.setLastImportDate(new Date());
    jobRepository.save(job);
    log.info("Import of job '{}' done", job.getName());
}
Also used : Secret(io.github.microcks.domain.Secret) Service(io.github.microcks.domain.Service) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) ServiceRef(io.github.microcks.domain.ServiceRef) Date(java.util.Date)

Example 4 with Secret

use of io.github.microcks.domain.Secret in project microcks by microcks.

the class TestRunnerService method handleJobRepositoryDownloadToFile.

/**
 * Download a remote HTTP URL repository into a temporary local file.
 */
private String handleJobRepositoryDownloadToFile(ImportJob job) throws IOException {
    // Check if job has an associated secret to retrieve.
    Secret jobSecret = null;
    if (job.getSecretRef() != null) {
        log.debug("Retrieving secret {} for job {}", job.getSecretRef().getName(), job.getName());
        jobSecret = secretRepository.findById(job.getSecretRef().getSecretId()).orElse(null);
    }
    File localFile = HTTPDownloader.handleHTTPDownloadToFile(job.getRepositoryUrl(), jobSecret, job.isRepositoryDisableSSLValidation());
    return localFile.getAbsolutePath();
}
Also used : Secret(io.github.microcks.domain.Secret) File(java.io.File)

Example 5 with Secret

use of io.github.microcks.domain.Secret in project microcks by microcks.

the class AsyncAPITestRunner method runTest.

@Override
public List<TestReturn> runTest(Service service, Operation operation, TestResult testResult, List<Request> requests, String endpointUrl, HttpMethod method) throws URISyntaxException, IOException {
    if (log.isDebugEnabled()) {
        log.debug("Launching test run on " + endpointUrl + " for ms");
    }
    // Retrieve the resource corresponding to OpenAPI specification if any.
    Resource asyncAPISpecResource = null;
    List<Resource> resources = resourceRepository.findByServiceId(service.getId());
    for (Resource resource : resources) {
        if (ResourceType.ASYNC_API_SPEC.equals(resource.getType())) {
            asyncAPISpecResource = resource;
            break;
        }
    }
    // Microcks-async-minion interface object building.
    ObjectNode jsonArg = mapper.createObjectNode();
    jsonArg.put("runnerType", TestRunnerType.ASYNC_API_SCHEMA.toString());
    jsonArg.put("testResultId", testResult.getId());
    jsonArg.put("serviceId", service.getId());
    jsonArg.put("operationName", operation.getName());
    jsonArg.put("endpointUrl", endpointUrl);
    jsonArg.put("timeoutMS", testResult.getTimeout());
    jsonArg.put("asyncAPISpec", asyncAPISpecResource.getContent());
    if (testResult.getSecretRef() != null) {
        Secret secret = secretRepository.findById(testResult.getSecretRef().getSecretId()).orElse(null);
        if (secret != null) {
            log.debug("Adding the secret '{}' to test specification request", secret.getName());
            jsonArg.set("secret", mapper.valueToTree(secret));
        }
    }
    URI asyncMinionURI = new URI(asyncMinionUrl + "/api/tests");
    ClientHttpRequest httpRequest = clientHttpRequestFactory.createRequest(asyncMinionURI, HttpMethod.POST);
    httpRequest.getBody().write(mapper.writeValueAsBytes(jsonArg));
    httpRequest.getHeaders().add("Content-Type", "application/json");
    // Actually execute request.
    ClientHttpResponse httpResponse = null;
    try {
        httpResponse = httpRequest.execute();
    } catch (IOException ioe) {
        log.error("IOException while executing request ", ioe);
    } finally {
        if (httpResponse != null) {
            httpResponse.close();
        }
    }
    return new ArrayList<>();
}
Also used : Secret(io.github.microcks.domain.Secret) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Resource(io.github.microcks.domain.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Aggregations

Secret (io.github.microcks.domain.Secret)5 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ImportJob (io.github.microcks.domain.ImportJob)1 Operation (io.github.microcks.domain.Operation)1 Request (io.github.microcks.domain.Request)1 Resource (io.github.microcks.domain.Resource)1 Service (io.github.microcks.domain.Service)1 ServiceRef (io.github.microcks.domain.ServiceRef)1 TestCaseResult (io.github.microcks.domain.TestCaseResult)1 TestResult (io.github.microcks.domain.TestResult)1 TestReturn (io.github.microcks.domain.TestReturn)1 MockRepositoryImportException (io.github.microcks.util.MockRepositoryImportException)1 File (java.io.File)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1 PageRequest (org.springframework.data.domain.PageRequest)1 HttpMethod (org.springframework.http.HttpMethod)1