-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds a pluggable image substitution mechanism using ServiceLoader, enabling users to perform custom substitution/auditing of images being used by their tests * provides a default implementation that behaves similarly to legacy `TestcontainersConfiguration` approach (`testcontainers.properties`)
- Loading branch information
Showing
27 changed files
with
349 additions
and
123 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
34 changes: 34 additions & 0 deletions
34
core/src/main/java/org/testcontainers/utility/DefaultImageNameSubstitutor.java
This file contains 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,34 @@ | ||
package org.testcontainers.utility; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* TODO: Javadocs | ||
*/ | ||
@Slf4j | ||
public class DefaultImageNameSubstitutor extends ImageNameSubstitutor { | ||
|
||
private final TestcontainersConfiguration configuration; | ||
|
||
public DefaultImageNameSubstitutor() { | ||
this(TestcontainersConfiguration.getInstance()); | ||
} | ||
|
||
@VisibleForTesting | ||
DefaultImageNameSubstitutor(TestcontainersConfiguration configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
@Override | ||
public DockerImageName apply(final DockerImageName original) { | ||
return configuration | ||
.getConfiguredSubstituteImage(original) | ||
.asCompatibleSubstituteFor(original); | ||
} | ||
|
||
@Override | ||
protected int getPriority() { | ||
return 0; | ||
} | ||
} |
This file contains 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
85 changes: 85 additions & 0 deletions
85
core/src/main/java/org/testcontainers/utility/ImageNameSubstitutor.java
This file contains 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,85 @@ | ||
package org.testcontainers.utility; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.ServiceLoader; | ||
import java.util.function.Function; | ||
import java.util.stream.StreamSupport; | ||
|
||
import static java.util.Comparator.comparingInt; | ||
|
||
/** | ||
* TODO: Javadocs | ||
*/ | ||
@Slf4j | ||
public abstract class ImageNameSubstitutor implements Function<DockerImageName, DockerImageName> { | ||
|
||
@VisibleForTesting | ||
static ImageNameSubstitutor instance; | ||
|
||
public synchronized static ImageNameSubstitutor instance() { | ||
if (instance == null) { | ||
final ServiceLoader<ImageNameSubstitutor> serviceLoader = ServiceLoader.load(ImageNameSubstitutor.class); | ||
|
||
instance = StreamSupport.stream(serviceLoader.spliterator(), false) | ||
.peek(it -> log.debug("Found ImageNameSubstitutor using ServiceLoader: {} (priority {}) ", it, it.getPriority())) | ||
.max(comparingInt(ImageNameSubstitutor::getPriority)) | ||
.map(ImageNameSubstitutor::wrapWithLogging) | ||
.orElseThrow(() -> new RuntimeException("Unable to find any ImageNameSubstitutor using ServiceLoader")); | ||
|
||
log.info("Using ImageNameSubstitutor: {}", instance); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
private static ImageNameSubstitutor wrapWithLogging(final ImageNameSubstitutor wrappedInstance) { | ||
return new LogWrappedImageNameSubstitutor(wrappedInstance); | ||
} | ||
|
||
/** | ||
* Substitute a {@link DockerImageName} for another, for example to replace a generic Docker Hub image name with a | ||
* private registry copy of the image. | ||
* | ||
* @param original original name to be replaced | ||
* @return a replacement name, or the original, as appropriate | ||
*/ | ||
public abstract DockerImageName apply(DockerImageName original); | ||
|
||
/** | ||
* Priority of this {@link ImageNameSubstitutor} compared to other instances that may be found by the service | ||
* loader. The highest priority instance found will always be used. | ||
* | ||
* @return a priority | ||
*/ | ||
protected abstract int getPriority(); | ||
|
||
static class LogWrappedImageNameSubstitutor extends ImageNameSubstitutor { | ||
@VisibleForTesting | ||
final ImageNameSubstitutor wrappedInstance; | ||
|
||
public LogWrappedImageNameSubstitutor(final ImageNameSubstitutor wrappedInstance) { | ||
this.wrappedInstance = wrappedInstance; | ||
} | ||
|
||
@Override | ||
public DockerImageName apply(final DockerImageName original) { | ||
final String className = wrappedInstance.getClass().getName(); | ||
final DockerImageName replacementImage = wrappedInstance.apply(original); | ||
|
||
if (!replacementImage.equals(original)) { | ||
log.info("Using {} as a substitute image for {} (using image substitutor: {})", replacementImage.asCanonicalNameString(), original.asCanonicalNameString(), className); | ||
return replacementImage; | ||
} else { | ||
log.debug("Did not find a substitute image for {} (using image substitutor: {})", original.asCanonicalNameString(), className); | ||
return original; | ||
} | ||
} | ||
|
||
@Override | ||
protected int getPriority() { | ||
return wrappedInstance.getPriority(); | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.