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

Pass options to compose down command #9040

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,37 @@
package org.testcontainers.containers;

import org.apache.commons.lang3.StringUtils;

import java.util.Set;

class ComposeCommand {

static String getDownCommand(ComposeDelegate.ComposeVersion composeVersion, Set<String> options) {
String composeOptions = optionsAsString(options);
if (composeOptions == null || composeOptions.equals("")) {
return composeVersion == ComposeDelegate.ComposeVersion.V1 ? "down" : "compose down";
}
String cmd = composeVersion == ComposeDelegate.ComposeVersion.V1 ? "%s down" : "compose %s down";
return String.format(cmd, composeOptions);
}

static String getUpCommand(ComposeDelegate.ComposeVersion composeVersion, Set<String> options) {
String composeOptions = optionsAsString(options);
if (composeOptions == null || composeOptions.equals("")) {
return composeVersion == ComposeDelegate.ComposeVersion.V1 ? "up -d" : "compose up -d";
}
String cmd = composeVersion == ComposeDelegate.ComposeVersion.V1 ? "%s up -d" : "compose %s up -d";
return String.format(cmd, composeOptions);
}

private static String optionsAsString(final Set<String> options) {
String optionsString = String.join(" ", options);
if (!optionsString.isEmpty()) {
// 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ public void stop() {
this.composeDelegate.getAmbassadorContainer().stop();

// Kill the services using docker
String cmd = "compose down";
String cmd = ComposeCommand.getDownCommand(ComposeDelegate.ComposeVersion.V2, this.options);

if (removeVolumes) {
cmd += " -v";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import lombok.NonNull;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.output.Slf4jLogConsumer;
Expand Down Expand Up @@ -146,7 +145,7 @@ void createServices(
.distinct()
.collect(Collectors.joining(" "));

String command = getUpCommand(optionsAsString(options));
String command = ComposeCommand.getUpCommand(this.composeVersion, options);

if (build) {
command += " --build";
Expand All @@ -164,25 +163,6 @@ void createServices(
runWithCompose(localCompose, command, env, fileCopyInclusions);
}

private String getUpCommand(String options) {
if (options == null || options.equals("")) {
return this.composeVersion == ComposeVersion.V1 ? "up -d" : "compose up -d";
}
String cmd = this.composeVersion == ComposeVersion.V1 ? "%s up -d" : "compose %s up -d";
return String.format(cmd, options);
}

private String optionsAsString(final Set<String> options) {
String optionsString = options.stream().collect(Collectors.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;
}
}

void waitUntilServiceStarted(boolean tailChildContainers) {
listChildContainers().forEach(container -> createServiceInstance(container, tailChildContainers));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void stop() {
this.composeDelegate.getAmbassadorContainer().stop();

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

if (removeVolumes) {
cmd += " -v";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.testcontainers.containers;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(Parameterized.class)
public class ComposeProfilesOptionTest {

@Parameterized.Parameters(name = "{0}")
public static Boolean[] local() {
return new Boolean[] { Boolean.TRUE, Boolean.FALSE };
}

@Parameterized.Parameter
public boolean local;

public static final File COMPOSE_FILE = new File("src/test/resources/compose-profile-option/compose-test.yml");

@Test
public void testProfileOption() {
try (
ComposeContainer compose = new ComposeContainer(COMPOSE_FILE)
.withOptions("--profile=cache")
.withLocalCompose(true)
) {
compose.start();
assertThat(compose.listChildContainers()).hasSize(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.testcontainers.containers;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(Parameterized.class)
public class DockerComposeProfilesOptionTest {

@Parameterized.Parameters(name = "{0}")
public static Boolean[] local() {
return new Boolean[] { Boolean.TRUE, Boolean.FALSE };
}

@Parameterized.Parameter
public boolean local;

public static final File COMPOSE_FILE = new File("src/test/resources/compose-profile-option/compose-test.yml");

@Test
public void testProfileOption() {
try (
DockerComposeContainer<?> compose = new DockerComposeContainer<>(COMPOSE_FILE)
.withOptions("--profile=cache")
.withLocalCompose(this.local)
) {
compose.start();
assertThat(compose.listChildContainers()).hasSize(1);
}
}
}
11 changes: 11 additions & 0 deletions core/src/test/resources/compose-profile-option/compose-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
redis:
image: redis
profiles:
- cache
db:
image: mysql:8.0.36
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "true"
profiles:
- db
Loading