Skip to content

Commit 7fae384

Browse files
HaMatthiasrnorth
authored andcommitted
Docker Compose: allow specific services to be started (#1528)
1 parent 06ea95b commit 7fae384

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> e
7575

7676
private static final Object MUTEX = new Object();
7777

78+
private List<String> services = new ArrayList<>();
79+
7880
/**
7981
* Properties that should be passed through to all Compose and ambassador containers (not
8082
* necessarily to containers that are spawned by Compose itself)
@@ -159,10 +161,18 @@ private void pullImages() {
159161
runWithCompose("pull");
160162
}
161163

164+
public SELF withServices(@NonNull String... services) {
165+
this.services = Arrays.asList(services);
166+
return self();
167+
}
162168

163169
private void createServices() {
164170
// Run the docker-compose container, which starts up the services
165-
runWithCompose("up -d");
171+
if(services.isEmpty()) {
172+
runWithCompose("up -d");
173+
} else {
174+
runWithCompose("up -d " + String.join(" ", services));
175+
}
166176
}
167177

168178
private void waitUntilServiceStarted() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.testcontainers.junit;
2+
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
import org.testcontainers.containers.DockerComposeContainer;
6+
7+
import static org.rnorth.visibleassertions.VisibleAssertions.assertNotNull;
8+
9+
import java.io.File;
10+
11+
public class DockerComposeServiceTest extends BaseDockerComposeTest {
12+
13+
@Rule
14+
public DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))
15+
.withServices("redis")
16+
.withExposedService("redis_1", REDIS_PORT);
17+
18+
19+
@Override
20+
protected DockerComposeContainer getEnvironment() {
21+
return environment;
22+
}
23+
24+
@Test(expected = NullPointerException.class)
25+
public void testDbIsNotStarting() {
26+
environment.getServicePort("db_1", 10001);
27+
}
28+
29+
@Test
30+
public void testRedisIsStarting() {
31+
assertNotNull("Redis server started", environment.getServicePort("redis_1", REDIS_PORT));
32+
}
33+
}

0 commit comments

Comments
 (0)