-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run correct subset of docker compose containers when
withServices
/`…
…withScaledService` used (#2922) Co-authored-by: Afshin <mnafshin@gmail.com>
- Loading branch information
Showing
4 changed files
with
184 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
core/src/test/java/org/testcontainers/containers/DockerComposeContainerWithServicesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; | ||
|
||
|
||
public class DockerComposeContainerWithServicesTest { | ||
|
||
public static final File SIMPLE_COMPOSE_FILE = new File("src/test/resources/compose-scaling-multiple-containers.yml"); | ||
public static final File COMPOSE_FILE_WITH_INLINE_SCALE = new File("src/test/resources/compose-with-inline-scale-test.yml"); | ||
|
||
@Test | ||
public void testDesiredSubsetOfServicesAreStarted() { | ||
try ( | ||
DockerComposeContainer<?> compose = new DockerComposeContainer<>(SIMPLE_COMPOSE_FILE) | ||
.withServices("redis") | ||
) { | ||
compose.start(); | ||
|
||
verifyStartedContainers(compose, "redis_1"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDesiredSubsetOfScaledServicesAreStarted() { | ||
try ( | ||
DockerComposeContainer<?> compose = new DockerComposeContainer<>(SIMPLE_COMPOSE_FILE) | ||
.withScaledService("redis", 2) | ||
) { | ||
compose.start(); | ||
|
||
verifyStartedContainers(compose, "redis_1", "redis_2"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDesiredSubsetOfSpecifiedAndScaledServicesAreStarted() { | ||
try ( | ||
DockerComposeContainer<?> compose = new DockerComposeContainer<>(SIMPLE_COMPOSE_FILE) | ||
.withServices("redis") | ||
.withScaledService("redis", 2) | ||
) { | ||
compose.start(); | ||
|
||
verifyStartedContainers(compose, "redis_1", "redis_2"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDesiredSubsetOfSpecifiedOrScaledServicesAreStarted() { | ||
try ( | ||
DockerComposeContainer<?> compose = new DockerComposeContainer<>(SIMPLE_COMPOSE_FILE) | ||
.withServices("other") | ||
.withScaledService("redis", 2) | ||
) { | ||
compose.start(); | ||
|
||
verifyStartedContainers(compose, "redis_1", "redis_2", "other_1"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testAllServicesAreStartedIfNotSpecified() { | ||
try ( | ||
DockerComposeContainer<?> compose = new DockerComposeContainer<>(SIMPLE_COMPOSE_FILE) | ||
) { | ||
compose.start(); | ||
|
||
verifyStartedContainers(compose, "redis_1", "other_1"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testScaleInComposeFileIsRespected() { | ||
try ( | ||
DockerComposeContainer<?> compose = new DockerComposeContainer<>(COMPOSE_FILE_WITH_INLINE_SCALE) | ||
) { | ||
compose.start(); | ||
|
||
// the compose file includes `scale: 3` for the redis container | ||
verifyStartedContainers(compose, "redis_1", "redis_2", "redis_3"); | ||
} | ||
} | ||
|
||
private void verifyStartedContainers(final DockerComposeContainer<?> compose, final String... names) { | ||
final List<String> containerNames = compose.listChildContainers().stream() | ||
.flatMap(container -> Stream.of(container.getNames())) | ||
.collect(Collectors.toList()); | ||
|
||
assertEquals("number of running services of docker-compose is the same as length of listOfServices", | ||
names.length, containerNames.size()); | ||
|
||
for (final String expectedName : names) { | ||
final long matches = containerNames.stream() | ||
.filter(foundName -> foundName.endsWith(expectedName)) | ||
.count(); | ||
|
||
assertEquals("container with name starting '" + expectedName + "' should be running", 1L, matches); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
core/src/test/resources/compose-scaling-multiple-containers.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: '2.4' | ||
services: | ||
redis: | ||
image: redis | ||
other: | ||
image: alpine:3.5 | ||
command: sleep 10000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
version: '2.4' | ||
services: | ||
redis: | ||
image: redis | ||
scale: 3 # legacy mechanism to specify scale |