Skip to content
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

Merged
merged 7 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

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?) a List.

private boolean tailChildContainers;

private String project;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just adding to the Set here would be enough.
String for runWithCompose can be constructed using Stream and Collectors.joining.

Copy link
Member

Choose a reason for hiding this comment

The 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 withOptions could take a varargs String, and just add all to the set. This would be quite nice semantically.

if (options.length() != 0 && !options.endsWith(" ")) {
options+=" ";
}
this.options = options;
return self();
}

/**
* Remove images after containers shutdown.
*
Expand Down
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