Skip to content

Commit

Permalink
Accept image names with library/ prefix as a valid substitute (#6174)
Browse files Browse the repository at this point in the history
Docker official images can be pulled with or without `library/`
prefix as part of the image name. For example, `mysql` or
`library/mysql`. This is explicit when using private repositories.

Co-authored-by: Kevin Wittek <[email protected]>
  • Loading branch information
eddumelendez and kiview authored Dec 14, 2022
1 parent 221a8da commit 3b24a7d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions core/src/main/java/org/testcontainers/utility/DockerImageName.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public final class DockerImageName {

private static final Pattern REPO_NAME = Pattern.compile(REPO_NAME_PART + "(/" + REPO_NAME_PART + ")*");

private static final String LIBRARY_PREFIX = "library/";

private final String rawName;

@With
Expand Down Expand Up @@ -231,8 +233,15 @@ public DockerImageName asCompatibleSubstituteFor(DockerImageName otherImageName)
* @return whether this image has declared compatibility.
*/
public boolean isCompatibleWith(DockerImageName other) {
// is this image already the same or equivalent?
if (other.equals(this)) {
// Make sure we always compare against a version of the image name containing the LIBRARY_PREFIX
String finalImageName;
if (this.repository.startsWith(LIBRARY_PREFIX)) {
finalImageName = this.repository;
} else {
finalImageName = LIBRARY_PREFIX + this.repository;
}
DockerImageName imageWithLibraryPrefix = DockerImageName.parse(finalImageName);
if (other.equals(this) || imageWithLibraryPrefix.equals(this)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public void testAssertMethodAcceptsCompatible() {
subject.assertCompatibleWith(DockerImageName.parse("bar"));
}

@Test
public void testAssertMethodAcceptsCompatibleLibraryPrefix() {
DockerImageName subject = DockerImageName.parse("library/foo");
subject.assertCompatibleWith(DockerImageName.parse("foo"));
}

@Test
public void testAssertMethodRejectsIncompatible() {
DockerImageName subject = DockerImageName.parse("foo");
Expand Down

0 comments on commit 3b24a7d

Please sign in to comment.