|
| 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 | +} |
0 commit comments