Skip to content

Commit 33e9f64

Browse files
authored
Pass options to compose down command (#9040)
Currently, setting options are supported for the `up` command. But, the same are not passed to the `down` command. Support for this has been added. Also, adding an example using `--profiles`. Fixes #5041
1 parent d26eaca commit 33e9f64

File tree

7 files changed

+122
-23
lines changed

7 files changed

+122
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.testcontainers.containers;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
import java.util.Set;
6+
7+
class ComposeCommand {
8+
9+
static String getDownCommand(ComposeDelegate.ComposeVersion composeVersion, Set<String> options) {
10+
String composeOptions = optionsAsString(options);
11+
if (composeOptions == null || composeOptions.equals("")) {
12+
return composeVersion == ComposeDelegate.ComposeVersion.V1 ? "down" : "compose down";
13+
}
14+
String cmd = composeVersion == ComposeDelegate.ComposeVersion.V1 ? "%s down" : "compose %s down";
15+
return String.format(cmd, composeOptions);
16+
}
17+
18+
static String getUpCommand(ComposeDelegate.ComposeVersion composeVersion, Set<String> options) {
19+
String composeOptions = optionsAsString(options);
20+
if (composeOptions == null || composeOptions.equals("")) {
21+
return composeVersion == ComposeDelegate.ComposeVersion.V1 ? "up -d" : "compose up -d";
22+
}
23+
String cmd = composeVersion == ComposeDelegate.ComposeVersion.V1 ? "%s up -d" : "compose %s up -d";
24+
return String.format(cmd, composeOptions);
25+
}
26+
27+
private static String optionsAsString(final Set<String> options) {
28+
String optionsString = String.join(" ", options);
29+
if (!optionsString.isEmpty()) {
30+
// ensures that there is a space between the options and 'up' if options are passed.
31+
return optionsString;
32+
} else {
33+
// otherwise two spaces would appear between 'docker-compose' and 'up'
34+
return StringUtils.EMPTY;
35+
}
36+
}
37+
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ public void stop() {
161161
this.composeDelegate.getAmbassadorContainer().stop();
162162

163163
// Kill the services using docker
164-
String cmd = "compose down";
164+
String cmd = ComposeCommand.getDownCommand(ComposeDelegate.ComposeVersion.V2, this.options);
165+
165166
if (removeVolumes) {
166167
cmd += " -v";
167168
}

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

+1-21
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import lombok.NonNull;
1111
import lombok.Setter;
1212
import lombok.extern.slf4j.Slf4j;
13-
import org.apache.commons.lang3.StringUtils;
1413
import org.testcontainers.DockerClientFactory;
1514
import org.testcontainers.containers.output.OutputFrame;
1615
import org.testcontainers.containers.output.Slf4jLogConsumer;
@@ -146,7 +145,7 @@ void createServices(
146145
.distinct()
147146
.collect(Collectors.joining(" "));
148147

149-
String command = getUpCommand(optionsAsString(options));
148+
String command = ComposeCommand.getUpCommand(this.composeVersion, options);
150149

151150
if (build) {
152151
command += " --build";
@@ -164,25 +163,6 @@ void createServices(
164163
runWithCompose(localCompose, command, env, fileCopyInclusions);
165164
}
166165

167-
private String getUpCommand(String options) {
168-
if (options == null || options.equals("")) {
169-
return this.composeVersion == ComposeVersion.V1 ? "up -d" : "compose up -d";
170-
}
171-
String cmd = this.composeVersion == ComposeVersion.V1 ? "%s up -d" : "compose %s up -d";
172-
return String.format(cmd, options);
173-
}
174-
175-
private String optionsAsString(final Set<String> options) {
176-
String optionsString = options.stream().collect(Collectors.joining(" "));
177-
if (optionsString.length() != 0) {
178-
// ensures that there is a space between the options and 'up' if options are passed.
179-
return optionsString;
180-
} else {
181-
// otherwise two spaces would appear between 'docker-compose' and 'up'
182-
return StringUtils.EMPTY;
183-
}
184-
}
185-
186166
void waitUntilServiceStarted(boolean tailChildContainers) {
187167
listChildContainers().forEach(container -> createServiceInstance(container, tailChildContainers));
188168

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void stop() {
167167
this.composeDelegate.getAmbassadorContainer().stop();
168168

169169
// Kill the services using docker-compose
170-
String cmd = "down";
170+
String cmd = ComposeCommand.getDownCommand(ComposeDelegate.ComposeVersion.V1, this.options);
171171

172172
if (removeVolumes) {
173173
cmd += " -v";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.testcontainers.containers;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
7+
import java.io.File;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
@RunWith(Parameterized.class)
12+
public class ComposeProfilesOptionTest {
13+
14+
@Parameterized.Parameters(name = "{0}")
15+
public static Boolean[] local() {
16+
return new Boolean[] { Boolean.TRUE, Boolean.FALSE };
17+
}
18+
19+
@Parameterized.Parameter
20+
public boolean local;
21+
22+
public static final File COMPOSE_FILE = new File("src/test/resources/compose-profile-option/compose-test.yml");
23+
24+
@Test
25+
public void testProfileOption() {
26+
try (
27+
ComposeContainer compose = new ComposeContainer(COMPOSE_FILE)
28+
.withOptions("--profile=cache")
29+
.withLocalCompose(true)
30+
) {
31+
compose.start();
32+
assertThat(compose.listChildContainers()).hasSize(1);
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.testcontainers.containers;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
7+
import java.io.File;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
@RunWith(Parameterized.class)
12+
public class DockerComposeProfilesOptionTest {
13+
14+
@Parameterized.Parameters(name = "{0}")
15+
public static Boolean[] local() {
16+
return new Boolean[] { Boolean.TRUE, Boolean.FALSE };
17+
}
18+
19+
@Parameterized.Parameter
20+
public boolean local;
21+
22+
public static final File COMPOSE_FILE = new File("src/test/resources/compose-profile-option/compose-test.yml");
23+
24+
@Test
25+
public void testProfileOption() {
26+
try (
27+
DockerComposeContainer<?> compose = new DockerComposeContainer<>(COMPOSE_FILE)
28+
.withOptions("--profile=cache")
29+
.withLocalCompose(this.local)
30+
) {
31+
compose.start();
32+
assertThat(compose.listChildContainers()).hasSize(1);
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
redis:
3+
image: redis
4+
profiles:
5+
- cache
6+
db:
7+
image: mysql:8.0.36
8+
environment:
9+
MYSQL_RANDOM_ROOT_PASSWORD: "true"
10+
profiles:
11+
- db

0 commit comments

Comments
 (0)