Best Testcontainers-java code snippet using org.testcontainers.junit.DockerComposeContainerWithBuildTest.isImagePresent
Source:DockerComposeContainerWithBuildTest.java
...43 .withBuild(true)44 .withRemoveImages(removeMode)) {45 environment.start();46 builtImageName.set(imageNameForRunningContainer("_customredis_1"));47 final boolean isBuiltImagePresentWhileRunning = isImagePresent(builtImageName.get());48 assertEquals("the built image is present while running", true, isBuiltImagePresentWhileRunning);49 pulledImageName.set(imageNameForRunningContainer("_normalredis_1"));50 final boolean isPulledImagePresentWhileRunning = isImagePresent(pulledImageName.get());51 assertEquals("the pulled image is present while running", true, isPulledImagePresentWhileRunning);52 }53 Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> {54 final boolean isBuiltImagePresentAfterRunning = isImagePresent(builtImageName.get());55 assertEquals("the built image is not present after running", shouldBuiltImageBePresentAfterRunning, isBuiltImagePresentAfterRunning);56 return null;57 });58 Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> {59 final boolean isPulledImagePresentAfterRunning = isImagePresent(pulledImageName.get());60 assertEquals("the pulled image is present after running", shouldPulledImageBePresentAfterRunning, isPulledImagePresentAfterRunning);61 return null;62 });63 }64 private String imageNameForRunningContainer(final String containerNameSuffix) {65 return DockerClientFactory.instance().client().listContainersCmd().exec().stream()66 .filter(it -> Stream.of(it.getNames()).anyMatch(name -> name.endsWith(containerNameSuffix)))67 .findFirst()68 .map(Container::getImage)69 .orElseThrow(IllegalStateException::new);70 }71 private boolean isImagePresent(final String imageName) {72 return DockerClientFactory.instance().client().listImagesCmd().withImageNameFilter(imageName).exec().stream()73 .findFirst()74 .isPresent();75 }76}...
isImagePresent
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer2import org.testcontainers.containers.wait.strategy.Wait3import org.testcontainers.junit.DockerComposeContainerWithBuildTest4import org.testcontainers.utility.DockerImageName5class DockerComposeContainerWithBuildTest extends DockerComposeContainerWithBuildTest {6 def "test if image exists"() {7 def isImagePresent = isImagePresent("testcontainers/ryuk:0.3.1")8 def dockerComposeContainer = new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))9 dockerComposeContainer.withLocalCompose(true)10 dockerComposeContainer.withExposedService("redis_1", 6379, Wait.forListeningPort())11 dockerComposeContainer.withExposedService("selenium-hub", 4444, Wait.forListeningPort())12 dockerComposeContainer.start()13 def redisContainer = dockerComposeContainer.getServiceContainer("redis_1")14 def seleniumHubContainer = dockerComposeContainer.getServiceContainer("selenium-hub")15 def redisPort = redisContainer.getMappedPort(6379)16 def seleniumHubPort = seleniumHubContainer.getMappedPort(4444)17 def redisHost = redisContainer.getHost()18 def seleniumHubHost = seleniumHubContainer.getHost()19 }20 def "test if image does not exist"() {21 def isImagePresent = isImagePresent("testcontainers/ryuk:0.3.1")22 def dockerComposeContainer = new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))23 dockerComposeContainer.withLocalCompose(true)24 dockerComposeContainer.withExposedService("redis_1", 6379, Wait.forListeningPort())25 dockerComposeContainer.withExposedService("selenium-hub", 4444, Wait.forListeningPort())26 dockerComposeContainer.start()27 def redisContainer = dockerComposeContainer.getServiceContainer("redis_1")28 def seleniumHubContainer = dockerComposeContainer.getServiceContainer("
isImagePresent
Using AI Code Generation
1public class DockerComposeContainerWithBuildTest {2 private DockerComposeContainer environment;3 private GenericContainer container;4 public final DockerComposeContainer environment = new DockerComposeContainer( new File( "src/test/resources/compose-build-test.yml" ))5 .withExposedService( "db_1" , 5432 )6 .withExposedService( "web_1" , 5000 );7 public final GenericContainer container = new GenericContainer(environment.getServiceHost( "db_1" , 5432 ),8 environment.getServicePort( "db_1" , 5432 ))9 .withExposedPorts( 5432 );10 public void testWithBuild() {11 container.start();12 await()13 .atMost( 10 , SECONDS )14 .until( () -> isImagePresent( "postgres:9.4" ));15 }16}17public class DockerComposeContainerWithPullTest {18 private DockerComposeContainer environment;19 private GenericContainer container;20 public final DockerComposeContainer environment = new DockerComposeContainer( new File( "src/test/resources/compose-pull-test.yml" ))21 .withExposedService( "db_1" , 5432 )22 .withExposedService( "web_1" , 5000 );23 public final GenericContainer container = new GenericContainer(environment.getServiceHost( "db_1" , 5432 ),24 environment.getServicePort( "db_1" , 5432 ))25 .withExposedPorts( 5432 );26 public void testWithPull() {27 container.start();28 await()29 .atMost( 10 , SECONDS )30 .until( () -> isImagePresent( "postgres:9.4" ));31 }32}33public class DockerComposeContainerWithFileTest {34 private DockerComposeContainer environment;35 private GenericContainer container;
isImagePresent
Using AI Code Generation
1public class DockerComposeContainerWithBuildTest {2 new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))3 .withExposedService("redis_1", 6379)4 .withExposedService("web_1", 5000, Wait.forHttp("/"));5 public void testSimple() throws IOException {6 String redisHost = environment.getServiceHost("redis_1", 6379);7 Integer redisPort = environment.getServicePort("redis_1", 6379);8 String webHost = environment.getServiceHost("web_1", 5000);9 Integer webPort = environment.getServicePort("web_1", 5000);10 assertThat(isImagePresent("redis:latest"), is(true));11 assertThat(isImagePresent("alpine:latest"), is(true));12 assertThat(isImagePresent("alpine:3.2"), is(true));13 assertThat(redisHost, not(isEmptyOrNullString()));14 assertThat(redisPort, not(isEmptyOrNullString()));15 assertThat(webHost, not(isEmptyOrNullString()));16 assertThat(webPort, not(isEmptyOrNullString()));17 String redisInfo = new Socket(redisHost, redisPort).getInputStream().toString();18 assertThat(redisInfo, containsString("redis_version:3.0.1"));19 String webInfo = new Socket(webHost, webPort).getInputStream().toString();20 assertThat(webInfo, containsString("Hello World!"));21 }22 private boolean isImagePresent(String imageName) {23 try {24 DockerClientFactory.instance().client().inspectImageCmd(imageName).exec();25 } catch (NotFoundException e) {26 return false;27 }28 return true;29 }30}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!