[Enhancement]: Containers declared with @Container can not depend od each other mapped ports using method withEnv(). Use Supplier<String> for env values
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Facilidade para iniciantes
- 35/100
Direção de pesquisa
Comece com GenericContainer.withEnv e com o caminho de inicialização e dependência de @Container descrito na issue. Rastreie quando os valores de ambiente são lidos em relação a dependsOn e à disponibilidade das portas mapeadas. O trabalho estará concluído quando um valor de ambiente baseado em Supplier for resolvido durante a inicialização do container dependente, permitindo que o exemplo mostrado funcione sem inicializações manuais.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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.
- Linguagem predominante
- Java
- Estrelas
- 8.8k
- Forks
- 2k
- Merge médio
- 5d 21h
- PRs com merge (30d)
- 15
Preparar o ambiente
Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.
- Inclui um Dockerfile ou arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de testcontainers/testcontainers-java
-
[Bug]: withStartupTimeout() changes the startup timeout of all containers that use the default wait strategyTalvez já em andamento @aymenjamaaoui assumiu há 1 dia. Abertatype/bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 64/100
testcontainers/testcontainers-java#12171 ·
Mantenedores costumam responder em até 1 dia
-
[Enhancement]: Document why singleton containers are required under Spring's test context cachingTalvez já em andamento @zakaullah075 assumiu há 95 dias. Abertatype/enhancement
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 85/100
testcontainers/testcontainers-java#11967 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
[Bug]: Cannot reuse Selenium BrowserWebDriverContainers under LinuxTalvez já em andamento @kdelay assumiu há 116 dias. Abertatype/bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
testcontainers/testcontainers-java#11941 ·
Mantenedores costumam responder em até 1 dia
-
[Bug]: DockerDesktopClientProviderStrategy is always applicableTalvez já em andamento @seonwooj0810 assumiu há 165 dias. Abertatype/bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
testcontainers/testcontainers-java#11829 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
[Enhancement]: cleanup document after removing junit4 supportTalvez já em andamento @SJvaca30 assumiu há 173 dias. Abertatype/enhancement
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
testcontainers/testcontainers-java#11578 ·
Mantenedores costumam responder em até 1 dia
Todas as issues de testcontainers/testcontainers-java
Issues semelhantes
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 85/100
yegor256/jping#122 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 75/100
SAP/ai-sdk-java#1206 ·
Mantenedores costumam responder em até 1 dia
-
enhancement good first issue insight-module
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 86/100
sdlc-pro/spring-lens#606 ·
Mantenedores costumam responder em até 1 dia
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
apache/cloudstack#14719 ·
Mantenedores costumam responder em até 1 dia
-
enhancement
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
apache/maven-surefire#3605 ·
Mantenedores costumam responder em até 1 dia