Skip to content

Commit fc3d600

Browse files
authored
Allow getContainerByServiceName to receive serviceName without suffix (#5776)
Fixes #5773
1 parent ffb88b3 commit fc3d600

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ public SELF withStartupTimeout(Duration startupTimeout) {
611611
}
612612

613613
public Optional<ContainerState> getContainerByServiceName(String serviceName) {
614-
return Optional.ofNullable(serviceInstanceMap.get(serviceName));
614+
String serviceInstantName = getServiceInstanceName(serviceName);
615+
return Optional.ofNullable(serviceInstanceMap.get(serviceInstantName));
615616
}
616617

617618
private void followLogs(String containerId, Consumer<OutputFrame> consumer) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void testGetServicePort() {
4444
public void shouldRetrieveContainerByServiceName() {
4545
String existingServiceName = "db_1";
4646
Optional<ContainerState> result = environment.getContainerByServiceName(existingServiceName);
47+
4748
assertThat(result)
4849
.as(String.format("Container should be found by service name %s", existingServiceName))
4950
.isPresent();
@@ -52,6 +53,19 @@ public void shouldRetrieveContainerByServiceName() {
5253
.isEqualTo(result.get().getExposedPorts());
5354
}
5455

56+
@Test
57+
public void shouldRetrieveContainerByServiceNameWithoutNumberedSuffix() {
58+
String existingServiceName = "db";
59+
Optional<ContainerState> result = environment.getContainerByServiceName(existingServiceName);
60+
61+
assertThat(result)
62+
.as(String.format("Container should be found by service name %s", existingServiceName))
63+
.isPresent();
64+
assertThat(result.get().getExposedPorts())
65+
.as("Mapped port for result container was wrong, perhaps wrong container was found")
66+
.isEqualTo(Collections.singletonList(3306));
67+
}
68+
5569
@Test
5670
public void shouldReturnEmptyResultOnNoneExistingService() {
5771
String notExistingServiceName = "db_256";

0 commit comments

Comments
 (0)