Skip to content

Commit aa38a96

Browse files
Add tests for compose file with container name
This commit adds tests for compose files with container_name specified. A compose file was added for this purpose.
1 parent 6d99516 commit aa38a96

File tree

3 files changed

+65
-32
lines changed

3 files changed

+65
-32
lines changed

core/src/test/java/org/testcontainers/containers/ParsedDockerComposeFileValidationTest.java

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,9 @@
1414
public class ParsedDockerComposeFileValidationTest {
1515

1616
@Test
17-
public void shouldValidate() {
17+
public void shouldAcceptContainerName() {
1818
File file = new File("src/test/resources/docker-compose-container-name-v1.yml");
19-
Assertions
20-
.assertThatThrownBy(() -> {
21-
new ParsedDockerComposeFile(file);
22-
})
23-
.hasMessageContaining(file.getAbsolutePath())
24-
.hasMessageContaining("'container_name' property set for service 'redis'");
25-
}
26-
27-
@Test
28-
public void shouldRejectContainerNameV1() {
29-
Assertions
30-
.assertThatThrownBy(() -> {
31-
new ParsedDockerComposeFile(ImmutableMap.of(
32-
"redis", ImmutableMap.of("container_name", "redis")
33-
));
34-
})
35-
.hasMessageContaining("'container_name' property set for service 'redis'");
36-
}
37-
38-
@Test
39-
public void shouldRejectContainerNameV2() {
40-
Assertions
41-
.assertThatThrownBy(() -> {
42-
new ParsedDockerComposeFile(ImmutableMap.of(
43-
"version", "2",
44-
"services", ImmutableMap.of(
45-
"redis", ImmutableMap.of("container_name", "redis")
46-
)
47-
));
48-
})
49-
.hasMessageContaining("'container_name' property set for service 'redis'");
19+
ParsedDockerComposeFile parsedDockerComposeFile = new ParsedDockerComposeFile(file);
5020
}
5121

5222
@Test
@@ -99,4 +69,5 @@ public void shouldObtainImageFromDockerfileBuildWithContext() {
9969
ParsedDockerComposeFile parsedFile = new ParsedDockerComposeFile(file);
10070
assertEquals("all defined images are found", Sets.newHashSet("redis", "mysql", "alpine:3.2"), parsedFile.getDependencyImageNames()); // redis, mysql from compose file, alpine:3.2 from Dockerfile build
10171
}
72+
10273
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.testcontainers.junit;
2+
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
import org.testcontainers.containers.ContainerState;
6+
import org.testcontainers.containers.DockerComposeContainer;
7+
8+
import java.io.File;
9+
import java.util.Optional;
10+
11+
import static org.junit.Assert.assertTrue;
12+
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
13+
import static org.rnorth.visibleassertions.VisibleAssertions.assertNotNull;
14+
15+
16+
public class DockerComposeContainerWithNameTest extends BaseDockerComposeTest {
17+
private final String REDIS_SERVICE_NAME = "redis";
18+
private final String REDIS_INSTANCE_NAME = "redis_1";
19+
private static final String DB_INSTANCE_NAME = "db_1";
20+
private static final int DB_PORT = 3306;
21+
22+
@Rule
23+
public DockerComposeContainer environment =
24+
new DockerComposeContainer(new File("src/test/resources/compose-test-with-name.yml"))
25+
.withExposedService(REDIS_INSTANCE_NAME, REDIS_PORT)
26+
.withExposedService(DB_INSTANCE_NAME, DB_PORT);
27+
28+
@Override
29+
public DockerComposeContainer getEnvironment() {
30+
return environment;
31+
}
32+
33+
@Test
34+
public void testGetServicePort() {
35+
int serviceWithInstancePort = environment.getServicePort(REDIS_INSTANCE_NAME, REDIS_PORT);
36+
assertNotNull("Port is set for service with instance number", serviceWithInstancePort);
37+
int serviceWithoutInstancePort = environment.getServicePort(REDIS_SERVICE_NAME, REDIS_PORT);
38+
assertNotNull("Port is set for service with instance number", serviceWithoutInstancePort);
39+
assertEquals("Service ports are the same", serviceWithInstancePort, serviceWithoutInstancePort);
40+
}
41+
42+
@Test
43+
public void containerNamesShouldBeCorrect() {
44+
String containerName = "/redis_container";
45+
Optional<ContainerState> result = environment.getContainerByServiceName(REDIS_INSTANCE_NAME);
46+
assertTrue(String.format("container should be found for service %s", REDIS_INSTANCE_NAME), result.isPresent());
47+
ContainerState state = result.get();
48+
assertEquals("container name should be same as compose file", containerName, state.getContainerInfo().getName());
49+
50+
result = environment.getContainerByServiceName(DB_INSTANCE_NAME);
51+
assertTrue(String.format("container should be found for service %s", DB_INSTANCE_NAME), result.isPresent());
52+
state = result.get();
53+
assertTrue("container name should contain service name", state.getContainerInfo().getName().contains(DB_INSTANCE_NAME));
54+
}
55+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
redis:
2+
image: redis
3+
container_name: redis_container
4+
db:
5+
image: mysql:5.7.22
6+
environment:
7+
MYSQL_RANDOM_ROOT_PASSWORD: "true"

0 commit comments

Comments
 (0)