Skip to content

Commit b60d19c

Browse files
committed
WIP
1 parent 058f6fe commit b60d19c

File tree

35 files changed

+290
-92
lines changed

35 files changed

+290
-92
lines changed

core/src/main/java/org/testcontainers/containers/FixedHostPortGenericContainer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* not available - which could manifest as flaky or unstable tests.</p>
1313
*/
1414
public class FixedHostPortGenericContainer<SELF extends FixedHostPortGenericContainer<SELF>> extends GenericContainer<SELF> {
15+
@Deprecated
1516
public FixedHostPortGenericContainer(@NotNull String dockerImageName) {
1617
super(dockerImageName);
1718
}

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
import java.util.Map.Entry;
8686
import java.util.Optional;
8787
import java.util.Set;
88-
import java.util.concurrent.CompletableFuture;
8988
import java.util.concurrent.ExecutionException;
9089
import java.util.concurrent.Future;
9190
import java.util.concurrent.TimeUnit;
@@ -243,6 +242,7 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
243242
@Setter(AccessLevel.NONE)
244243
private boolean shouldBeReused = false;
245244

245+
@Deprecated
246246
public GenericContainer() {
247247
this(TestcontainersConfiguration.getInstance().getTinyImage());
248248
}
@@ -251,21 +251,15 @@ public GenericContainer(DockerImageName dockerImageName) {
251251
this.image = new RemoteDockerImage(dockerImageName);
252252
}
253253

254-
public GenericContainer(CompletableFuture<DockerImageName> futureDockerImageName) {
255-
this.image = new RemoteDockerImage(futureDockerImageName);
256-
}
257-
258254
@Deprecated
259255
public GenericContainer(@NonNull final String dockerImageName) {
260256
this.setDockerImageName(dockerImageName);
261257
}
262258

263-
@Deprecated
264259
public GenericContainer(@NonNull final Future<String> image) {
265260
setImage(image);
266261
}
267262

268-
@Deprecated
269263
public void setImage(Future<String> image) {
270264
this.image = new RemoteDockerImage(image);
271265
}

core/src/main/java/org/testcontainers/containers/SocatContainer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.testcontainers.containers;
22

33
import org.testcontainers.utility.Base58;
4+
import org.testcontainers.utility.DockerImageName;
45
import org.testcontainers.utility.TestcontainersConfiguration;
56

67
import java.util.HashMap;
@@ -15,8 +16,13 @@ public class SocatContainer extends GenericContainer<SocatContainer> {
1516

1617
private final Map<Integer, String> targets = new HashMap<>();
1718

19+
@Deprecated
1820
public SocatContainer() {
19-
super(TestcontainersConfiguration.getInstance().getSocatContainerImage());
21+
this(DockerImageName.of(TestcontainersConfiguration.getInstance().getSocatContainerImage()));
22+
}
23+
24+
public SocatContainer(final DockerImageName dockerImageName) {
25+
super(dockerImageName);
2026
withCreateContainerCmdModifier(it -> it.withEntrypoint("/bin/sh"));
2127
withCreateContainerCmdModifier(it -> it.withName("testcontainers-socat-" + Base58.randomString(8)));
2228
}
@@ -39,4 +45,4 @@ protected void configure() {
3945
.collect(Collectors.joining(" & "))
4046
);
4147
}
42-
}
48+
}

core/src/main/java/org/testcontainers/images/RemoteDockerImage.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ public RemoteDockerImage(DockerImageName dockerImageName) {
4242
this.imageNameFuture = CompletableFuture.completedFuture(dockerImageName);
4343
}
4444

