-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Docker compose withOptions(...)
#2827
Changes from 1 commit
354298b
23802c4
afc3ee0
c447fd9
988edf3
f80cc03
6bc3613
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,7 @@ public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> e | |
private boolean localCompose; | ||
private boolean pull = true; | ||
private boolean build = false; | ||
private String options = StringUtils.EMPTY; | ||
private boolean tailChildContainers; | ||
|
||
private String project; | ||
|
@@ -209,9 +210,9 @@ private void createServices() { | |
|
||
// Run the docker-compose container, which starts up the services | ||
if(Strings.isNullOrEmpty(servicesWithScalingSettings)) { | ||
runWithCompose("up " + flags); | ||
runWithCompose(options + "up " + flags); | ||
} else { | ||
runWithCompose("up " + flags + " " + servicesWithScalingSettings); | ||
runWithCompose(options + "up " + flags + " " + servicesWithScalingSettings); | ||
} | ||
} | ||
|
||
|
@@ -500,6 +501,20 @@ public SELF withBuild(boolean build) { | |
return self(); | ||
} | ||
|
||
/** | ||
* Adds options to the docker-compose command, e.g. docker-compose --compatibility. Options must be space separated | ||
* Options are raw strings to allow future options to be accommodated transparently. | ||
* | ||
* @return this instance, for chaining | ||
*/ | ||
public SELF withOptions(String options) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just adding to the Set here would be enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Building on @kiview's suggestion to use a set, this also would mean that |
||
if (options.length() != 0 && !options.endsWith(" ")) { | ||
options+=" "; | ||
} | ||
this.options = options; | ||
return self(); | ||
} | ||
|
||
/** | ||
* Remove images after containers shutdown. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.testcontainers.junit; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.testcontainers.containers.DockerComposeContainer; | ||
|
||
import java.io.File; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests the options associated with the docker-compose command. | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class DockerComposeContainerWithOptionsTest { | ||
|
||
public DockerComposeContainerWithOptionsTest(final File composeFile, final boolean local, final String options, final boolean expectError) { | ||
this.composeFile = composeFile; | ||
this.local = local; | ||
this.options = options; | ||
this.expectError = expectError; | ||
} | ||
|
||
private final File composeFile; | ||
private final boolean local; | ||
public final String options; | ||
private final boolean expectError; | ||
|
||
@Parameterized.Parameters(name = "docker-compose test [compose file: {0}, local: {1}, option: {2}, expected result: {3}]") | ||
public static Object[][] params() { | ||
return new Object[][]{ | ||
// Test the happy day case. THe compatibility option should be accepted by docker-compose. | ||
{new File("src/test/resources/compose-options-test/with-deploy-block.yml"), false, "--compatibility", false}, | ||
// Test with flags absent. Docker compose will warn but continue, ignoring the deploy block. | ||
{new File("src/test/resources/compose-options-test/with-deploy-block.yml"), false, "", false}, | ||
// Test with a bad option. Compose will complain. | ||
{new File("src/test/resources/compose-options-test/with-deploy-block.yml"), false, "--bad-option", true}, | ||
// Local compose | ||
{new File("src/test/resources/compose-options-test/with-deploy-block.yml"), true, "--compatibility", false}, | ||
}; | ||
} | ||
|
||
@Test | ||
public void performTest() { | ||
|
||
try (DockerComposeContainer<?> environment = new DockerComposeContainer<>(composeFile) | ||
.withOptions(options) | ||
.withLocalCompose(local)) { | ||
environment.start(); | ||
assertThat(expectError).isEqualTo(false); | ||
} catch (Exception e) { | ||
assertThat(expectError).isEqualTo(true); | ||
} | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version: '3.7' | ||
services: | ||
redis: | ||
image: redis:2.6.17 | ||
deploy: | ||
resources: | ||
limits: | ||
memory: 150M |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use a
Set<String>
to which to add the options. Or in case order would be important (it probably is not?) aList
.