Skip to content
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

Reap docker-compose networks after each test #211

Merged
merged 1 commit into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ private void registerContainersForShutdown() {

// Ensure that the default network for this compose environment, if any, is also cleaned up
ResourceReaper.instance().registerNetworkForCleanup(identifier + "_default");
// Compose can define their own networks as well; ensure these are cleaned up
dockerClient.listNetworksCmd().exec().forEach(network -> {
if (network.getName().contains(identifier)) {
ResourceReaper.instance().registerNetworkForCleanup(network.getName());
}
});

// remember the IDs to allow containers to be killed as soon as we reach stop()
spawnedContainerIds = containers.stream()
Expand Down Expand Up @@ -185,6 +191,9 @@ public void finished(Description description) {
// shut down all the ambassador containers
ambassadorContainers.forEach((String address, AmbassadorContainer container) -> container.stop());

// remove the networks before removing the containers
ResourceReaper.instance().removeNetworks(identifier);

// kill the spawned service containers
spawnedContainerIds.forEach(id -> ResourceReaper.instance().stopAndRemoveContainer(id));
spawnedContainerIds.clear();
Expand Down Expand Up @@ -305,4 +314,4 @@ public void start() {
}
logger().info("Docker compose has finished running");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ public void registerNetworkForCleanup(String networkName) {
registeredNetworks.add(networkName);
}

/**
* Removes any networks that contain the identifier.
* @param identifier
*/
public void removeNetworks(String identifier) {
removeNetwork(identifier);
}

private void removeNetwork(String networkName) {
List<Network> networks;
try {
Expand All @@ -133,10 +141,11 @@ private void removeNetwork(String networkName) {
for (Network network : networks) {
try {
dockerClient.removeNetworkCmd(network.getId()).exec();
registeredNetworks.remove(network.getId());
LOGGER.debug("Removed network: {}", networkName);
} catch (DockerException e) {
LOGGER.trace("Error encountered removing network (name: {}) - it may not have been removed", network.getName());
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package org.testcontainers.junit;

import com.github.dockerjava.api.model.Network;
import org.jetbrains.annotations.NotNull;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.*;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.utility.TestEnvironment;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.is;
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertThat;

/**
* Created by rnorth on 21/05/2016.
Expand All @@ -23,6 +28,8 @@ public abstract class BaseDockerComposeTest {

protected abstract DockerComposeContainer getEnvironment();

private List<String> existingNetworks = new ArrayList<>();

@BeforeClass
public static void checkVersion() {
Assume.assumeTrue(TestEnvironment.dockerApiAtLeast("1.22"));
Expand Down Expand Up @@ -59,6 +66,23 @@ public void secondTest() {
// However, @Rule creates a separate DockerComposeContainer instance per test, so this just shouldn't happen
}

@Before
public void captureNetworks() {
existingNetworks.addAll(findAllNetworks());
}

@After
public void verifyNoNetworks() {
assertThat("The networks", findAllNetworks(), is(existingNetworks));
}

private List<String> findAllNetworks() {
return DockerClientFactory.instance().client().listNetworksCmd().exec().stream()
.map(Network::getName)
.sorted()
.collect(Collectors.toList());
}

@NotNull
private Callable<Boolean> getLivenessCheck(Jedis jedis) {
return () -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.testcontainers.junit;

import java.io.File;

import org.junit.Rule;
import org.testcontainers.containers.DockerComposeContainer;

public class DockerComposeV2WithNetworkTest extends BaseDockerComposeTest {

@Rule
public DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/v2-compose-test-with-network.yml"))
.withExposedService("redis_1", REDIS_PORT);

@Override
protected DockerComposeContainer getEnvironment() {
return environment;
}



}
9 changes: 9 additions & 0 deletions core/src/test/resources/v2-compose-test-with-network.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '2'
services:
redis:
image: redis
networks:
- redis-net

networks:
redis-net: