diff --git a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/WaitStrategyHelper.java b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/WaitStrategyHelper.java new file mode 100644 index 0000000000..0aac700670 --- /dev/null +++ b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/WaitStrategyHelper.java @@ -0,0 +1,63 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.testcontainers; + +import java.net.URL; +import java.time.Duration; + +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.containers.wait.strategy.WaitStrategy; +import org.testcontainers.containers.wait.strategy.WaitStrategyTarget; + +/** + * Utility class helps to create proper Testcontainers wait strategies from different input. + */ +public final class WaitStrategyHelper { + + private WaitStrategyHelper() { + // prevent instantiation of utility class + } + + public static WaitStrategy getNoopStrategy() { + return new WaitStrategy() { + @Override + public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) { + } + + @Override + public WaitStrategy withStartupTimeout(Duration startupTimeout) { + return this; + } + }; + } + + public static WaitStrategy waitFor(URL url) { + if ("https".equals(url.getProtocol())) { + return Wait.forHttps(url.getPath()); + } else { + return Wait.forHttp(url.getPath()); + } + } + + public static WaitStrategy waitFor(String logMessage) { + return waitFor(logMessage, 1); + } + + public static WaitStrategy waitFor(String logMessage, int times) { + return Wait.forLogMessage(logMessage, times); + } +} diff --git a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/actions/StartTestcontainersAction.java b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/actions/StartTestcontainersAction.java index 4e064e1e11..3432f62c6c 100644 --- a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/actions/StartTestcontainersAction.java +++ b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/actions/StartTestcontainersAction.java @@ -29,11 +29,11 @@ import org.citrusframework.spi.Resource; import org.citrusframework.spi.Resources; import org.citrusframework.testcontainers.TestContainersSettings; +import org.citrusframework.testcontainers.WaitStrategyHelper; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.Network; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.WaitStrategy; -import org.testcontainers.containers.wait.strategy.WaitStrategyTarget; import org.testcontainers.utility.MountableFile; import static org.citrusframework.testcontainers.TestcontainersHelper.getEnvVarName; @@ -256,11 +256,7 @@ public B waitFor(WaitStrategy waitStrategy) { } public B waitFor(URL url) { - if ("https".equals(url.getProtocol())) { - this.waitStrategy = Wait.forHttps(url.getPath()); - } else { - this.waitStrategy = Wait.forHttp(url.getPath()); - } + this.waitStrategy = WaitStrategyHelper.waitFor(url); return self; } @@ -274,16 +270,7 @@ public B waitFor(String logMessage, int times) { } public B waitStrategyDisabled() { - this.waitStrategy = new WaitStrategy() { - @Override - public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) { - } - - @Override - public WaitStrategy withStartupTimeout(Duration startupTimeout) { - return this; - } - }; + this.waitStrategy = WaitStrategyHelper.getNoopStrategy(); return self; } diff --git a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/actions/TestcontainersActionBuilder.java b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/actions/TestcontainersActionBuilder.java index 6d124cbd4c..4c3d9bc951 100644 --- a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/actions/TestcontainersActionBuilder.java +++ b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/actions/TestcontainersActionBuilder.java @@ -17,7 +17,10 @@ package org.citrusframework.testcontainers.actions; import org.citrusframework.TestActionBuilder; +import org.citrusframework.spi.Resource; import org.citrusframework.testcontainers.aws2.StartLocalStackAction; +import org.citrusframework.testcontainers.compose.ComposeDownAction; +import org.citrusframework.testcontainers.compose.ComposeUpAction; import org.citrusframework.testcontainers.kafka.StartKafkaAction; import org.citrusframework.testcontainers.mongodb.StartMongoDBAction; import org.citrusframework.testcontainers.postgresql.StartPostgreSQLAction; @@ -85,6 +88,15 @@ public KafkaActionBuilder kafka() { return new KafkaActionBuilder(); } + + /** + * Manage Docker compose. + * @return + */ + public ComposeActionBuilder compose() { + return new ComposeActionBuilder(); + } + @Override public TestcontainersAction build() { ObjectHelper.assertNotNull(delegate, "Missing delegate action to build"); @@ -128,6 +140,48 @@ public StopTestcontainersAction.Builder stop() { } } + public class ComposeActionBuilder { + /** + * Start compose testcontainers instance. + */ + public ComposeUpAction.Builder up() { + ComposeUpAction.Builder builder = new ComposeUpAction.Builder(); + delegate = builder; + return builder; + } + + /** + * Start compose testcontainers instance. + */ + public ComposeUpAction.Builder up(String filePath) { + ComposeUpAction.Builder builder = new ComposeUpAction.Builder() + .file(filePath); + delegate = builder; + return builder; + } + + + + /** + * Start compose testcontainers instance. + */ + public ComposeUpAction.Builder up(Resource fileResource) { + ComposeUpAction.Builder builder = new ComposeUpAction.Builder() + .file(fileResource); + delegate = builder; + return builder; + } + + /** + * Stop compose testcontainers instance. + */ + public ComposeDownAction.Builder down() { + ComposeDownAction.Builder builder = new ComposeDownAction.Builder(); + delegate = builder; + return builder; + } + } + public class LocalStackActionBuilder { /** * Start LocalStack testcontainers instance. diff --git a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/compose/ComposeContainerSettings.java b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/compose/ComposeContainerSettings.java new file mode 100644 index 0000000000..b7fb447d37 --- /dev/null +++ b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/compose/ComposeContainerSettings.java @@ -0,0 +1,70 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.testcontainers.compose; + +import java.util.Optional; + +import org.citrusframework.testcontainers.TestContainersSettings; + +public class ComposeContainerSettings { + + private static final String COMPOSE_PROPERTY_PREFIX = TestContainersSettings.TESTCONTAINERS_PROPERTY_PREFIX + "compose."; + private static final String COMPOSE_ENV_PREFIX = TestContainersSettings.TESTCONTAINERS_ENV_PREFIX + "COMPOSE_"; + + private static final String CONTAINER_NAME_PROPERTY = COMPOSE_PROPERTY_PREFIX + "container.name"; + private static final String CONTAINER_NAME_ENV = COMPOSE_ENV_PREFIX + "CONTAINER_NAME"; + + private static final String USE_COMPOSE_BINARY_PROPERTY = COMPOSE_PROPERTY_PREFIX + "use.compose.binary"; + private static final String USE_COMPOSE_BINARY_ENV = COMPOSE_ENV_PREFIX + "USE_COMPOSE_BINARY"; + public static final String USE_COMPOSE_BINARY_DEFAULT = "true"; + + private static final String STARTUP_TIMEOUT_PROPERTY = COMPOSE_PROPERTY_PREFIX + "startup.timeout"; + private static final String STARTUP_TIMEOUT_ENV = COMPOSE_ENV_PREFIX + "STARTUP_TIMEOUT"; + + private ComposeContainerSettings() { + // prevent instantiation of utility class + } + + /** + * LocalStack container name. + * @return the container name. + */ + public static String getContainerName() { + return System.getProperty(CONTAINER_NAME_PROPERTY, + System.getenv(CONTAINER_NAME_ENV) != null ? System.getenv(CONTAINER_NAME_ENV) : ""); + } + + /** + * Time in seconds to wait for the container to startup and accept connections. + * @return + */ + public static int getStartupTimeout() { + return Optional.ofNullable(System.getProperty(STARTUP_TIMEOUT_PROPERTY, System.getenv(STARTUP_TIMEOUT_ENV))) + .map(Integer::parseInt) + .orElseGet(TestContainersSettings::getStartupTimeout); + } + + /** + * Uses local Docker compose binary when set to true. + * If enabled the experience is closest to using the Docker compose commands (e.g. docker compose up). + * @return + */ + public static boolean isUseComposeBinary() { + return Boolean.parseBoolean(System.getProperty(USE_COMPOSE_BINARY_PROPERTY, + System.getenv(USE_COMPOSE_BINARY_ENV) != null ? System.getenv(USE_COMPOSE_BINARY_ENV) : USE_COMPOSE_BINARY_DEFAULT)); + } +} diff --git a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/compose/ComposeDownAction.java b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/compose/ComposeDownAction.java new file mode 100644 index 0000000000..5b8cc0ef76 --- /dev/null +++ b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/compose/ComposeDownAction.java @@ -0,0 +1,70 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.testcontainers.compose; + +import org.citrusframework.context.TestContext; +import org.citrusframework.testcontainers.actions.AbstractTestcontainersAction; +import org.testcontainers.containers.ComposeContainer; + +public class ComposeDownAction extends AbstractTestcontainersAction { + + private final String containerName; + private final ComposeContainer container; + + public ComposeDownAction(Builder builder) { + super("compose-down", builder); + + this.containerName = builder.containerName; + this.container = builder.container; + } + + @Override + public void doExecute(TestContext context) { + if (container != null) { + container.stop(); + } else if (containerName != null && context.getReferenceResolver().isResolvable(containerName)) { + Object maybeContainer = context.getReferenceResolver().resolve(containerName); + if (maybeContainer instanceof ComposeContainer composeContainer) { + composeContainer.stop(); + } + } + } + + /** + * Action builder. + */ + public static class Builder extends AbstractTestcontainersAction.Builder { + + protected String containerName; + private ComposeContainer container; + + public Builder containerName(String name) { + this.containerName = name; + return this; + } + + public Builder container(ComposeContainer container) { + this.container = container; + return this; + } + + @Override + public ComposeDownAction doBuild() { + return new ComposeDownAction(this); + } + } +} diff --git a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/compose/ComposeUpAction.java b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/compose/ComposeUpAction.java new file mode 100644 index 0000000000..02b887bbec --- /dev/null +++ b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/compose/ComposeUpAction.java @@ -0,0 +1,252 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.testcontainers.compose; + +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import org.citrusframework.context.TestContext; +import org.citrusframework.exceptions.CitrusRuntimeException; +import org.citrusframework.spi.Resource; +import org.citrusframework.spi.Resources; +import org.citrusframework.testcontainers.TestContainersSettings; +import org.citrusframework.testcontainers.actions.AbstractTestcontainersAction; +import org.citrusframework.util.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.ComposeContainer; +import org.testcontainers.containers.ContainerState; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.containers.wait.strategy.WaitStrategy; +import org.testcontainers.utility.Base58; + +import static org.citrusframework.testcontainers.TestcontainersHelper.getEnvVarName; +import static org.citrusframework.testcontainers.actions.TestcontainersActionBuilder.testcontainers; + +public class ComposeUpAction extends AbstractTestcontainersAction { + + private final ComposeContainer container; + private final String containerName; + + private final boolean autoRemoveResources; + + private final Map exposedServices; + + /** Logger */ + private static final Logger logger = LoggerFactory.getLogger(ComposeUpAction.class); + + public ComposeUpAction(Builder builder) { + super("compose-up", builder); + + this.container = builder.container; + this.containerName = builder.containerName; + this.autoRemoveResources = builder.autoRemoveResources; + this.exposedServices = builder.exposedServices; + } + + @Override + public void doExecute(TestContext context) { + container.start(); + + if (containerName != null && !context.getReferenceResolver().isResolvable(containerName)) { + context.getReferenceResolver().bind(containerName, container); + } + + exposeConnectionSettings(container, context); + + if (autoRemoveResources) { + context.doFinally(testcontainers() + .compose() + .down() + .container(container)); + } + } + + /** + * Sets the connection settings in current test context in the form of test variables. + * @param container + * @param context + */ + protected void exposeConnectionSettings(ComposeContainer container, TestContext context) { + for (Map.Entry service : exposedServices.entrySet()) { + String containerType = service.getKey().toUpperCase().replaceAll("-", "_").replaceAll("\\.", "_"); + + Optional containerState = container.getContainerByServiceName(service.getKey()); + if (containerState.isPresent()) { + if (containerState.get().getContainerId() != null) { + String dockerContainerId = containerState.get().getContainerId().substring(0, 12); + String dockerContainerName = containerState.get().getContainerInfo().getName(); + + if (dockerContainerName.startsWith("/")) { + dockerContainerName = dockerContainerName.substring(1); + } + + context.setVariable(getEnvVarName(containerType, "SERVICE_HOST"), container.getServiceHost(service.getKey(), service.getValue())); + context.setVariable(getEnvVarName(containerType, "SERVICE_PORT"), container.getServicePort(service.getKey(), service.getValue())); + + context.setVariable(getEnvVarName(containerType, "HOST"), containerState.get().getHost()); + context.setVariable(getEnvVarName(containerType, "CONTAINER_IP"), container.getServiceHost(service.getKey(), service.getValue())); + context.setVariable(getEnvVarName(containerType, "CONTAINER_ID"), dockerContainerId); + context.setVariable(getEnvVarName(containerType, "CONTAINER_NAME"), dockerContainerName); + + if (!containerState.get().getExposedPorts().isEmpty()) { + context.setVariable(getEnvVarName(containerType, "PORT"), containerState.get().getFirstMappedPort()); + } + } + } + } + + } + + /** + * Action builder. + */ + public static class Builder extends AbstractTestcontainersAction.Builder { + + private ComposeContainer container; + + private String containerName = ComposeContainerSettings.getContainerName(); + + private String filePath; + + private Resource fileResource; + + private Duration startupTimeout = Duration.ofSeconds(TestContainersSettings.getStartupTimeout()); + + private boolean autoRemoveResources = TestContainersSettings.isAutoRemoveResources(); + + private final Map exposedServices = new HashMap<>(); + + private final Map waitStrategies = new HashMap<>(); + + private boolean useComposeBinary = ComposeContainerSettings.isUseComposeBinary(); + + public Builder() { + withStartupTimeout(ComposeContainerSettings.getStartupTimeout()); + } + + public Builder containerName(String name) { + this.containerName = name; + return self; + } + + public Builder container(ComposeContainer container) { + this.container = container; + return self; + } + + public Builder container(String name, ComposeContainer container) { + this.containerName = name; + this.container = container; + return self; + } + + public Builder file(String filePath) { + this.filePath = filePath; + return this; + } + + public Builder file(Resource fileResource) { + this.fileResource = fileResource; + return this; + } + + public Builder withStartupTimeout(int timeout) { + this.startupTimeout = Duration.ofSeconds(timeout); + return this; + } + + public Builder withStartupTimeout(Duration timeout) { + this.startupTimeout = timeout; + return this; + } + + public Builder autoRemove(boolean enabled) { + this.autoRemoveResources = enabled; + return self; + } + + public Builder useComposeBinary(boolean enabled) { + this.useComposeBinary = enabled; + return self; + } + + public Builder withExposedService(String serviceName, int port) { + this.exposedServices.put(serviceName, port); + return self; + } + + public Builder withExposedService(String serviceName, int port, WaitStrategy waitStrategy) { + this.exposedServices.put(serviceName, port); + this.waitStrategies.put(serviceName, waitStrategy); + return self; + } + + public Builder withExposedServices(Map services) { + exposedServices.putAll(services); + return self; + } + + public Builder withWaitStrategy(String serviceName, WaitStrategy waitStrategy) { + this.waitStrategies.put(serviceName, waitStrategy); + return self; + } + + public Builder withWaitStrategies(Map strategies) { + waitStrategies.putAll(strategies); + return self; + } + + @Override + public ComposeUpAction doBuild() { + String identifier = StringUtils.hasText(containerName) ? containerName.toLowerCase() : Base58.randomString(6).toLowerCase(); + + if (fileResource != null) { + container = new ComposeContainer(identifier, fileResource.getFile()); + } else if (StringUtils.hasText(filePath)) { + container = new ComposeContainer(identifier, Resources.create(filePath).getFile()); + } else if (referenceResolver != null) { + if (StringUtils.hasText(containerName) && referenceResolver.isResolvable(containerName, ComposeContainer.class)) { + container = referenceResolver.resolve(containerName, ComposeContainer.class); + } else if (referenceResolver.isResolvable(ComposeContainer.class)) { + container = referenceResolver.resolve(ComposeContainer.class); + } + } + + if (container == null && Resources.create("compose.yaml").exists()) { + container = new ComposeContainer(identifier, Resources.create("compose.yaml").getFile()); + } + + if (container == null) { + throw new CitrusRuntimeException("Missing proper ComposeContainer specification - either provide a container name or compose file resource"); + } + + if (useComposeBinary) { + container.withLocalCompose(true); + } + + for (Map.Entry service : exposedServices.entrySet()) { + WaitStrategy waitStrategy = waitStrategies.getOrDefault(service.getKey(), Wait.forListeningPort().withStartupTimeout(startupTimeout)); + container.withExposedService(service.getKey(), service.getValue(), waitStrategy); + } + + return new ComposeUpAction(this); + } + } +} diff --git a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/xml/Compose.java b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/xml/Compose.java new file mode 100644 index 0000000000..148cbacc12 --- /dev/null +++ b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/xml/Compose.java @@ -0,0 +1,273 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.testcontainers.xml; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlAttribute; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlType; +import org.citrusframework.TestActor; +import org.citrusframework.exceptions.CitrusRuntimeException; +import org.citrusframework.spi.ReferenceResolver; +import org.citrusframework.spi.ReferenceResolverAware; +import org.citrusframework.testcontainers.TestContainersSettings; +import org.citrusframework.testcontainers.WaitStrategyHelper; +import org.citrusframework.testcontainers.actions.AbstractTestcontainersAction; +import org.citrusframework.testcontainers.compose.ComposeDownAction; +import org.citrusframework.testcontainers.compose.ComposeUpAction; +import org.citrusframework.util.ObjectHelper; +import org.citrusframework.util.StringUtils; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.containers.wait.strategy.WaitStrategy; + +public class Compose extends AbstractTestcontainersAction.Builder implements ReferenceResolverAware { + + private AbstractTestcontainersAction.Builder delegate; + + @XmlElement + public void setUp(Up composeUp) { + ComposeUpAction.Builder builder = new ComposeUpAction.Builder(); + + builder.containerName(composeUp.getName()); + builder.file(composeUp.getFile()); + builder.autoRemove(composeUp.isAutoRemove()); + + if (composeUp.getStartUpTimeout() > 0) { + builder.withStartupTimeout(composeUp.getStartUpTimeout()); + } + + composeUp.getExposedServices().forEach(exposedService -> { + if (exposedService.getWaitFor() != null) { + WaitStrategy waitStrategy; + if (exposedService.getWaitFor().isDisabled()) { + waitStrategy = WaitStrategyHelper.getNoopStrategy(); + } else if (StringUtils.hasText(exposedService.getWaitFor().getLogMessage())) { + waitStrategy = WaitStrategyHelper.waitFor(exposedService.getWaitFor().getLogMessage()); + } else if (StringUtils.hasText(exposedService.getWaitFor().getUrl())) { + try { + waitStrategy = WaitStrategyHelper.waitFor(new URL(exposedService.getWaitFor().getUrl())); + } catch (MalformedURLException e) { + throw new CitrusRuntimeException("Invalid Http(s) URL to wait for: %s".formatted(exposedService.getWaitFor().getUrl()), e); + } + } else { + waitStrategy = Wait.defaultWaitStrategy(); + } + + builder.withExposedService(exposedService.getName(), exposedService.getPort(), waitStrategy); + } else { + builder.withExposedService(exposedService.getName(), exposedService.getPort()); + } + }); + + delegate = builder; + } + + @XmlElement + public void setDown(Down composeDown) { + ComposeDownAction.Builder builder = new ComposeDownAction.Builder(); + builder.containerName(composeDown.getName()); + delegate = builder; + } + + @Override + public Compose description(String description) { + delegate.description(description); + return this; + } + + @Override + public Compose actor(TestActor actor) { + delegate.actor(actor); + return this; + } + + @Override + public void setReferenceResolver(ReferenceResolver referenceResolver) { + this.delegate.setReferenceResolver(referenceResolver); + } + + @Override + public AbstractTestcontainersAction doBuild() { + ObjectHelper.assertNotNull(delegate); + return delegate.build(); + } + + @XmlAccessorType(XmlAccessType.FIELD) + @XmlType(name = "", propOrder = { + "exposedServices", + }) + public static class Up { + + @XmlAttribute + private String name; + + @XmlAttribute + private String file; + + @XmlAttribute + private int startUpTimeout; + + @XmlAttribute + protected boolean autoRemove = TestContainersSettings.isAutoRemoveResources(); + + @XmlElement + protected List exposedServices; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getFile() { + return file; + } + + public void setFile(String file) { + this.file = file; + } + + public boolean isAutoRemove() { + return autoRemove; + } + + public void setAutoRemove(boolean autoRemove) { + this.autoRemove = autoRemove; + } + + public int getStartUpTimeout() { + return startUpTimeout; + } + + public void setStartUpTimeout(int startUpTimeout) { + this.startUpTimeout = startUpTimeout; + } + + public void setExposedServices(List services) { + this.exposedServices = services; + } + + public List getExposedServices() { + if (exposedServices == null) { + exposedServices = new ArrayList<>(); + } + return exposedServices; + } + } + + @XmlAccessorType(XmlAccessType.FIELD) + @XmlType(name = "", propOrder = { + "waitFor", + }) + public static class ExposedService { + + @XmlAttribute + protected String name; + + @XmlAttribute + protected int port; + + @XmlElement(name = "wait-for") + protected WaitFor waitFor; + + public String getName() { + return name; + } + + public void setName(String value) { + this.name = value; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public WaitFor getWaitFor() { + return waitFor; + } + + public void setWaitFor(WaitFor waitFor) { + this.waitFor = waitFor; + } + + @XmlAccessorType(XmlAccessType.FIELD) + @XmlType(name = "") + public static class WaitFor { + + @XmlAttribute(name = "log-message") + private String logMessage; + + @XmlAttribute + private String url; + + @XmlAttribute + private boolean disabled; + + public String getLogMessage() { + return logMessage; + } + + public void setLogMessage(String logMessage) { + this.logMessage = logMessage; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public boolean isDisabled() { + return disabled; + } + + public void setDisabled(boolean disabled) { + this.disabled = disabled; + } + } + } + + @XmlAccessorType(XmlAccessType.FIELD) + @XmlType(name = "") + public static class Down { + + @XmlAttribute + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} diff --git a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/xml/Testcontainers.java b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/xml/Testcontainers.java index fe5e054045..14748164d2 100644 --- a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/xml/Testcontainers.java +++ b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/xml/Testcontainers.java @@ -50,6 +50,11 @@ public Testcontainers setActor(String actor) { return this; } + @XmlElement(name = "compose") + public void setCompose(Compose builder) { + this.builder = builder; + } + @XmlElement(name = "start") public void setStart(Start builder) { this.builder = builder; diff --git a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/yaml/Compose.java b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/yaml/Compose.java new file mode 100644 index 0000000000..2fb202195a --- /dev/null +++ b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/yaml/Compose.java @@ -0,0 +1,242 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.testcontainers.yaml; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.citrusframework.TestActor; +import org.citrusframework.exceptions.CitrusRuntimeException; +import org.citrusframework.spi.ReferenceResolver; +import org.citrusframework.spi.ReferenceResolverAware; +import org.citrusframework.testcontainers.TestContainersSettings; +import org.citrusframework.testcontainers.WaitStrategyHelper; +import org.citrusframework.testcontainers.actions.AbstractTestcontainersAction; +import org.citrusframework.testcontainers.compose.ComposeDownAction; +import org.citrusframework.testcontainers.compose.ComposeUpAction; +import org.citrusframework.util.ObjectHelper; +import org.citrusframework.util.StringUtils; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.containers.wait.strategy.WaitStrategy; + +public class Compose extends AbstractTestcontainersAction.Builder implements ReferenceResolverAware { + + private AbstractTestcontainersAction.Builder delegate; + + public void setUp(Up composeUp) { + ComposeUpAction.Builder builder = new ComposeUpAction.Builder(); + + builder.containerName(composeUp.getName()); + builder.file(composeUp.getFile()); + builder.autoRemove(composeUp.isAutoRemove()); + + if (composeUp.getStartUpTimeout() > 0) { + builder.withStartupTimeout(composeUp.getStartUpTimeout()); + } + + composeUp.getExposedServices().forEach(exposedService -> { + if (exposedService.getWaitFor() != null) { + WaitStrategy waitStrategy; + if (exposedService.getWaitFor().isDisabled()) { + waitStrategy = WaitStrategyHelper.getNoopStrategy(); + } else if (StringUtils.hasText(exposedService.getWaitFor().getLogMessage())) { + waitStrategy = WaitStrategyHelper.waitFor(exposedService.getWaitFor().getLogMessage()); + } else if (StringUtils.hasText(exposedService.getWaitFor().getUrl())) { + try { + waitStrategy = WaitStrategyHelper.waitFor(new URL(exposedService.getWaitFor().getUrl())); + } catch (MalformedURLException e) { + throw new CitrusRuntimeException("Invalid Http(s) URL to wait for: %s".formatted(exposedService.getWaitFor().getUrl()), e); + } + } else { + waitStrategy = Wait.defaultWaitStrategy(); + } + + builder.withExposedService(exposedService.getName(), exposedService.getPort(), waitStrategy); + } else { + builder.withExposedService(exposedService.getName(), exposedService.getPort()); + } + }); + + delegate = builder; + } + + public void setDown(Down composeDown) { + ComposeDownAction.Builder builder = new ComposeDownAction.Builder(); + builder.containerName(composeDown.getName()); + delegate = builder; + } + + @Override + public Compose description(String description) { + delegate.description(description); + return this; + } + + @Override + public Compose actor(TestActor actor) { + delegate.actor(actor); + return this; + } + + @Override + public void setReferenceResolver(ReferenceResolver referenceResolver) { + this.delegate.setReferenceResolver(referenceResolver); + } + + @Override + public AbstractTestcontainersAction doBuild() { + ObjectHelper.assertNotNull(delegate); + return delegate.build(); + } + + public static class Up { + + private String name; + + private String file; + + private int startUpTimeout; + + protected boolean autoRemove = TestContainersSettings.isAutoRemoveResources(); + + protected List exposedServices; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getFile() { + return file; + } + + public void setFile(String file) { + this.file = file; + } + + public boolean isAutoRemove() { + return autoRemove; + } + + public void setAutoRemove(boolean autoRemove) { + this.autoRemove = autoRemove; + } + + public int getStartUpTimeout() { + return startUpTimeout; + } + + public void setStartUpTimeout(int startUpTimeout) { + this.startUpTimeout = startUpTimeout; + } + + public void setExposedServices(List services) { + this.exposedServices = services; + } + + public List getExposedServices() { + if (exposedServices == null) { + exposedServices = new ArrayList<>(); + } + return exposedServices; + } + } + + public static class ExposedService { + + protected String name; + + protected int port; + + protected WaitFor waitFor; + + public String getName() { + return name; + } + + public void setName(String value) { + this.name = value; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public WaitFor getWaitFor() { + return waitFor; + } + + public void setWaitFor(WaitFor waitFor) { + this.waitFor = waitFor; + } + + public static class WaitFor { + + private String logMessage; + + private String url; + + private boolean disabled; + + public String getLogMessage() { + return logMessage; + } + + public void setLogMessage(String logMessage) { + this.logMessage = logMessage; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public boolean isDisabled() { + return disabled; + } + + public void setDisabled(boolean disabled) { + this.disabled = disabled; + } + } + } + + public static class Down { + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} diff --git a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/yaml/Testcontainers.java b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/yaml/Testcontainers.java index 888b6a4e85..714d857ac1 100644 --- a/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/yaml/Testcontainers.java +++ b/connectors/citrus-testcontainers/src/main/java/org/citrusframework/testcontainers/yaml/Testcontainers.java @@ -44,6 +44,10 @@ public Testcontainers setActor(String actor) { return this; } + public void setCompose(Compose builder) { + this.builder = builder; + } + public void setStart(Start builder) { this.builder = builder; } diff --git a/connectors/citrus-testcontainers/src/test/java/org/citrusframework/testcontainers/integration/compose/ComposeIT.java b/connectors/citrus-testcontainers/src/test/java/org/citrusframework/testcontainers/integration/compose/ComposeIT.java new file mode 100644 index 0000000000..848407b0f1 --- /dev/null +++ b/connectors/citrus-testcontainers/src/test/java/org/citrusframework/testcontainers/integration/compose/ComposeIT.java @@ -0,0 +1,76 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.testcontainers.integration.compose; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +import org.citrusframework.TestAction; +import org.citrusframework.actions.AbstractTestAction; +import org.citrusframework.annotations.CitrusTest; +import org.citrusframework.context.TestContext; +import org.citrusframework.exceptions.CitrusRuntimeException; +import org.citrusframework.spi.Resources; +import org.citrusframework.testcontainers.integration.AbstractTestcontainersIT; +import org.citrusframework.util.FileUtils; +import org.testng.Assert; +import org.testng.annotations.Test; + +import static org.citrusframework.container.FinallySequence.Builder.doFinally; +import static org.citrusframework.container.Wait.Builder.waitFor; +import static org.citrusframework.testcontainers.actions.TestcontainersActionBuilder.testcontainers; + +public class ComposeIT extends AbstractTestcontainersIT { + + @Test + @CitrusTest + public void shouldStartContainer() { + given(doFinally().actions(testcontainers().compose() + .down())); + + when(testcontainers() + .compose() + .up(Resources.create("compose.yaml", ComposeIT.class))); + + then(waitFor().http().url("http://localhost:8880")); + + then(verifyExposedHttpEndpoint()); + } + + private TestAction verifyExposedHttpEndpoint() { + return new AbstractTestAction() { + @Override + public void doExecute(TestContext context) { + try { + HttpResponse response = HttpClient.newHttpClient().send( + HttpRequest.newBuilder() + .uri(new URI("http://localhost:8880")) + .GET() + .build(), HttpResponse.BodyHandlers.ofString()); + + Assert.assertEquals(response.statusCode(), 200); + Assert.assertEquals(response.body(), + FileUtils.readToString(Resources.create("html/index.html", ComposeIT.class))); + } catch (Exception e) { + throw new CitrusRuntimeException("Failed to verify exposed Http endpoint", e); + } + } + }; + } +} diff --git a/connectors/citrus-testcontainers/src/test/java/org/citrusframework/testcontainers/integration/xml/ComposeTest.java b/connectors/citrus-testcontainers/src/test/java/org/citrusframework/testcontainers/integration/xml/ComposeTest.java new file mode 100644 index 0000000000..3e7395e338 --- /dev/null +++ b/connectors/citrus-testcontainers/src/test/java/org/citrusframework/testcontainers/integration/xml/ComposeTest.java @@ -0,0 +1,41 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.testcontainers.integration.xml; + +import org.citrusframework.TestCase; +import org.citrusframework.TestCaseMetaInfo; +import org.citrusframework.testcontainers.compose.ComposeUpAction; +import org.citrusframework.xml.XmlTestLoader; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class ComposeTest extends AbstractXmlActionTest { + + @Test + public void shouldLoadTestcontainersActions() { + XmlTestLoader testLoader = createTestLoader("classpath:org/citrusframework/testcontainers/xml/compose-test.xml"); + + testLoader.load(); + TestCase result = testLoader.getTestCase(); + Assert.assertEquals(result.getName(), "ComposeTest"); + Assert.assertEquals(result.getMetaInfo().getAuthor(), "Christoph"); + Assert.assertEquals(result.getMetaInfo().getStatus(), TestCaseMetaInfo.Status.FINAL); + Assert.assertEquals(result.getActionCount(), 4L); + Assert.assertEquals(result.getTestAction(0).getClass(), ComposeUpAction.class); + Assert.assertTrue(result.getTestResult().isSuccess()); + } +} diff --git a/connectors/citrus-testcontainers/src/test/java/org/citrusframework/testcontainers/integration/yaml/ComposeTest.java b/connectors/citrus-testcontainers/src/test/java/org/citrusframework/testcontainers/integration/yaml/ComposeTest.java new file mode 100644 index 0000000000..85e74ccb7e --- /dev/null +++ b/connectors/citrus-testcontainers/src/test/java/org/citrusframework/testcontainers/integration/yaml/ComposeTest.java @@ -0,0 +1,41 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.testcontainers.integration.yaml; + +import org.citrusframework.TestCase; +import org.citrusframework.TestCaseMetaInfo; +import org.citrusframework.testcontainers.compose.ComposeUpAction; +import org.citrusframework.yaml.YamlTestLoader; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class ComposeTest extends AbstractYamlActionTest { + + @Test + public void shouldLoadTestcontainersActions() { + YamlTestLoader testLoader = createTestLoader("classpath:org/citrusframework/testcontainers/yaml/compose-test.yaml"); + + testLoader.load(); + TestCase result = testLoader.getTestCase(); + Assert.assertEquals(result.getName(), "ComposeTest"); + Assert.assertEquals(result.getMetaInfo().getAuthor(), "Christoph"); + Assert.assertEquals(result.getMetaInfo().getStatus(), TestCaseMetaInfo.Status.FINAL); + Assert.assertEquals(result.getActionCount(), 4L); + Assert.assertEquals(result.getTestAction(0).getClass(), ComposeUpAction.class); + Assert.assertTrue(result.getTestResult().isSuccess()); + } +} diff --git a/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/integration/compose/compose.yaml b/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/integration/compose/compose.yaml new file mode 100644 index 0000000000..1d5cdb79d1 --- /dev/null +++ b/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/integration/compose/compose.yaml @@ -0,0 +1,7 @@ +services: + web: + image: nginx:latest + ports: + - '8880:80' + volumes: + - ./html:/usr/share/nginx/html:ro diff --git a/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/integration/compose/html/index.html b/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/integration/compose/html/index.html new file mode 100644 index 0000000000..701c32b679 --- /dev/null +++ b/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/integration/compose/html/index.html @@ -0,0 +1,21 @@ + + + + +

Hello World

+ + diff --git a/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/xml/compose-test.xml b/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/xml/compose-test.xml new file mode 100644 index 0000000000..2ffd2e008b --- /dev/null +++ b/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/xml/compose-test.xml @@ -0,0 +1,52 @@ + + + + Sample test in XML + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/yaml/compose-test.yaml b/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/yaml/compose-test.yaml new file mode 100644 index 0000000000..5b044498c7 --- /dev/null +++ b/connectors/citrus-testcontainers/src/test/resources/org/citrusframework/testcontainers/yaml/compose-test.yaml @@ -0,0 +1,29 @@ +name: "ComposeTest" +author: "Christoph" +status: "FINAL" +description: Sample test in XML +actions: + - testcontainers: + compose: + up: + file: "classpath:org/citrusframework/testcontainers/integration/compose/compose.yaml" + - waitFor: + http: + url: "http://localhost:8880" + - http: + client: "http://localhost:8880" + sendRequest: + GET: {} + - http: + client: "http://localhost:8880" + receiveResponse: + response: + status: "200" + reasonPhrase: "OK" + body: + resource: + file: "classpath:org/citrusframework/testcontainers/integration/compose/html/index.html" +finally: + - testcontainers: + compose: + down: {} diff --git a/runtime/citrus-xml/src/main/resources/org/citrusframework/schema/xml/testcase/citrus-testcase-4.5.0-SNAPSHOT.xsd b/runtime/citrus-xml/src/main/resources/org/citrusframework/schema/xml/testcase/citrus-testcase-4.5.0-SNAPSHOT.xsd index abab21874e..5181e4bef3 100644 --- a/runtime/citrus-xml/src/main/resources/org/citrusframework/schema/xml/testcase/citrus-testcase-4.5.0-SNAPSHOT.xsd +++ b/runtime/citrus-xml/src/main/resources/org/citrusframework/schema/xml/testcase/citrus-testcase-4.5.0-SNAPSHOT.xsd @@ -1918,6 +1918,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/runtime/citrus-xml/src/main/resources/org/citrusframework/schema/xml/testcase/citrus-testcase.xsd b/runtime/citrus-xml/src/main/resources/org/citrusframework/schema/xml/testcase/citrus-testcase.xsd index abab21874e..5181e4bef3 100644 --- a/runtime/citrus-xml/src/main/resources/org/citrusframework/schema/xml/testcase/citrus-testcase.xsd +++ b/runtime/citrus-xml/src/main/resources/org/citrusframework/schema/xml/testcase/citrus-testcase.xsd @@ -1918,6 +1918,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/manual/connector-testcontainers.adoc b/src/manual/connector-testcontainers.adoc index 2729d5e3c1..629cb7a269 100644 --- a/src/manual/connector-testcontainers.adoc +++ b/src/manual/connector-testcontainers.adoc @@ -609,3 +609,127 @@ Citrus exposes special test variables that represent connection settings for the |=== You can use these test variables to connect to the Redpanda Testcontainers instance. + +[[testcontainers-compose]] +== Docker compose + +The Testcontainers project provides the opportunity to interact with Docker compose. +In Citrus you can start a Docker compose specification (`compose.yaml`) as part of the test. + +IMPORTANT: By default, the Testcontainers compose container uses your local Docker compose executable binary. This means you need to have Docker compose installed on your machine. You can verify the installation with (`docker compose version`). + +NOTE: If for some reason you are not able to install Docker compose you may also disable the `useComposeBinary` setting in Citrus (e.g. via System property or environment variable). The Testcontainers library will then use arbitrary Docker containers in combination with an ambassador container that stars and manages the services defined in the Docker compose specification. + +A Docker compose specification may look like this: + +.compose.yaml +[source,yaml,indent=0] +---- +services: + web: + image: nginx:latest + ports: + - '8080:80' + volumes: + - ./html:/usr/share/nginx/html:ro +---- + +The specification defines one or more services that are started as part of the `compose up` command: + +.Java +[source,java,indent=0,role="primary"] +---- +@CitrusTest +public void composeTest() { + given(testcontainers() + .compose() + .up() + .file("compose.yaml")); +} +---- + +.XML +[source,xml,indent=0,role="secondary"] +---- + + + + + + + + + +---- + +.YAML +[source,yaml,indent=0,role="secondary"] +---- +name: ComposeTest +actions: + - testcontainers: + compose: + up: + file: "compose.yaml" +---- + +.Spring XML +[source,xml,indent=0,role="secondary"] +---- + + + +---- + +NOTE: The Docker containers started with Docker compose will be automatically stopped and removed after the test. +You can change this behavior by setting one of the System property or environment variable settings: `citrus.testcontainers.compose.auto.remove.resources=false` or `CITRUS_TESTCONTAINERS_COMPOSE_AUTO_REMOVE_RESOURCES=false` + +You may explicitly stop all Docker containers with the `compose down` command: + +.Java +[source,java,indent=0,role="primary"] +---- +@CitrusTest +public void composeTest() { + given(testcontainers() + .compose() + .down()); +} +---- + +.XML +[source,xml,indent=0,role="secondary"] +---- + + + + + + + + + +---- + +.YAML +[source,yaml,indent=0,role="secondary"] +---- +name: ComposeTest +actions: + - testcontainers: + compose: + down: {} +---- + +.Spring XML +[source,xml,indent=0,role="secondary"] +---- + + + +---- + +IMPORTANT: The `compose down` test action searches for a single Testcontainers compose container that has been started before. +In case you are operating with multiple `compose up` commands in a test you may identify each of those compose containers with a unique name and reference this name in the `compose down` test action.