Search in sources :

Example 1 with Slf4jLogConsumer

use of org.testcontainers.containers.output.Slf4jLogConsumer in project testcontainers-java by testcontainers.

the class OutputStreamTest method testLogConsumer.

@Test
public void testLogConsumer() throws TimeoutException {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOGGER);
    Consumer<OutputFrame> composedConsumer = logConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);
    waitingConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=2"));
}
Also used : OutputFrame(org.testcontainers.containers.output.OutputFrame) Slf4jLogConsumer(org.testcontainers.containers.output.Slf4jLogConsumer) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer) Test(org.junit.Test)

Example 2 with Slf4jLogConsumer

use of org.testcontainers.containers.output.Slf4jLogConsumer in project testcontainers-java by testcontainers.

the class SimpleMySQLTest method testSpecificVersion.

@Test
public void testSpecificVersion() throws SQLException {
    MySQLContainer mysqlOldVersion = (MySQLContainer) new MySQLContainer("mysql:5.5").withConfigurationOverride("somepath/mysql_conf_override").withLogConsumer(new Slf4jLogConsumer(logger));
    mysqlOldVersion.start();
    try {
        ResultSet resultSet = performQuery(mysqlOldVersion, "SELECT VERSION()");
        String resultSetString = resultSet.getString(1);
        assertTrue("The database version can be set using a container rule parameter", resultSetString.startsWith("5.5"));
    } finally {
        mysqlOldVersion.stop();
    }
}
Also used : MySQLContainer(org.testcontainers.containers.MySQLContainer) ResultSet(java.sql.ResultSet) Slf4jLogConsumer(org.testcontainers.containers.output.Slf4jLogConsumer) Test(org.junit.Test)

Example 3 with Slf4jLogConsumer

use of org.testcontainers.containers.output.Slf4jLogConsumer in project testcontainers-java by testcontainers.

the class SimpleMySQLTest method testSimple.

/*
     * Ordinarily you wouldn't try and run multiple containers simultaneously - this is just used for testing.
     * To avoid memory issues with the default, low memory, docker machine setup, we instantiate only one container
     * at a time, inside the test methods themselves.
     */
/*
    @ClassRule
    public static MySQLContainer mysql = new MySQLContainer();

    @ClassRule
    public static MySQLContainer mysqlOldVersion = new MySQLContainer("mysql:5.5");

    @ClassRule
    public static MySQLContainer mysqlCustomConfig = new MySQLContainer("mysql:5.6")
                                                            .withConfigurationOverride("somepath/mysql_conf_override");
    */
@Test
public void testSimple() throws SQLException {
    MySQLContainer mysql = (MySQLContainer) new MySQLContainer().withConfigurationOverride("somepath/mysql_conf_override").withLogConsumer(new Slf4jLogConsumer(logger));
    mysql.start();
    try {
        ResultSet resultSet = performQuery(mysql, "SELECT 1");
        int resultSetInt = resultSet.getInt(1);
        assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
    } finally {
        mysql.stop();
    }
}
Also used : MySQLContainer(org.testcontainers.containers.MySQLContainer) ResultSet(java.sql.ResultSet) Slf4jLogConsumer(org.testcontainers.containers.output.Slf4jLogConsumer) Test(org.junit.Test)

Example 4 with Slf4jLogConsumer

use of org.testcontainers.containers.output.Slf4jLogConsumer in project testcontainers-java by testcontainers.

the class LocalDockerCompose method invoke.

