-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Image substitution #3102
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
Image substitution #3102
Changes from all commits
Commits
Show all changes
18 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 51c320e
Merge remote-tracking branch 'origin/master' into image-substitutor
rnorth b02c734
Remove extraneous change
rnorth 1924759
Un-ignore docs example test by implementing a 'reversing' image name …
rnorth f94089b
Use configuration, not service loader, to select an ImageNameSubstitutor
rnorth 3d08669
Add check for order of config setting precedence
rnorth f8dabd4
Extract classpath scanner and support finding of multiple resources
rnorth 23ac394
Introduce deterministic merging of classpath properties files
rnorth 09e4e7c
Update docs
rnorth d197405
Update docs
rnorth 3cf653b
Remove service loader reference
rnorth 9651ade
Chain substitution through default and configured implementations
rnorth bcd2f76
Small tweaks following review
rnorth 008b036
Fix test compile error
rnorth 6587210
Add UnstableAPI annotation
rnorth 16ef03d
Move TestSpecificImageNameSubstitutor back to original package and re…
rnorth 38f2695
Merge branch 'master' into image-substitutor
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
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
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
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
59 changes: 59 additions & 0 deletions
59
core/src/main/java/org/testcontainers/utility/ClasspathScanner.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,59 @@ | ||
| package org.testcontainers.utility; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.jetbrains.annotations.VisibleForTesting; | ||
|
|
||
| import java.net.URL; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.Objects; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * Utility for identifying resource files on classloaders. | ||
| */ | ||
| @Slf4j | ||
| class ClasspathScanner { | ||
|
|
||
| @VisibleForTesting | ||
| static Stream<URL> scanFor(final String name, ClassLoader... classLoaders) { | ||
| return Stream | ||
| .of(classLoaders) | ||
| .flatMap(classLoader -> getAllPropertyFilesOnClassloader(classLoader, name)) | ||
| .filter(Objects::nonNull) | ||
| .sorted( | ||
| Comparator | ||
| .comparing(ClasspathScanner::filesFileSchemeFirst) // resolve 'local' files first | ||
| .thenComparing(URL::toString) // sort alphabetically for the sake of determinism | ||
| ) | ||
| .distinct(); | ||
| } | ||
|
|
||
| private static Integer filesFileSchemeFirst(final URL t) { | ||
| return t.getProtocol().equals("file") ? 0 : 1; | ||
| } | ||
|
|
||
| /** | ||
| * @param name the resource name to search for | ||
| * @return distinct, ordered stream of resources found by searching this class' classloader and the current thread's | ||
| * context classloader. Results are currently alphabetically sorted. | ||
| */ | ||
| static Stream<URL> scanFor(final String name) { | ||
| return scanFor( | ||
| name, | ||
| ClasspathScanner.class.getClassLoader(), | ||
| Thread.currentThread().getContextClassLoader() | ||
| ); | ||
| } | ||
|
|
||
| @Nullable | ||
| private static Stream<URL> getAllPropertyFilesOnClassloader(final ClassLoader it, final String s) { | ||
| try { | ||
| return Collections.list(it.getResources(s)).stream(); | ||
| } catch (Exception e) { | ||
| log.error("Unable to read configuration from classloader {} - this is probably a bug", it, e); | ||
| return Stream.empty(); | ||
| } | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
core/src/main/java/org/testcontainers/utility/ConfigurationFileImageNameSubstitutor.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,44 @@ | ||
| package org.testcontainers.utility; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| /** | ||
| * {@link ImageNameSubstitutor} which takes replacement image names from configuration. | ||
| * See {@link TestcontainersConfiguration} for the subset of image names which can be substituted using this mechanism. | ||
| */ | ||
| @Slf4j | ||
| final class ConfigurationFileImageNameSubstitutor extends ImageNameSubstitutor { | ||
|
|
||
| private final TestcontainersConfiguration configuration; | ||
|
|
||
| public ConfigurationFileImageNameSubstitutor() { | ||
| this(TestcontainersConfiguration.getInstance()); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| ConfigurationFileImageNameSubstitutor(TestcontainersConfiguration configuration) { | ||
| this.configuration = configuration; | ||
| } | ||
|
|
||
| @Override | ||
| public DockerImageName apply(final DockerImageName original) { | ||
| final DockerImageName result = configuration | ||
| .getConfiguredSubstituteImage(original) | ||
| .asCompatibleSubstituteFor(original); | ||
|
|
||
| if (!result.equals(original)) { | ||
| log.warn("Image name {} was substituted by configuration to {}. This approach is deprecated and will be removed in the future", | ||
| original, | ||
| result | ||
| ); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getDescription() { | ||
| return getClass().getSimpleName(); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
core/src/main/java/org/testcontainers/utility/DefaultImageNameSubstitutor.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,35 @@ | ||
| package org.testcontainers.utility; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| /** | ||
| * Testcontainers' default implementation of {@link ImageNameSubstitutor}. | ||
| * Delegates to {@link ConfigurationFileImageNameSubstitutor}. | ||
| */ | ||
| @Slf4j | ||
| final class DefaultImageNameSubstitutor extends ImageNameSubstitutor { | ||
|
|
||
| private final ConfigurationFileImageNameSubstitutor configurationFileImageNameSubstitutor; | ||
|
|
||
| public DefaultImageNameSubstitutor() { | ||
| configurationFileImageNameSubstitutor = new ConfigurationFileImageNameSubstitutor(); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| DefaultImageNameSubstitutor( | ||
| final ConfigurationFileImageNameSubstitutor configurationFileImageNameSubstitutor | ||
| ) { | ||
| this.configurationFileImageNameSubstitutor = configurationFileImageNameSubstitutor; | ||
| } | ||
|
|
||
| @Override | ||
| public DockerImageName apply(final DockerImageName original) { | ||
| return configurationFileImageNameSubstitutor.apply(original); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getDescription() { | ||
| return "DefaultImageNameSubstitutor (" + configurationFileImageNameSubstitutor.getDescription() + ")"; | ||
| } | ||
| } | ||
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.