-
-
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.
DockerComposeContainer: add 'removeVolumes' parameter (#7009)
Closes #7008 Co-authored-by: Eddú Meléndez <[email protected]>
- Loading branch information
1 parent
31430f6
commit 5b9c639
Showing
2 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
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
92 changes: 92 additions & 0 deletions
92
core/src/test/java/org/testcontainers/junit/DockerComposeContainerVolumeRemovalTest.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,92 @@ | ||
package org.testcontainers.junit; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.testcontainers.DockerClientFactory; | ||
import org.testcontainers.containers.DockerComposeContainer; | ||
|
||
import java.io.File; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
@RunWith(Parameterized.class) | ||
public class DockerComposeContainerVolumeRemovalTest { | ||
|
||
public DockerComposeContainerVolumeRemovalTest( | ||
final boolean removeVolumes, | ||
final boolean shouldVolumesBePresentAfterRunning | ||
) { | ||
this.removeVolumes = removeVolumes; | ||
this.shouldVolumesBePresentAfterRunning = shouldVolumesBePresentAfterRunning; | ||
} | ||
|
||
public final boolean removeVolumes; | ||
|
||
public final boolean shouldVolumesBePresentAfterRunning; | ||
|
||
@Parameterized.Parameters(name = "removeVolumes = {0}") | ||
public static Object[][] params() { | ||
return new Object[][] { { true, false }, { false, true } }; | ||
} | ||
|
||
@Test | ||
public void performTest() { | ||
final File composeFile = new File("src/test/resources/compose-test.yml"); | ||
|
||
final AtomicReference<String> volumeName = new AtomicReference<>(""); | ||
try ( | ||
DockerComposeContainer environment = new DockerComposeContainer<>(composeFile) | ||
.withExposedService("redis", 6379) | ||
.withRemoveVolumes(this.removeVolumes) | ||
.withRemoveImages(DockerComposeContainer.RemoveImages.ALL) | ||
) { | ||
environment.start(); | ||
|
||
volumeName.set(volumeNameForRunningContainer("_redis_1")); | ||
final boolean isVolumePresentWhileRunning = isVolumePresent(volumeName.get()); | ||
assertThat(isVolumePresentWhileRunning).as("the container volume is present while running").isEqualTo(true); | ||
} | ||
|
||
await() | ||
.untilAsserted(() -> { | ||
final boolean isVolumePresentAfterRunning = isVolumePresent(volumeName.get()); | ||
assertThat(isVolumePresentAfterRunning) | ||
.as("the container volume is present after running") | ||
.isEqualTo(this.shouldVolumesBePresentAfterRunning); | ||
}); | ||
} | ||
|
||
private String volumeNameForRunningContainer(final String containerNameSuffix) { | ||
return DockerClientFactory | ||
.instance() | ||
.client() | ||
.listContainersCmd() | ||
.exec() | ||
.stream() | ||
.filter(it -> Stream.of(it.getNames()).anyMatch(name -> name.endsWith(containerNameSuffix))) | ||
.findFirst() | ||
.map(container -> container.getMounts().get(0).getName()) | ||
.orElseThrow(IllegalStateException::new); | ||
} | ||
|
||
private boolean isVolumePresent(final String volumeName) { | ||
Set<String> nameFilter = new LinkedHashSet<>(1); | ||
nameFilter.add(volumeName); | ||
return DockerClientFactory | ||
.instance() | ||
.client() | ||
.listVolumesCmd() | ||
.withFilter("name", nameFilter) | ||
.exec() | ||
.getVolumes() | ||
.stream() | ||
.findFirst() | ||
.isPresent(); | ||
} | ||
} |