How to use withStartupCheckStrategy method of org.testcontainers.containers.GenericContainer class

Best Testcontainers-java code snippet using org.testcontainers.containers.GenericContainer.withStartupCheckStrategy

Source:ImagePullPolicyTest.java Github

copy

Full Screen

...59 @Test60 public void pullsByDefault() {61 try (62 GenericContainer<?> container = new GenericContainer<>(imageName)63 .withStartupCheckStrategy(new OneShotStartupCheckStrategy())64 ) {65 container.start();66 }67 }68 @Test69 public void shouldAlwaysPull() {70 try (71 GenericContainer<?> container = new GenericContainer<>(imageName)72 .withStartupCheckStrategy(new OneShotStartupCheckStrategy())73 ) {74 container.start();75 }76 DockerClientFactory.instance().client().removeImageCmd(imageName).withForce(true).exec();77 try (78 GenericContainer<?> container = new GenericContainer<>(imageName)79 .withStartupCheckStrategy(new OneShotStartupCheckStrategy())80 ) {81 expectToFailWithNotFoundException(container);82 }83 try (84 // built_in_image_pull_policy {85 GenericContainer<?> container = new GenericContainer<>(imageName)86 .withImagePullPolicy(PullPolicy.alwaysPull())87 // }88 ) {89 container.withStartupCheckStrategy(new OneShotStartupCheckStrategy());90 container.start();91 }92 }93 @Test94 public void shouldSupportCustomPolicies() {95 try (96 // custom_image_pull_policy {97 GenericContainer<?> container = new GenericContainer<>(imageName)98 .withImagePullPolicy(new AbstractImagePullPolicy() {99 @Override100 protected boolean shouldPullCached(DockerImageName imageName, ImageData localImageData) {101 return System.getenv("ALWAYS_PULL_IMAGE") != null;102 }103 })104 // }105 ) {106 container.withStartupCheckStrategy(new OneShotStartupCheckStrategy());107 container.start();108 }109 }110 @Test111 public void shouldCheckPolicy() {112 ImagePullPolicy policy = Mockito.spy(new AbstractImagePullPolicy() {113 @Override114 protected boolean shouldPullCached(DockerImageName imageName, ImageData localImageData) {115 return false;116 }117 });118 try (119 GenericContainer<?> container = new GenericContainer<>(imageName)120 .withImagePullPolicy(policy)121 .withStartupCheckStrategy(new OneShotStartupCheckStrategy())122 ) {123 container.start();124 Mockito.verify(policy).shouldPull(any());125 }126 }127 @Test128 public void shouldNotForcePulling() {129 try (130 GenericContainer<?> container = new GenericContainer<>(imageName)131 .withImagePullPolicy(__ -> false)132 .withStartupCheckStrategy(new OneShotStartupCheckStrategy())133 ) {134 expectToFailWithNotFoundException(container);135 }136 }137 private void expectToFailWithNotFoundException(GenericContainer<?> container) {138 try {139 container.start();140 fail("Should fail");141 } catch (ContainerLaunchException e) {142 Throwable throwable = e;143 while (throwable.getCause() != null) {144 throwable = throwable.getCause();145 if (throwable.getCause() instanceof NotFoundException) {146 VisibleAssertions.pass("Caused by NotFoundException");...

Full Screen

Full Screen

Source:DirectoryTarResourceTest.java Github

copy

Full Screen

...16 WaitingConsumer wait = new WaitingConsumer();17 final ToStringConsumer toString = new ToStringConsumer();18 // 'src' is expected to be the project base directory, so all source code/resources should be copied in19 File directory = new File("src");20 GenericContainer container = new GenericContainer(new ImageFromDockerfile().withDockerfileFromBuilder(( builder) -> builder.from("alpine:3.3").copy("/tmp/foo", "/foo").cmd("cat /foo/test/resources/test-recursive-file.txt").build()).withFileFromFile("/tmp/foo", directory)).withStartupCheckStrategy(new OneShotStartupCheckStrategy()).withLogConsumer(wait.andThen(toString));21 container.start();22 wait.waitUntilEnd(60, TimeUnit.SECONDS);23 final String results = toString.toUtf8String();24 assertTrue("The container has a file that was copied in via a recursive copy", results.contains("Used for DirectoryTarResourceTest"));25 }26 @Test27 public void simpleRecursiveFileWithPermissionTest() throws TimeoutException {28 WaitingConsumer wait = new WaitingConsumer();29 final ToStringConsumer toString = new ToStringConsumer();30 GenericContainer container = new GenericContainer(new ImageFromDockerfile().withDockerfileFromBuilder(( builder) -> builder.from("alpine:3.3").copy("/tmp/foo", "/foo").cmd("ls", "-al", "/").build()).withFileFromFile("/tmp/foo", new File("/mappable-resource/test-resource.txt"), 492)).withStartupCheckStrategy(new OneShotStartupCheckStrategy()).withLogConsumer(wait.andThen(toString));31 container.start();32 wait.waitUntilEnd(60, TimeUnit.SECONDS);33 String listing = toString.toUtf8String();34 assertThat("Listing shows that file is copied with mode requested.", Arrays.asList(listing.split("\\n")), DirectoryTarResourceTest.exactlyNItems(1, CoreMatchers.allOf(CoreMatchers.containsString("-rwxr-xr--"), CoreMatchers.containsString("foo"))));35 }36 @Test37 public void simpleRecursiveClasspathResourceTest() throws TimeoutException {38 // This test combines the copying of classpath resources from JAR files with the recursive TAR approach, to allow JARed classpath resources to be copied in to an image39 WaitingConsumer wait = new WaitingConsumer();40 final ToStringConsumer toString = new ToStringConsumer();41 GenericContainer container = // here we use /org/junit as a directory that really should exist on the classpath42 new GenericContainer(new ImageFromDockerfile().withDockerfileFromBuilder(( builder) -> builder.from("alpine:3.3").copy("/tmp/foo", "/foo").cmd("ls -lRt /foo").build()).withFileFromClasspath("/tmp/foo", "/recursive/dir")).withStartupCheckStrategy(new OneShotStartupCheckStrategy()).withLogConsumer(wait.andThen(toString));43 container.start();44 wait.waitUntilEnd(60, TimeUnit.SECONDS);45 final String results = toString.toUtf8String();46 // ExternalResource.class is known to exist in a subdirectory of /org/junit so should be successfully copied in47 assertTrue("The container has a file that was copied in via a recursive copy from a JAR resource", results.contains("content.txt"));48 }49}...

Full Screen

Full Screen

withStartupCheckStrategy

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.utility.DockerImageName;3public class TestContainer {4 public static void main(String[] args) {5 GenericContainer container = new GenericContainer(DockerImageName.parse("alpine:3.9.4"))6 .withCommand("tail", "-f", "/dev/null")7 .withStartupCheckStrategy(new MyStrategy());8 container.start();9 }10}11import org.testcontainers.containers.GenericContainer;12import org.testcontainers.containers.wait.strategy.WaitStrategy;13import org.testcontainers.utility.DockerImageName;14import java.util.concurrent.TimeUnit;15public class TestContainer {16 public static void main(String[] args) {17 GenericContainer container = new GenericContainer(DockerImageName.parse("alpine:3.9.4"))18 .withCommand("tail", "-f", "/dev/null")19 .withStartupCheckStrategy(new MyStrategy());20 container.start();21 }22}23import org.testcontainers.containers.GenericContainer;24import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;25import org.testcontainers.containers.wait.strategy.WaitStrategy;26import org.testcontainers.utility.DockerImageName;27import java.util.concurrent.TimeUnit;28public class TestContainer {29 public static void main(String[] args) {30 GenericContainer container = new GenericContainer(DockerImageName.parse("alpine:3.9.4"))31 .withCommand("tail", "-f", "/dev/null")32 .withStartupCheckStrategy(new MyStrategy());33 container.start();34 }35}36import org.testcontainers.containers.GenericContainer;37import org.testcontainers.containers.wait.strategy.WaitAllStrategy;38import org.testcontainers.containers.wait.strategy.WaitStrategy;39import org.testcontainers.utility.DockerImageName;40import java.util.concurrent.TimeUnit;41public class TestContainer {42 public static void main(String[] args) {43 GenericContainer container = new GenericContainer(DockerImageName.parse("alpine:3.9.4"))44 .withCommand("tail", "-f", "/dev/null")

Full Screen

Full Screen

withStartupCheckStrategy

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2public class 1 {3 public static void main(String[] args) {4 GenericContainer container = new GenericContainer("alpine:3.12")5 .withCommand("sh", "-c", "while true; do echo hello world; sleep 1; done")6 .withStartupCheckStrategy(new OneShotStartupCheckStrategy())7 .withStartupTimeout(Duration.ofSeconds(5));8 container.start();9 }10}11Caused by: org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [32781] should be listening)

Full Screen

Full Screen

withStartupCheckStrategy

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.testcontainers.containers.GenericContainer;3public class TestContainer {4 public void testContainer() {5 try (GenericContainer container = new GenericContainer("ubuntu:16.04")6 .withStartupCheckStrategy(new GenericContainer.AbstractWaitStrategy() {7 protected void waitUntilReady() {8 }9 })) {10 container.start();11 }12 }13}14import org.junit.Test;15import org.testcontainers.containers.GenericContainer;16public class TestContainer {17 public void testContainer() {18 try (GenericContainer container = new GenericContainer("ubuntu:16.04")19 .withStartupCheckStrategy(new GenericContainer.AbstractWaitStrategy() {20 protected void waitUntilReady() {21 }22 })23 .withStartupTimeoutSeconds(5)) {24 container.start();25 }26 }27}28import org.junit.Test;29import org.testcontainers.containers.GenericContainer;30public class TestContainer {31 public void testContainer() {32 try (GenericContainer container = new GenericContainer("ubuntu:16.04")33 .withStartupCheckStrategy(new GenericContainer.AbstractWaitStrategy() {34 protected void waitUntilReady() {35 }36 })37 .withStartupTimeoutSeconds(5)38 .withStartupCheckMaxRetries(2)) {39 container.start();40 }41 }42}43import org.junit.Test;44import org.testcontainers.containers.GenericContainer;45public class TestContainer {46 public void testContainer() {47 try (GenericContainer container = new GenericContainer("ubuntu:16.04")48 .withStartupCheckStrategy(new GenericContainer.AbstractWaitStrategy() {49 protected void waitUntilReady() {50 }51 })52 .withStartupTimeoutSeconds(5)53 .withStartupCheckMaxRetries(2)54 .withStartupCheckRetryIntervalSeconds(1)) {

Full Screen

Full Screen

withStartupCheckStrategy

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2public class DockerContainer {3 public static void main(String[] args) {4 GenericContainer container = new GenericContainer("alpine:3.12")5 .withStartupCheckStrategy(new StartupCheckStrategy() {6 public boolean isReady() {7 return false;8 }9 });10 container.start();11 }12}13import org.testcontainers.containers.GenericContainer;14import org.testcontainers.containers.wait.strategy.Wait;15public class DockerContainer {16 public static void main(String[] args) {17 GenericContainer container = new GenericContainer("alpine:3.12")18 .withStartupCheckStrategy(new StartupCheckStrategy() {19 public boolean isReady() {20 return false;21 }22 })23 .waitingFor(Wait.forLogMessage(".*", 1));24 container.start();25 }26}27import org.testcontainers.containers.GenericContainer;28import org.testcontainers.containers.wait.strategy.Wait;29public class DockerContainer {30 public static void main(String[] args) {31 GenericContainer container = new GenericContainer("alpine:3.12")32 .withStartupCheckStrategy(new StartupCheckStrategy() {33 public boolean isReady() {34 return false;35 }36 })37 .waitingFor(Wait.forLogMessage(".*", 1))38 .withCommand("/bin/sh", "-c", "while true; do echo 'Hello'; sleep 1; done");39 container.start();40 }41}42import org.testcontainers.containers.GenericContainer;43import org.testcontainers.containers.wait.strategy.Wait;44public class DockerContainer {45 public static void main(String[] args) {46 GenericContainer container = new GenericContainer("alpine:3.12")47 .withStartupCheckStrategy(new StartupCheckStrategy() {48 public boolean isReady() {49 return false;50 }51 })52 .waitingFor(Wait.forLogMessage(".*", 1))53 .withCommand("/bin/sh", "-c", "while true

Full Screen

Full Screen

withStartupCheckStrategy

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.testcontainers.containers.GenericContainer;3public class TestContainer {4 public void testContainer() {5 GenericContainer container = new GenericContainer("redis:latest")6 .withStartupCheckStrategy(new GenericContainer.AbstractStartupCheckStrategy() {7 protected void waitUntilReady() {8 }9 });10 container.start();11 }12}13import org.junit.Test;14import org.testcontainers.containers.GenericContainer;15public class TestContainer {16 public void testContainer() {17 GenericContainer container = new GenericContainer("redis:latest")18 .withStartupTimeout(Duration.ofSeconds(60));19 container.start();20 }21}22import org.junit.Test;23import org.testcontainers.containers.GenericContainer;24public class TestContainer {25 public void testContainer() {26 GenericContainer container = new GenericContainer("redis:latest")27 .withCommand("redis-server --test-memory 2");28 container.start();29 }30}31import org.junit.Test;32import org.testcontainers.containers.GenericContainer;33public class TestContainer {34 public void testContainer() {35 GenericContainer container = new GenericContainer("redis:latest")36 .withEnv("REDIS_DISABLE_COMMANDS", "FLUSHDB,FLUSHALL");37 container.start();38 }39}40import org.junit.Test;41import org.testcontainers.containers.GenericContainer;42public class TestContainer {43 public void testContainer() {44 GenericContainer container = new GenericContainer("redis:latest")45 .withExposedPorts(6379);46 container.start();47 }48}

Full Screen

Full Screen

withStartupCheckStrategy

Using AI Code Generation

copy

Full Screen

1public class Test {2 public static void main(String[] args) {3 try (GenericContainer container = new GenericContainer("busybox:1.31.1")4 .withStartupCheckStrategy(new StartupCheckStrategy() {5 public boolean isStartupSuccessful(GenericContainer container) {6 return container.isRunning();7 }8 })9 .withCommand("sh", "-c", "sleep 5; exit 1")) {10 container.start();11 }12 }13}14public class Test {15 public static void main(String[] args) {16 try (GenericContainer container = new GenericContainer("busybox:1.31.1")17 .withStartupCheckStrategy(new StartupCheckStrategy() {18 public boolean isStartupSuccessful(GenericContainer container) {19 return container.isRunning();20 }21 })22 .withCommand("sh", "-c", "sleep 5")) {23 container.start();24 }25 }26}27public class Test {28 public static void main(String[] args) {29 try (GenericContainer container = new GenericContainer("busybox:1.31.1")30 .withStartupCheckStrategy(new StartupCheckStrategy() {31 public boolean isStartupSuccessful(GenericContainer container) {32 return container.isRunning();33 }34 })35 .withCommand("sh", "-c", "sleep 5; exit 1")) {36 container.start();37 }38 }39}

Full Screen

Full Screen

withStartupCheckStrategy

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.startupcheck.StartupCheckStrategy;3import org.testcontainers.containers.wait.strategy.Wait;4import org.testcontainers.containers.wait.strategy.WaitAllStrategy;5import org.testcontainers.containers.wait.strategy.WaitStrategy;6import org.testcontainers.images.builder.ImageFromDockerfile;7import java.io.File;8public class TestContainerExample {9 public static void main(String[] args) {10 GenericContainer container = new GenericContainer(new ImageFromDockerfile()11 .withFileFromFile("1.java", new File("/Users/abhisheksingh/Downloads/1.java")))12 .withStartupCheckStrategy(new StartupCheckStrategy() {13 public boolean isStartupComplete(GenericContainer container) {14 if (response.equals("200")) {15 return true;16 }17 return false;18 }19 })20 .withExposedPorts(8080)21 .waitingFor(Wait.forHttp("/").forStatusCode(200));22 container.start();23 System.out.println(container.getLogs());24 }25}

Full Screen

Full Screen

withStartupCheckStrategy

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.testcontainers.containers.startupcheck.StartupCheckStrategy;4import org.testcontainers.containers.wait.strategy.Wait;5import org.testcontainers.utility.DockerImageName;6import org.slf4j.Logger;7import org.slf4j.LoggerFactory;8public class TestContainer {9 private static final Logger logger = LoggerFactory.getLogger(TestContainer.class);10 public static void main(String[] args) {11 try (GenericContainer<?> container = new GenericContainer<>(DockerImageName.parse("ubuntu:latest"))12 .withStartupCheckStrategy(new StartupCheckStrategy() {13 public boolean isStartupSuccessful(GenericContainer container) {14 logger.info("Startup check");15 return true;16 }17 })18 .withLogConsumer(new Slf4jLogConsumer(logger))19 .waitingFor(Wait.forLogMessage(".*", 1))) {20 container.start();21 }22 }23}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful