Skip to content

Commit 2a850d6

Browse files
Copilotkiview
andcommitted
Add comprehensive tests for compose variable substitution feature
Co-authored-by: kiview <[email protected]>
1 parent a5037e4 commit 2a850d6

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.testcontainers.containers;
2+
3+
import org.junit.Test;
4+
import org.testcontainers.containers.output.Slf4jLogConsumer;
5+
6+
import java.io.File;
7+
8+
import static org.assertj.core.api.Assertions.assertThatNoException;
9+
10+
/**
11+
* Integration test for Docker Compose variable substitution.
12+
* Tests the scenario described in the GitHub issue where image names with variables
13+
* like ${TAG_CONFLUENT} were causing warnings.
14+
*/
15+
public class ComposeVariableSubstitutionIntegrationTest {
16+
17+
@Test
18+
public void shouldHandleVariableSubstitutionWithoutWarnings() {
19+
// Set up environment variable as if it was set by the user
20+
System.setProperty("TAG_CONFLUENT", "7.0.0");
21+
System.setProperty("REDIS_VERSION", "alpine");
22+
23+
try {
24+
// This should not generate warnings about invalid image names
25+
// because variable substitution should resolve ${TAG_CONFLUENT} to 7.0.0
26+
assertThatNoException().isThrownBy(() -> {
27+
try (ComposeContainer compose = new ComposeContainer(
28+
new File("src/test/resources/docker-compose-variable-substitution.yml")
29+
)
30+
.withLocalCompose(true)
31+
.withPull(false) // Don't actually pull images in test
32+
.withLogConsumer("confluent", new Slf4jLogConsumer(org.slf4j.LoggerFactory.getLogger("confluent-test")))
33+
) {
34+
// Just validate the compose container can be created without exceptions
35+
// The key test is that ParsedDockerComposeFile doesn't throw on variable substitution
36+
// We're not actually starting the containers to avoid requiring Docker in the test
37+
}
38+
});
39+
} finally {
40+
// Clean up
41+
System.clearProperty("TAG_CONFLUENT");
42+
System.clearProperty("REDIS_VERSION");
43+
}
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.testcontainers.containers;
2+
3+
import org.junit.Test;
4+
5+
import java.io.File;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
/**
10+
* Test to demonstrate the fix for the original issue where variable substitution was not working.
11+
*/
12+
public class OriginalIssueDemo {
13+
14+
@Test
15+
public void demonstrateOriginalIssueIsFixed() {
16+
// Set the environment variable that was mentioned in the issue
17+
System.setProperty("TAG_CONFLUENT", "7.0.0");
18+
19+
try {
20+
// Parse the compose file that contains the problematic image name
21+
ParsedDockerComposeFile parsedFile = new ParsedDockerComposeFile(
22+
new File("src/test/resources/docker-compose-variable-substitution.yml")
23+
);
24+
25+
// Before our fix, this would contain "confluentinc/cp-server:${TAG_CONFLUENT}"
26+
// After our fix, this should contain "confluentinc/cp-server:7.0.0"
27+
String actualImageName = parsedFile.getServiceNameToImageNames()
28+
.get("confluent")
29+
.iterator()
30+
.next();
31+
32+
assertThat(actualImageName)
33+
.as("Image name should have variable substituted")
34+
.isEqualTo("confluentinc/cp-server:7.0.0")
35+
.doesNotContain("${TAG_CONFLUENT}");
36+
37+
System.out.println("✅ SUCCESS: Variable substitution is working!");
38+
System.out.println(" Original: confluentinc/cp-server:${TAG_CONFLUENT}");
39+
System.out.println(" Resolved: " + actualImageName);
40+
41+
} finally {
42+
System.clearProperty("TAG_CONFLUENT");
43+
}
44+
}
45+
}

core/src/test/java/org/testcontainers/containers/ParsedDockerComposeFileValidationTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,50 @@ public void shouldSubstituteEnvironmentVariablesFromFile() {
231231
System.clearProperty("REDIS_VERSION");
232232
}
233233
}
234+
235+
@Test
236+
public void shouldHandleEdgeCasesInVariableSubstitution() {
237+
// Test various edge cases
238+
System.setProperty("SIMPLE_VAR", "simple-value");
239+
System.setProperty("WITH_SPECIAL_CHARS", "registry.io/app:v1.2.3");
240+
System.setProperty("EMPTY_VAR", "");
241+
242+
try {
243+
ParsedDockerComposeFile parsedFile = new ParsedDockerComposeFile(
244+
ImmutableMap.of(
245+
"version", "2",
246+
"services", ImmutableMap.of(
247+
// Test $VAR syntax
248+
"service1", ImmutableMap.of("image", "redis:$SIMPLE_VAR"),
249+
// Test complex image names with registry
250+
"service2", ImmutableMap.of("image", "${WITH_SPECIAL_CHARS}"),
251+
// Test multiple variables in one string
252+
"service3", ImmutableMap.of("image", "${WITH_SPECIAL_CHARS}-${SIMPLE_VAR}"),
253+
// Test undefined variables remain unchanged
254+
"service4", ImmutableMap.of("image", "app:${UNDEFINED_VAR}"),
255+
// Test variables with special characters that don't need substitution
256+
"service5", ImmutableMap.of("image", "app:latest"),
257+
// Test empty variable with default
258+
"service6", ImmutableMap.of("image", "nginx:${EMPTY_VAR:-default}")
259+
)
260+
)
261+
);
262+
263+
assertThat(parsedFile.getServiceNameToImageNames())
264+
.as("edge cases in variable substitution work correctly")
265+
.contains(
266+
entry("service1", Sets.newHashSet("redis:simple-value")),
267+
entry("service2", Sets.newHashSet("registry.io/app:v1.2.3")),
268+
entry("service3", Sets.newHashSet("registry.io/app:v1.2.3-simple-value")),
269+
entry("service4", Sets.newHashSet("app:${UNDEFINED_VAR}")),
270+
entry("service5", Sets.newHashSet("app:latest")),
271+
entry("service6", Sets.newHashSet("nginx:default"))
272+
);
273+
} finally {
274+
// Clean up
275+
System.clearProperty("SIMPLE_VAR");
276+
System.clearProperty("WITH_SPECIAL_CHARS");
277+
System.clearProperty("EMPTY_VAR");
278+
}
279+
}
234280
}

0 commit comments

Comments
 (0)