Repository navigation
Add reuse existing container strategy #786
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
Closed
AnkBurov
wants to merge
1
commit into
testcontainers:master
from
AnkBurov:feature/reuse-existing-container
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
9 changes: 9 additions & 0 deletions
9
core/src/main/java/org/testcontainers/containers/ContainerStrategyType.java
This file contains hidden or 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,9 @@ | ||
| package org.testcontainers.containers; | ||
|
|
||
| /** | ||
| * @author Eugeny Karpov | ||
| */ | ||
| public enum ContainerStrategyType { | ||
| DISPOSABLE, | ||
| REUSABLE | ||
| } |
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
core/src/main/java/org/testcontainers/containers/ReusableContainerConfiguration.java
This file contains hidden or 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,43 @@ | ||
| package org.testcontainers.containers; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.apache.commons.lang.StringUtils; | ||
|
|
||
| /** | ||
| * @author Eugeny Karpov | ||
| */ | ||
| @Getter | ||
| @RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public class ReusableContainerConfiguration { | ||
|
|
||
| private final String containerName; | ||
| private final boolean isEnabled; | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private String containerName; | ||
| private Boolean isEnabled = true; | ||
|
|
||
| public Builder withContainerName(String containerName) { | ||
| this.containerName = containerName; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder isEnabled(boolean isEnabled) { | ||
| this.isEnabled = isEnabled; | ||
| return this; | ||
| } | ||
|
|
||
| public ReusableContainerConfiguration build() { | ||
| if (StringUtils.isBlank(containerName)) { | ||
| throw new IllegalArgumentException("Container name must be specified with REUSABLE mode"); | ||
| } | ||
| return new ReusableContainerConfiguration(containerName, isEnabled); | ||
| } | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
core/src/main/java/org/testcontainers/utility/DockerUtils.java
This file contains hidden or 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,25 @@ | ||
| package org.testcontainers.utility; | ||
|
|
||
| import com.github.dockerjava.api.model.Container; | ||
| import lombok.experimental.UtilityClass; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| /** | ||
| * @author Eugeny Karpov | ||
| */ | ||
| @UtilityClass | ||
| public class DockerUtils { | ||
|
|
||
| /** | ||
| * Check if container has specified name | ||
| * | ||
| * If container name starts with / (all names in com.github.dockerjava.api.model.Container.names are starting with /) | ||
| * then omit first letter | ||
| */ | ||
| public static boolean isContainerNameEqual(Container container, String containerName) { | ||
| return Arrays.stream(container.getNames()) | ||
| .map(name -> name.startsWith("/") ? name.substring(1) : name) | ||
| .anyMatch(name -> name.equals(containerName)); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would extract tryUseReusableContainer() and tryStartDisposableContainer() methods into separate classes like ReusableContainerLaunchStrategy and also methods createContainer() (with applyConfiguration private method) and startContainer() to Command pattern classes but it would require moving a lot of existing code from GenericContainer so to keep diff as small as possible I wrote new code in a procedure way like the remaining code of GenericContainer.