Skip to content

Commit 672cbee

Browse files
bsideuprnorth
andauthored
Add ContainerState#getHost replacing getContainerIpAddress (#2742)
* Add `ContainerState#getHost` replacing `getContainerIpAddress` * post-merge fixes * Ignore `METHOD_NEW_DEFAULT` in japicmp * Update docs/features/networking.md Co-authored-by: Richard North <[email protected]> * Add a tip about `getContainerIpAddress()` * Fix the codeincludes Co-authored-by: Richard North <[email protected]>
1 parent ede19c8 commit 672cbee

File tree

62 files changed

+111
-110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+111
-110
lines changed

core/build.gradle

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,14 @@ task japicmp(type: me.champeau.gradle.japicmp.JapicmpTask) {
8484
failOnModification = true
8585
failOnSourceIncompatibility = true
8686

87-
// TODO cleanup excludes after we release the version based on docker-java 3.2.0
88-
8987
packageExcludes = [
9088
"com.github.dockerjava.*",
9189
"org.testcontainers.shaded.*",
9290
]
9391

94-
classExcludes = [
95-
// 'METHOD_REMOVED_IN_SUPERCLASS'
96-
"org.testcontainers.containers.output.FrameConsumerResultCallback",
97-
"org.testcontainers.dockerclient.LogToStringContainerCallback",
98-
99-
// 'METHOD_RETURN_TYPE_CHANGED', exposed `com.github.dockerjava.core.command.PullImageResultCallback` to the public API
100-
"org.testcontainers.images.TimeLimitedLoggedPullImageResultCallback",
92+
methodExcludes = [
93+
// METHOD_NEW_DEFAULT
94+
"org.testcontainers.containers.ContainerState#getHost()"
10195
]
10296

10397
onlyBinaryIncompatibleModified = true

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public Integer getMappedPort(int originalPort) {
5454
* {@inheritDoc}
5555
*/
5656
@Override
57-
public String getContainerIpAddress() {
58-
return proxyContainer.getContainerIpAddress();
57+
public String getHost() {
58+
return proxyContainer.getHost();
5959
}
6060

6161
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,18 @@ public interface ContainerState {
4343
* Get the IP address that this container may be reached on (may not be the local machine).
4444
*
4545
* @return an IP address
46+
* @see #getHost()
4647
*/
4748
default String getContainerIpAddress() {
49+
return getHost();
50+
}
51+
52+
/**
53+
* Get the host that this container may be reached on (may not be the local machine).
54+
*
55+
* @return a host
56+
*/
57+
default String getHost() {
4858
return DockerClientFactory.instance().dockerHostIpAddress();
4959
}
5060

@@ -107,6 +117,7 @@ default InspectContainerResponse getCurrentContainerInfo() {
107117

108118
/**
109119
* Get the actual mapped port for a first port exposed by the container.
120+
* Should be used in conjunction with {@link #getHost()}.
110121
*
111122
* @return the port that the exposed port is mapped to
112123
* @throws IllegalStateException if there are no exposed ports
@@ -121,6 +132,7 @@ default Integer getFirstMappedPort() {
121132

122133
/**
123134
* Get the actual mapped port for a given port exposed by the container.
135+
* Should be used in conjunction with {@link #getHost()}.
124136
*
125137
* @param originalPort the original TCP port that is exposed
126138
* @return the port that the exposed port is mapped to, or null if it is not exposed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public SELF waitingFor(String serviceName, @NonNull WaitStrategy waitStrategy) {
402402
* @return a host IP address or hostname that can be used for accessing the service container.
403403
*/
404404
public String getServiceHost(String serviceName, Integer servicePort) {
405-
return ambassadorContainer.getContainerIpAddress();
405+
return ambassadorContainer.getHost();
406406
}
407407

408408
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ public SELF withCopyFileToContainer(MountableFile mountableFile, String containe
12441244
*/
12451245
@Deprecated
12461246
public String getIpAddress() {
1247-
return getContainerIpAddress();
1247+
return getHost();
12481248
}
12491249

12501250
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private Connection createSSHSession() {
4040
);
4141
container.start();
4242

43-
Connection connection = new Connection(container.getContainerIpAddress(), container.getMappedPort(22));
43+
Connection connection = new Connection(container.getHost(), container.getMappedPort(22));
4444

4545
connection.setTCPNoDelay(true);
4646
connection.connect(
@@ -60,7 +60,7 @@ private Connection createSSHSession() {
6060
public void exposeHostPort(int port) {
6161
exposeHostPort(port, port);
6262
}
63-
63+
6464
@SneakyThrows
6565
public void exposeHostPort(int hostPort, int containerPort) {
6666
if (exposedPorts.add(new AbstractMap.SimpleEntry<>(hostPort, containerPort))) {

core/src/main/java/org/testcontainers/containers/wait/internal/ExternalPortListeningCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ExternalPortListeningCheck implements Callable<Boolean> {
1818

1919
@Override
2020
public Boolean call() {
21-
String address = containerState.getContainerIpAddress();
21+
String address = containerState.getHost();
2222

2323
externalLivenessCheckPorts.parallelStream().forEach(externalPort -> {
2424
try {

core/src/main/java/org/testcontainers/containers/wait/strategy/HostPortWaitStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected void waitUntilReady() {
4646

4747
} catch (TimeoutException e) {
4848
throw new ContainerLaunchException("Timed out waiting for container port to open (" +
49-
waitStrategyTarget.getContainerIpAddress() +
49+
waitStrategyTarget.getHost() +
5050
" ports: " +
5151
externalLivenessCheckPorts +
5252
" should be listening)");

core/src/main/java/org/testcontainers/containers/wait/strategy/HttpWaitStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ protected void waitUntilReady() {
225225
*/
226226
private URI buildLivenessUri(int livenessCheckPort) {
227227
final String scheme = (tlsEnabled ? "https" : "http") + "://";
228-
final String host = waitStrategyTarget.getContainerIpAddress();
228+
final String host = waitStrategyTarget.getHost();
229229

230230
final String portSuffix;
231231
if ((tlsEnabled && 443 == livenessCheckPort) || (!tlsEnabled && 80 == livenessCheckPort)) {

core/src/test/java/org/testcontainers/containers/wait/internal/ExternalPortListeningCheckTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void setUp() throws Exception {
2929
nonListeningSocket.close();
3030

3131
mockContainer = mock(WaitStrategyTarget.class);
32-
when(mockContainer.getContainerIpAddress()).thenReturn("127.0.0.1");
32+
when(mockContainer.getHost()).thenReturn("127.0.0.1");
3333
}
3434

3535
@Test

core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ImagePullPolicyTest {
3333

3434
@BeforeClass
3535
public static void beforeClass() throws Exception {
36-
String testRegistryAddress = registry.getContainerIpAddress() + ":" + registry.getFirstMappedPort();
36+
String testRegistryAddress = registry.getHost() + ":" + registry.getFirstMappedPort();
3737
String testImageName = testRegistryAddress + "/image-pull-policy-test";
3838
String tag = UUID.randomUUID().toString();
3939
imageName = testImageName + ":" + tag;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class DockerfileContainerTest {
3131

3232
@Test
3333
public void simpleDslTest() throws IOException {
34-
String address = String.format("http://%s:%s", dslContainer.getContainerIpAddress(), dslContainer.getMappedPort(80));
34+
String address = String.format("http://%s:%s", dslContainer.getHost(), dslContainer.getMappedPort(80));
3535

3636
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
3737
HttpGet get = new HttpGet(address);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private String readResponse(GenericContainer container, Integer port) throws IOE
8585
final BufferedReader reader = Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS,
8686
() -> {
8787
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
88-
final Socket socket = new Socket(container.getContainerIpAddress(), port);
88+
final Socket socket = new Socket(container.getHost(), port);
8989
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
9090
}
9191
)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void withTmpFsTest() throws Exception {
162162
@Test
163163
public void simpleRabbitMqTest() throws IOException, TimeoutException {
164164
ConnectionFactory factory = new ConnectionFactory();
165-
factory.setHost(rabbitMq.getContainerIpAddress());
165+
factory.setHost(rabbitMq.getHost());
166166
factory.setPort(rabbitMq.getMappedPort(RABBITMQ_PORT));
167167
Connection connection = factory.newConnection();
168168

@@ -194,7 +194,7 @@ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProp
194194

195195
@Test
196196
public void simpleMongoDbTest() {
197-
MongoClient mongoClient = new MongoClient(mongo.getContainerIpAddress(), mongo.getMappedPort(MONGO_PORT));
197+
MongoClient mongoClient = new MongoClient(mongo.getHost(), mongo.getMappedPort(MONGO_PORT));
198198
MongoDatabase database = mongoClient.getDatabase("test");
199199
MongoCollection<Document> collection = database.getCollection("testCollection");
200200

@@ -366,7 +366,7 @@ private BufferedReader getReaderForContainerPort80(GenericContainer container) {
366366
return Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> {
367367
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
368368

369-
Socket socket = new Socket(container.getContainerIpAddress(), container.getFirstMappedPort());
369+
Socket socket = new Socket(container.getHost(), container.getFirstMappedPort());
370370
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
371371
});
372372
}

core/src/test/java/org/testcontainers/utility/AuthenticatedImagePullTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static void setUp() throws InterruptedException {
6565
originalAuthLocatorSingleton = RegistryAuthLocator.instance();
6666
client = DockerClientFactory.instance().client();
6767

68-
String testRegistryAddress = authenticatedRegistry.getContainerIpAddress() + ":" + authenticatedRegistry.getFirstMappedPort();
68+
String testRegistryAddress = authenticatedRegistry.getHost() + ":" + authenticatedRegistry.getFirstMappedPort();
6969
testImageName = testRegistryAddress + "/alpine";
7070
testImageNameWithTag = testImageName + ":latest";
7171

docs/examples/junit4/generic/src/test/java/generic/MultiplePortsExposedTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public void fetchFirstMappedPort() {
3131
}
3232

3333
@Test
34-
public void getContainerIpAddressOnly() {
35-
String ipAddress = container.getContainerIpAddress();
34+
public void getHostOnly() {
35+
String ipAddress = container.getHost();
3636
}
3737

3838
@Test
39-
public void getContainerIpAddressAndMappedPort() {
39+
public void getHostAndMappedPort() {
4040
String address =
41-
container.getContainerIpAddress() + ":" + container.getMappedPort(2424);
41+
container.getHost() + ":" + container.getMappedPort(2424);
4242
}
4343
}

docs/examples/junit4/redis/src/test/java/quickstart/RedisBackedCacheIntTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class RedisBackedCacheIntTest {
2121

2222
@Before
2323
public void setUp() {
24-
String address = redis.getContainerIpAddress();
24+
String address = redis.getHost();
2525
Integer port = redis.getFirstMappedPort();
2626

2727
// Now we have an address and port for Redis, no matter where it is running

docs/examples/junit5/redis/src/test/java/quickstart/RedisBackedCacheIntTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class RedisBackedCacheIntTest {
2424

2525
@BeforeEach
2626
public void setUp() {
27-
String address = redis.getContainerIpAddress();
27+
String address = redis.getHost();
2828
Integer port = redis.getFirstMappedPort();
2929

3030
// Now we have an address and port for Redis, no matter where it is running

docs/examples/spock/redis/src/test/groovy/quickstart/RedisBackedCacheIntTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RedisBackedCacheIntTest extends Specification {
1515
// }
1616

1717
void setup() {
18-
String address = redis.containerIpAddress
18+
String address = redis.host
1919
Integer port = redis.firstMappedPort
2020

2121
// Now we have an address and port for Redis, no matter where it is running

docs/features/networking.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,27 @@ There is also a `getFirstMappedPort` method for convenience, for the fairly comm
3333
[Retrieving the first mapped port](../examples/junit4/generic/src/test/java/generic/MultiplePortsExposedTest.java) inside_block:fetchFirstMappedPort
3434
<!--/codeinclude-->
3535

36-
## Getting the container IP address
36+
## Getting the container host
3737

3838
When running with a local Docker daemon, exposed ports will usually be reachable on `localhost`.
3939
However, in some CI environments they may instead be reachable on a different host.
4040

41-
As such, Testcontainers provides a convenience method to obtain an IP address on which the container should be reachable from the host machine.
41+
As such, Testcontainers provides a convenience method to obtain an address on which the container should be reachable from the host machine.
4242

4343
<!--codeinclude-->
44-
[Getting the container IP address](../examples/junit4/generic/src/test/java/generic/MultiplePortsExposedTest.java) inside_block:getContainerIpAddressOnly
44+
[Getting the container host](../examples/junit4/generic/src/test/java/generic/MultiplePortsExposedTest.java) inside_block:getHostOnly
4545
<!--/codeinclude-->
4646

47-
It is normally advisable to use `getContainerIpAddress` and `getMappedPort` together when constructing addresses - for example:
47+
It is normally advisable to use `getHost` and `getMappedPort` together when constructing addresses - for example:
4848

4949
<!--codeinclude-->
50-
[Getting the container IP address and mapped port](../examples/junit4/generic/src/test/java/generic/MultiplePortsExposedTest.java) inside_block:getContainerIpAddressAndMappedPort
50+
[Getting the container host and mapped port](../examples/junit4/generic/src/test/java/generic/MultiplePortsExposedTest.java) inside_block:getHostAndMappedPort
5151
<!--/codeinclude-->
5252

53+
!!! tip
54+
`getHost()` is a replacement for `getContainerIpAddress()` and returns the same result.
55+
`getContainerIpAddress()` is believed to be confusingly named, and will eventually be deprecated.
56+
5357
## Exposing host ports to the container
5458

5559
In some cases it is necessary to make a network connection from a container to a socket that is listening on the host machine.
@@ -86,4 +90,4 @@ Docker provides the ability for you to create custom networks and place containe
8690

8791
<!--codeinclude-->
8892
[Creating custom networks](../../core/src/test/java/org/testcontainers/containers/NetworkTest.java) inside_block:useCustomNetwork
89-
<!--/codeinclude-->
93+
<!--/codeinclude-->

docs/modules/vault.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class SomeTest {
2121

2222
//There are many integration clients for Vault so let's just define a general one here:
2323
VaultClient client = new VaultClient(
24-
vaultContainer.getContainerIpAddress(),
24+
vaultContainer.getHost(),
2525
vaultContainer.getMappedPort(8200),
2626
"my-root-token");
2727

docs/quickstart/junit_4_quickstart.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ We can do this in our test `setUp` method, to set up our component under test:
6565
<!--/codeinclude-->
6666

6767
!!! tip
68-
Notice that we also ask Testcontainers for the container's actual address with `redis.getContainerIpAddress();`,
68+
Notice that we also ask Testcontainers for the container's actual address with `redis.getHost();`,
6969
rather than hard-coding `localhost`. `localhost` may work in some environments but not others - for example it may
7070
not work on your current or future CI environment. As such, **avoid hard-coding** the address, and use
71-
`getContainerIpAddress()` instead.
71+
`getHost()` instead.
7272

7373
## 4. Run the tests!
7474

docs/quickstart/junit_5_quickstart.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ We can do this in our test `setUp` method, to set up our component under test:
9494
<!--/codeinclude-->
9595

9696
!!! tip
97-
Notice that we also ask Testcontainers for the container's actual address with `redis.getContainerIpAddress();`,
97+
Notice that we also ask Testcontainers for the container's actual address with `redis.getHost();`,
9898
rather than hard-coding `localhost`. `localhost` may work in some environments but not others - for example it may
9999
not work on your current or future CI environment. As such, **avoid hard-coding** the address, and use
100-
`getContainerIpAddress()` instead.
100+
`getHost()` instead.
101101

102102
## 4. Run the tests!
103103

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public Cluster getCluster() {
161161

162162
public static Cluster getCluster(ContainerState containerState, boolean enableJmxReporting) {
163163
final Cluster.Builder builder = Cluster.builder()
164-
.addContactPoint(containerState.getContainerIpAddress())
164+
.addContactPoint(containerState.getHost())
165165
.withPort(containerState.getMappedPort(CQL_PORT));
166166
if (!enableJmxReporting) {
167167
builder.withoutJMXReporting();

modules/cassandra/src/test/java/org/testcontainers/containers/CassandraContainerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private void testInitScript(CassandraContainer cassandraContainer) {
116116

117117
private ResultSet performQuery(CassandraContainer cassandraContainer, String cql) {
118118
Cluster explicitCluster = Cluster.builder()
119-
.addContactPoint(cassandraContainer.getContainerIpAddress())
119+
.addContactPoint(cassandraContainer.getHost())
120120
.withPort(cassandraContainer.getMappedPort(CassandraContainer.CQL_PORT))
121121
.build();
122122
return performQuery(explicitCluster, cql);

modules/clickhouse/src/main/java/org/testcontainers/containers/ClickHouseContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public String getDriverClassName() {
4848

4949
@Override
5050
public String getJdbcUrl() {
51-
return JDBC_URL_PREFIX + getContainerIpAddress() + ":" + getMappedPort(HTTP_PORT) + "/" + databaseName;
51+
return JDBC_URL_PREFIX + getHost() + ":" + getMappedPort(HTTP_PORT) + "/" + databaseName;
5252
}
5353

5454
@Override

modules/cockroachdb/src/main/java/org/testcontainers/containers/CockroachContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public String getDriverClassName() {
4343

4444
@Override
4545
public String getJdbcUrl() {
46-
return JDBC_URL_PREFIX + "://" + getContainerIpAddress() + ":" + getMappedPort(DB_PORT) + "/" + databaseName;
46+
return JDBC_URL_PREFIX + "://" + getHost() + ":" + getMappedPort(DB_PORT) + "/" + databaseName;
4747
}
4848

4949
@Override

0 commit comments

Comments
 (0)