[Enhancement]: Containers declared with @Container can not depend od each other mapped ports using method withEnv(). Use Supplier<String> for env values
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
調査の方向性
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時間
- マージ済み PR(30日)
- 15
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
yegor256/jping#122 · コメント 1 件 ·
メンテナーはふだん 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 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
apache/cloudstack#14719 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
apache/maven-surefire#3605 ·
メンテナーはふだん 1 日以内に返信