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

missing cleanup if FailureDetectingExternalResource::starting throws exception #335

Closed
tbcs opened this issue May 4, 2017 · 0 comments
Closed

Comments

@tbcs
Copy link
Contributor

tbcs commented May 4, 2017

If FailureDetectingExternalResource::starting throws an exception then FailureDetectingExternalResource::finished won't get invoked. This affects cleanup routines implemented in subclasses, such as in DockerComposeContainer::finished. An exception in starting() can lead to lingering Docker containers and networks.

Steps to reproduce

Define a service via docker-compose that exposes a service port to the host which is already in use (in this example it's a Cassandra server listening on localhost's TCP port 9042). Add another service to the docker-compose definition -- this is the container that will be left over after a failed start (in this example it's a Redis container). Run a unit test using a DockerComposeContainer with the said docker-compose service definition. Observe the lingering container after the unit test finished.

docker-compose service definition:

version: '2.1'
services:
  redis:
    image: redis
  cassandra:
    image: cassandra:2.2.3
    ports:
      - "127.0.0.1:9042:9042"

unit test:

package org.testcontainers.junit;
import lombok.extern.slf4j.Slf4j;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.DockerComposeContainer;
import java.io.File;
@Slf4j
public class StartFailsCleanupMissing {
    private static final int CASSANDRA_PORT = 9042;
    @Rule
    public DockerComposeContainer environment =
            new DockerComposeContainer(new File ("src/test/resources/start-fails-cleanup-missing.yml"))
                    .withExposedService("cassandra_1", CASSANDRA_PORT)
                    .withTailChildContainers(true)
                    .withLocalCompose(true);
    @Test
    public void test1() throws InterruptedException {
        Thread.sleep(60000);
    }
}

log output of unit test:

[...]
16:02:24.007 ERROR [docker-compose] - Creating network "ud59hm_default" with the default driver
16:02:24.235 ERROR [docker-compose] - Creating ud59hm_redis_1
16:02:24.237 ERROR [docker-compose] - Creating ud59hm_cassandra_1
16:02:25.931 ERROR [docker-compose] - ERROR: for cassandra  Cannot start service cassandra: driver failed programming external connectivity on endpoint ud59hm_cassandra_1 (9961a92541c51345ff0abdc44423b92a717cac069c35f3f22d32b275c78d08cb): Error starting userland proxy: listen tcp 127.0.0.1:9042: bind: address already in use
16:02:25.931 ERROR [docker-compose] - Encountered errors while bringing up the project.

org.testcontainers.containers.ContainerLaunchException: Local Docker Compose exited abnormally with code 1 whilst running command: up -d

	at org.testcontainers.containers.LocalDockerCompose.start(DockerComposeContainer.java:503)
	at org.testcontainers.containers.DockerComposeContainer.createServices(DockerComposeContainer.java:127)
	at org.testcontainers.containers.DockerComposeContainer.starting(DockerComposeContainer.java:109)
	at org.testcontainers.containers.FailureDetectingExternalResource$1.evaluate(FailureDetectingExternalResource.java:28)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
tbcs added a commit to tbcs/testcontainers-java that referenced this issue May 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant