-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass options to compose down command (#9040)
Currently, setting options are supported for the `up` command. But, the same are not passed to the `down` command. Support for this has been added. Also, adding an example using `--profiles`. Fixes #5041
- Loading branch information
1 parent
d26eaca
commit 33e9f64
Showing
7 changed files
with
122 additions
and
23 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
core/src/main/java/org/testcontainers/containers/ComposeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
core/src/test/java/org/testcontainers/containers/ComposeProfilesOptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
core/src/test/java/org/testcontainers/containers/DockerComposeProfilesOptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
core/src/test/resources/compose-profile-option/compose-test.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |