Skip to content

Commit 77251a1

Browse files
Gapmeister66rnorth
andauthored
Add support for Docker compose withOptions(...) (#2827)
Co-authored-by: Peter Lewis <[email protected]> Co-authored-by: Richard North <[email protected]>
1 parent e01495d commit 77251a1

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.Arrays;
4646
import java.util.Collections;
4747
import java.util.HashMap;
48+
import java.util.HashSet;
4849
import java.util.List;
4950
import java.util.Map;
5051
import java.util.Objects;
@@ -81,6 +82,7 @@ public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> e
8182
private boolean localCompose;
8283
private boolean pull = true;
8384
private boolean build = false;
85+
private Set<String> options = new HashSet<>();
8486
private boolean tailChildContainers;
8587

8688
private String project;
@@ -213,7 +215,7 @@ private void createServices() {
213215
.distinct()
214216
.collect(joining(" "));
215217

216-
String command = "up -d";
218+
String command = optionsAsString() + "up -d";
217219

218220
if (build) {
219221
command += " --build";
@@ -231,6 +233,19 @@ private void createServices() {
231233
runWithCompose(command);
232234
}
233235

236+
private String optionsAsString() {
237+
String optionsString = options
238+
.stream()
239+
.collect(joining(" "));
240+
if (optionsString.length() !=0 ) {
241+
// ensures that there is a space between the options and 'up' if options are passed.
242+
return optionsString + " ";
243+
} else {
244+
// otherwise two spaces would appear between 'docker-compose' and 'up'
245+
return StringUtils.EMPTY;
246+
}
247+
}
248+
234249
private void waitUntilServiceStarted() {
235250
listChildContainers().forEach(this::createServiceInstance);
236251

@@ -517,6 +532,16 @@ public SELF withBuild(boolean build) {
517532
return self();
518533
}
519534

535+
/**
536+
* Adds options to the docker-compose command, e.g. docker-compose --compatibility.
537+
*
538+
* @return this instance, for chaining
539+
*/
540+
public SELF withOptions(String... options) {
541+
this.options = new HashSet<>(Arrays.asList(options));
542+
return self();
543+
}
544+
520545
/**
521546
* Remove images after containers shutdown.
522547
*
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.testcontainers.junit;
2+
3+
import com.google.common.collect.ImmutableSet;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.Parameterized;
7+
import org.testcontainers.containers.DockerComposeContainer;
8+
9+
import java.io.File;
10+
import java.util.Set;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
/**
15+
* Tests the options associated with the docker-compose command.
16+
*/
17+
@RunWith(Parameterized.class)
18+
public class DockerComposeContainerWithOptionsTest {
19+
20+
public DockerComposeContainerWithOptionsTest(final File composeFile, final boolean local, final Set<String> options, final boolean expectError) {
21+
this.composeFile = composeFile;
22+
this.local = local;
23+
this.options = options;
24+
this.expectError = expectError;
25+
}
26+
27+
private final File composeFile;
28+
private final boolean local;
29+
30+
private final Set<String> options;
31+
private final boolean expectError;
32+
33+
@Parameterized.Parameters(name = "docker-compose test [compose file: {0}, local: {1}, options: {2}, expected result: {3}]")
34+
35+
public static Object[][] params() {
36+
return new Object[][]{
37+
// Test the happy day case. THe compatibility option should be accepted by docker-compose.
38+
{new File("src/test/resources/compose-options-test/with-deploy-block.yml"), false, ImmutableSet.of("--compatibility"), false},
39+
// Test with flags absent. Docker compose will warn but continue, ignoring the deploy block.
40+
{new File("src/test/resources/compose-options-test/with-deploy-block.yml"), false, ImmutableSet.of(""), false},
41+
// Test with a bad option. Compose will complain.
42+
{new File("src/test/resources/compose-options-test/with-deploy-block.yml"), false, ImmutableSet.of("--bad-option"), true},
43+
// Local compose
44+
{new File("src/test/resources/compose-options-test/with-deploy-block.yml"), true, ImmutableSet.of("--compatibility"), false},
45+
};
46+
}
47+
48+
@Test
49+
public void performTest() {
50+
51+
try (DockerComposeContainer<?> environment = new DockerComposeContainer<>(composeFile)
52+
.withOptions(options.stream().toArray(String[]::new))
53+
.withLocalCompose(local)) {
54+
environment.start();
55+
assertThat(expectError).isEqualTo(false);
56+
} catch (Exception e) {
57+
assertThat(expectError).isEqualTo(true);
58+
}
59+
60+
}
61+
62+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: '3.7'
2+
services:
3+
redis:
4+
image: redis:2.6.17
5+
deploy:
6+
resources:
7+
limits:
8+
memory: 150M

0 commit comments

Comments
 (0)