-
-
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.
Allow copy specific files to docker compose (#8848)
This commit adds support for a `withCopyFilesInContainer` method on `ComposeContainer` and `DockerComposeContainer`. It allows to specify what files or directories should be copied, instead of just copying all files. If not used, the current behavior is preserved. Fixes #8847 --------- Co-authored-by: Eddú Meléndez <[email protected]>
- Loading branch information
1 parent
6a07650
commit a321cfa
Showing
15 changed files
with
310 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
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
87 changes: 87 additions & 0 deletions
87
core/src/test/java/org/testcontainers/junit/ComposeContainerWithCopyFilesTest.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,87 @@ | ||
package org.testcontainers.junit; | ||
|
||
import io.restassured.RestAssured; | ||
import org.junit.Test; | ||
import org.testcontainers.containers.ComposeContainer; | ||
import org.testcontainers.containers.ContainerLaunchException; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
public class ComposeContainerWithCopyFilesTest { | ||
|
||
@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(); | ||
|
||
String response = readStringFromURL(environment); | ||
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) | ||
.withCopyFilesInContainer("Dockerfile", "EnvVariableRestEndpoint.java", ".env") | ||
) { | ||
environment.start(); | ||
|
||
String response = readStringFromURL(environment); | ||
|
||
// 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) | ||
.withCopyFilesInContainer("Dockerfile", "EnvVariableRestEndpoint.java", "test") | ||
) { | ||
environment.start(); | ||
|
||
String response = readStringFromURL(environment); | ||
// 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) | ||
.withCopyFilesInContainer("Dockerfile", "EnvVariableRestEndpoint.java") | ||
) { | ||
assertThatExceptionOfType(ContainerLaunchException.class) | ||
.isThrownBy(environment::start) | ||
.withMessageContaining("Container startup failed for image docker"); | ||
} | ||
} | ||
|
||
private static String readStringFromURL(ComposeContainer container) throws IOException { | ||
Integer servicePort = container.getServicePort("app-1", 8080); | ||
String serviceHost = container.getServiceHost("app-1", 8080); | ||
String requestURL = "http://" + serviceHost + ":" + servicePort + "/env"; | ||
return RestAssured.get(requestURL).thenReturn().body().asString(); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithCopyFilesTest.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,70 @@ | ||
package org.testcontainers.junit; | ||
|
||
import io.restassured.RestAssured; | ||
import org.junit.Test; | ||
import org.testcontainers.containers.DockerComposeContainer; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class DockerComposeContainerWithCopyFilesTest { | ||
|
||
@Test | ||
public void testShouldCopyAllFilesByDefault() throws IOException { | ||
try ( | ||
DockerComposeContainer environment = new DockerComposeContainer( | ||
new File("src/test/resources/compose-file-copy-inclusions/compose.yml") | ||
) | ||
.withExposedService("app", 8080) | ||
) { | ||
environment.start(); | ||
|
||
String response = readStringFromURL(environment); | ||
assertThat(response).isEqualTo("MY_ENV_VARIABLE: override"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWithFileCopyInclusionUsingFilePath() throws IOException { | ||
try ( | ||
DockerComposeContainer environment = new DockerComposeContainer( | ||
new File("src/test/resources/compose-file-copy-inclusions/compose-root-only.yml") | ||
) | ||
.withExposedService("app", 8080) | ||
.withCopyFilesInContainer("Dockerfile", "EnvVariableRestEndpoint.java", ".env") | ||
) { | ||
environment.start(); | ||
|
||
String response = readStringFromURL(environment); | ||
|
||
// 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 ( | ||
DockerComposeContainer environment = new DockerComposeContainer( | ||
new File("src/test/resources/compose-file-copy-inclusions/compose-test-only.yml") | ||
) | ||
.withExposedService("app", 8080) | ||
.withCopyFilesInContainer("Dockerfile", "EnvVariableRestEndpoint.java", "test") | ||
) { | ||
environment.start(); | ||
|
||
String response = readStringFromURL(environment); | ||
// The test directory (with its contents) is copied, so we get the override | ||
assertThat(response).isEqualTo("MY_ENV_VARIABLE: override"); | ||
} | ||
} | ||
|
||
private static String readStringFromURL(DockerComposeContainer container) throws IOException { | ||
Integer servicePort = container.getServicePort("app_1", 8080); | ||
String serviceHost = container.getServiceHost("app_1", 8080); | ||
String requestURL = "http://" + serviceHost + ":" + servicePort + "/env"; | ||
return RestAssured.get(requestURL).thenReturn().body().asString(); | ||
} | ||
} |
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' |
Oops, something went wrong.