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

0 commit comments

Comments
 (0)