From 3902ead2c511d388ea6c8e2127d7671428586ea1 Mon Sep 17 00:00:00 2001 From: lukidzi Date: Sun, 25 Jun 2017 20:30:07 +0200 Subject: [PATCH 1/9] First concept. Created method which copy file from repository which we point to container path. --- .../testcontainers/containers/Container.java | 2 ++ .../containers/GenericContainer.java | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/core/src/main/java/org/testcontainers/containers/Container.java b/core/src/main/java/org/testcontainers/containers/Container.java index 2f2d9fa0917..6a1815770a6 100644 --- a/core/src/main/java/org/testcontainers/containers/Container.java +++ b/core/src/main/java/org/testcontainers/containers/Container.java @@ -418,6 +418,8 @@ ExecResult execInContainer(Charset outputCharset, String... command) InspectContainerResponse getContainerInfo(); + void copyFileToContanier(String localPath, String containerPath); + void setExposedPorts(List exposedPorts); void setPortBindings(List portBindings); diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 6b055860256..36df1425531 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -850,6 +850,27 @@ public ExecResult execInContainer(String... command) return execInContainer(UTF8, command); } + /** + * + * Method allow to copy file which we have in our repository to docker container + * + * @param localPath path with file which we would like to place in container + * @param containerPath path where we want to copy file + */ + @Override + public void copyFileToContanier(String localPath, String containerPath){ + + if (!isRunning()) { + throw new IllegalStateException("Container is not running so copy cannot be run"); + } + + this.dockerClient + .copyArchiveToContainerCmd(this.containerId) + .withHostResource(localPath) + .withRemotePath(containerPath) + .exec(); + } + /** * {@inheritDoc} */ From 6d340170e9f8caadccaab50b3e14a494e48a3233 Mon Sep 17 00:00:00 2001 From: lukidzi Date: Mon, 26 Jun 2017 20:10:01 +0200 Subject: [PATCH 2/9] Added unit test which check if file exist on container. Made changes after PR review. --- .../testcontainers/containers/Container.java | 14 ++++++-- .../containers/GenericContainer.java | 17 +++------ .../junit/GenericContainerRuleTest.java | 35 +++++++++++++++++++ .../test/resources/test_copy_to_container.txt | 2 ++ 4 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 core/src/test/resources/test_copy_to_container.txt diff --git a/core/src/main/java/org/testcontainers/containers/Container.java b/core/src/main/java/org/testcontainers/containers/Container.java index 6a1815770a6..03fc27d81c7 100644 --- a/core/src/main/java/org/testcontainers/containers/Container.java +++ b/core/src/main/java/org/testcontainers/containers/Container.java @@ -10,6 +10,7 @@ import org.testcontainers.containers.traits.LinkableContainer; import org.testcontainers.containers.wait.Wait; import org.testcontainers.containers.wait.WaitStrategy; +import org.testcontainers.utility.MountableFile; import java.io.IOException; import java.nio.charset.Charset; @@ -392,6 +393,17 @@ ExecResult execInContainer(String... command) ExecResult execInContainer(Charset outputCharset, String... command) throws UnsupportedOperationException, IOException, InterruptedException; + /** + * + * Method allow to copy file which we have in our repository to docker container + * + * @param mountableLocalFile path to file which we would like to place in container + * @param containerPath path where we want to copy file + * @throws IOException if there's an issue communicating with Docker + * @throws InterruptedException if the thread waiting for the response is interrupted + */ + void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) throws IOException, InterruptedException; + List getExposedPorts(); List getPortBindings(); @@ -418,8 +430,6 @@ ExecResult execInContainer(Charset outputCharset, String... command) InspectContainerResponse getContainerInfo(); - void copyFileToContanier(String localPath, String containerPath); - void setExposedPorts(List exposedPorts); void setPortBindings(List portBindings); diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 36df1425531..19b67b2ed8c 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -30,8 +30,7 @@ import org.testcontainers.images.RemoteDockerImage; import org.testcontainers.utility.*; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.nio.charset.Charset; import java.nio.file.Path; import java.time.Duration; @@ -851,22 +850,14 @@ public ExecResult execInContainer(String... command) } /** - * - * Method allow to copy file which we have in our repository to docker container - * - * @param localPath path with file which we would like to place in container - * @param containerPath path where we want to copy file + * {@inheritDoc} */ @Override - public void copyFileToContanier(String localPath, String containerPath){ - - if (!isRunning()) { - throw new IllegalStateException("Container is not running so copy cannot be run"); - } + public void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) throws IOException, InterruptedException { this.dockerClient .copyArchiveToContainerCmd(this.containerId) - .withHostResource(localPath) + .withHostResource(mountableLocalFile.getDescription()) .withRemotePath(containerPath) .exec(); } diff --git a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java index 45c1a78d10f..96b1cf4c8e5 100644 --- a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java +++ b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java @@ -1,5 +1,6 @@ package org.testcontainers.junit; +import com.github.dockerjava.api.exception.NotFoundException; import com.google.common.collect.ImmutableMap; import com.google.common.util.concurrent.Uninterruptibles; import com.mongodb.MongoClient; @@ -12,6 +13,7 @@ import org.rnorth.ducttape.unreliables.Unreliables; import org.testcontainers.containers.GenericContainer; import org.testcontainers.utility.Base58; +import org.testcontainers.utility.MountableFile; import org.testcontainers.utility.TestEnvironment; import java.io.*; @@ -121,6 +123,13 @@ public static void setupContent() throws FileNotFoundException { .withExtraHost("somehost", "192.168.1.10") .withCommand("/bin/sh", "-c", "while true; do cat /etc/hosts | nc -l -p 80; done"); + /** + * Create a container which wait for some time to test if copy to container works. + */ + @ClassRule + public static GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") + .withCommand("sleep 9999"); + // @Test // public void simpleRedisTest() { // String ipAddress = redis.getContainerIpAddress(); @@ -320,6 +329,32 @@ public void createContainerCmdHookTest() { } } + @Test + public void copyToContainerTest() throws Exception { + final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); + alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); + + try (final InputStream response = alpineCopyToContainer + .getDockerClient() + .copyArchiveFromContainerCmd(alpineCopyToContainer.getContainerId(), "/home/test_copy_to_container.txt") + .exec()) { + boolean bytesAvailable = response.available() > 0; + assertTrue("The file was copied to the container.", bytesAvailable); + } + } + + @Test(expected = NotFoundException.class) + public void copyToContainerShouldFailBecauseNoFileTest() throws NotFoundException, IOException { + + try (final InputStream response = alpineCopyToContainer + .getDockerClient() + .copyArchiveFromContainerCmd(alpineCopyToContainer.getContainerId(), "/tmp/test.txt") + .exec()) { + boolean bytesAvailable = response.available() > 0; + assertTrue("The file was copied to the container.", bytesAvailable); + } + } + private BufferedReader getReaderForContainerPort80(GenericContainer container) { return Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { diff --git a/core/src/test/resources/test_copy_to_container.txt b/core/src/test/resources/test_copy_to_container.txt new file mode 100644 index 00000000000..2c377842030 --- /dev/null +++ b/core/src/test/resources/test_copy_to_container.txt @@ -0,0 +1,2 @@ +Some Test +Message \ No newline at end of file From 7b6b1f7b98f138e71fadd8c1930b2dafe08564a4 Mon Sep 17 00:00:00 2001 From: lukidzi Date: Sun, 2 Jul 2017 21:52:03 +0200 Subject: [PATCH 3/9] Code review. Moved container creation to try-with-resources and changed method to getResolvedPath --- .../containers/GenericContainer.java | 2 +- .../junit/GenericContainerRuleTest.java | 45 ++++++++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 19b67b2ed8c..a0324665625 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -857,7 +857,7 @@ public void copyFileToContainer(MountableFile mountableLocalFile, String contain this.dockerClient .copyArchiveToContainerCmd(this.containerId) - .withHostResource(mountableLocalFile.getDescription()) + .withHostResource(mountableLocalFile.getResolvedPath()) .withRemotePath(containerPath) .exec(); } diff --git a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java index 96b1cf4c8e5..5d90ddebb45 100644 --- a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java +++ b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java @@ -331,27 +331,42 @@ public void createContainerCmdHookTest() { @Test public void copyToContainerTest() throws Exception { - final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); - alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); - - try (final InputStream response = alpineCopyToContainer - .getDockerClient() - .copyArchiveFromContainerCmd(alpineCopyToContainer.getContainerId(), "/home/test_copy_to_container.txt") - .exec()) { - boolean bytesAvailable = response.available() > 0; - assertTrue("The file was copied to the container.", bytesAvailable); + + try ( + GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") + .withCommand("sleep 9999") + ){ + alpineCopyToContainer.start(); + + final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); + alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); + + try (final InputStream response = alpineCopyToContainer + .getDockerClient() + .copyArchiveFromContainerCmd(alpineCopyToContainer.getContainerId(), "/home/test_copy_to_container.txt") + .exec()) { + boolean bytesAvailable = response.available() > 0; + assertTrue("The file wasn't copied to the container.", bytesAvailable); + } } } @Test(expected = NotFoundException.class) public void copyToContainerShouldFailBecauseNoFileTest() throws NotFoundException, IOException { - try (final InputStream response = alpineCopyToContainer - .getDockerClient() - .copyArchiveFromContainerCmd(alpineCopyToContainer.getContainerId(), "/tmp/test.txt") - .exec()) { - boolean bytesAvailable = response.available() > 0; - assertTrue("The file was copied to the container.", bytesAvailable); + try ( + final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") + .withCommand("sleep 9999") + ) { + alpineCopyToContainer.start(); + + try (final InputStream response = alpineCopyToContainer + .getDockerClient() + .copyArchiveFromContainerCmd(alpineCopyToContainer.getContainerId(), "/tmp/test.txt") + .exec()) { + boolean bytesAvailable = response.available() > 0; + assertTrue("The file wasn't copied to the container.", bytesAvailable); + } } } From eaf07e56e699225fb672001d34984f03bdf4e791 Mon Sep 17 00:00:00 2001 From: lukidzi Date: Sun, 2 Jul 2017 21:56:31 +0200 Subject: [PATCH 4/9] Removed ClassRule level container creation --- .../testcontainers/junit/GenericContainerRuleTest.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java index 5d90ddebb45..16900e84c10 100644 --- a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java +++ b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java @@ -122,14 +122,7 @@ public static void setupContent() throws FileNotFoundException { .withExposedPorts(80) .withExtraHost("somehost", "192.168.1.10") .withCommand("/bin/sh", "-c", "while true; do cat /etc/hosts | nc -l -p 80; done"); - - /** - * Create a container which wait for some time to test if copy to container works. - */ - @ClassRule - public static GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") - .withCommand("sleep 9999"); - + // @Test // public void simpleRedisTest() { // String ipAddress = redis.getContainerIpAddress(); From 01fed46c9104e0d5283efcd7bb37ee77652f9b2d Mon Sep 17 00:00:00 2001 From: lukidzi Date: Mon, 16 Oct 2017 22:46:49 +0200 Subject: [PATCH 5/9] Added code review changes. --- core/src/main/java/org/testcontainers/containers/Container.java | 2 +- .../java/org/testcontainers/junit/GenericContainerRuleTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/testcontainers/containers/Container.java b/core/src/main/java/org/testcontainers/containers/Container.java index 03fc27d81c7..2e04b21b59a 100644 --- a/core/src/main/java/org/testcontainers/containers/Container.java +++ b/core/src/main/java/org/testcontainers/containers/Container.java @@ -395,7 +395,7 @@ ExecResult execInContainer(Charset outputCharset, String... command) /** * - * Method allow to copy file which we have in our repository to docker container + * Method allow to copy file which is inside classpath to docker container * * @param mountableLocalFile path to file which we would like to place in container * @param containerPath path where we want to copy file diff --git a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java index 16900e84c10..499dbb4c9db 100644 --- a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java +++ b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java @@ -122,7 +122,7 @@ public static void setupContent() throws FileNotFoundException { .withExposedPorts(80) .withExtraHost("somehost", "192.168.1.10") .withCommand("/bin/sh", "-c", "while true; do cat /etc/hosts | nc -l -p 80; done"); - + // @Test // public void simpleRedisTest() { // String ipAddress = redis.getContainerIpAddress(); From 4dd3f444f03a6307baff55fa322c0c4e52a188ad Mon Sep 17 00:00:00 2001 From: lukidzi Date: Wed, 15 Nov 2017 17:54:48 +0100 Subject: [PATCH 6/9] Added changes to java doc --- .../main/java/org/testcontainers/containers/Container.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/testcontainers/containers/Container.java b/core/src/main/java/org/testcontainers/containers/Container.java index 2e04b21b59a..55f92dca0ee 100644 --- a/core/src/main/java/org/testcontainers/containers/Container.java +++ b/core/src/main/java/org/testcontainers/containers/Container.java @@ -395,10 +395,10 @@ ExecResult execInContainer(Charset outputCharset, String... command) /** * - * Method allow to copy file which is inside classpath to docker container + * Copies a file which resides inside the classpath to the container. * - * @param mountableLocalFile path to file which we would like to place in container - * @param containerPath path where we want to copy file + * @param mountableLocalFile file which is copied into the container + * @param containerPath destination path inside the container * @throws IOException if there's an issue communicating with Docker * @throws InterruptedException if the thread waiting for the response is interrupted */ From 3d028fc068453090e9f6087f3feb9369e5bc988e Mon Sep 17 00:00:00 2001 From: lukidzi Date: Sun, 19 Nov 2017 00:37:16 +0100 Subject: [PATCH 7/9] Added copy from container method --- .../testcontainers/containers/Container.java | 10 +++ .../containers/GenericContainer.java | 43 +++++++++++++ .../junit/GenericContainerRuleTest.java | 64 +++++++++++++------ core/src/test/resources/copy-from/info | 1 + 4 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 core/src/test/resources/copy-from/info diff --git a/core/src/main/java/org/testcontainers/containers/Container.java b/core/src/main/java/org/testcontainers/containers/Container.java index 55f92dca0ee..9606d53ec10 100644 --- a/core/src/main/java/org/testcontainers/containers/Container.java +++ b/core/src/main/java/org/testcontainers/containers/Container.java @@ -404,6 +404,16 @@ ExecResult execInContainer(Charset outputCharset, String... command) */ void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) throws IOException, InterruptedException; + /** + * Copies a file which resides inside the container to user defined directory + * + * @param containerPath path to file which is copied from container + * @param destinationPath destination path to which file is copied with file name + * @throws IOException if there's an issue communicating with Docker or receiving entry from TarArchiveInputStream + * @throws InterruptedException if the thread waiting for the response is interrupted + */ + void copyFileFromContainer(String containerPath, String destinationPath) throws IOException, InterruptedException; + List getExposedPorts(); List getPortBindings(); diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index a0324665625..8ceaee6d8e5 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -9,6 +9,8 @@ import com.google.common.base.Preconditions; import com.google.common.base.Strings; import lombok.*; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.jetbrains.annotations.Nullable; import org.junit.runner.Description; import org.rnorth.ducttape.ratelimits.RateLimiter; @@ -862,6 +864,47 @@ public void copyFileToContainer(MountableFile mountableLocalFile, String contain .exec(); } + /** + * {@inheritDoc} + */ + @Override + public void copyFileFromContainer(String containerPath, String destinationPath) throws IOException, InterruptedException { + try (final InputStream response = this.dockerClient + .copyArchiveFromContainerCmd(this.containerId, containerPath) + .exec()) { + try (final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(response)) { + createFileFromTarArchiveInputStream(destinationPath, tarInputStream); + } + } + } + + /** + * Create file from archive stream + * + * @param destinationPath path with file name where file will be saved + * @param tarArchiveInputStream archive stream from container + * @throws IOException if there will be error during getNextTarEntry + */ + private void createFileFromTarArchiveInputStream(final String destinationPath, final TarArchiveInputStream tarArchiveInputStream) throws IOException { + try (final FileOutputStream out = new FileOutputStream(destinationPath)) { + final TarArchiveEntry entry = tarArchiveInputStream.getNextTarEntry(); + long size = entry.getSize(); + byte[] buf = new byte[1024 * 16]; + int read; + int left = buf.length; + if (left > size) + left = (int) size; + while ((read = tarArchiveInputStream.read(buf, 0, left)) != -1) { + + out.write(buf, 0, read); + + size = size - read; + if (left > size) + left = (int) size; + } + } + } + /** * {@inheritDoc} */ diff --git a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java index 499dbb4c9db..5f3f61a1584 100644 --- a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java +++ b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java @@ -7,6 +7,7 @@ import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.rabbitmq.client.*; +import org.apache.commons.io.FileUtils; import org.bson.Document; import org.junit.*; import org.rnorth.ducttape.RetryCountExceededException; @@ -324,45 +325,66 @@ public void createContainerCmdHookTest() { @Test public void copyToContainerTest() throws Exception { + //compare purpose + File outputFile = new File("src/test/resources/copy-from/test_copy_to_container.txt"); + File currentFile = new File("src/test/resources/test_copy_to_container.txt"); + if(outputFile.exists()){ + outputFile.delete(); + } - try ( - GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") - .withCommand("sleep 9999") - ){ + try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") + .withCommand("top")){ alpineCopyToContainer.start(); final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); + alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", "src/test/resources/copy-from/test_copy_to_container.txt"); + + assertTrue("Files aren't same ", FileUtils.contentEquals(currentFile, outputFile)); - try (final InputStream response = alpineCopyToContainer - .getDockerClient() - .copyArchiveFromContainerCmd(alpineCopyToContainer.getContainerId(), "/home/test_copy_to_container.txt") - .exec()) { - boolean bytesAvailable = response.available() > 0; - assertTrue("The file wasn't copied to the container.", bytesAvailable); + //clean up + if(outputFile.exists()){ + outputFile.delete(); } } } @Test(expected = NotFoundException.class) - public void copyToContainerShouldFailBecauseNoFileTest() throws NotFoundException, IOException { + public void copyFromContainerShouldFailBecauseNoFileTest() throws NotFoundException, IOException, InterruptedException { - try ( - final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") - .withCommand("sleep 9999") - ) { + try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") + .withCommand("top")) { alpineCopyToContainer.start(); + alpineCopyToContainer.copyFileFromContainer("/home/test.txt", "src/test/resources/copy-from/test.txt"); + } + } + + @Test + public void shouldCopyFileFromContainerTest() throws IOException, InterruptedException { - try (final InputStream response = alpineCopyToContainer - .getDockerClient() - .copyArchiveFromContainerCmd(alpineCopyToContainer.getContainerId(), "/tmp/test.txt") - .exec()) { - boolean bytesAvailable = response.available() > 0; - assertTrue("The file wasn't copied to the container.", bytesAvailable); + File outputFile = new File("src/test/resources/copy-from/test_copy_to_container.txt"); + File currentFile = new File("src/test/resources/test_copy_to_container.txt"); + if(outputFile.exists()){ + outputFile.delete(); + } + + try (GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") + .withCommand("top")) { + alpineCopyToContainer.start(); + final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); + alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); + alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", "src/test/resources/copy-from/test_copy_to_container.txt"); + + assertTrue("Files aren't same ", FileUtils.contentEquals(currentFile, outputFile)); + + //clean up + if(outputFile.exists()){ + outputFile.delete(); } } } + private BufferedReader getReaderForContainerPort80(GenericContainer container) { return Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { diff --git a/core/src/test/resources/copy-from/info b/core/src/test/resources/copy-from/info new file mode 100644 index 00000000000..8dd4c925bc6 --- /dev/null +++ b/core/src/test/resources/copy-from/info @@ -0,0 +1 @@ +This file purpose is to keep folder created \ No newline at end of file From e2f7df4bc0e43b5567fabd5c8b0372f3bb37145c Mon Sep 17 00:00:00 2001 From: lukidzi Date: Mon, 20 Nov 2017 19:49:28 +0100 Subject: [PATCH 8/9] Code review changes --- .../containers/GenericContainer.java | 42 +++------------- .../junit/GenericContainerRuleTest.java | 50 ++++++------------- core/src/test/resources/copy-from/info | 1 - 3 files changed, 24 insertions(+), 69 deletions(-) delete mode 100644 core/src/test/resources/copy-from/info diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 8ceaee6d8e5..e4f98646296 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -9,7 +9,7 @@ import com.google.common.base.Preconditions; import com.google.common.base.Strings; import lombok.*; -import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.jetbrains.annotations.Nullable; import org.junit.runner.Description; @@ -32,7 +32,9 @@ import org.testcontainers.images.RemoteDockerImage; import org.testcontainers.utility.*; -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Path; import java.time.Duration; @@ -869,39 +871,11 @@ public void copyFileToContainer(MountableFile mountableLocalFile, String contain */ @Override public void copyFileFromContainer(String containerPath, String destinationPath) throws IOException, InterruptedException { - try (final InputStream response = this.dockerClient + try (final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(this.dockerClient .copyArchiveFromContainerCmd(this.containerId, containerPath) - .exec()) { - try (final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(response)) { - createFileFromTarArchiveInputStream(destinationPath, tarInputStream); - } - } - } - - /** - * Create file from archive stream - * - * @param destinationPath path with file name where file will be saved - * @param tarArchiveInputStream archive stream from container - * @throws IOException if there will be error during getNextTarEntry - */ - private void createFileFromTarArchiveInputStream(final String destinationPath, final TarArchiveInputStream tarArchiveInputStream) throws IOException { - try (final FileOutputStream out = new FileOutputStream(destinationPath)) { - final TarArchiveEntry entry = tarArchiveInputStream.getNextTarEntry(); - long size = entry.getSize(); - byte[] buf = new byte[1024 * 16]; - int read; - int left = buf.length; - if (left > size) - left = (int) size; - while ((read = tarArchiveInputStream.read(buf, 0, left)) != -1) { - - out.write(buf, 0, read); - - size = size - read; - if (left > size) - left = (int) size; - } + .exec())) { + tarInputStream.getNextTarEntry(); + IOUtils.copy(tarInputStream, new FileOutputStream(destinationPath)); } } diff --git a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java index 5f3f61a1584..4eb6934927b 100644 --- a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java +++ b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java @@ -42,15 +42,14 @@ public class GenericContainerRuleTest { private static final String RABBITMQ_TEST_MESSAGE = "Hello world"; private static final int RABBITMQ_PORT = 5672; private static final int MONGO_PORT = 27017; - + private static final File CONTENT_FOLDER = new File(System.getProperty("user.home") + "/.tmp-test-container"); /* * Test data setup */ @BeforeClass public static void setupContent() throws FileNotFoundException { - File contentFolder = new File(System.getProperty("user.home") + "/.tmp-test-container"); - contentFolder.mkdir(); - writeStringToFile(contentFolder, "file", "Hello world!"); + CONTENT_FOLDER.mkdir(); + writeStringToFile(CONTENT_FOLDER, "file", "Hello world!"); } /** @@ -325,12 +324,6 @@ public void createContainerCmdHookTest() { @Test public void copyToContainerTest() throws Exception { - //compare purpose - File outputFile = new File("src/test/resources/copy-from/test_copy_to_container.txt"); - File currentFile = new File("src/test/resources/test_copy_to_container.txt"); - if(outputFile.exists()){ - outputFile.delete(); - } try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") .withCommand("top")){ @@ -338,14 +331,12 @@ public void copyToContainerTest() throws Exception { final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); - alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", "src/test/resources/copy-from/test_copy_to_container.txt"); + alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", + CONTENT_FOLDER.getAbsolutePath() + "/test_copy_to_container.txt"); - assertTrue("Files aren't same ", FileUtils.contentEquals(currentFile, outputFile)); - - //clean up - if(outputFile.exists()){ - outputFile.delete(); - } + File expectedFile = new File(mountableFile.getResolvedPath()); + File actualFile = new File(CONTENT_FOLDER.getAbsolutePath() + "/test_copy_to_container.txt"); + assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile,actualFile)); } } @@ -361,30 +352,21 @@ public void copyFromContainerShouldFailBecauseNoFileTest() throws NotFoundExcept @Test public void shouldCopyFileFromContainerTest() throws IOException, InterruptedException { - - File outputFile = new File("src/test/resources/copy-from/test_copy_to_container.txt"); - File currentFile = new File("src/test/resources/test_copy_to_container.txt"); - if(outputFile.exists()){ - outputFile.delete(); - } - - try (GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") + try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") .withCommand("top")) { + alpineCopyToContainer.start(); final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); - alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", "src/test/resources/copy-from/test_copy_to_container.txt"); - - assertTrue("Files aren't same ", FileUtils.contentEquals(currentFile, outputFile)); + alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", + CONTENT_FOLDER.getAbsolutePath() + "/test_copy_from_container.txt"); - //clean up - if(outputFile.exists()){ - outputFile.delete(); - } + File expectedFile = new File(mountableFile.getResolvedPath()); + File actualFile = new File(CONTENT_FOLDER.getAbsolutePath() + "/test_copy_from_container.txt"); + assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile,actualFile)); } } - - + private BufferedReader getReaderForContainerPort80(GenericContainer container) { return Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { diff --git a/core/src/test/resources/copy-from/info b/core/src/test/resources/copy-from/info deleted file mode 100644 index 8dd4c925bc6..00000000000 --- a/core/src/test/resources/copy-from/info +++ /dev/null @@ -1 +0,0 @@ -This file purpose is to keep folder created \ No newline at end of file From 752ef0feb47c05562ff66b3476be1e7d00094160 Mon Sep 17 00:00:00 2001 From: lukidzi Date: Mon, 20 Nov 2017 20:18:23 +0100 Subject: [PATCH 9/9] Changed to temp folder --- .../junit/GenericContainerRuleTest.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java index 4eb6934927b..e1dc2764161 100644 --- a/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java +++ b/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java @@ -2,6 +2,7 @@ import com.github.dockerjava.api.exception.NotFoundException; import com.google.common.collect.ImmutableMap; +import com.google.common.io.Files; import com.google.common.util.concurrent.Uninterruptibles; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; @@ -42,14 +43,15 @@ public class GenericContainerRuleTest { private static final String RABBITMQ_TEST_MESSAGE = "Hello world"; private static final int RABBITMQ_PORT = 5672; private static final int MONGO_PORT = 27017; - private static final File CONTENT_FOLDER = new File(System.getProperty("user.home") + "/.tmp-test-container"); + /* * Test data setup */ @BeforeClass public static void setupContent() throws FileNotFoundException { - CONTENT_FOLDER.mkdir(); - writeStringToFile(CONTENT_FOLDER, "file", "Hello world!"); + File contentFolder = new File(System.getProperty("user.home") + "/.tmp-test-container"); + contentFolder.mkdir(); + writeStringToFile(contentFolder, "file", "Hello world!"); } /** @@ -324,18 +326,19 @@ public void createContainerCmdHookTest() { @Test public void copyToContainerTest() throws Exception { + final File tempResultFolder = Files.createTempDir(); try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") .withCommand("top")){ + alpineCopyToContainer.start(); - final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", - CONTENT_FOLDER.getAbsolutePath() + "/test_copy_to_container.txt"); + tempResultFolder.getAbsolutePath() + "/test_copy_to_container.txt"); File expectedFile = new File(mountableFile.getResolvedPath()); - File actualFile = new File(CONTENT_FOLDER.getAbsolutePath() + "/test_copy_to_container.txt"); + File actualFile = new File(tempResultFolder.getAbsolutePath() + "/test_copy_to_container.txt"); assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile,actualFile)); } } @@ -352,6 +355,8 @@ public void copyFromContainerShouldFailBecauseNoFileTest() throws NotFoundExcept @Test public void shouldCopyFileFromContainerTest() throws IOException, InterruptedException { + final File tempResultFolder = Files.createTempDir(); + try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") .withCommand("top")) { @@ -359,14 +364,14 @@ public void shouldCopyFileFromContainerTest() throws IOException, InterruptedExc final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", - CONTENT_FOLDER.getAbsolutePath() + "/test_copy_from_container.txt"); + tempResultFolder.getAbsolutePath() + "/test_copy_from_container.txt"); File expectedFile = new File(mountableFile.getResolvedPath()); - File actualFile = new File(CONTENT_FOLDER.getAbsolutePath() + "/test_copy_from_container.txt"); + File actualFile = new File(tempResultFolder.getAbsolutePath() + "/test_copy_from_container.txt"); assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile,actualFile)); } } - + private BufferedReader getReaderForContainerPort80(GenericContainer container) { return Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> {