October 22, 2024
Chicago 12, Melborne City, USA
java

How to delete Test containers from docker after JUnit 5 all class tests completed


  • here we create containers
  • also in the project there is a property ryuk.container.image=testcontainers/ryuk:0.3.3 which creates a container that should delete all test containers after the tests are completed, but for some reason the deletion does not occur

public class TestContainersSpringContextCustomizerFactory implements ContextCustomizerFactory {

  private static SqlTestContainer prodTestContainer;
  private static S3TestContainer minioContainer;
  private static ElasticsearchTestContainer elasticsearchBean;
  private static RabbitMQTestContainer rabbitMQContainer;
  private Logger log = LoggerFactory.getLogger(TestContainersSpringContextCustomizerFactory.class);

  @Override
  public ContextCustomizer createContextCustomizer(Class<?> testClass,
                                                   List<ContextConfigurationAttributes> configAttributes) {
    return (context, mergedConfig) -> {
      ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
      TestPropertyValues testValues = TestPropertyValues.empty();

      EmbeddedElasticsearch elasticsearchAnnotation = AnnotatedElementUtils.findMergedAnnotation(
          testClass,
          EmbeddedElasticsearch.class
      );
      if (null != elasticsearchAnnotation) {
        log.debug("detected the EmbeddedElasticsearch annotation on class {}", testClass.getName());
        log.info("Warming up the elastic database");
        if (null == elasticsearchBean) {
          elasticsearchBean = beanFactory.createBean(ElasticsearchTestContainer.class);
          beanFactory.registerSingleton(ElasticsearchTestContainer.class.getName(),
              elasticsearchBean);
          // ((DefaultListableBeanFactory)beanFactory).registerDisposableBean(ElasticsearchTestContainer.class.getName(), elasticsearchBean);
        }
        testValues =
            testValues.and(
                "spring.elasticsearch.uris=http://"
                    + elasticsearchBean.getElasticsearchContainer().getHttpHostAddress()
            );
      }
//similary code other for other containers
}


as an example I will show you the code of one of the 4 containers, the rest are written similarly

/**
 * Base class for starting/stopping ElasticSearch during tests.
 */
public class ElasticsearchTestContainer implements InitializingBean, DisposableBean {

  private static final Logger log = LoggerFactory.getLogger(ElasticsearchTestContainer.class);
  private static final Integer CONTAINER_STARTUP_TIMEOUT_MINUTES = 10;
  private ElasticsearchContainer elasticsearchContainer;

  @Override
  public void destroy() {
    if (null != elasticsearchContainer && elasticsearchContainer.isRunning()) {
      elasticsearchContainer.close();
    }
  }

  @Override
  public void afterPropertiesSet() {
    if (null == elasticsearchContainer) {
      elasticsearchContainer =
          new ElasticsearchContainer(
              DockerImageName.parse("docker-persistent.nexus.jaga.rt.ru/elasticsearch")
                  .asCompatibleSubstituteFor("docker.elastic.co/elasticsearch/elasticsearch")
                  .withTag("7.17.4"))
              .withStartupTimeout(
                  Duration.of(CONTAINER_STARTUP_TIMEOUT_MINUTES, ChronoUnit.MINUTES))
              .withSharedMemorySize(256000000L)
              .withEnv("ES_JAVA_OPTS", "-Xms256m -Xmx256m")
              .withEnv("xpack.security.enabled", "false")
              .withLogConsumer(new Slf4jLogConsumer(log))
              .withReuse(true);
    }
    if (!elasticsearchContainer.isRunning()) {
      elasticsearchContainer.start();
    }
  }

  public ElasticsearchContainer getElasticsearchContainer() {
    return elasticsearchContainer;
  }
}





You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video