@Override
public void invoke() {
    super.start();
    this.followOutput(new Slf4jLogConsumer(logger()));
    // wait for the compose container to stop, which should only happen after it has spawned all the service containers
    logger().info("Docker Compose container is running for command: {}", Joiner.on(" ").join(this.getCommandParts()));
    while (this.isRunning()) {
        logger().trace("Compose container is still running");
        Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
    }
    logger().info("Docker Compose has finished running");
    AuditLogger.doComposeLog(this.getCommandParts(), this.getEnv());
    final Integer exitCode = this.dockerClient.inspectContainerCmd(containerId).exec().getState().getExitCode();
    if (exitCode == null || exitCode != 0) {
        throw new ContainerLaunchException("Containerised Docker Compose exited abnormally with code " + exitCode + " whilst running command: " + StringUtils.join(this.getCommandParts(), ' '));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Slf4jLogConsumer(org.testcontainers.containers.output.Slf4jLogConsumer)

Example 5 with Slf4jLogConsumer

use of org.testcontainers.containers.output.Slf4jLogConsumer in project testcontainers-java by testcontainers.

the class GenericContainer method tryStart.

private void tryStart(Profiler profiler) {
    try {
        String dockerImageName = image.get();
        logger().debug("Starting container: {}", dockerImageName);
        logger().info("Creating container for image: {}", dockerImageName);
        profiler.start("Create container");
        CreateContainerCmd createCommand = dockerClient.createContainerCmd(dockerImageName);
        applyConfiguration(createCommand);
        containerId = createCommand.exec().getId();
        logger().info("Starting container with ID: {}", containerId);
        profiler.start("Start container");
        dockerClient.startContainerCmd(containerId).exec();
        // For all registered output consumers, start following as close to container startup as possible
        this.logConsumers.forEach(this::followOutput);
        logger().info("Container {} is starting: {}", dockerImageName, containerId);
        // Tell subclasses that we're starting
        profiler.start("Inspecting container");
        containerInfo = dockerClient.inspectContainerCmd(containerId).exec();
        containerName = containerInfo.getName();
        profiler.start("Call containerIsStarting on subclasses");
        containerIsStarting(containerInfo);
        // Wait until the container is running (may not be fully started)
        profiler.start("Wait until container has started properly, or there's evidence it failed to start.");
        if (!this.startupCheckStrategy.waitUntilStartupSuccessful(dockerClient, containerId)) {
            // (Exception thrown here will be caught below and wrapped)
            throw new IllegalStateException("Container did not start correctly.");
        }
        profiler.start("Wait until container started properly");
        waitUntilContainerStarted();
        logger().info("Container {} started", dockerImageName);
        containerIsStarted(containerInfo);
    } catch (Exception e) {
        logger().error("Could not start container", e);
        // Log output if startup failed, either due to a container failure or exception (including timeout)
        logger().error("Container log output (if any) will follow:");
        FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback();
        resultCallback.addConsumer(STDOUT, new Slf4jLogConsumer(logger()));
        resultCallback.addConsumer(STDERR, new Slf4jLogConsumer(logger()));
        dockerClient.logContainerCmd(containerId).withStdOut(true).withStdErr(true).exec(resultCallback);
        // Try to ensure that container log output is shown before proceeding
        try {
            resultCallback.getCompletionLatch().await(1, TimeUnit.MINUTES);
        } catch (InterruptedException ignored) {
        // Cannot do anything at this point
        }
        throw new ContainerLaunchException("Could not create/start container", e);
    } finally {
        profiler.stop();
    }
}
Also used : CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) FrameConsumerResultCallback(org.testcontainers.containers.output.FrameConsumerResultCallback) Slf4jLogConsumer(org.testcontainers.containers.output.Slf4jLogConsumer) IOException(java.io.IOException)

Aggregations

Slf4jLogConsumer (org.testcontainers.containers.output.Slf4jLogConsumer)5 Test (org.junit.Test)3 ResultSet (java.sql.ResultSet)2 MySQLContainer (org.testcontainers.containers.MySQLContainer)2 CreateContainerCmd (com.github.dockerjava.api.command.CreateContainerCmd)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 FrameConsumerResultCallback (org.testcontainers.containers.output.FrameConsumerResultCallback)1 OutputFrame (org.testcontainers.containers.output.OutputFrame)1 WaitingConsumer (org.testcontainers.containers.output.WaitingConsumer)1