How to use LogToStringContainerCallback class of org.testcontainers.dockerclient package

Best Testcontainers-java code snippet using org.testcontainers.dockerclient.LogToStringContainerCallback

Source:DockerRunTest.java Github

copy

Full Screen

...10import org.junit.jupiter.api.Timeout;11import org.slf4j.Logger;12import org.slf4j.LoggerFactory;13import org.testcontainers.dockerclient.DockerClientProviderStrategy;14import org.testcontainers.dockerclient.LogToStringContainerCallback;15import org.testcontainers.utility.TestcontainersConfiguration;16import java.util.ArrayList;17import java.util.List;18import java.util.ServiceLoader;19import static java.util.concurrent.TimeUnit.SECONDS;20import static org.junit.Assert.assertTrue;21import static org.testcontainers.DockerClientFactory.DEFAULT_LABELS;22class DockerRunTest {23 @Test24 @DisplayName("Test run and exit docker container")25 @Timeout(value = 30, unit = SECONDS)26 void testRunDockerInDocker() {27 final Logger logger = LoggerFactory.getLogger(this.getClass());28 final List<DockerClientProviderStrategy> configurationStrategies = new ArrayList<>();29 final ServiceLoader<DockerClientProviderStrategy> load = ServiceLoader.load(DockerClientProviderStrategy.class);30 load.forEach(configurationStrategies::add);31 final DockerClientProviderStrategy strategy = DockerClientProviderStrategy.getFirstValidStrategy(configurationStrategies);32 final DockerClient strategyDockerClient = strategy.getClient();33 final String TINY_IMAGE = TestcontainersConfiguration.getInstance().getTinyImage();34 logger.info("Pulling {} image", TINY_IMAGE);35 logger.info("Image {} pulled successfully", TINY_IMAGE);36 strategyDockerClient.pullImageCmd(TINY_IMAGE);37 final CreateContainerCmd createContainerCmd = strategyDockerClient38 .createContainerCmd(TINY_IMAGE)39 .withLabels(DEFAULT_LABELS)40 .withCmd("sh", "-c", "ip route|awk '/default/ { print $3 }' ; exit");41 final String createContainerCmdId = createContainerCmd.exec().getId();42 logger.info("createContainerCmdId={}", createContainerCmdId);43 try {44 strategyDockerClient.startContainerCmd(createContainerCmdId).exec();45 final LogToStringContainerCallback loggingCallback = new LogToStringContainerCallback();46 final LogContainerCmdImpl logContainerCmd2 = (LogContainerCmdImpl) strategyDockerClient47 .logContainerCmd(createContainerCmdId)48 .withStdOut(true)49 .withStdErr(true)50 .withFollowStream(true);51 logger.info("logContainerCmd2={}", logContainerCmd2);52 final LogToStringContainerCallback exec = logContainerCmd2.exec(loggingCallback);53 try {54 logger.info("exec={}", exec);55 final LogContainerResultCallback logContainerResultCallback = exec.awaitStarted();56 logger.info("logContainerResultCallback={}", logContainerResultCallback);57 final boolean awaitCompletion = loggingCallback.awaitCompletion(3L, SECONDS);58 logger.info("awaitCompletion={}", awaitCompletion);59 final String loggingCallbackResult = loggingCallback.toString().trim();60 logger.info("loggingCallbackResult={}", loggingCallbackResult);61 assertTrue(loggingCallbackResult.matches("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$"));62 return;63 } catch (Exception ex) {64 logger.error("Can't parse the default gateway IP", ex);65 }66 } finally {...

Full Screen

Full Screen

Source:DockerClientFactoryTest.java Github

copy

Full Screen

...9import org.junit.Test;10import org.mockito.Mockito;11import org.rnorth.visibleassertions.VisibleAssertions;12import org.testcontainers.DockerClientFactory.DiskSpaceUsage;13import org.testcontainers.dockerclient.LogToStringContainerCallback;14import org.testcontainers.utility.MockTestcontainersConfigurationRule;15import org.testcontainers.utility.TestcontainersConfiguration;16/**17 * Test for {@link DockerClientFactory}.18 */19public class DockerClientFactoryTest {20 @Rule21 public MockTestcontainersConfigurationRule configurationMock = new MockTestcontainersConfigurationRule();22 @Test23 public void runCommandInsideDockerShouldNotFailIfImageDoesNotExistsLocally() {24 final DockerClientFactory dockFactory = DockerClientFactory.instance();25 try {26 //remove tiny image, so it will be pulled during next command run27 dockFactory.client()28 .removeImageCmd(TINY_IMAGE.asCanonicalNameString())29 .withForce(true).exec();30 } catch (NotFoundException ignored) {31 // Do not fail if it's not pulled yet32 }33 dockFactory.runInsideDocker(34 cmd -> cmd.withCmd("sh", "-c", "echo 'SUCCESS'"),35 (client, id) ->36 client.logContainerCmd(id)37 .withStdOut(true)38 .exec(new LogToStringContainerCallback())39 .toString()40 );41 }42 @Test43 public void shouldHandleBigDiskSize() throws Exception {44 String dfOutput = "/dev/disk1 2982480572 1491240286 2982480572 31% /";45 DiskSpaceUsage usage = DockerClientFactory.instance().parseAvailableDiskSpace(dfOutput);46 VisibleAssertions.assertEquals("Available MB is correct", 2982480572L / 1024L, usage.availableMB.orElse(0L));47 VisibleAssertions.assertEquals("Available percentage is correct", 31, usage.usedPercent.orElse(0));48 }49 @Test50 public void dockerHostIpAddress() {51 DockerClientFactory instance = new DockerClientFactory();52 instance.strategy = null;...

Full Screen

Full Screen

LogToStringContainerCallback

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4import org.testcontainers.containers.output.WaitingConsumer;5import org.testcontainers.dockerclient.LogToStringContainerCallback;6import java.util.concurrent.TimeUnit;7public class TestContainersDemo {8 public static void main(String[] args) throws InterruptedException {9 GenericContainer container = new GenericContainer("alpine")10 .withCommand("sh", "-c", "echo hello world")11 .withLogConsumer(new LogToStringContainerCallback());12 container.start();13 System.out.println(container.getLogs());14 }15}16import org.testcontainers.containers.GenericContainer;17import org.testcontainers.containers.output.OutputFrame;18import org.testcontainers.containers.output.ToStringConsumer;19import org.testcontainers.containers.output.WaitingConsumer;20import org.testcontainers.dockerclient.LogToStringContainerCallback;21import java.util.concurrent.TimeUnit;22public class TestContainersDemo {23 public static void main(String[] args) throws InterruptedException {24 GenericContainer container = new GenericContainer("alpine")25 .withCommand("sh", "-c", "echo hello world")26 .withLogConsumer(new WaitingConsumer());27 container.start();28 System.out.println(container.getLogs());29 }30}31import org.testcontainers.containers.GenericContainer;32import org.testcontainers.containers.output.OutputFrame;33import org.testcontainers.containers.output.ToStringConsumer;34import org.testcontainers.containers.output.WaitingConsumer;35import org.testcontainers.dockerclient.LogToStringContainerCallback;36import java.util.concurrent.TimeUnit;37public class TestContainersDemo {38 public static void main(String[] args) throws InterruptedException {39 GenericContainer container = new GenericContainer("alpine")40 .withCommand("sh", "-c", "echo hello world")41 .withLogConsumer(new ToStringConsumer());42 container.start();43 System.out.println(container.getLogs());44 }45}46import org.testcontainers.containers.GenericContainer;47import org.testcontainers.containers.output.OutputFrame;48import org.testcontainers.containers.output.ToStringConsumer;49import org.testcontainers.containers.output.WaitingConsumer;50import org.testcontainers.dockerclient.LogToStringContainer

Full Screen

Full Screen

LogToStringContainerCallback

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4import org.testcontainers.containers.output.WaitingConsumer;5import org.testcontainers.containers.output.WaitingConsumer.WaitStrategy;6import org.testcontainers.containers.output.WaitingConsumer.WaitStrategyType;7public class LogToStringContainerCallback {8 public static void main(String[] args) throws Exception {9 try (GenericContainer container = new GenericContainer("busybox:latest")10 .withCommand("sh", "-c", "for i in `seq 1 10`; do echo $i; sleep 1; done")11 .withLogConsumer(new ToStringConsumer())) {12 container.start();13 String log = container.getLogs();14 System.out.println(log);15 }16 }17}18import org.testcontainers.containers.GenericContainer;19import org.testcontainers.containers.output.OutputFrame;20import org.testcontainers.containers.output.ToStringConsumer;21import org.testcontainers.containers.output.WaitingConsumer;22import org.testcontainers.containers.output.WaitingConsumer.WaitStrategy;23import org.testcontainers.containers.output.WaitingConsumer.WaitStrategyType;24public class LogToStringContainerCallback {25 public static void main(String[] args) throws Exception {26 try (GenericContainer container = new GenericContainer("busybox:latest")27 .withCommand("sh", "-c", "for i in `seq 1 10`; do echo $i; sleep 1; done")28 .withLogConsumer(new WaitingConsumer().withWaitStrategy(WaitStrategyType.LOG_CONTAINS, "5"))) {29 container.start();30 String log = container.getLogs();31 System.out.println(log);32 }33 }34}35import org.testcontainers.containers.GenericContainer;36import org.testcontainers.containers.output.OutputFrame;37import org.testcontainers.containers.output.ToStringConsumer;38import org.testcontainers.containers.output.WaitingConsumer;39import org.testcontainers.containers.output.WaitingConsumer.WaitStrategy;40import org.testcontainers.containers.output.WaitingConsumer.WaitStrategyType;41public class LogToStringContainerCallback {42 public static void main(String[] args) throws Exception {43 try (GenericContainer container = new GenericContainer("busybox:latest")

Full Screen

Full Screen

LogToStringContainerCallback

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.dockerclient.LogToStringContainerCallback;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.output.OutputFrame;4import org.testcontainers.containers.output.ToStringConsumer;5public class 1 {6 public static void main(String[] args) {7 try (GenericContainer container = new GenericContainer("ubuntu:latest")) {8 container.start();9 container.followOutput(new ToStringConsumer() {10 public void accept(OutputFrame outputFrame) {11 System.out.println(outputFrame.getUtf8String());12 }13 });14 }15 }16}17import org.testcontainers.containers.GenericContainer;18import org.testcontainers.containers.output.OutputFrame;19import org.testcontainers.containers.output.ToStringConsumer;20import java.io.IOException;21public class 2 {22 public static void main(String[] args) throws IOException, InterruptedException {23 try (GenericContainer container = new GenericContainer("ubuntu:latest")) {24 container.start();25 container.followOutput(new ToStringConsumer() {26 public void accept(OutputFrame outputFrame) {27 System.out.println(outputFrame.getUtf8String());28 }29 });30 }31 }32}33import org.testcontainers.containers.GenericContainer;34import org.testcontainers.containers.output.OutputFrame;35import org.testcontainers.containers.output.ToStringConsumer;36import java.io.IOException;37public class 3 {38 public static void main(String[] args) throws IOException, InterruptedException {39 try (GenericContainer container = new GenericContainer("ubuntu:latest")) {40 container.start();41 container.followOutput(new ToStringConsumer() {42 public void accept(OutputFrame outputFrame) {43 System.out.println(outputFrame.getUtf8String());44 }45 });46 }47 }48}49import org.testcontainers.containers.GenericContainer;50import org.testcontainers.containers.output.OutputFrame;51import org.testcontainers.containers.output.ToStringConsumer;52import java.io.IOException;53public class 4 {54 public static void main(String[] args) throws IOException, InterruptedException {55 try (GenericContainer container = new GenericContainer("ubuntu:latest")) {56 container.start();

Full Screen

Full Screen

LogToStringContainerCallback

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4import org.testcontainers.containers.output.WaitingConsumer;5import org.testcontainers.containers.output.OutputFrame.OutputType;6import org.testcontainers.containers.output.ToStringConsumer;7import org.testcontainers.containers.output.WaitingConsumer;8import org.testcontainers.containers.output.OutputFrame.OutputType;9import org.testcontainers.dockerclient.LogToStringContainerCallback;10import org.testcontainers.images.builder.ImageFromDockerfile;11import java.io.File;12import java.util.concurrent.TimeUnit;13import java.util.concurrent.TimeoutException;14public class LogToStringContainerCallbackTest {15 public static void main(String[] args) throws TimeoutException, InterruptedException {16 GenericContainer container = new GenericContainer(17 new ImageFromDockerfile()18 .withFileFromFile("Dockerfile", new File("Dockerfile"))19 .withFileFromFile("test.sh", new File("test.sh"))20 );21 container.start();22 LogToStringContainerCallback callback = new LogToStringContainerCallback();23 container.followOutput(callback);24 callback.waitUntilEnd(10, TimeUnit.SECONDS);25 System.out.println(callback.toString());26 }27}28import org.testcontainers.containers.GenericContainer;29import org.testcontainers.containers.output.OutputFrame;30import org.testcontainers.containers.output.ToStringConsumer;31import org.testcontainers.containers.output.WaitingConsumer;32import org.testcontainers.containers.output.OutputFrame.OutputType;33import org.testcontainers.dockerclient.LogToStringContainerCallback;34import org.testcontainers.images.builder.ImageFromDockerfile;35import java.io.File;36import java.util.concurrent.TimeUnit;37import java.util.concurrent.TimeoutException;38public class LogToStringContainerCallbackTest {

Full Screen

Full Screen

LogToStringContainerCallback

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.dockerclient;2import com.github.dockerjava.api.DockerClient;3import com.github.dockerjava.api.command.CreateContainerCmd;4import com.github.dockerjava.api.command.CreateContainerResponse;5import com.github.dockerjava.api.model.Bind;6import com.github.dockerjava.api.model.ExposedPort;7import com.github.dockerjava.api.model.Ports;8import com.github.dockerjava.core.DockerClientBuilder;9import org.testcontainers.containers.GenericContainer;10import org.testcontainers.containers.output.OutputFrame;11import org.testcontainers.containers.output.ToStringConsumer;12import java.io.File;13import java.util.concurrent.TimeUnit;14import static org.testcontainers.containers.BindMode.READ_WRITE;15public class TestContainerDockerClient {16 public static void main(String[] args) throws Exception {17 DockerClient dockerClient = DockerClientBuilder.getInstance().build();18 CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd("alpine");19 createContainerCmd.withName("test");20 createContainerCmd.withCmd("ping", "google.com");21 CreateContainerResponse container = createContainerCmd.exec();22 dockerClient.startContainerCmd(container.getId()).exec();23 LogToStringContainerCallback logToStringContainerCallback = new LogToStringContainerCallback();24 dockerClient.logContainerCmd(container.getId()).withStdOut(true).withStdErr(true).exec(logToStringContainerCallback).awaitCompletion(10, TimeUnit.SECONDS);25 System.out.println(logToStringContainerCallback.toString());26 }27}28package org.testcontainers;29import com.github.dockerjava.api.DockerClient;30import com.github.dockerjava.api.command.CreateContainerCmd;31import com.github.dockerjava.api.command.CreateContainerResponse;32import com.github.dockerjava.api.model.Bind;33import com.github.dockerjava.api.model.ExposedPort;34import com.github.dockerjava.api.model.Ports;35import com.github.dockerjava.core.DockerClientBuilder;36import org.testcontainers.containers.GenericContainer;37import org.testcontainers.containers.output.OutputFrame;38import org.testcontainers.containers.output.ToStringConsumer;39import java.io.File;40import java.util.concurrent.TimeUnit;41import static org.testcontainers.containers.BindMode.READ_WRITE;42public class TestContainerDockerClient {43 public static void main(String[] args) throws Exception {44 DockerClient dockerClient = DockerClientBuilder.getInstance().build();

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in LogToStringContainerCallback

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful