Search in sources :

Example 1 with WaitForMessagesHandler

use of io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler in project elastest-torm by elastest.

the class DockerServiceItTest method readLogInRabbit.

@Test
public void readLogInRabbit() throws Exception {
    String imageId = "alpine";
    if (!existsImage(imageId)) {
        log.info("Pulling image '{}'", imageId);
        dockerClient.pullImageCmd(imageId).exec(new PullImageResultCallback()).awaitSuccess();
    }
    String queueId = "test.default_log.1.log";
    String tag = "test_1_exec";
    WaitForMessagesHandler handler = connectToRabbitQueue(queueId);
    LogConfig logConfig = getLogConfig(tag);
    log.info("Creating container");
    CreateContainerResponse container = dockerClient.createContainerCmd(imageId).withCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done").withTty(false).withLogConfig(logConfig).withNetworkMode("bridge").exec();
    String containerId = container.getId();
    try {
        log.info("Created container: {}", container.toString());
        long start = System.currentTimeMillis();
        dockerClient.startContainerCmd(containerId).exec();
        log.info("Waiting for logs messages in Rabbit");
        handler.waitForCompletion(5, TimeUnit.SECONDS);
        long duration = System.currentTimeMillis() - start;
        log.info("Log received in Rabbit in {} millis", duration);
    } catch (Exception ex) {
        log.info("Log NOT received in Rabbit");
        throw ex;
    } finally {
        log.info("Cleaning up resources");
        try {
            log.info("Removing container " + containerId);
            try {
                dockerClient.stopContainerCmd(containerId).exec();
            } catch (Exception ex) {
                log.warn("Error stopping container {}", containerId, ex);
            }
            dockerClient.removeContainerCmd(containerId).exec();
        } catch (Exception ex) {
            log.warn("Error on ending test execution {}", containerId, ex);
        }
    }
}
Also used : PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) WaitForMessagesHandler(io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler) TimeoutException(java.util.concurrent.TimeoutException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) LogConfig(com.github.dockerjava.api.model.LogConfig) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with WaitForMessagesHandler

use of io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler in project elastest-torm by elastest.

the class TJobExecutionApiItTest method testExecuteTJob.

private void testExecuteTJob(boolean withSut) throws InterruptedException, ExecutionException, TimeoutException, MultipleFailuresError {
    log.info("Start the test testExecuteTJob " + (withSut ? "with" : "without") + " SuT");
    TJob tJob;
    if (withSut) {
        Long sutId = createSut(projectId).getId();
        tJob = createTJob(projectId, sutId);
    } else {
        tJob = createTJob(projectId);
    }
    StompSession stompSession = connectToRabbitMQ(serverPort);
    log.info("POST /api/tjob/{tjobId}/exec");
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    String body = "{\"tJobParams\" : [{\"Param1\":\"NewValue1\"}], \"sutParams\" : [{\"Param1\":\"NewValue1\"}]}";
    HttpEntity<String> entity = new HttpEntity<>(body, headers);
    Map<String, Object> urlParams = new HashMap<>();
    urlParams.put("tjobId", tJob.getId());
    ResponseEntity<TJobExecution> response = httpClient.postForEntity("/api/tjob/{tjobId}/exec", entity, TJobExecution.class, urlParams);
    TJobExecution exec = response.getBody();
    log.info("TJobExecution creation response: " + response);
    if (withSut) {
        String queueToSuscribe = "/topic/" + "sut.default_log." + exec.getId() + ".log";
        log.info("Sut log queue '" + queueToSuscribe + "'");
        WaitForMessagesHandler handler = new WaitForMessagesHandler();
        stompSession.subscribe(queueToSuscribe, handler);
        handler.waitForCompletion(5, TimeUnit.SECONDS);
        log.info("Sut log queue received a message");
    }
    String queueToSuscribe = "/topic/" + "test.default_log." + exec.getId() + ".log";
    log.info("TJob log queue '" + queueToSuscribe + "'");
    WaitForMessagesHandler handler = new WaitForMessagesHandler(msg -> msg.contains("BUILD SUCCESS") || msg.contains("BUILD FAILURE"));
    stompSession.subscribe(queueToSuscribe, handler);
    handler.waitForCompletion(180, TimeUnit.SECONDS);
    assertAll("Validating TJobExecution Properties", () -> assertNotNull(response.getBody()), () -> assertNotNull(response.getBody().getId()), () -> assertTrue(response.getBody().getTjob().getId().equals(urlParams.get("tjobId"))));
    while (true) {
        exec = getTJobExecutionById(exec.getId(), tJob.getId()).getBody();
        log.info("TJobExecution: " + exec);
        if (exec.getResult() != ResultEnum.IN_PROGRESS) {
            log.info("Test results:" + exec.getTestSuites());
            break;
        }
        sleep(500);
    }
    deleteTJobExecution(exec.getId(), tJob.getId());
    deleteTJob(tJob.getId());
    log.info("Finished.");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) StompSession(org.springframework.messaging.simp.stomp.StompSession) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) TJobExecution(io.elastest.etm.model.TJobExecution) TJob(io.elastest.etm.model.TJob) WaitForMessagesHandler(io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler)

Example 3 with WaitForMessagesHandler

use of io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler in project elastest-torm by elastest.

the class DockerServiceItTest method connectToRabbitQueue.

private WaitForMessagesHandler connectToRabbitQueue(String queueId) throws InterruptedException, ExecutionException, TimeoutException {
    StompSession stompSession = connectToRabbitMQ(serverPort);
    String queueToSuscribe = "/topic/" + queueId;
    log.info("Container log queue '" + queueToSuscribe + "'");
    WaitForMessagesHandler handler = new WaitForMessagesHandler("1");
    stompSession.subscribe(queueToSuscribe, handler);
    return handler;
}
Also used : StompSession(org.springframework.messaging.simp.stomp.StompSession) WaitForMessagesHandler(io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler)

Aggregations

WaitForMessagesHandler (io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler)3 StompSession (org.springframework.messaging.simp.stomp.StompSession)2 CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)1 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)1 LogConfig (com.github.dockerjava.api.model.LogConfig)1 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)1 TJob (io.elastest.etm.model.TJob)1 TJobExecution (io.elastest.etm.model.TJobExecution)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1