【问题标题】:Test containers not deleted from docker after JUnit 5 all class tests completed在 JUnit 5 完成所有类测试后,测试容器未从 docker 中删除
【发布时间】:2021-10-24 16:11:53
【问题描述】:

我正在使用测试容器对 micronaut 应用程序进行集成测试和端到端测试。

这里是测试容器的配置

@Testcontainers
public abstract class TestContainerFixture {
    protected static final GenericContainer mongoDBContainer;
    protected static final Map<String, Object> properties;

    static {
        mongoDBContainer = new GenericContainer(DockerImageName.parse("mongo:4.0.10"))
                .withExposedPorts(27017)
                .withReuse(true);
        mongoDBContainer.start();
        properties = Map.of("mongodb.uri",
                String.format("mongodb://%s:%s", mongoDBContainer.getContainerIpAddress(), mongoDBContainer.getMappedPort(27017)));
    }

    protected ApplicationContext applicationContext;
}

扩展测试容器的类

public class DiscountUpdateListenerTest extends TestContainerFixture {
}

因为我使用.withReuse(true); 为所有其他测试类重用测试容器。如果我在集成测试运行时禁用每个类上的.withReuse(false),则会创建容器,这需要更长的时间来执行测试。

所以,为了重用同一个容器,我使用了 .withReuse(true) 功能。由于容器在那里停留的时间更长。所以我想每 1-2 小时移除一次容器

【问题讨论】:

  • “所以我想每隔 1-2 小时删除一次容器” - 您是否想要一个脚本或每隔 1-2 小时运行一次的东西来删除某些容器?
  • @JeffScottBrown 我不确定如何处理这个问题。如果我长时间不使用,我只是要移除容器

标签: java junit micronaut testcontainers testcontainers-junit5


【解决方案1】:

Testcontainers 的重用功能目前处于 alpha 状态,还不支持清理容器。

Kevin Wittek(Testcontainers 的核心维护者之一)最近分享了即将清理容器和资源以用于可重用容器的信息(请参阅他在实时聊天中的消息here)。

目前,我建议创建一个基本的 bash/CMD 脚本并让一个 cron 作业触发它:

docker system prune -f --volumes

【讨论】:

    猜你喜欢
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2017-04-15
    • 2021-07-20
    相关资源
    最近更新 更多