Skip to content
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

Accept image names with library/ prefix as a valid substitute #6174

Merged
merged 11 commits into from
Dec 14, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static Map<String, String> markerLabels() {
return Collections.unmodifiableMap(labels);
}

private static final DockerImageName TINY_IMAGE = DockerImageName.parse("alpine:3.16");
private static final DockerImageName TINY_IMAGE = DockerImageName.parse("library/alpine:3.16");
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved

private static DockerClientFactory instance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.testcontainers.utility.Versioning.Sha256Versioning;
import org.testcontainers.utility.Versioning.TagVersioning;

import java.util.Arrays;
import java.util.regex.Pattern;

@EqualsAndHashCode(exclude = { "rawName", "compatibleSubstituteFor" })
Expand All @@ -26,6 +27,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 @@ -258,13 +261,18 @@ public void assertCompatibleWith(DockerImageName... anyOthers) {
throw new IllegalArgumentException("anyOthers parameter must be non-empty");
}

for (DockerImageName anyOther : anyOthers) {
String imageWithoutLibraryPrefix = this.repository.replace(LIBRARY_PREFIX, "");
DockerImageName[] dockerImageNames = Arrays.copyOf(anyOthers, anyOthers.length + 1);
dockerImageNames[dockerImageNames.length - 1] =
DockerImageName.parse(LIBRARY_PREFIX + imageWithoutLibraryPrefix);

for (DockerImageName anyOther : dockerImageNames) {
if (this.isCompatibleWith(anyOther)) {
return;
}
}

final DockerImageName exampleOther = anyOthers[0];
final DockerImageName exampleOther = dockerImageNames[0];

throw new IllegalStateException(
String.format(
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() {
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved
DockerImageName subject = DockerImageName.parse("library/foo");
subject.assertCompatibleWith(DockerImageName.parse("foo"));
}

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