[Enhancement]: Containers declared with @Container can not depend od each other mapped ports using method withEnv(). Use Supplier<String> for env values
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
從 GenericContainer.withEnv 以及 issue 中描述的 @Container 啟動和相依路徑開始。追蹤相對於 dependsOn 和對映連接埠可用性,環境值何時被讀取。完成標準是:基於 Supplier 的環境值會在相依容器啟動期間解析,從而使所示範例無需手動啟動即可運作。
由索引模型根據 Issue 內容生成。
描述
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
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 提供 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
testcontainers/testcontainers-java 的其他 Issue
-
[Bug]: withStartupTimeout() changes the startup timeout of all containers that use the default wait strategy可能已有人在做 @aymenjamaaoui 於 1 天前認領。 未關閉type/bug
難度 2/5 1-3 小時 新手友好度 64/100
testcontainers/testcontainers-java#12171 ·
維護者通常 1 天內回覆
-
[Enhancement]: Document why singleton containers are required under Spring's test context caching可能已有人在做 @zakaullah075 於 95 天前認領。 未關閉type/enhancement
難度 1/5 1 小時以內 新手友好度 85/100
testcontainers/testcontainers-java#11967 · 1 則留言 ·
維護者通常 1 天內回覆
-
[Bug]: Cannot reuse Selenium BrowserWebDriverContainers under Linux可能已有人在做 @kdelay 於 116 天前認領。 未關閉type/bug
難度 2/5 1-3 小時 新手友好度 78/100
testcontainers/testcontainers-java#11941 ·
維護者通常 1 天內回覆
-
[Bug]: DockerDesktopClientProviderStrategy is always applicable可能已有人在做 @seonwooj0810 於 165 天前認領。 未關閉type/bug
難度 2/5 1-3 小時 新手友好度 74/100
testcontainers/testcontainers-java#11829 · 1 則留言 ·
維護者通常 1 天內回覆
-
[Enhancement]: cleanup document after removing junit4 support可能已有人在做 @SJvaca30 於 173 天前認領。 未關閉type/enhancement
難度 2/5 1-3 小時 新手友好度 65/100
testcontainers/testcontainers-java#11578 ·
維護者通常 1 天內回覆
查看 testcontainers/testcontainers-java 的全部 Issue
相似的 Issue
-
難度 1/5 1 小時以內 新手友好度 68/100
pact-foundation/pact-plugins#125 ·
-
難度 2/5 1-3 小時 新手友好度 66/100
opencollab/flexdock#28 · 1 則留言 ·
-
bug
難度 2/5 1-3 小時 新手友好度 85/100
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 75/100
SAP/ai-sdk-java#1206 ·
維護者通常 1 天內回覆
-
enhancement good first issue insight-module
難度 2/5 1-3 小時 新手友好度 86/100
sdlc-pro/spring-lens#606 ·
維護者通常 1 天內回覆