[Enhancement]: Containers declared with @Container can not depend od each other mapped ports using method withEnv(). Use Supplier<String> for env values

未关闭
#8,823 2 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
docker, java
领域
testing

调研方向

从 GenericContainer.withEnv 以及 issue 中描述的 @Container 启动和依赖路径开始。跟踪相对于 dependsOn 和映射端口可用性,环境值何时被读取。完成标准是:基于 Supplier 的环境值会在依赖容器启动期间解析,从而使所示示例无需手动启动即可运行。

由索引模型根据 Issue 内容生成。

描述

type/enhancement
Module

Core

Proposal

When configuring test case like:

    @Container
    public static final GenericContainer<?> ACTIVE_MQ_CONTAINER = new GenericContainer<>(ACTIVE_MQ_IMAGE)
            .withExposedPorts(61616, 8161, 5672)
            .withExtraHost("host.docker.internal", "host-gateway")
            .withAccessToHost(true)
            .withNetwork(NETWORK);
            
            
    @Container
    private static final GenericContainer<?> MOCK_SERVER = new GenericContainer<>(MOCK_SERVER_IMAGE)
            .withExposedPorts(1080)
            .withNetwork(NETWORK)
            .withExtraHost("host.docker.internal", "host-gateway")
            .withAccessToHost(true); 
            
    @Container
    private static final GenericContainer MS_SQL_CONTAINER = new GenericContainer<>(MS_SQL_IMAGE)
            .withEnv("ACCEPT_EULA", "Y")
            .withEnv("SA_PASSWORD", "yourStrong(!)Password")
            .withNetwork(NETWORK)
            .withExposedPorts(1433)
            .withNetworkAliases("base");

    @Container
    private static final GenericContainer<?> APP_SERVER = new GenericContainer<>(APP_IMAGE)
            .dependsOn(MS_SQL_CONTAINER,MOCK_SERVER, ACTIVE_MQ_CONTAINER)
            .withEnv("ConnectionStrings__AzureServiceBus", "amqp://host.docker.internal:" + ACTIVE_MQ_CONTAINER.getMappedPort(5672))
...


dependency will fail because the first three Testcontainers are not started and line:
.withEnv("ConnectionStrings__AzureServiceBus", "amqp://host.docker.internal:" + ACTIVE_MQ_CONTAINER.getMappedPort(5672))
will throw an error because can not read the value from the not-started container.

Automated init sequence when using @Container annotation can be replaced with:

 private static final GenericContainer<?> FIRST_CONTAINER = new GenericContainer<>(
        JUnitJupiterTestImages.HTTPD_IMAGE).withExposedPorts(80);

    private static final GenericContainer<?> SECOND_CONTAINER = new GenericContainer<>(
        JUnitJupiterTestImages.HTTPD_IMAGE).withExposedPorts(80);

    private static final GenericContainer<?> APP_CONTAINER = new GenericContainer<>(JUnitJupiterTestImages.HTTPD_IMAGE)
        .dependsOn(FIRST_CONTAINER, SECOND_CONTAINER).withExposedPorts(80)
              .withEnv(FIRST_DEPENDENCY_ENV_KEY, String.valueOf(FIRST_CONTAINER.getFirstMappedPort()))
              .withEnv(SECOND_DEPENDENCY_ENV_KEY, String.valueOf(SECOND_CONTAINER.getFirstMappedPort()));

    @BeforeAll
    public static void setupWithException() {
                FIRST_CONTAINER.start();
                SECOND_CONTAINER.start();
                APP_CONTAINER.start();

A possible solution is to defer the resolution of dependency of ACTIVE_MQ_CONTAINER.getMappedPort(5672)) to a read stage of the dependent container (APP_SERVER container startup time).

Using Supplier<String> instead of String for type of value in Env Map. With this change, the example from above will work.

