From 7fae384751da6101a451aa7d0a753ebd6dbf13ce Mon Sep 17 00:00:00 2001 From: HaMatthias <38075918+HaMatthias@users.noreply.github.com> Date: Sat, 6 Jul 2019 23:19:45 +0200 Subject: [PATCH] Docker Compose: allow specific services to be started (#1528) --- .../containers/DockerComposeContainer.java | 12 ++++++- .../junit/DockerComposeServiceTest.java | 33 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/org/testcontainers/junit/DockerComposeServiceTest.java diff --git a/core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java b/core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java index ff92289431e..46f4c561e5d 100644 --- a/core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java +++ b/core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java @@ -75,6 +75,8 @@ public class DockerComposeContainer> e private static final Object MUTEX = new Object(); + private List services = new ArrayList<>(); + /** * Properties that should be passed through to all Compose and ambassador containers (not * necessarily to containers that are spawned by Compose itself) @@ -159,10 +161,18 @@ private void pullImages() { runWithCompose("pull"); } + public SELF withServices(@NonNull String... services) { + this.services = Arrays.asList(services); + return self(); + } private void createServices() { // Run the docker-compose container, which starts up the services - runWithCompose("up -d"); + if(services.isEmpty()) { + runWithCompose("up -d"); + } else { + runWithCompose("up -d " + String.join(" ", services)); + } } private void waitUntilServiceStarted() { diff --git a/core/src/test/java/org/testcontainers/junit/DockerComposeServiceTest.java b/core/src/test/java/org/testcontainers/junit/DockerComposeServiceTest.java new file mode 100644 index 00000000000..c0360050d27 --- /dev/null +++ b/core/src/test/java/org/testcontainers/junit/DockerComposeServiceTest.java @@ -0,0 +1,33 @@ +package org.testcontainers.junit; + +import org.junit.Rule; +import org.junit.Test; +import org.testcontainers.containers.DockerComposeContainer; + +import static org.rnorth.visibleassertions.VisibleAssertions.assertNotNull; + +import java.io.File; + +public class DockerComposeServiceTest extends BaseDockerComposeTest { + + @Rule + public DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/compose-test.yml")) + .withServices("redis") + .withExposedService("redis_1", REDIS_PORT); + + + @Override + protected DockerComposeContainer getEnvironment() { + return environment; + } + + @Test(expected = NullPointerException.class) + public void testDbIsNotStarting() { + environment.getServicePort("db_1", 10001); + } + + @Test + public void testRedisIsStarting() { + assertNotNull("Redis server started", environment.getServicePort("redis_1", REDIS_PORT)); + } +}