From 971af1a927c299b2b69a51d3b6adf0e29049c322 Mon Sep 17 00:00:00 2001 From: Richard North Date: Wed, 22 Apr 2020 21:14:06 +0100 Subject: [PATCH 1/2] Use relative path for temp dirs in test --- .../utility/AuthenticatedImagePullTest.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/org/testcontainers/utility/AuthenticatedImagePullTest.java b/core/src/test/java/org/testcontainers/utility/AuthenticatedImagePullTest.java index 56945643a88..66d1f761f1e 100644 --- a/core/src/test/java/org/testcontainers/utility/AuthenticatedImagePullTest.java +++ b/core/src/test/java/org/testcontainers/utility/AuthenticatedImagePullTest.java @@ -5,6 +5,7 @@ import com.github.dockerjava.api.command.PullImageResultCallback; import com.github.dockerjava.api.model.AuthConfig; import org.intellij.lang.annotations.Language; +import org.jetbrains.annotations.NotNull; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -110,7 +111,7 @@ public void testThatAuthLocatorIsUsedForContainerCreation() { @Test public void testThatAuthLocatorIsUsedForDockerfileBuild() throws IOException { // Prepare a simple temporary Dockerfile which requires our custom private image - Path tempContext = Files.createTempDirectory(Paths.get("."), this.getClass().getSimpleName() + "-test-"); + Path tempContext = getLocalTempDir(); Path tempFile = Files.createTempFile(tempContext, "test", ".Dockerfile"); String dockerFileContent = "FROM " + testImageNameWithTag; Files.write(tempFile, dockerFileContent.getBytes()); @@ -130,7 +131,7 @@ public void testThatAuthLocatorIsUsedForDockerfileBuild() throws IOException { @Test public void testThatAuthLocatorIsUsedForDockerComposePull() throws IOException { // Prepare a simple temporary Docker Compose manifest which requires our custom private image - Path tempContext = Files.createTempDirectory(Paths.get("."), this.getClass().getSimpleName() + "-test-"); + Path tempContext = getLocalTempDir(); Path tempFile = Files.createTempFile(tempContext, "test", ".docker-compose.yml"); @Language("yaml") String composeFileContent = "version: '2.0'\n" + @@ -153,6 +154,13 @@ public void testThatAuthLocatorIsUsedForDockerComposePull() throws IOException { } } + @NotNull + private Path getLocalTempDir() throws IOException { + Path projectRoot = Paths.get("."); + Path tempDirectory = Files.createTempDirectory(projectRoot, this.getClass().getSimpleName() + "-test-"); + return projectRoot.relativize(tempDirectory); + } + private static void putImageInRegistry() throws InterruptedException { // It doesn't matter which image we use for this test, but use one that's likely to have been pulled already final String dummySourceImage = TestcontainersConfiguration.getInstance().getRyukImage(); From d081dbd9653fe7bcfcab74a01c4ae5968fc68a43 Mon Sep 17 00:00:00 2001 From: Richard North Date: Thu, 23 Apr 2020 10:38:27 +0100 Subject: [PATCH 2/2] Ensure cleanup of temporary file/directory --- .../utility/AuthenticatedImagePullTest.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/src/test/java/org/testcontainers/utility/AuthenticatedImagePullTest.java b/core/src/test/java/org/testcontainers/utility/AuthenticatedImagePullTest.java index 66d1f761f1e..5f4417f218b 100644 --- a/core/src/test/java/org/testcontainers/utility/AuthenticatedImagePullTest.java +++ b/core/src/test/java/org/testcontainers/utility/AuthenticatedImagePullTest.java @@ -5,7 +5,6 @@ import com.github.dockerjava.api.command.PullImageResultCallback; import com.github.dockerjava.api.model.AuthConfig; import org.intellij.lang.annotations.Language; -import org.jetbrains.annotations.NotNull; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -111,8 +110,7 @@ public void testThatAuthLocatorIsUsedForContainerCreation() { @Test public void testThatAuthLocatorIsUsedForDockerfileBuild() throws IOException { // Prepare a simple temporary Dockerfile which requires our custom private image - Path tempContext = getLocalTempDir(); - Path tempFile = Files.createTempFile(tempContext, "test", ".Dockerfile"); + Path tempFile = getLocalTempFile(".Dockerfile"); String dockerFileContent = "FROM " + testImageNameWithTag; Files.write(tempFile, dockerFileContent.getBytes()); @@ -131,8 +129,7 @@ public void testThatAuthLocatorIsUsedForDockerfileBuild() throws IOException { @Test public void testThatAuthLocatorIsUsedForDockerComposePull() throws IOException { // Prepare a simple temporary Docker Compose manifest which requires our custom private image - Path tempContext = getLocalTempDir(); - Path tempFile = Files.createTempFile(tempContext, "test", ".docker-compose.yml"); + Path tempFile = getLocalTempFile(".docker-compose.yml"); @Language("yaml") String composeFileContent = "version: '2.0'\n" + "services:\n" + @@ -154,11 +151,16 @@ public void testThatAuthLocatorIsUsedForDockerComposePull() throws IOException { } } - @NotNull - private Path getLocalTempDir() throws IOException { + private Path getLocalTempFile(String s) throws IOException { Path projectRoot = Paths.get("."); Path tempDirectory = Files.createTempDirectory(projectRoot, this.getClass().getSimpleName() + "-test-"); - return projectRoot.relativize(tempDirectory); + Path relativeTempDirectory = projectRoot.relativize(tempDirectory); + Path tempFile = Files.createTempFile(relativeTempDirectory, "test", s); + + tempDirectory.toFile().deleteOnExit(); + tempFile.toFile().deleteOnExit(); + + return tempFile; } private static void putImageInRegistry() throws InterruptedException {