The current workaround is to start containers manually without @ Container annotation and using manually written checks 'is container started'. Containers in the test class must be declared in an ordered way.
Code snippet for manual container start-check:

    public static final GenericContainer<?> ACTIVE_MQ_CONTAINER;

    static {
        ACTIVE_MQ_CONTAINER = new GenericContainer<>(ACTIVE_MQ_IMAGE)
                .withExposedPorts(61616, 8161, 5672)
                .withExtraHost("host.docker.internal", "host-gateway")
                .withAccessToHost(true)
                .withNetwork(NETWORK);
        ACTIVE_MQ_CONTAINER.start();
    }

    static {
        boolean started = false;
        while (!started) {
            try {
                log.info("ACTIVE_MQ_CONTAINER.getFirstMappedPort(): " + ACTIVE_MQ_CONTAINER.getMappedPort(5672));
                started = true;
            } catch (Exception e) {
                // nothing
                log.info("in loop error");
            }
        }
    }

alternative is:

 private static final GenericContainer<?> FIRST_CONTAINER = new GenericContainer<>(
        JUnitJupiterTestImages.HTTPD_IMAGE).withExposedPorts(80);

    private static final GenericContainer<?> SECOND_CONTAINER = new GenericContainer<>(
        JUnitJupiterTestImages.HTTPD_IMAGE).withExposedPorts(80);

    private static final GenericContainer<?> APP_CONTAINER = new GenericContainer<>(JUnitJupiterTestImages.HTTPD_IMAGE)
        .dependsOn(FIRST_CONTAINER, SECOND_CONTAINER).withExposedPorts(80);

    @BeforeAll
    public static void setupWithException() {
                FIRST_CONTAINER.start();
                SECOND_CONTAINER.start();
                // read mapped ports after containers are started
                APP_CONTAINER
                     .withEnv(FIRST_DEPENDENCY_ENV_KEY, String.valueOf(FIRST_CONTAINER.getFirstMappedPort()))
                     .withEnv(SECOND_DEPENDENCY_ENV_KEY, String.valueOf(SECOND_CONTAINER.getFirstMappedPort()));

                APP_CONTAINER.start();

Expected behavior should be:

    @Container
    public static final GenericContainer<?> ACTIVE_MQ_CONTAINER = new GenericContainer<>(ACTIVE_MQ_IMAGE)
            .withExposedPorts(61616, 8161, 5672);
            
            
    @Container
    private static final GenericContainer<?> MOCK_SERVER = new GenericContainer<>(MOCK_SERVER_IMAGE)
            .withExposedPorts(1080);
            
    @Container
    private static final GenericContainer MS_SQL_CONTAINER = new GenericContainer<>(MS_SQL_IMAGE)
            .withEnv("ACCEPT_EULA", "Y")
            .withEnv("SA_PASSWORD", "yourStrong(!)Password")
            .withNetwork(NETWORK)
            .withExposedPorts(1433);

    @Container
    private static final GenericContainer<?> APP_SERVER = new GenericContainer<>(APP_IMAGE)
            .dependsOn(MS_SQL_CONTAINER,MOCK_SERVER, ACTIVE_MQ_CONTAINER)
            .withEnv("ConnectionStrings__AzureServiceBus", () -> "amqp://host.docker.internal:" + ACTIVE_MQ_CONTAINER.getMappedPort(5672))
...


Last line:
.withEnv("ConnectionStrings__AzureServiceBus", "amqp://host.docker.internal:" + ACTIVE_MQ_CONTAINER.getMappedPort(5672))
replaced by deferred value retrieval:
.withEnv("ConnectionStrings__AzureServiceBus", () -> "amqp://host.docker.internal:" + ACTIVE_MQ_CONTAINER.getMappedPort(5672))

ensures proper startup order when one container depends on some runtime value of another container.

主要语言
Java
星标
8.8k
派生
2k
平均合并
5 天 21 小时
30 天内合并 PR
15

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

testcontainers/testcontainers-java 的其他 Issue

查看 testcontainers/testcontainers-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。