Skip to content

Commit 1e89afd

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

File tree

8 files changed

+162
-0
lines changed

8 files changed

+162
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.testcontainers.junit;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.net.URL;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.Scanner;
11+
import org.junit.Test;
12+
import org.testcontainers.containers.ComposeContainer;
13+
import org.testcontainers.containers.ContainerLaunchException;
14+
15+
public class ComposeContainerFileCopyInclusionsTest {
16+
17+
@Test
18+
public void testShouldCopyAllFilesByDefault() throws IOException {
19+
try (ComposeContainer environment = new ComposeContainer(
20+
new File("src/test/resources/compose-file-copy-inclusions/compose.yml"))
21+
.withExposedService("app", 8080)) {
22+
environment.start();
23+
24+
Integer servicePort = environment.getServicePort("app-1", 8080);
25+
String serviceHost = environment.getServiceHost("app-1", 8080);
26+
String response = readStringFromURL("http://" + serviceHost + ":" + servicePort + "/env");
27+
assertThat(response).isEqualTo("MY_ENV_VARIABLE: override");
28+
}
29+
}
30+
31+
@Test
32+
public void testWithFileCopyInclusionUsingFilePath() throws IOException {
33+
try (ComposeContainer environment = new ComposeContainer(
34+
new File("src/test/resources/compose-file-copy-inclusions/compose-root-only.yml"))
35+
.withExposedService("app", 8080)
36+
.withFileCopyInclusions("Dockerfile", "EnvVariableRestEndpoint.java", ".env")) {
37+
environment.start();
38+
39+
Integer servicePort = environment.getServicePort("app-1", 8080);
40+
String serviceHost = environment.getServiceHost("app-1", 8080);
41+
String response = readStringFromURL("http://" + serviceHost + ":" + servicePort + "/env");
42+
43+
// The `test/.env` file is not copied, now so we get the original value
44+
assertThat(response).isEqualTo("MY_ENV_VARIABLE: original");
45+
}
46+
}
47+
48+
@Test
49+
public void testWithFileCopyInclusionUsingDirectoryPath() throws IOException {
50+
try (ComposeContainer environment = new ComposeContainer(
51+
new File("src/test/resources/compose-file-copy-inclusions/compose-test-only.yml"))
52+
.withExposedService("app", 8080)
53+
.withFileCopyInclusions("Dockerfile", "EnvVariableRestEndpoint.java", "test")) {
54+
environment.start();
55+
56+
Integer servicePort = environment.getServicePort("app-1", 8080);
57+
String serviceHost = environment.getServiceHost("app-1", 8080);
58+
String response = readStringFromURL("http://" + serviceHost + ":" + servicePort + "/env");
59+
// The test directory (with its contents) is copied, so we get the override
60+
assertThat(response).isEqualTo("MY_ENV_VARIABLE: override");
61+
}
62+
}
63+
64+
@Test
65+
public void testShouldNotBeAbleToStartIfNeededEnvFileIsNotCopied() {
66+
try (ComposeContainer environment = new ComposeContainer(
67+
new File("src/test/resources/compose-file-copy-inclusions/compose-test-only.yml"))
68+
.withExposedService("app", 8080)
69+
.withFileCopyInclusions("Dockerfile", "EnvVariableRestEndpoint.java")) {
70+
71+
assertThatExceptionOfType(ContainerLaunchException.class)
72+
.isThrownBy(environment::start)
73+
.withMessage("Container startup failed for image docker:24.0.2");
74+
}
75+
}
76+
77+
private static String readStringFromURL(String requestURL) throws IOException {
78+
try (Scanner scanner = new Scanner(new URL(requestURL).openStream(),
79+
StandardCharsets.UTF_8.toString())) {
80+
scanner.useDelimiter("\\A");
81+
return scanner.hasNext() ? scanner.next() : "";
82+
}
83+
}
84+
}
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)