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"));
}
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();
}
}
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();
}
}
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(), ' '));
}
}
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();
}
}
Aggregations