Skip to content

Commit fa1cacd

Browse files
committed
Add tests for configuration of files that are copied for docker compose (#8847)
1 parent fe57080 commit fa1cacd

File tree

8 files changed

+173
-0
lines changed

8 files changed

+173
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.testcontainers.junit;
2+
3+
import org.junit.Test;
4+
import org.testcontainers.containers.ComposeContainer;
5+
import org.testcontainers.containers.ContainerLaunchException;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.net.URL;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.Scanner;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
15+
16+
public class ComposeContainerFileCopyInclusionsTest {
17+
18+
@Test
19+
public void testShouldCopyAllFilesByDefault() throws IOException {
20+
try (
21+
ComposeContainer environment = new ComposeContainer(
22+
new File("src/test/resources/compose-file-copy-inclusions/compose.yml")
23+
)
24+
.withExposedService("app", 8080)
25+
) {
26+
environment.start();
27+
28+
Integer servicePort = environment.getServicePort("app-1", 8080);
29+
String serviceHost = environment.getServiceHost("app-1", 8080);
30+
String response = readStringFromURL("http://" + serviceHost + ":" + servicePort + "/env");
31+
assertThat(response).isEqualTo("MY_ENV_VARIABLE: override");
32+
}
33+
}
34+
35+
@Test
36+
public void testWithFileCopyInclusionUsingFilePath() throws IOException {
37+
try (
38+
ComposeContainer environment = new ComposeContainer(
39+
new File("src/test/resources/compose-file-copy-inclusions/compose-root-only.yml")
40+
)
41+
.withExposedService("app", 8080)
42+
.withFileCopyInclusions("Dockerfile", "EnvVariableRestEndpoint.java", ".env")
43+
) {
44+
environment.start();
45+
46+
Integer servicePort = environment.getServicePort("app-1", 8080);
47+
String serviceHost = environment.getServiceHost("app-1", 8080);
48+
String response = readStringFromURL("http://" + serviceHost + ":" + servicePort + "/env");
49+
50+
// The `test/.env` file is not copied, now so we get the original value
51+
assertThat(response).isEqualTo("MY_ENV_VARIABLE: original");
52+
}
53+
}
54+
55+
@Test
56+
public void testWithFileCopyInclusionUsingDirectoryPath() throws IOException {
57+
try (
58+
ComposeContainer environment = new ComposeContainer(
59+
new File("src/test/resources/compose-file-copy-inclusions/compose-test-only.yml")
60+
)
61+
.withExposedService("app", 8080)
62+
.withFileCopyInclusions("Dockerfile", "EnvVariableRestEndpoint.java", "test")
63+
) {
64+
environment.start();
65+
66+
Integer servicePort = environment.getServicePort("app-1", 8080);
67+
String serviceHost = environment.getServiceHost("app-1", 8080);
68+
String response = readStringFromURL("http://" + serviceHost + ":" + servicePort + "/env");
69+
// The test directory (with its contents) is copied, so we get the override
70+
assertThat(response).isEqualTo("MY_ENV_VARIABLE: override");
71+
}
72+
}
73+
74+
@Test
75+
public void testShouldNotBeAbleToStartIfNeededEnvFileIsNotCopied() {
76+
try (
77+
ComposeContainer environment = new ComposeContainer(
78+
new File("src/test/resources/compose-file-copy-inclusions/compose-test-only.yml")
79+
)
80+
.withExposedService("app", 8080)
81+
.withFileCopyInclusions("Dockerfile", "EnvVariableRestEndpoint.java")
82+
) {
83+
assertThatExceptionOfType(ContainerLaunchException.class)
84+
.isThrownBy(environment::start)
85+
.withMessage("Container startup failed for image docker:24.0.2");
86+
}
87+
}
88+
89+
private static String readStringFromURL(String requestURL) throws IOException {
90+
try (Scanner scanner = new Scanner(new URL(requestURL).openStream(), StandardCharsets.UTF_8.toString())) {
91+
scanner.useDelimiter("\\A");
92+
return scanner.hasNext() ? scanner.next() : "";
93+
}
94+
}
95+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MY_ENV_VARIABLE=original
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM jbangdev/jbang-action
2+
3+
WORKDIR /app
4+
COPY EnvVariableRestEndpoint.java .
5+
6+
RUN jbang export portable --force EnvVariableRestEndpoint.java
7+
8+
EXPOSE 8080
9+
CMD ["./EnvVariableRestEndpoint.java"]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
import com.sun.net.httpserver.HttpServer;
4+
import com.sun.net.httpserver.HttpHandler;
5+
import com.sun.net.httpserver.HttpExchange;
6+
7+
import java.io.IOException;
8+
import java.io.OutputStream;
9+
import java.net.InetSocketAddress;
10+
11+
public class EnvVariableRestEndpoint {
12+
private static final String ENV_VARIABLE_NAME = "MY_ENV_VARIABLE";
13+
private static final int PORT = 8080;
14+
15+
public static void main(String[] args) throws IOException {
16+
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0);
17+
server.createContext("/env", new EnvVariableHandler());
18+
server.setExecutor(null);
19+
server.start();
20+
System.out.println("Server started on port " + PORT);
21+
}
22+
23+
static class EnvVariableHandler implements HttpHandler {
24+
@Override
25+
public void handle(HttpExchange exchange) throws IOException {
26+
if ("GET".equals(exchange.getRequestMethod())) {
27+
String envValue = System.getenv(ENV_VARIABLE_NAME);
28+
String response = envValue != null
29+
? ENV_VARIABLE_NAME + ": " + envValue
30+
: "Environment variable " + ENV_VARIABLE_NAME + " not found";
31+
32+
exchange.sendResponseHeaders(200, response.length());
33+
try (OutputStream os = exchange.getResponseBody()) {
34+
os.write(response.getBytes());
35+
}
36+
} else {
37+
String response = "Method not allowed";
38+
exchange.sendResponseHeaders(405, response.length());
39+
try (OutputStream os = exchange.getResponseBody()) {
40+
os.write(response.getBytes());
41+
}
42+
}
43+
}
44+
}
45+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
app:
3+
build: .
4+
ports:
5+
- "8080:8080"
6+
env_file:
7+
- '.env'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
app:
3+
build: .
4+
ports:
5+
- "8080:8080"
6+
env_file:
7+
- './test/.env'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
app:
3+
build: .
4+
ports:
5+
- "8080:8080"
6+
env_file:
7+
- '.env'
8+
- './test/.env'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MY_ENV_VARIABLE=override

0 commit comments

Comments
 (0)