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.
@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.
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?
Testcontainers will stop it automatically after the tests when environment is not in the reuse mode, no need to worry :)
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);
}
}
@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
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<>....
@bojanv55 we're working on a new DSL that will eliminate the need to do these nasty generics :) Stay tuned!
Most helpful comment
@bojanv55 we're working on a new DSL that will eliminate the need to do these nasty generics :) Stay tuned!