testcontainers / testcontainers-java

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
https://testcontainers.org
MIT License
8.03k stars 1.65k forks source link

Multiple postgres containers launch during tests #612

Closed jeacott1 closed 6 years ago

jeacott1 commented 6 years ago

I have a spring boot application and a junit 4 test marked with:

@SpringBootTest(classes=Application.class)
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@WebAppConfiguration
public class SimpleTest {

    @Rule
    public ElasticsearchResource elasticsearch = new ElasticsearchResource("docker.elastic.co/elasticsearch/elasticsearch-oss");

    @Rule
    public PostgreSQLContainer postgres = new PostgreSQLContainer();

...

    @Test
    public void testSleep() throws Exception {
            Thread.sleep(50000L);
    }
}

but I'm noticing that docker is actually running 2 instances of postgres:

$ docker ps
CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                                              NAMES
d47fb2c45c24        testcontainers/bzqmcegyqqvkpcbt   "/usr/local/bin/dock…"   8 seconds ago       Up 6 seconds        0.0.0.0:33231->9200/tcp, 0.0.0.0:33230->9300/tcp   fervent_neumann
77939249e514        postgres:latest                   "docker-entrypoint.s…"   12 seconds ago      Up 10 seconds       0.0.0.0:33229->5432/tcp                            amazing_goldberg
2f99d2b44cf7        postgres:latest                   "docker-entrypoint.s…"   22 seconds ago      Up 21 seconds       0.0.0.0:33228->5432/tcp                            vigilant_einstein
c353bb492d33        bsideup/moby-ryuk:0.2.2           "/app"                   27 seconds ago      Up 26 seconds       0.0.0.0:33227->8080/tcp                            testcontainers-ryuk-51119120-eaf7-428e-a7ac-3de4af4659e4

it all eventually gets cleaned up, but this looks wrong.

I never get 2 instances of Elasticsearch, and everything else seems fine.

if I configure a regular spring test and don't point the config at the main springBoot Application class, but just regular @Configuration annotated classes, then I get just a single instance of the postgres docker, but that obviously isn't useful for integration tests.

bsideup commented 6 years ago

@jeacott1 are you using JDBC-based support? If so, just remove PostgreSQLContainer from your code.

jeacott commented 6 years ago

aah, right, I missed that the 2 methods are independent. thats a shame as without the rule wouldnt I lose test lifecycle support?, but modifying the jdbc url makes testing with testcontainers as easy as with h2.
I had thought the custom driver was doing the dynamic discovery of a container launched via the rule, and perhaps thats an option that could be considered? regardless its now clear why I see 2 instances launched.

thanks.

bsideup commented 6 years ago

@jeacott Testcontainers will automatically stop the container when you close your JDBC driver connection (i.e. when Spring app stops), so you're not loosing the lifecycle support actually :)