From 797c9caa093b909577ca7b53ff248c0136ca4a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 8 Sep 2023 17:01:39 -0600 Subject: [PATCH 1/9] Allow to define a custom ImagePullPolicy via configuration --- .../images/RemoteDockerImage.java | 33 ++++++++++++++++++- .../utility/TestcontainersConfiguration.java | 4 +++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java b/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java index 8e61ea58013..0a6a0ba19be 100644 --- a/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java +++ b/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java @@ -11,6 +11,7 @@ import lombok.SneakyThrows; import lombok.ToString; import lombok.With; +import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.testcontainers.DockerClientFactory; import org.testcontainers.containers.ContainerFetchException; @@ -18,6 +19,7 @@ import org.testcontainers.utility.DockerLoggerFactory; import org.testcontainers.utility.ImageNameSubstitutor; import org.testcontainers.utility.LazyFuture; +import org.testcontainers.utility.TestcontainersConfiguration; import java.time.Duration; import java.time.Instant; @@ -25,6 +27,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +@Slf4j @ToString @AllArgsConstructor(access = AccessLevel.PACKAGE) public class RemoteDockerImage extends LazyFuture { @@ -35,7 +38,35 @@ public class RemoteDockerImage extends LazyFuture { private Future imageNameFuture; @With - private ImagePullPolicy imagePullPolicy = PullPolicy.defaultPolicy(); + private ImagePullPolicy imagePullPolicy = getImagePullPolicy(); + + private ImagePullPolicy getImagePullPolicy() { + String imagePullPolicyClassName = TestcontainersConfiguration.getInstance().getImagePullPolicy(); + if (imagePullPolicyClassName != null) { + log.debug("Attempting to instantiate an ImagePullPolicy with class: {}", imagePullPolicyClassName); + ImagePullPolicy configuredInstance; + try { + configuredInstance = + (ImagePullPolicy) Thread + .currentThread() + .getContextClassLoader() + .loadClass(imagePullPolicyClassName) + .getConstructor() + .newInstance(); + } catch (Exception e) { + throw new IllegalArgumentException( + "Configured Pull Policy could not be loaded: " + imagePullPolicyClassName, + e + ); + } + + log.info("Found configured Pull Policy: {}", configuredInstance.getClass()); + + return configuredInstance; + } else { + return PullPolicy.defaultPolicy(); + } + } @With private ImageNameSubstitutor imageNameSubstitutor = ImageNameSubstitutor.instance(); diff --git a/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java b/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java index a96f373d454..921e36f80cd 100644 --- a/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java +++ b/core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java @@ -213,6 +213,10 @@ public String getImageSubstitutorClassName() { return getEnvVarOrProperty("image.substitutor", null); } + public String getImagePullPolicy() { + return getEnvVarOrProperty("pull.policy", null); + } + public Integer getClientPingTimeout() { return Integer.parseInt(getEnvVarOrProperty("client.ping.timeout", "10")); } From c75f96ed2f2eefebd1fd04a92de16b8c85d4a8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Sun, 10 Sep 2023 16:12:21 -0600 Subject: [PATCH 2/9] Add test --- .../images/ImagePullPolicyTest.java | 23 +++++++++++++++++++ .../utility/FakeImagePullPolicy.java | 12 ++++++++++ 2 files changed, 35 insertions(+) create mode 100644 core/src/test/java/org/testcontainers/utility/FakeImagePullPolicy.java diff --git a/core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java b/core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java index a885c035e5c..363c115c890 100644 --- a/core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java +++ b/core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java @@ -2,6 +2,7 @@ import com.github.dockerjava.api.exception.NotFoundException; import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; import org.mockito.Mockito; import org.testcontainers.DockerClientFactory; @@ -10,12 +11,19 @@ import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy; import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.FakeImagePullPolicy; +import org.testcontainers.utility.MockTestcontainersConfigurationRule; +import org.testcontainers.utility.TestcontainersConfiguration; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; public class ImagePullPolicyTest { + @Rule + public MockTestcontainersConfigurationRule config = new MockTestcontainersConfigurationRule(); + @ClassRule public static DockerRegistryContainer registry = new DockerRegistryContainer(); @@ -103,6 +111,21 @@ public void shouldNotForcePulling() { } } + @Test + public void simpleConfigurationTest() { + Mockito + .doReturn(FakeImagePullPolicy.class.getCanonicalName()) + .when(TestcontainersConfiguration.getInstance()) + .getImagePullPolicy(); + + try (GenericContainer container = new GenericContainer<>(imageName).withExposedPorts(8080)) { + container.start(); + assertThat(container.getImage()) + .asString() + .contains("imagePullPolicy=org.testcontainers.utility.FakeImagePullPolicy"); + } + } + private void expectToFailWithNotFoundException(GenericContainer container) { try { container.start(); diff --git a/core/src/test/java/org/testcontainers/utility/FakeImagePullPolicy.java b/core/src/test/java/org/testcontainers/utility/FakeImagePullPolicy.java new file mode 100644 index 00000000000..5ecead64d4e --- /dev/null +++ b/core/src/test/java/org/testcontainers/utility/FakeImagePullPolicy.java @@ -0,0 +1,12 @@ +package org.testcontainers.utility; + +import org.testcontainers.images.AbstractImagePullPolicy; +import org.testcontainers.images.ImageData; + +public class FakeImagePullPolicy extends AbstractImagePullPolicy { + + @Override + protected boolean shouldPullCached(DockerImageName imageName, ImageData localImageData) { + return false; + } +} From ec7d825a26986d3bdb0562d8b61fb23d0db85a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 13 Sep 2023 14:32:12 -0600 Subject: [PATCH 3/9] Move implementation to PullPolicy#defaultPolicy --- .../org/testcontainers/images/PullPolicy.java | 29 +++++++++++++++- .../images/RemoteDockerImage.java | 33 +------------------ 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/org/testcontainers/images/PullPolicy.java b/core/src/main/java/org/testcontainers/images/PullPolicy.java index a8b8344a8c2..d7aba25193a 100644 --- a/core/src/main/java/org/testcontainers/images/PullPolicy.java +++ b/core/src/main/java/org/testcontainers/images/PullPolicy.java @@ -1,6 +1,8 @@ package org.testcontainers.images; import lombok.experimental.UtilityClass; +import lombok.extern.slf4j.Slf4j; +import org.testcontainers.utility.TestcontainersConfiguration; import java.time.Duration; @@ -8,6 +10,7 @@ * Convenience class with logic for building common {@link ImagePullPolicy} instances. * */ +@Slf4j @UtilityClass public class PullPolicy { @@ -16,7 +19,31 @@ public class PullPolicy { * @return {@link ImagePullPolicy} */ public static ImagePullPolicy defaultPolicy() { - return new DefaultPullPolicy(); + String imagePullPolicyClassName = TestcontainersConfiguration.getInstance().getImagePullPolicy(); + if (imagePullPolicyClassName != null) { + log.debug("Attempting to instantiate an ImagePullPolicy with class: {}", imagePullPolicyClassName); + ImagePullPolicy configuredInstance; + try { + configuredInstance = + (ImagePullPolicy) Thread + .currentThread() + .getContextClassLoader() + .loadClass(imagePullPolicyClassName) + .getConstructor() + .newInstance(); + } catch (Exception e) { + throw new IllegalArgumentException( + "Configured Pull Policy could not be loaded: " + imagePullPolicyClassName, + e + ); + } + + log.info("Found configured Pull Policy: {}", configuredInstance.getClass()); + + return configuredInstance; + } else { + return new DefaultPullPolicy(); + } } /** diff --git a/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java b/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java index 0a6a0ba19be..8e61ea58013 100644 --- a/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java +++ b/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java @@ -11,7 +11,6 @@ import lombok.SneakyThrows; import lombok.ToString; import lombok.With; -import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.testcontainers.DockerClientFactory; import org.testcontainers.containers.ContainerFetchException; @@ -19,7 +18,6 @@ import org.testcontainers.utility.DockerLoggerFactory; import org.testcontainers.utility.ImageNameSubstitutor; import org.testcontainers.utility.LazyFuture; -import org.testcontainers.utility.TestcontainersConfiguration; import java.time.Duration; import java.time.Instant; @@ -27,7 +25,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -@Slf4j @ToString @AllArgsConstructor(access = AccessLevel.PACKAGE) public class RemoteDockerImage extends LazyFuture { @@ -38,35 +35,7 @@ public class RemoteDockerImage extends LazyFuture { private Future imageNameFuture; @With - private ImagePullPolicy imagePullPolicy = getImagePullPolicy(); - - private ImagePullPolicy getImagePullPolicy() { - String imagePullPolicyClassName = TestcontainersConfiguration.getInstance().getImagePullPolicy(); - if (imagePullPolicyClassName != null) { - log.debug("Attempting to instantiate an ImagePullPolicy with class: {}", imagePullPolicyClassName); - ImagePullPolicy configuredInstance; - try { - configuredInstance = - (ImagePullPolicy) Thread - .currentThread() - .getContextClassLoader() - .loadClass(imagePullPolicyClassName) - .getConstructor() - .newInstance(); - } catch (Exception e) { - throw new IllegalArgumentException( - "Configured Pull Policy could not be loaded: " + imagePullPolicyClassName, - e - ); - } - - log.info("Found configured Pull Policy: {}", configuredInstance.getClass()); - - return configuredInstance; - } else { - return PullPolicy.defaultPolicy(); - } - } + private ImagePullPolicy imagePullPolicy = PullPolicy.defaultPolicy(); @With private ImageNameSubstitutor imageNameSubstitutor = ImageNameSubstitutor.instance(); From 07910056a4c41008631f2bf0f3160e3c847a4a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 13 Sep 2023 15:03:43 -0600 Subject: [PATCH 4/9] Cache ImagePullPolicy --- .../org/testcontainers/images/PullPolicy.java | 50 +++++++++++-------- .../images/OverrideImagePullPolicyTest.java | 36 +++++++++++++ 2 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java diff --git a/core/src/main/java/org/testcontainers/images/PullPolicy.java b/core/src/main/java/org/testcontainers/images/PullPolicy.java index d7aba25193a..7403b856f7f 100644 --- a/core/src/main/java/org/testcontainers/images/PullPolicy.java +++ b/core/src/main/java/org/testcontainers/images/PullPolicy.java @@ -14,36 +14,42 @@ @UtilityClass public class PullPolicy { + private static ImagePullPolicy IMAGE_PULL_POLICY; + /** * Convenience method for returning the {@link DefaultPullPolicy} default image pull policy * @return {@link ImagePullPolicy} */ public static ImagePullPolicy defaultPolicy() { - String imagePullPolicyClassName = TestcontainersConfiguration.getInstance().getImagePullPolicy(); - if (imagePullPolicyClassName != null) { - log.debug("Attempting to instantiate an ImagePullPolicy with class: {}", imagePullPolicyClassName); - ImagePullPolicy configuredInstance; - try { - configuredInstance = - (ImagePullPolicy) Thread - .currentThread() - .getContextClassLoader() - .loadClass(imagePullPolicyClassName) - .getConstructor() - .newInstance(); - } catch (Exception e) { - throw new IllegalArgumentException( - "Configured Pull Policy could not be loaded: " + imagePullPolicyClassName, - e - ); - } + if (IMAGE_PULL_POLICY == null) { + String imagePullPolicyClassName = TestcontainersConfiguration.getInstance().getImagePullPolicy(); + if (imagePullPolicyClassName != null) { + log.debug("Attempting to instantiate an ImagePullPolicy with class: {}", imagePullPolicyClassName); + ImagePullPolicy configuredInstance; + try { + configuredInstance = + (ImagePullPolicy) Thread + .currentThread() + .getContextClassLoader() + .loadClass(imagePullPolicyClassName) + .getConstructor() + .newInstance(); + } catch (Exception e) { + throw new IllegalArgumentException( + "Configured Pull Policy could not be loaded: " + imagePullPolicyClassName, + e + ); + } - log.info("Found configured Pull Policy: {}", configuredInstance.getClass()); + log.info("Found configured Pull Policy: {}", configuredInstance.getClass()); - return configuredInstance; - } else { - return new DefaultPullPolicy(); + IMAGE_PULL_POLICY = configuredInstance; + } else { + IMAGE_PULL_POLICY = new DefaultPullPolicy(); + } } + + return IMAGE_PULL_POLICY; } /** diff --git a/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java b/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java new file mode 100644 index 00000000000..ebb05a9ef19 --- /dev/null +++ b/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java @@ -0,0 +1,36 @@ +package org.testcontainers.images; + +import org.junit.Rule; +import org.junit.Test; +import org.mockito.Mockito; +import org.testcontainers.DockerRegistryContainer; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.utility.FakeImagePullPolicy; +import org.testcontainers.utility.MockTestcontainersConfigurationRule; +import org.testcontainers.utility.TestcontainersConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; + +public class OverrideImagePullPolicyTest { + + @Rule + public MockTestcontainersConfigurationRule config = new MockTestcontainersConfigurationRule(); + + @Test + public void simpleConfigurationTest() { + Mockito + .doReturn(FakeImagePullPolicy.class.getCanonicalName()) + .when(TestcontainersConfiguration.getInstance()) + .getImagePullPolicy(); + + try (DockerRegistryContainer registry = new DockerRegistryContainer()) { + registry.start(); + GenericContainer container = new GenericContainer<>(registry.createImage()).withExposedPorts(8080); + container.start(); + assertThat(container.getImage()) + .asString() + .contains("imagePullPolicy=org.testcontainers.utility.FakeImagePullPolicy"); + container.stop(); + } + } +} From 9e0703a9b4fed5e80042a8bf180c14f3e4b741f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 13 Sep 2023 15:48:06 -0600 Subject: [PATCH 5/9] cleanup test --- .../images/ImagePullPolicyTest.java | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java b/core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java index 363c115c890..a885c035e5c 100644 --- a/core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java +++ b/core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java @@ -2,7 +2,6 @@ import com.github.dockerjava.api.exception.NotFoundException; import org.junit.ClassRule; -import org.junit.Rule; import org.junit.Test; import org.mockito.Mockito; import org.testcontainers.DockerClientFactory; @@ -11,19 +10,12 @@ import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy; import org.testcontainers.utility.DockerImageName; -import org.testcontainers.utility.FakeImagePullPolicy; -import org.testcontainers.utility.MockTestcontainersConfigurationRule; -import org.testcontainers.utility.TestcontainersConfiguration; -import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; public class ImagePullPolicyTest { - @Rule - public MockTestcontainersConfigurationRule config = new MockTestcontainersConfigurationRule(); - @ClassRule public static DockerRegistryContainer registry = new DockerRegistryContainer(); @@ -111,21 +103,6 @@ public void shouldNotForcePulling() { } } - @Test - public void simpleConfigurationTest() { - Mockito - .doReturn(FakeImagePullPolicy.class.getCanonicalName()) - .when(TestcontainersConfiguration.getInstance()) - .getImagePullPolicy(); - - try (GenericContainer container = new GenericContainer<>(imageName).withExposedPorts(8080)) { - container.start(); - assertThat(container.getImage()) - .asString() - .contains("imagePullPolicy=org.testcontainers.utility.FakeImagePullPolicy"); - } - } - private void expectToFailWithNotFoundException(GenericContainer container) { try { container.start(); From 843abf6aa8c4d1e0a694176b32a4d4a2427675f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 13 Sep 2023 16:53:23 -0600 Subject: [PATCH 6/9] Fix test --- .../org/testcontainers/images/PullPolicy.java | 15 +++++++++----- .../images/OverrideImagePullPolicyTest.java | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/testcontainers/images/PullPolicy.java b/core/src/main/java/org/testcontainers/images/PullPolicy.java index 7403b856f7f..00576ca2c46 100644 --- a/core/src/main/java/org/testcontainers/images/PullPolicy.java +++ b/core/src/main/java/org/testcontainers/images/PullPolicy.java @@ -1,5 +1,6 @@ package org.testcontainers.images; +import com.google.common.annotations.VisibleForTesting; import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; import org.testcontainers.utility.TestcontainersConfiguration; @@ -14,14 +15,18 @@ @UtilityClass public class PullPolicy { - private static ImagePullPolicy IMAGE_PULL_POLICY; + @VisibleForTesting + static ImagePullPolicy instance; + + @VisibleForTesting + static ImagePullPolicy defaultImplementation = new DefaultPullPolicy(); /** * Convenience method for returning the {@link DefaultPullPolicy} default image pull policy * @return {@link ImagePullPolicy} */ public static ImagePullPolicy defaultPolicy() { - if (IMAGE_PULL_POLICY == null) { + if (instance == null) { String imagePullPolicyClassName = TestcontainersConfiguration.getInstance().getImagePullPolicy(); if (imagePullPolicyClassName != null) { log.debug("Attempting to instantiate an ImagePullPolicy with class: {}", imagePullPolicyClassName); @@ -43,13 +48,13 @@ public static ImagePullPolicy defaultPolicy() { log.info("Found configured Pull Policy: {}", configuredInstance.getClass()); - IMAGE_PULL_POLICY = configuredInstance; + instance = configuredInstance; } else { - IMAGE_PULL_POLICY = new DefaultPullPolicy(); + instance = defaultImplementation; } } - return IMAGE_PULL_POLICY; + return instance; } /** diff --git a/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java b/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java index ebb05a9ef19..c7bda04d609 100644 --- a/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java +++ b/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java @@ -1,5 +1,7 @@ package org.testcontainers.images; +import org.junit.After; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.mockito.Mockito; @@ -16,6 +18,24 @@ public class OverrideImagePullPolicyTest { @Rule public MockTestcontainersConfigurationRule config = new MockTestcontainersConfigurationRule(); + private ImagePullPolicy originalInstance; + + private ImagePullPolicy originalDefaultImplementation; + + @Before + public void setUp() { + this.originalInstance = PullPolicy.instance; + this.originalDefaultImplementation = PullPolicy.defaultImplementation; + PullPolicy.instance = null; + PullPolicy.defaultImplementation = Mockito.mock(ImagePullPolicy.class); + } + + @After + public void tearDown() { + PullPolicy.instance = originalInstance; + PullPolicy.defaultImplementation = originalDefaultImplementation; + } + @Test public void simpleConfigurationTest() { Mockito From 9a652b7607f254cae29023482677c408fb7814cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Thu, 14 Sep 2023 11:19:45 -0600 Subject: [PATCH 7/9] Fix comments --- .../src/main/java/org/testcontainers/images/PullPolicy.java | 6 ++++-- .../java/org/testcontainers/images/RemoteDockerImage.java | 2 +- .../testcontainers/images/OverrideImagePullPolicyTest.java | 4 +--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/testcontainers/images/PullPolicy.java b/core/src/main/java/org/testcontainers/images/PullPolicy.java index 00576ca2c46..5f13fe2785f 100644 --- a/core/src/main/java/org/testcontainers/images/PullPolicy.java +++ b/core/src/main/java/org/testcontainers/images/PullPolicy.java @@ -41,17 +41,19 @@ public static ImagePullPolicy defaultPolicy() { .newInstance(); } catch (Exception e) { throw new IllegalArgumentException( - "Configured Pull Policy could not be loaded: " + imagePullPolicyClassName, + "Configured ImagePullPolicy could not be loaded: " + imagePullPolicyClassName, e ); } - log.info("Found configured Pull Policy: {}", configuredInstance.getClass()); + log.info("Found configured Image Pull Policy: {}", configuredInstance.getClass()); instance = configuredInstance; } else { instance = defaultImplementation; } + + log.info("Image pull policy will be performed by: {}", instance); } return instance; diff --git a/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java b/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java index 8e61ea58013..78a0e2a583c 100644 --- a/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java +++ b/core/src/main/java/org/testcontainers/images/RemoteDockerImage.java @@ -35,7 +35,7 @@ public class RemoteDockerImage extends LazyFuture { private Future imageNameFuture; @With - private ImagePullPolicy imagePullPolicy = PullPolicy.defaultPolicy(); + ImagePullPolicy imagePullPolicy = PullPolicy.defaultPolicy(); @With private ImageNameSubstitutor imageNameSubstitutor = ImageNameSubstitutor.instance(); diff --git a/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java b/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java index c7bda04d609..3b410fd5ab9 100644 --- a/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java +++ b/core/src/test/java/org/testcontainers/images/OverrideImagePullPolicyTest.java @@ -47,9 +47,7 @@ public void simpleConfigurationTest() { registry.start(); GenericContainer container = new GenericContainer<>(registry.createImage()).withExposedPorts(8080); container.start(); - assertThat(container.getImage()) - .asString() - .contains("imagePullPolicy=org.testcontainers.utility.FakeImagePullPolicy"); + assertThat(container.getImage().imagePullPolicy).isInstanceOf(FakeImagePullPolicy.class); container.stop(); } } From 7c9dcdd6134cc4b9d6476315d658c41ef7de70fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 27 Sep 2023 09:22:38 -0600 Subject: [PATCH 8/9] Fix comments --- .../org/testcontainers/images/PullPolicy.java | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/org/testcontainers/images/PullPolicy.java b/core/src/main/java/org/testcontainers/images/PullPolicy.java index 5f13fe2785f..12d05b6fe5a 100644 --- a/core/src/main/java/org/testcontainers/images/PullPolicy.java +++ b/core/src/main/java/org/testcontainers/images/PullPolicy.java @@ -25,37 +25,39 @@ public class PullPolicy { * Convenience method for returning the {@link DefaultPullPolicy} default image pull policy * @return {@link ImagePullPolicy} */ - public static ImagePullPolicy defaultPolicy() { - if (instance == null) { - String imagePullPolicyClassName = TestcontainersConfiguration.getInstance().getImagePullPolicy(); - if (imagePullPolicyClassName != null) { - log.debug("Attempting to instantiate an ImagePullPolicy with class: {}", imagePullPolicyClassName); - ImagePullPolicy configuredInstance; - try { - configuredInstance = - (ImagePullPolicy) Thread - .currentThread() - .getContextClassLoader() - .loadClass(imagePullPolicyClassName) - .getConstructor() - .newInstance(); - } catch (Exception e) { - throw new IllegalArgumentException( - "Configured ImagePullPolicy could not be loaded: " + imagePullPolicyClassName, - e - ); - } - - log.info("Found configured Image Pull Policy: {}", configuredInstance.getClass()); + public static synchronized ImagePullPolicy defaultPolicy() { + if (instance != null) { + return instance; + } - instance = configuredInstance; - } else { - instance = defaultImplementation; + String imagePullPolicyClassName = TestcontainersConfiguration.getInstance().getImagePullPolicy(); + if (imagePullPolicyClassName != null) { + log.debug("Attempting to instantiate an ImagePullPolicy with class: {}", imagePullPolicyClassName); + ImagePullPolicy configuredInstance; + try { + configuredInstance = + (ImagePullPolicy) Thread + .currentThread() + .getContextClassLoader() + .loadClass(imagePullPolicyClassName) + .getConstructor() + .newInstance(); + } catch (Exception e) { + throw new IllegalArgumentException( + "Configured ImagePullPolicy could not be loaded: " + imagePullPolicyClassName, + e + ); } - log.info("Image pull policy will be performed by: {}", instance); + log.info("Found configured Image Pull Policy: {}", configuredInstance.getClass()); + + instance = configuredInstance; + } else { + instance = defaultImplementation; } + log.info("Image pull policy will be performed by: {}", instance); + return instance; } From 90216433ab0c53ac86294f3854249de347a346e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 27 Sep 2023 09:54:29 -0600 Subject: [PATCH 9/9] Docs --- docs/features/advanced_options.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/features/advanced_options.md b/docs/features/advanced_options.md index cd9c57f99eb..9dd86edccd5 100644 --- a/docs/features/advanced_options.md +++ b/docs/features/advanced_options.md @@ -31,6 +31,15 @@ It is possible to specify an Image Pull Policy to determine at runtime whether a [Custom image pull policy](../../core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java) inside_block:custom_image_pull_policy +You can also configure Testcontainers to use your custom implementation by using `pull.policy` + +=== "`src/test/resources/testcontainers.properties`" + ```text + pull.policy=com.mycompany.testcontainers.ExampleImagePullPolicy + ``` + +Please see [the documentation on configuration mechanisms](./configuration.md) for more information. + ## Customizing the container ### Using docker-java