-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for configuration of files that are copied for docker compo…
…se (#8847)
- Loading branch information
1 parent
fe57080
commit fa1cacd
Showing
8 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
core/src/test/java/org/testcontainers/junit/ComposeContainerFileCopyInclusionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.testcontainers.junit; | ||
|
||
import org.junit.Test; | ||
import org.testcontainers.containers.ComposeContainer; | ||
import org.testcontainers.containers.ContainerLaunchException; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Scanner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
public class ComposeContainerFileCopyInclusionsTest { | ||
|
||
@Test | ||
public void testShouldCopyAllFilesByDefault() throws IOException { | ||
try ( | ||
ComposeContainer environment = new ComposeContainer( | ||
new File("src/test/resources/compose-file-copy-inclusions/compose.yml") | ||
) | ||
.withExposedService("app", 8080) | ||
) { | ||
environment.start(); | ||
|
||
Integer servicePort = environment.getServicePort("app-1", 8080); | ||
String serviceHost = environment.getServiceHost("app-1", 8080); | ||
String response = readStringFromURL("http://" + serviceHost + ":" + servicePort + "/env"); | ||
assertThat(response).isEqualTo("MY_ENV_VARIABLE: override"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWithFileCopyInclusionUsingFilePath() throws IOException { | ||
try ( | ||
ComposeContainer environment = new ComposeContainer( | ||
new File("src/test/resources/compose-file-copy-inclusions/compose-root-only.yml") | ||
) | ||
.withExposedService("app", 8080) | ||
.withFileCopyInclusions("Dockerfile", "EnvVariableRestEndpoint.java", ".env") | ||
) { | ||
environment.start(); | ||
|
||
Integer servicePort = environment.getServicePort("app-1", 8080); | ||
String serviceHost = environment.getServiceHost("app-1", 8080); | ||
String response = readStringFromURL("http://" + serviceHost + ":" + servicePort + "/env"); | ||
|
||
// The `test/.env` file is not copied, now so we get the original value | ||
assertThat(response).isEqualTo("MY_ENV_VARIABLE: original"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWithFileCopyInclusionUsingDirectoryPath() throws IOException { | ||
try ( | ||
ComposeContainer environment = new ComposeContainer( | ||
new File("src/test/resources/compose-file-copy-inclusions/compose-test-only.yml") | ||
) | ||
.withExposedService("app", 8080) | ||
.withFileCopyInclusions("Dockerfile", "EnvVariableRestEndpoint.java", "test") | ||
) { | ||
environment.start(); | ||
|
||
Integer servicePort = environment.getServicePort("app-1", 8080); | ||
String serviceHost = environment.getServiceHost("app-1", 8080); | ||
String response = readStringFromURL("http://" + serviceHost + ":" + servicePort + "/env"); | ||
// The test directory (with its contents) is copied, so we get the override | ||
assertThat(response).isEqualTo("MY_ENV_VARIABLE: override"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testShouldNotBeAbleToStartIfNeededEnvFileIsNotCopied() { | ||
try ( | ||
ComposeContainer environment = new ComposeContainer( | ||
new File("src/test/resources/compose-file-copy-inclusions/compose-test-only.yml") | ||
) | ||
.withExposedService("app", 8080) | ||
.withFileCopyInclusions("Dockerfile", "EnvVariableRestEndpoint.java") | ||
) { | ||
assertThatExceptionOfType(ContainerLaunchException.class) | ||
.isThrownBy(environment::start) | ||
.withMessage("Container startup failed for image docker:24.0.2"); | ||
} | ||
} | ||
|
||
private static String readStringFromURL(String requestURL) throws IOException { | ||
try (Scanner scanner = new Scanner(new URL(requestURL).openStream(), StandardCharsets.UTF_8.toString())) { | ||
scanner.useDelimiter("\\A"); | ||
return scanner.hasNext() ? scanner.next() : ""; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MY_ENV_VARIABLE=original |
9 changes: 9 additions & 0 deletions
9
core/src/test/resources/compose-file-copy-inclusions/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FROM jbangdev/jbang-action | ||
|
||
WORKDIR /app | ||
COPY EnvVariableRestEndpoint.java . | ||
|
||
RUN jbang export portable --force EnvVariableRestEndpoint.java | ||
|
||
EXPOSE 8080 | ||
CMD ["./EnvVariableRestEndpoint.java"] |
45 changes: 45 additions & 0 deletions
45
core/src/test/resources/compose-file-copy-inclusions/EnvVariableRestEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
|
||
import com.sun.net.httpserver.HttpServer; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpExchange; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
|
||
public class EnvVariableRestEndpoint { | ||
private static final String ENV_VARIABLE_NAME = "MY_ENV_VARIABLE"; | ||
private static final int PORT = 8080; | ||
|
||
public static void main(String[] args) throws IOException { | ||
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0); | ||
server.createContext("/env", new EnvVariableHandler()); | ||
server.setExecutor(null); | ||
server.start(); | ||
System.out.println("Server started on port " + PORT); | ||
} | ||
|
||
static class EnvVariableHandler implements HttpHandler { | ||
@Override | ||
public void handle(HttpExchange exchange) throws IOException { | ||
if ("GET".equals(exchange.getRequestMethod())) { | ||
String envValue = System.getenv(ENV_VARIABLE_NAME); | ||
String response = envValue != null | ||
? ENV_VARIABLE_NAME + ": " + envValue | ||
: "Environment variable " + ENV_VARIABLE_NAME + " not found"; | ||
|
||
exchange.sendResponseHeaders(200, response.length()); | ||
try (OutputStream os = exchange.getResponseBody()) { | ||
os.write(response.getBytes()); | ||
} | ||
} else { | ||
String response = "Method not allowed"; | ||
exchange.sendResponseHeaders(405, response.length()); | ||
try (OutputStream os = exchange.getResponseBody()) { | ||
os.write(response.getBytes()); | ||
} | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
core/src/test/resources/compose-file-copy-inclusions/compose-root-only.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
services: | ||
app: | ||
build: . | ||
ports: | ||
- "8080:8080" | ||
env_file: | ||
- '.env' |
7 changes: 7 additions & 0 deletions
7
core/src/test/resources/compose-file-copy-inclusions/compose-test-only.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
services: | ||
app: | ||
build: . | ||
ports: | ||
- "8080:8080" | ||
env_file: | ||
- './test/.env' |
8 changes: 8 additions & 0 deletions
8
core/src/test/resources/compose-file-copy-inclusions/compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
services: | ||
app: | ||
build: . | ||
ports: | ||
- "8080:8080" | ||
env_file: | ||
- '.env' | ||
- './test/.env' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MY_ENV_VARIABLE=override |