From 70bce69bddee070b5c438ed9c3d78d0c6bd25563 Mon Sep 17 00:00:00 2001 From: Afshin Date: Tue, 14 Jan 2020 14:33:41 +0100 Subject: [PATCH 1/3] Fix 2191. Is is not running all services of a docker-compose, when a list of them is provided via withServices(...). --- .../containers/DockerComposeContainer.java | 4 +- ...ockerComposeContainerWithServicesTest.java | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java diff --git a/core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java b/core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java index 7dec6399b8f..4457467f20c 100644 --- a/core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java +++ b/core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java @@ -194,7 +194,7 @@ private void createServices() { if(Strings.isNullOrEmpty(servicesWithScalingSettings)) { runWithCompose("up " + flags); } else { - runWithCompose("up " + flags + " " + servicesWithScalingSettings); + runWithCompose("up " + flags + " " + servicesWithScalingSettings + " " + String.join(" ", this.services)); } } @@ -253,7 +253,7 @@ private void registerContainersForShutdown() { )); } - private List listChildContainers() { + public List listChildContainers() { return dockerClient.listContainersCmd() .withShowAll(true) .exec().stream() diff --git a/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java b/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java new file mode 100644 index 00000000000..37af834bb7c --- /dev/null +++ b/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java @@ -0,0 +1,43 @@ +package org.testcontainers.junit; + +import com.github.dockerjava.api.model.Container; +import org.junit.Rule; +import org.junit.Test; +import org.testcontainers.containers.DockerComposeContainer; + +import java.io.File; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; + + +public class DockerComposeContainerWithServicesTest extends BaseDockerComposeTest { + + private static final String[] listOfServices = new String[]{"redis"/*, "db"*/}; + + @Rule + public DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/compose-test.yml")) + .withServices(listOfServices); + + @Override + protected DockerComposeContainer getEnvironment() { + return environment; + } + + @Test + public void testGivenDockerCompose_WhenSublistOfServicesSelected_ThenOnlyThoseServicesAreStarted() { + Set runningList = new HashSet<>(); + + List list = environment.listChildContainers(); + for (Container container : list) { + for (String service : listOfServices) + if (container.getNames()[0].contains(service)) { + runningList.add(service); + } + } + assertEquals("List of assigned services is equal to list of running services of the docker-compose", + listOfServices.length, runningList.size()); + } +} From e0427fcea0c48f8896ddef2ab9b1510d6426fb03 Mon Sep 17 00:00:00 2001 From: Afshin Date: Tue, 14 Jan 2020 15:02:03 +0100 Subject: [PATCH 2/3] Fix 2191. removed not useful extend of BaseDockerComposeTest for DockerComposeContainerWithServicesTest --- .../junit/DockerComposeContainerWithServicesTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java b/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java index 37af834bb7c..8a48d2b5ba7 100644 --- a/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java +++ b/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java @@ -13,7 +13,7 @@ import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; -public class DockerComposeContainerWithServicesTest extends BaseDockerComposeTest { +public class DockerComposeContainerWithServicesTest { private static final String[] listOfServices = new String[]{"redis"/*, "db"*/}; @@ -21,7 +21,6 @@ public class DockerComposeContainerWithServicesTest extends BaseDockerComposeTes public DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/compose-test.yml")) .withServices(listOfServices); - @Override protected DockerComposeContainer getEnvironment() { return environment; } From c486f5604974abf976be5c87c8750d5f6a1a4652 Mon Sep 17 00:00:00 2001 From: Afshin Date: Tue, 14 Jan 2020 16:21:32 +0100 Subject: [PATCH 3/3] Fix 2191. missing test condition for making sure only those services are running are added --- .../junit/DockerComposeContainerWithServicesTest.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java b/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java index 8a48d2b5ba7..bafa9c3293b 100644 --- a/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java +++ b/core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithServicesTest.java @@ -21,10 +21,6 @@ public class DockerComposeContainerWithServicesTest { public DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/compose-test.yml")) .withServices(listOfServices); - protected DockerComposeContainer getEnvironment() { - return environment; - } - @Test public void testGivenDockerCompose_WhenSublistOfServicesSelected_ThenOnlyThoseServicesAreStarted() { Set runningList = new HashSet<>(); @@ -36,7 +32,9 @@ public void testGivenDockerCompose_WhenSublistOfServicesSelected_ThenOnlyThoseSe runningList.add(service); } } - assertEquals("List of assigned services is equal to list of running services of the docker-compose", + assertEquals("number of running services of docker-compose is the same as length of listOfServices", + listOfServices.length, environment.listChildContainers().size()); + assertEquals("Running services are the same as the requested listOfServices", listOfServices.length, runningList.size()); } }