-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Testcontainers Docker compose support
- Users can call `compose up` and `compose down` commands as part of the test - By default requires Docker compose binary being installed
- Loading branch information
1 parent
07228fc
commit 560aff7
Showing
20 changed files
with
1,499 additions
and
16 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...s-testcontainers/src/main/java/org/citrusframework/testcontainers/WaitStrategyHelper.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,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); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
...rs/src/main/java/org/citrusframework/testcontainers/compose/ComposeContainerSettings.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,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)); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...ontainers/src/main/java/org/citrusframework/testcontainers/compose/ComposeDownAction.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,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<ComposeDownAction, 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); | ||
} | ||
} | ||
} |
Oops, something went wrong.