45-
public RemoteDockerImage(CompletableFuture<DockerImageName> futureDockerImageName) {
46-
this.imageNameFuture = futureDockerImageName;
47-
}
48-
4945
@Deprecated
5046
public RemoteDockerImage(String dockerImageName) {
5147
this.imageNameFuture = CompletableFuture.completedFuture(new DockerImageName(dockerImageName));
@@ -56,7 +52,6 @@ public RemoteDockerImage(@NonNull String repository, @NonNull String tag) {
5652
this.imageNameFuture = CompletableFuture.completedFuture(new DockerImageName(repository, tag));
5753
}
5854

59-
@Deprecated
6055
public RemoteDockerImage(@NonNull Future<String> imageFuture) {
6156
this.imageNameFuture = Futures.lazyTransform(imageFuture, DockerImageName::new);
6257
}

core/src/main/java/org/testcontainers/utility/DockerImageName.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ public final class DockerImageName {
2424
private final String repo;
2525
private final Versioning versioning;
2626

27+
/**
28+
* Parses a docker image name from a provided string.
29+
*
30+
* @param fullImageName in standard Docker format, e.g. <code>name:tag</code>,
31+
* <code>some.registry/path/name:tag</code>,
32+
* <code>some.registry/path/name@sha256:abcdef...</code>, etc.
33+
* @return a parsed representation of the image name
34+
*/
35+
public static DockerImageName of(final String fullImageName) {
36+
return new DockerImageName(fullImageName);
37+
}
38+
39+
/**
40+
* @deprecated use {@link DockerImageName#of} instead
41+
*/
2742
@Deprecated
2843
public DockerImageName(String name) {
2944
this.rawName = name;
@@ -53,6 +68,9 @@ public DockerImageName(String name) {
5368
}
5469
}
5570

71+
/**
72+
* @deprecated use {@link DockerImageName#of} instead, optionally in conjunction with {@link DockerImageName#withTag(String)}
73+
*/
5674
@Deprecated
5775
public DockerImageName(String name, String tag) {
5876
this.rawName = name;
@@ -79,10 +97,6 @@ public DockerImageName(String name, String tag) {
7997
}
8098
}
8199

82-
public static DockerImageName of(final String fullImageName) {
83-
return new DockerImageName(fullImageName);
84-
}
85-
86100
/**
87101
* @return the unversioned (non 'tag') part of this name
88102
*/
@@ -139,6 +153,7 @@ public DockerImageName withTag(final String newTag) {
139153

140154
private interface Versioning {
141155
boolean isValid();
156+
142157
String getSeparator();
143158
}
144159

@@ -168,7 +183,7 @@ public String toString() {
168183
}
169184

170185
@Data
171-
private class Sha256Versioning implements Versioning {
186+
private static class Sha256Versioning implements Versioning {
172187
public static final String HASH_REGEX = "[0-9a-fA-F]{32,}";
173188
private final String hash;
174189

core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.testcontainers.containers.GenericContainer;
2626
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
2727
import org.testcontainers.utility.Base58;
28+
import org.testcontainers.utility.DockerImageName;
2829
import org.testcontainers.utility.TestEnvironment;
2930

3031
import java.io.BufferedReader;
@@ -66,6 +67,11 @@ public class GenericContainerRuleTest {
6667
private static final int RABBITMQ_PORT = 5672;
6768
private static final int MONGO_PORT = 27017;
6869

70+
public static final DockerImageName REDIS_IMAGE = DockerImageName.of("redis:3.0.2");
71+
public static final DockerImageName RABBITMQ_IMAGE = DockerImageName.of("rabbitmq:3.5.3");
72+
public static final DockerImageName MONGODB_IMAGE = DockerImageName.of("mongo:3.1.5");
73+
public static final DockerImageName ALPINE_IMAGE = DockerImageName.of("alpine:3.2");
74+
6975
/*
7076
* Test data setup
7177
*/
@@ -80,29 +86,27 @@ public static void setupContent() throws FileNotFoundException {
8086
* Redis
8187
*/
8288
@ClassRule
83-
public static GenericContainer redis = new GenericContainer(org.testcontainers.utility.DockerImageName.of("redis:3.0.2"))
89+
public static GenericContainer<?> redis = new GenericContainer<>(REDIS_IMAGE)
8490
.withExposedPorts(REDIS_PORT);
8591

8692
/**
8793
* RabbitMQ
8894
*/
8995
@ClassRule
90-
public static GenericContainer rabbitMq = new GenericContainer(org.testcontainers.utility.DockerImageName.of("rabbitmq:3.5.3"))
96+
public static GenericContainer<?> rabbitMq = new GenericContainer<>(RABBITMQ_IMAGE)
9197
.withExposedPorts(RABBITMQ_PORT);
92-
9398
/**
9499
* MongoDB
95100
*/
96101
@ClassRule
97-
public static GenericContainer mongo = new GenericContainer(org.testcontainers.utility.DockerImageName.of("mongo:3.1.5"))
102+
public static GenericContainer<?> mongo = new GenericContainer<>(MONGODB_IMAGE)
98103
.withExposedPorts(MONGO_PORT);
99-
100104
/**
101105
* Pass an environment variable to the container, then run a shell script that exposes the variable in a quick and
102106
* dirty way for testing.
103107
*/
104108
@ClassRule
105-
public static GenericContainer alpineEnvVar = new GenericContainer(org.testcontainers.utility.DockerImageName.of("alpine:3.2"))
109+
public static GenericContainer<?> alpineEnvVar = new GenericContainer<>(ALPINE_IMAGE)
106110
.withExposedPorts(80)
107111
.withEnv("MAGIC_NUMBER", "4")
108112
.withEnv("MAGIC_NUMBER", oldValue -> oldValue.orElse("") + "2")
@@ -113,7 +117,7 @@ public static void setupContent() throws FileNotFoundException {
113117
* dirty way for testing.
114118
*/
115119
@ClassRule
116-
public static GenericContainer alpineEnvVarFromMap = new GenericContainer(org.testcontainers.utility.DockerImageName.of("alpine:3.2"))
120+
public static GenericContainer<?> alpineEnvVarFromMap = new GenericContainer<>(ALPINE_IMAGE)
117121
.withExposedPorts(80)
118122
.withEnv(ImmutableMap.of(
119123
"FIRST", "42",
@@ -125,7 +129,7 @@ public static void setupContent() throws FileNotFoundException {
125129
* Map a file on the classpath to a file in the container, and then expose the content for testing.
126130
*/
127131
@ClassRule
128-
public static GenericContainer alpineClasspathResource = new GenericContainer(org.testcontainers.utility.DockerImageName.of("alpine:3.2"))
132+
public static GenericContainer<?> alpineClasspathResource = new GenericContainer<>(ALPINE_IMAGE)
129133
.withExposedPorts(80)
130134
.withClasspathResourceMapping("mappable-resource/test-resource.txt", "/content.txt", READ_ONLY)
131135
.withCommand("/bin/sh", "-c", "while true; do cat /content.txt | nc -l -p 80; done");
@@ -134,7 +138,7 @@ public static void setupContent() throws FileNotFoundException {
134138
* Map a file on the classpath to a file in the container, and then expose the content for testing.
135139
*/
136140
@ClassRule
137-
public static GenericContainer alpineClasspathResourceSelinux = new GenericContainer(org.testcontainers.utility.DockerImageName.of("alpine:3.2"))
141+
public static GenericContainer<?> alpineClasspathResourceSelinux = new GenericContainer<>(ALPINE_IMAGE)
138142
.withExposedPorts(80)
139143
.withClasspathResourceMapping("mappable-resource/test-resource.txt", "/content.txt", READ_WRITE, SHARED)
140144
.withCommand("/bin/sh", "-c", "while true; do cat /content.txt | nc -l -p 80; done");
@@ -143,14 +147,14 @@ public static void setupContent() throws FileNotFoundException {
143147
* Create a container with an extra host entry and expose the content of /etc/hosts for testing.
144148
*/
145149
@ClassRule
146-
public static GenericContainer alpineExtrahost = new GenericContainer(org.testcontainers.utility.DockerImageName.of("alpine:3.2"))
150+
public static GenericContainer<?> alpineExtrahost = new GenericContainer<>(ALPINE_IMAGE)
147151
.withExposedPorts(80)
148152
.withExtraHost("somehost", "192.168.1.10")
149153
.withCommand("/bin/sh", "-c", "while true; do cat /etc/hosts | nc -l -p 80; done");
150154

151155
@Test
152156
public void testIsRunning() {
153-
try (GenericContainer container = new GenericContainer().withCommand("top")) {
157+
try (GenericContainer container = new GenericContainer<>().withCommand("top")) {
154158
assertFalse("Container is not started and not running", container.isRunning());
155159
container.start();
156160
assertTrue("Container is started and running", container.isRunning());
@@ -160,7 +164,7 @@ public void testIsRunning() {
160164
@Test
161165
public void withTmpFsTest() throws Exception {
162166
try (
163-
GenericContainer container = new GenericContainer()
167+
GenericContainer container = new GenericContainer<>()
164168
.withCommand("top")
165169
.withTmpFs(singletonMap("/testtmpfs", "rw"))
166170
) {
@@ -241,7 +245,7 @@ public void environmentFromMapTest() throws IOException {
241245

242246
@Test
243247
public void customLabelTest() {
244-
try (final GenericContainer alpineCustomLabel = new GenericContainer(org.testcontainers.utility.DockerImageName.of("alpine:3.2"))
248+
try (final GenericContainer alpineCustomLabel = new GenericContainer<>(ALPINE_IMAGE)
245249
.withLabel("our.custom", "label")
246250
.withCommand("top")) {
247251

@@ -259,7 +263,7 @@ public void exceptionThrownWhenTryingToOverrideTestcontainersLabels() {
259263
assertThrows("When trying to overwrite an 'org.testcontainers' label, withLabel() throws an exception",
260264
IllegalArgumentException.class,
261265
() -> {
262-
new GenericContainer(org.testcontainers.utility.DockerImageName.of("alpine:3.2"))
266+
new GenericContainer<>(ALPINE_IMAGE)
263267
.withLabel("org.testcontainers.foo", "false");
264268
}
265269
);
@@ -301,7 +305,7 @@ protected static void writeStringToFile(File contentFolder, String filename, Str
301305
public void failFastWhenContainerHaltsImmediately() throws Exception {
302306

303307
long startingTimeMs = System.currentTimeMillis();
304-
final GenericContainer failsImmediately = new GenericContainer(org.testcontainers.utility.DockerImageName.of("alpine:3.2"))
308+
final GenericContainer failsImmediately = new GenericContainer<>(ALPINE_IMAGE)
305309
.withCommand("/bin/sh", "-c", "return false")
306310
.withMinimumRunningDuration(Duration.ofMillis(100));
307311

@@ -365,7 +369,7 @@ public void createContainerCmdHookTest() {
365369
// Use random name to avoid the conflicts between the tests
366370
String randomName = Base58.randomString(5);
367371
try(
368-
GenericContainer container = new GenericContainer(org.testcontainers.utility.DockerImageName.of("redis:3.0.2"))
372+
GenericContainer<?> container = new GenericContainer<>(REDIS_IMAGE)
369373
.withCommand("redis-server", "--help")
370374
.withCreateContainerCmdModifier(cmd -> cmd.withName("overrideMe"))
371375
// Preserves the order
@@ -409,7 +413,7 @@ public void addingExposedPortTwiceShouldNotFail() {
409413

410414
@Test
411415
public void sharedMemorySetTest() {
412-
try (GenericContainer containerWithSharedMemory = new GenericContainer()
416+
try (GenericContainer containerWithSharedMemory = new GenericContainer<>()
413417
.withSharedMemorySize(42L * FileUtils.ONE_MB)
414418
.withStartupCheckStrategy(new OneShotStartupCheckStrategy())) {
415419

modules/cassandra/src/main/java/org/testcontainers/containers/CassandraContainer.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
*/
2626
public class CassandraContainer<SELF extends CassandraContainer<SELF>> extends GenericContainer<SELF> {
2727

28-
public static final DockerImageName STANDARD_IMAGE = DockerImageName.of("cassandra");
29-
3028
@Deprecated
3129
public static final String IMAGE = "cassandra";
3230

@@ -39,23 +37,29 @@ public class CassandraContainer<SELF extends CassandraContainer<SELF>> extends G
3937
private String initScriptPath;
4038
private boolean enableJmxReporting;
4139

42-
public CassandraContainer(DockerImageName dockerImageName) {
43-
super(dockerImageName);
44-
addExposedPort(CQL_PORT);
45-
setStartupAttempts(3);
46-
this.enableJmxReporting = false;
47-
}
48-
40+
/**
41+
* @deprecated use {@link #CassandraContainer(DockerImageName)} instead
42+
*/
4943
@Deprecated
5044
public CassandraContainer() {
51-
this(STANDARD_IMAGE.withTag("3.11.2"));
45+
this("cassandra:3.11.2");
5246
}
5347

48+
/**
49+
* @deprecated use {@link #CassandraContainer(DockerImageName)} instead
50+
*/
5451
@Deprecated
5552
public CassandraContainer(String dockerImageName) {
5653
this(DockerImageName.of(dockerImageName));
5754
}
5855

56+
public CassandraContainer(DockerImageName dockerImageName) {
57+
super(dockerImageName);
58+
addExposedPort(CQL_PORT);
59+
setStartupAttempts(3);
60+
this.enableJmxReporting = false;
61+
}
62+
5963
@Override
6064
protected void configure() {
6165
optionallyMapResourceParameterAsVolume(CONTAINER_CONFIG_LOCATION, configLocation);

0 commit comments

Comments
 (0)