Skip to content

Commit

Permalink
Add tests for configuration of files that are copied for docker compo…
Browse files Browse the repository at this point in the history
…se (#8847)
  • Loading branch information
wimdeblauwe committed Jul 13, 2024
1 parent fe57080 commit fa1cacd
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 0 deletions.
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() : "";
}
}
}
1 change: 1 addition & 0 deletions core/src/test/resources/compose-file-copy-inclusions/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MY_ENV_VARIABLE=original
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"]
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());
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
app:
build: .
ports:
- "8080:8080"
env_file:
- '.env'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
app:
build: .
ports:
- "8080:8080"
env_file:
- './test/.env'
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'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MY_ENV_VARIABLE=override

0 comments on commit fa1cacd

Please sign in to comment.