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 6 commits
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 @@ -42,6 +42,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -78,6 +79,7 @@ public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> e
private boolean localCompose;
private boolean pull = true;
private boolean build = false;
private Set<String> options = new HashSet<>();
private boolean tailChildContainers;

private String project;
Expand Down Expand Up @@ -209,9 +211,22 @@ private void createServices() {

// Run the docker-compose container, which starts up the services
if(Strings.isNullOrEmpty(servicesWithScalingSettings)) {
runWithCompose("up " + flags);
runWithCompose(optionsAsString() + "up " + flags);
} else {
runWithCompose("up " + flags + " " + servicesWithScalingSettings);
runWithCompose(optionsAsString() + "up " + flags + " " + servicesWithScalingSettings);
}
}

private String optionsAsString() {
String optionsString = options
.stream()
.collect(joining(" "));
if (optionsString.length() !=0 ) {
// ensures that there is a space between the options and 'up' if options are passed.
return optionsString + " ";
} else {
// otherwise two spaces would appear between 'docker-compose' and 'up'
return StringUtils.EMPTY;
}
}

Expand Down Expand Up @@ -500,6 +515,16 @@ public SELF withBuild(boolean build) {
return self();
}

/**
* Adds options to the docker-compose command, e.g. docker-compose --compatibility.
*
* @return this instance, for chaining
*/
public SELF withOptions(String... options) {
this.options = new HashSet<>(Arrays.asList(options));
return self();
}

/**
* Remove images after containers shutdown.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.testcontainers.junit;

import com.google.common.collect.ImmutableSet;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.testcontainers.containers.DockerComposeContainer;

import java.io.File;
import java.util.Set;

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 Set<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;

private final Set<String> options;
private final boolean expectError;

@Parameterized.Parameters(name = "docker-compose test [compose file: {0}, local: {1}, options: {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, ImmutableSet.of("--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, ImmutableSet.of(""), false},
// Test with a bad option. Compose will complain.
{new File("src/test/resources/compose-options-test/with-deploy-block.yml"), false, ImmutableSet.of("--bad-option"), true},
// Local compose
{new File("src/test/resources/compose-options-test/with-deploy-block.yml"), true, ImmutableSet.of("--compatibility"), false},
};
}

@Test
public void performTest() {

try (DockerComposeContainer<?> environment = new DockerComposeContainer<>(composeFile)
.withOptions(options.stream().toArray(String[]::new))
.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