|
| 1 | +package org.testcontainers.junit; |
| 2 | + |
| 3 | +import io.restassured.RestAssured; |
| 4 | +import org.junit.Test; |
| 5 | +import org.testcontainers.containers.ComposeContainer; |
| 6 | +import org.testcontainers.containers.ContainerLaunchException; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | + |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; |
| 12 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 13 | + |
| 14 | +public class ComposeContainerWithCopyFilesTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void testShouldCopyAllFilesByDefault() throws IOException { |
| 18 | + try ( |
| 19 | + ComposeContainer environment = new ComposeContainer( |
| 20 | + new File("src/test/resources/compose-file-copy-inclusions/compose.yml") |
| 21 | + ) |
| 22 | + .withExposedService("app", 8080) |
| 23 | + ) { |
| 24 | + environment.start(); |
| 25 | + |
| 26 | + String response = readStringFromURL(environment); |
| 27 | + assertThat(response).isEqualTo("MY_ENV_VARIABLE: override"); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testWithFileCopyInclusionUsingFilePath() throws IOException { |
| 33 | + try ( |
| 34 | + ComposeContainer environment = new ComposeContainer( |
| 35 | + new File("src/test/resources/compose-file-copy-inclusions/compose-root-only.yml") |
| 36 | + ) |
| 37 | + .withExposedService("app", 8080) |
| 38 | + .withCopyFilesInContainer("Dockerfile", "EnvVariableRestEndpoint.java", ".env") |
| 39 | + ) { |
| 40 | + environment.start(); |
| 41 | + |
| 42 | + String response = readStringFromURL(environment); |
| 43 | + |
| 44 | + // The `test/.env` file is not copied, now so we get the original value |
| 45 | + assertThat(response).isEqualTo("MY_ENV_VARIABLE: original"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testWithFileCopyInclusionUsingDirectoryPath() throws IOException { |
| 51 | + try ( |
| 52 | + ComposeContainer environment = new ComposeContainer( |
| 53 | + new File("src/test/resources/compose-file-copy-inclusions/compose-test-only.yml") |
| 54 | + ) |
| 55 | + .withExposedService("app", 8080) |
| 56 | + .withCopyFilesInContainer("Dockerfile", "EnvVariableRestEndpoint.java", "test") |
| 57 | + ) { |
| 58 | + environment.start(); |
| 59 | + |
| 60 | + String response = readStringFromURL(environment); |
| 61 | + // The test directory (with its contents) is copied, so we get the override |
| 62 | + assertThat(response).isEqualTo("MY_ENV_VARIABLE: override"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testShouldNotBeAbleToStartIfNeededEnvFileIsNotCopied() { |
| 68 | + try ( |
| 69 | + ComposeContainer environment = new ComposeContainer( |
| 70 | + new File("src/test/resources/compose-file-copy-inclusions/compose-test-only.yml") |
| 71 | + ) |
| 72 | + .withExposedService("app", 8080) |
| 73 | + .withCopyFilesInContainer("Dockerfile", "EnvVariableRestEndpoint.java") |
| 74 | + ) { |
| 75 | + assertThatExceptionOfType(ContainerLaunchException.class) |
| 76 | + .isThrownBy(environment::start) |
| 77 | + .withMessageContaining("Container startup failed for image docker"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static String readStringFromURL(ComposeContainer container) throws IOException { |
| 82 | + Integer servicePort = container.getServicePort("app-1", 8080); |
| 83 | + String serviceHost = container.getServiceHost("app-1", 8080); |
| 84 | + String requestURL = "http://" + serviceHost + ":" + servicePort + "/env"; |
| 85 | + return RestAssured.get(requestURL).thenReturn().body().asString(); |
| 86 | + } |
| 87 | +} |
0 commit comments