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

Trying to reuse container - shut down in the end #2352

Closed
bojanv55 opened this issue Feb 13, 2020 · 10 comments
Closed

Trying to reuse container - shut down in the end #2352

bojanv55 opened this issue Feb 13, 2020 · 10 comments

Comments

@bojanv55
Copy link

bojanv55 commented Feb 13, 2020

I'm trying to reuse container, using this config:

@Testcontainers
public class DemoTest {

  @Container
  public static GenericContainer<?> pact =
      new GenericContainer<>("pactfoundation/pact-broker")
          .withExposedPorts(9292)
          .withEnv("PACT_BROKER_DATABASE_ADAPTER", "sqlite")
          .withEnv("PACT_BROKER_DATABASE_NAME", "pact_broker.sqlite")
          .withEnv("PACT_BROKER_DISABLE_SSL_VERIFICATION", "true")
          .withEnv("PACT_BROKER_WEBHOOK_SCHEME_WHITELIST", "http")
          .withReuse(true);

  @BeforeAll
  public static void setUp() {
    System.out.println(pact.getContainerIpAddress());
    System.out.println(pact.getFirstMappedPort());
  }

  @Test
  public void demoTest() {
    assertThat(true).isTrue();
    assertThat(pact.getExposedPorts()).contains(9292);
  }

}

The problem is that on each run I get "Can't find a reusable running container with hash: 6a8b6aa299e529bb2a649319168324794464ee26" and then on next run again same message.

Other MySQL container works OK (reusable) on my machine. Using latest 1.12.4 and JUNIT 5. Executed also echo "testcontainers.reuse.enable=true" > ~/.testcontainers.properties so it should work I guess.

@bsideup
Copy link
Member

bsideup commented Feb 13, 2020

@bojanv55

you have @Container which will stop the container after the test, no magic here.

Consider removing it and either manually triggering .start() from @BeforeAll or use the singleton container approach.

@bojanv55
Copy link
Author

OK. Will try that.
If I use manually start() and stop() it will still be removed. If I do not use stop() it will be not stopped and will be reused.

The problem is that I want it to be reused in my dev. env. (having that config in .testcontainers.properties), while in CI env. I would like container to be stopped after all tests are run.

Is this thing possible?

@bsideup
Copy link
Member

bsideup commented Feb 13, 2020

Testcontainers will stop it automatically after the tests when environment is not in the reuse mode, no need to worry :)

@bojanv55
Copy link
Author

OK. Then I guess this should work:

@Testcontainers
public class DemoTest {

  public static GenericContainer<?> pact =
      new GenericContainer<>("pactfoundation/pact-broker")
          .withExposedPorts(9292)
          .withEnv("PACT_BROKER_DATABASE_ADAPTER", "sqlite")
          .withEnv("PACT_BROKER_DATABASE_NAME", "pact_broker.sqlite")
          .withEnv("PACT_BROKER_DISABLE_SSL_VERIFICATION", "true")
          .withEnv("PACT_BROKER_WEBHOOK_SCHEME_WHITELIST", "http")
          .withReuse(true);

  @BeforeAll
  public static void setUp() {
    pact.start();
    System.out.println(pact.getContainerIpAddress());
    System.out.println(pact.getFirstMappedPort());
  }

  @AfterAll
  public static void tearDown(){
    //pact.stop(); //do not stop (if in reuse mode), but WILL BE STOPPED if in CI and no .testcontainers.properties present
  }

  @Test
  public void demoTest() {
    assertThat(true).isTrue();
    assertThat(pact.getExposedPorts()).contains(9292);
  }

}

@bsideup
Copy link
Member

bsideup commented Feb 13, 2020

@bojanv55 that's correct.

And you don't need the @AfterAll block at all (unless you really need to stop it after this test and not after all tests, which is unlikely), the "if in reuse mode" part will be automatically applied by Testcontainers

@bojanv55
Copy link
Author

Yes. It is there just to put comment on it with description.

Also, I saw in doucumentation on main website, you use GenericContainer a = new GenericContainer... instead GenericContainer<?> a = new GenericContainer<>....

@bsideup
Copy link
Member

bsideup commented Feb 13, 2020

@bojanv55 we're working on a new DSL that will eliminate the need to do these nasty generics :) Stay tuned!

@brneto
Copy link

brneto commented Aug 4, 2021

@bsideup Is this "stopping the container after each test" happening even if I use the @container on a static field, like this?

@Testcontainers
public abstract class PostgresTestContainer {
    
    @Container
    private static final PostgreSQLContainer<?> postgresContainer =
            new PostgreSQLContainer<>("postgres:13.3-alpine").withReuse(true);
}

@bsideup
Copy link
Member

bsideup commented Aug 4, 2021

@brneto Yes. These annotations take care of both starting and stopping.

@brneto
Copy link

brneto commented Aug 4, 2021

@bsideup Ok! Thanks a lot! Just one more thing! When I use the singleton container approach it works but I loose the test container start up logs.

Is there any way to I have those logs back even using the singleton approach?

brneto added a commit to brneto/film-library that referenced this issue Aug 4, 2021
Due to the issue testcontainers/testcontainers-java#2352 MysqlTestContainer class has been refactored to avoid the start up of one different container for each test.
brneto pushed a commit to neueda/java-reactive-microservices-chassis that referenced this issue Aug 4, 2021
Due to the issue testcontainers/testcontainers-java#2352 PostgresTestContainer class had been refactored to stop the creation of a new container before each test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants