Skip to content

Commit 94348f1

Browse files
committed
enhancement lazy env vars
1 parent 3d48b93 commit 94348f1

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

modules/application-server-commons/src/main/java/org/testcontainers/applicationserver/ApplicationServerContainer.java

+34
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
import java.time.Duration;
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.HashMap;
2122
import java.util.List;
23+
import java.util.Map;
2224
import java.util.Objects;
2325
import java.util.concurrent.Future;
26+
import java.util.function.Function;
27+
import java.util.function.Predicate;
28+
import java.util.function.Supplier;
2429
import java.util.stream.Collectors;
2530
import java.util.stream.Stream;
2631

@@ -56,6 +61,9 @@ public abstract class ApplicationServerContainer extends GenericContainer<Applic
5661
// Expected path for Microprofile platforms to query for readiness
5762
static final String MP_HEALTH_READINESS_PATH = "/health/ready";
5863

64+
// Lazy environment variables
65+
private Map<String, Supplier<String>> lazyEnvVars = new HashMap<>();
66+
5967
//Constructors
6068

6169
public ApplicationServerContainer(@NonNull final Future<String> image) {
@@ -83,6 +91,11 @@ protected void configure() {
8391
for(MountableFile archive : archives) {
8492
withCopyFileToContainer(archive, getApplicationInstallDirectory() + extractApplicationName(archive));
8593
}
94+
95+
// Add lazy env variables
96+
for(Map.Entry<String, Supplier<String>> entry : lazyEnvVars.entrySet()) {
97+
withEnv(entry.getKey(), entry.getValue().get());
98+
}
8699
}
87100

88101
@Override
@@ -236,6 +249,27 @@ public ApplicationServerContainer withHttpWaitTimeout(Duration httpWaitTimeout)
236249
return this;
237250
}
238251

252+
/**
253+
* An environment variable whose value needs to wait until the configuration stage to be evaluated.
254+
* The common use case for this would be to pass inter-container connection data.
255+
*
256+
* For example an application container often needs to have connection data to a dependent database:
257+
* <pre>
258+
* OracleContainer db = new OracleContainer(...).withExposedPorts(1521);
259+
* ApplicationContainer app = new ApplicationContainer(...)
260+
* .withLazyEnv( "oracle.url", () -&gt; db.getJdbcUrl() ) // Need to wait until 'db' container is configured
261+
* .dependsOn(db);
262+
* </pre>
263+
*
264+
* @param key - the key
265+
* @param valueSupplier - the function that supplies the value
266+
* @return self
267+
*/
268+
public ApplicationServerContainer withLazyEnv(String key, Supplier<String> valueSupplier) {
269+
lazyEnvVars.put(key, valueSupplier);
270+
return this;
271+
}
272+
239273
//Getters
240274

241275
/**

modules/liberty/src/test/java/org/testcontainers/containers/LibertyContainerTest.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,32 @@
1717

1818
public class LibertyContainerTest {
1919

20+
/*
21+
* Typically application servers have a dependency on a database or other microservice during integration testing
22+
* Use a mockServer to ensure dependsOn and lazy env variables work.
23+
*/
24+
public static final DockerImageName mockImage = DockerImageName
25+
.parse("mockserver/mockserver")
26+
.withTag("mockserver-5.15.0");
27+
28+
private static GenericContainer<?> dependantService = new GenericContainer<>(mockImage)
29+
.withExposedPorts(80,81);
30+
31+
//The liberty test container
2032
private static final DockerImageName libertyImage = DockerImageName.parse(LibertyServerContainer.IMAGE)
2133
.withTag("23.0.0.3-full-java17-openj9");
2234

2335
private static ApplicationServerContainer testContainer = new LibertyServerContainer(libertyImage)
2436
.withArchvies(createDeployment())
25-
.withAppContextRoot("test/app/service/");
26-
37+
.withAppContextRoot("test/app/service/")
38+
.withLazyEnv("mock.port", () -> "" + dependantService.getMappedPort(80))
39+
.dependsOn(dependantService);
40+
41+
/**
42+
* Creates a deployment using Shrinkwrap at runtime.
43+
*
44+
* @return - the application archive
45+
*/
2746
private static Archive<?> createDeployment() {
2847
return ShrinkWrap.create(WebArchive.class, "test.war")
2948
.addPackage("org.testcontainers.containers.app");

0 commit comments

Comments
 (0)