-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Default prefixing image substitutor #3413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0f51dfa
Refactor Testcontainers configuration to allow config by env var
rnorth 950af34
Add Image substitution mechanism
rnorth 58179b3
Add Image substitution mechanism
rnorth 658c98a
Improve default image name substitution
rnorth 8325872
Merge remote-tracking branch 'origin/master' into default-prefixing-i…
rnorth 9bf3097
Rework - WIP
rnorth 2176ef8
Provisional changes - single config setting, applied to Hub image nam…
rnorth 8565f7f
Tidy up merge issue
rnorth 45ac5f4
Tidy up merge issue
rnorth 73d2291
Tidy up merge issue
rnorth b63f2a8
Merge branch 'master' into default-prefixing-image-substitutor
rnorth c7c0a09
Remove excess para
rnorth b845b8a
Merge remote-tracking branch 'origin/default-prefixing-image-substitu…
rnorth a7aced5
Merge branch 'master' into default-prefixing-image-substitutor
rnorth 0d706f3
Update core/src/main/java/org/testcontainers/utility/PrefixingImageNa…
rnorth 6370a63
Simplify Hub image detection
rnorth 714f482
Update test to reflect changed hub identification logic
rnorth abdcd93
Add tests that the prefix is applied literally (both for slash-presen…
rnorth cd63ec6
Remove duplicate getter (Lombok @Getter exists)
rnorth 9984aa2
Update docs/features/image_name_substitution.md
rnorth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
core/src/main/java/org/testcontainers/utility/PrefixingImageNameSubstitutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package org.testcontainers.utility; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| /** | ||
| * An {@link ImageNameSubstitutor} which applies a prefix to all image names, e.g. a private registry host and path. | ||
| * The prefix may be set via an environment variable (<code>TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX</code>) or an equivalent | ||
| * configuration file entry (see {@link TestcontainersConfiguration}). | ||
| */ | ||
| @NoArgsConstructor | ||
| @Slf4j | ||
| final class PrefixingImageNameSubstitutor extends ImageNameSubstitutor { | ||
|
|
||
| @VisibleForTesting | ||
| static final String PREFIX_PROPERTY_KEY = "hub.image.name.prefix"; | ||
|
|
||
| private TestcontainersConfiguration configuration = TestcontainersConfiguration.getInstance(); | ||
|
|
||
| @VisibleForTesting | ||
| PrefixingImageNameSubstitutor(final TestcontainersConfiguration configuration) { | ||
| this.configuration = configuration; | ||
| } | ||
|
|
||
| @Override | ||
| public DockerImageName apply(DockerImageName original) { | ||
| final String configuredPrefix = configuration.getEnvVarOrProperty(PREFIX_PROPERTY_KEY, ""); | ||
|
|
||
| if (configuredPrefix.isEmpty()) { | ||
| log.debug("No prefix is configured"); | ||
| return original; | ||
| } | ||
|
|
||
| boolean isAHubImage = original.getRegistry().isEmpty(); | ||
| if (!isAHubImage) { | ||
| log.debug("Image {} is not a Docker Hub image - not applying registry/repository change", original); | ||
| return original; | ||
| } | ||
|
|
||
| log.debug( | ||
| "Applying changes to image name {}: applying prefix '{}'", | ||
| original, | ||
| configuredPrefix | ||
| ); | ||
|
|
||
| DockerImageName prefixAsImage = DockerImageName.parse(configuredPrefix); | ||
|
|
||
| return original | ||
| .withRegistry(prefixAsImage.getRegistry()) | ||
| .withRepository(prefixAsImage.getRepository() + original.getRepository()); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getDescription() { | ||
| return getClass().getSimpleName(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
core/src/main/resources/META-INF/services/org.testcontainers.utility.ImageNameSubstitutor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| org.testcontainers.utility.DefaultImageNameSubstitutor |
8 changes: 4 additions & 4 deletions
8
core/src/test/java/org/testcontainers/utility/DockerImageNameCompatibilityTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
core/src/test/java/org/testcontainers/utility/PrefixingImageNameSubstitutorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package org.testcontainers.utility; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; | ||
| import static org.testcontainers.utility.PrefixingImageNameSubstitutor.PREFIX_PROPERTY_KEY; | ||
|
|
||
| public class PrefixingImageNameSubstitutorTest { | ||
|
|
||
| private TestcontainersConfiguration mockConfiguration; | ||
| private PrefixingImageNameSubstitutor underTest; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| mockConfiguration = mock(TestcontainersConfiguration.class); | ||
| underTest = new PrefixingImageNameSubstitutor(mockConfiguration); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHappyPath() { | ||
| when(mockConfiguration.getEnvVarOrProperty(eq(PREFIX_PROPERTY_KEY), any())).thenReturn("someregistry.com/our-mirror/"); | ||
|
|
||
| final DockerImageName result = underTest.apply(DockerImageName.parse("some/image:tag")); | ||
|
bsideup marked this conversation as resolved.
|
||
|
|
||
| assertEquals( | ||
| "The prefix is applied", | ||
| "someregistry.com/our-mirror/some/image:tag", | ||
| result.asCanonicalNameString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void hubIoRegistryIsNotChanged() { | ||
| when(mockConfiguration.getEnvVarOrProperty(eq(PREFIX_PROPERTY_KEY), any())).thenReturn("someregistry.com/our-mirror/"); | ||
|
|
||
| final DockerImageName result = underTest.apply(DockerImageName.parse("docker.io/some/image:tag")); | ||
|
|
||
| assertEquals( | ||
| "The prefix is applied", | ||
| "docker.io/some/image:tag", | ||
| result.asCanonicalNameString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void hubComRegistryIsNotChanged() { | ||
| when(mockConfiguration.getEnvVarOrProperty(eq(PREFIX_PROPERTY_KEY), any())).thenReturn("someregistry.com/our-mirror/"); | ||
|
|
||
| final DockerImageName result = underTest.apply(DockerImageName.parse("registry.hub.docker.com/some/image:tag")); | ||
|
|
||
| assertEquals( | ||
| "The prefix is applied", | ||
| "registry.hub.docker.com/some/image:tag", | ||
| result.asCanonicalNameString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void thirdPartyRegistriesNotAffected() { | ||
| when(mockConfiguration.getEnvVarOrProperty(eq(PREFIX_PROPERTY_KEY), any())).thenReturn("someregistry.com/our-mirror/"); | ||
|
|
||
| final DockerImageName result = underTest.apply(DockerImageName.parse("gcr.io/something/image:tag")); | ||
|
|
||
| assertEquals( | ||
| "The prefix is not applied if a third party registry is used", | ||
| "gcr.io/something/image:tag", | ||
| result.asCanonicalNameString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNoDoublePrefixing() { | ||
| when(mockConfiguration.getEnvVarOrProperty(eq(PREFIX_PROPERTY_KEY), any())).thenReturn("someregistry.com/our-mirror/"); | ||
|
|
||
| final DockerImageName result = underTest.apply(DockerImageName.parse("someregistry.com/some/image:tag")); | ||
|
|
||
| assertEquals( | ||
| "The prefix is not applied if already present", | ||
| "someregistry.com/some/image:tag", | ||
| result.asCanonicalNameString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHandlesEmptyValue() { | ||
| when(mockConfiguration.getEnvVarOrProperty(eq(PREFIX_PROPERTY_KEY), any())).thenReturn(""); | ||
|
|
||
| final DockerImageName result = underTest.apply(DockerImageName.parse("some/image:tag")); | ||
|
|
||
| assertEquals( | ||
| "The prefix is not applied if the env var is not set", | ||
| "some/image:tag", | ||
| result.asCanonicalNameString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHandlesRegistryOnlyWithTrailingSlash() { | ||
| when(mockConfiguration.getEnvVarOrProperty(eq(PREFIX_PROPERTY_KEY), any())).thenReturn("someregistry.com/"); | ||
|
|
||
| final DockerImageName result = underTest.apply(DockerImageName.parse("some/image:tag")); | ||
|
|
||
| assertEquals( | ||
| "The prefix is applied", | ||
| "someregistry.com/some/image:tag", | ||
| result.asCanonicalNameString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCombinesLiterallyForRegistryOnlyWithoutTrailingSlash() { | ||
| when(mockConfiguration.getEnvVarOrProperty(eq(PREFIX_PROPERTY_KEY), any())).thenReturn("someregistry.com"); | ||
|
|
||
| final DockerImageName result = underTest.apply(DockerImageName.parse("some/image:tag")); | ||
|
|
||
| assertEquals( | ||
| "The prefix is applied", | ||
| "someregistry.comsome/image:tag", // treating the prefix literally, for predictability | ||
| result.asCanonicalNameString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCombinesLiterallyForBothPartsWithoutTrailingSlash() { | ||
| when(mockConfiguration.getEnvVarOrProperty(eq(PREFIX_PROPERTY_KEY), any())).thenReturn("someregistry.com/our-mirror"); | ||
|
|
||
| final DockerImageName result = underTest.apply(DockerImageName.parse("some/image:tag")); | ||
|
|
||
| assertEquals( | ||
| "The prefix is applied", | ||
| "someregistry.com/our-mirrorsome/image:tag", // treating the prefix literally, for predictability | ||
| result.asCanonicalNameString() | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.