|
| 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 | +} |
0 commit comments