Best Testcontainers-java code snippet using org.testcontainers.images.ImagePullPolicyTest.DockerRegistryContainer
Source:ImagePullPolicyTest.java
...3import org.junit.ClassRule;4import org.junit.Test;5import org.mockito.Mockito;6import org.testcontainers.DockerClientFactory;7import org.testcontainers.DockerRegistryContainer;8import org.testcontainers.containers.ContainerLaunchException;9import org.testcontainers.containers.GenericContainer;10import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;11import org.testcontainers.utility.DockerImageName;12import static org.assertj.core.api.Assertions.fail;13import static org.mockito.ArgumentMatchers.any;14public class ImagePullPolicyTest {15 @ClassRule16 public static DockerRegistryContainer registry = new DockerRegistryContainer();17 private final DockerImageName imageName = registry.createImage();18 @Test19 public void pullsByDefault() {20 try (GenericContainer<?> container = new GenericContainer<>(imageName).withExposedPorts(8080)) {21 container.start();22 }23 }24 @Test25 public void shouldAlwaysPull() {26 try (GenericContainer<?> container = new GenericContainer<>(imageName).withExposedPorts(8080)) {27 container.start();28 }29 removeImage();30 try (GenericContainer<?> container = new GenericContainer<>(imageName).withExposedPorts(8080)) {...
DockerRegistryContainer
Using AI Code Generation
1 public void testDockerRegistryContainer() throws Exception {2 DockerRegistryContainer registry = new DockerRegistryContainer();3 registry.start();4 String registryUrl = registry.getRegistryUrl();5 DockerClientFactory.instance().client().pullImageCmd(registryUrl + "/busybox:latest")6 .exec(new PullImageResultCallback()).awaitCompletion();7 DockerClientFactory.instance().client().pullImageCmd(registryUrl + "/busybox:1.30.1")8 .exec(new PullImageResultCallback()).awaitCompletion();9 ImagePullPolicy policy = new DockerRegistryContainerImagePullPolicy(registryUrl);10 assertThat(policy.shouldPull("busybox:latest"), is(false));11 assertThat(policy.shouldPull("busybox:1.30.1"), is(false));12 assertThat(policy.shouldPull("busybox:1.30.2"), is(true));13 registry.close();14 }15}16class DockerRegistryContainerImagePullPolicy implements ImagePullPolicy {17 private final String registryUrl;18 public DockerRegistryContainerImagePullPolicy(String registryUrl) {19 this.registryUrl = registryUrl;20 }21 public boolean shouldPull(String image) {22 try {23 DockerClientFactory.instance().client().inspectImageCmd(image).exec();24 return false;25 } catch (NotFoundException e) {26 return true;27 }28 }29 public String getRegistryUrl() {30 return registryUrl;31 }32}33public class DockerRegistryContainer extends GenericContainer<DockerRegistryContainer> {34 private static final String IMAGE_NAME = "registry:2";35 private static final int DEFAULT_PORT = 5000;36 private static final String DEFAULT_TAG = "latest";37 public DockerRegistryContainer() {38 this(IMAGE_NAME + ":" + DEFAULT_TAG);39 }40 public DockerRegistryContainer(String imageName) {41 super(imageName);42 withExposedPorts(DEFAULT_PORT);43 waitingFor(Wait.forHttp("/v2/"));44 }45 public String getRegistryUrl() {46 }47}48public class DockerClientFactory {
DockerRegistryContainer
Using AI Code Generation
1 public void testDockerRegistryContainer() throws Exception {2 try (DockerRegistryContainer container = new DockerRegistryContainer()) {3 container.start();4 String registryAddress = container.getRegistryAddress();5 String image = "alpine:3.7";6 dockerClient.pullImageCmd(image).exec(new PullImageResultCallback()).awaitSuccess();7 dockerClient.tagImageCmd(image, registryAddress + "/alpine", "3.7").exec();8 dockerClient.pushImageCmd(registryAddress + "/alpine").withTag("3.7").exec(new PushImageResultCallback()).awaitSuccess();9 List<Image> images = dockerClient.listImagesCmd().withImageNameFilter("alpine").exec();10 assertThat(images, Matchers.hasSize(1));11 }12 }13}14The DockerRegistryContainer class also has a method called withStartupAttempts() that takes the number of startup attempts as an argument. The default number of startup attempts is 3
DockerRegistryContainer
Using AI Code Generation
1 public void testDockerRegistryContainer() {2 ImagePullPolicy policy = new ImagePullPolicy();3 policy.pullPolicy = "DockerRegistryContainer";4 try {5 policy.pullImage(DockerImageName.parse("alpine:3.12.0"));6 } catch (Exception e) {7 fail(e.getMessage());8 }9 }10 public void testDockerRegistryContainerWithDockerClient() {11 ImagePullPolicy policy = new ImagePullPolicy();12 policy.pullPolicy = "DockerRegistryContainer";13 try {14 policy.pullImage(DockerImageName.parse("alpine:3.12.0"), DockerClientFactory.instance().client());15 } catch (Exception e) {16 fail(e.getMessage());17 }18 }19 public void testDockerRegistryContainerWithDockerClientAndAuthConfig() {20 ImagePullPolicy policy = new ImagePullPolicy();21 policy.pullPolicy = "DockerRegistryContainer";22 try {23 policy.pullImage(DockerImageName.parse("alpine:3.12.0"), DockerClientFactory.instance().client(), AuthConfig.empty());24 } catch (Exception e) {25 fail(e.getMessage());26 }27 }28 public void testDockerRegistryContainerWithDockerClientAndAuthConfigAndRegistry() {29 ImagePullPolicy policy = new ImagePullPolicy();30 policy.pullPolicy = "DockerRegistryContainer";31 try {32 policy.pullImage(DockerImageName.parse("alpine:3.12.0"), DockerClientFactory.instance().client(), AuthConfig.empty(), "registry");33 } catch (Exception e) {34 fail(e.getMessage());35 }36 }37 public void testDockerRegistryContainerWithDockerClientAndAuthConfigAndRegistryAndImagePullProgressListener() {38 ImagePullPolicy policy = new ImagePullPolicy();39 policy.pullPolicy = "DockerRegistryContainer";40 try {41 policy.pullImage(DockerImageName.parse("alpine:
DockerRegistryContainer
Using AI Code Generation
1 public void testPullPolicy() {2 try (DockerRegistryContainer registry = new DockerRegistryContainer()) {3 registry.start();4 String registryUrl = registry.getRegistryUrl();5 String imageName = registryUrl + "/image:tag";6 DockerClientFactory.instance().client().pullImageCmd(imageName).exec(new PullImageResultCallback()).awaitSuccess();7 ImagePullPolicy policy = new ImagePullPolicy();8 DockerClientFactory.instance().client().pullImageCmd(imageName).withTag("tag").withImagePullPolicy(policy).exec(new PullImageResultCallback()).awaitSuccess();9 Assert.assertTrue(policy.pullImage);10 Assert.assertFalse(policy.pullImage);11 }12 }13}
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!!