From 06efeda05299f3254e36026734be717546809ffc Mon Sep 17 00:00:00 2001 From: Matthias Wiedemann Date: Sun, 20 Feb 2022 07:48:23 +0100 Subject: [PATCH 1/3] load testing from classpath path --- .../cmd/CopyArchiveFromContainerCmdIT.java | 2 +- .../cmd/CopyArchiveToContainerCmdIT.java | 56 +++++++++---------- .../dockerjava/utils/TestResources.java | 5 +- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveFromContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveFromContainerCmdIT.java index a0eb585eb..b3e390d13 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveFromContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveFromContainerCmdIT.java @@ -66,7 +66,7 @@ public void copyFromContainerBinaryFile() throws Exception { assertThat(container.getId(), not(isEmptyOrNullString())); Path temp = Files.createTempFile("", ".tar.gz"); - Path binaryFile = Paths.get("src/test/resources/testCopyFromArchive/binary.dat"); + Path binaryFile = Paths.get(ClassLoader.getSystemResource("testCopyFromArchive/binary.dat").toURI()); CompressArchiveUtil.tar(binaryFile, temp, true, false); try (InputStream uploadStream = Files.newInputStream(temp)) { diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java index c8bac183a..01f8d00f2 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java @@ -28,12 +28,12 @@ public class CopyArchiveToContainerCmdIT extends CmdIT { public void copyFileToContainer() throws Exception { CreateContainerResponse container = prepareContainerForCopy("1"); Path temp = Files.createTempFile("", ".tar.gz"); - CompressArchiveUtil.tar(Paths.get("src/test/resources/testReadFile"), temp, true, false); + CompressArchiveUtil.tar(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()), temp, true, false); try (InputStream uploadStream = Files.newInputStream(temp)) { dockerRule.getClient() - .copyArchiveToContainerCmd(container.getId()) - .withTarInputStream(uploadStream) - .exec(); + .copyArchiveToContainerCmd(container.getId()) + .withTarInputStream(uploadStream) + .exec(); assertFileCopied(container); } } @@ -42,16 +42,16 @@ public void copyFileToContainer() throws Exception { public void copyStreamToContainer() throws Exception { CreateContainerResponse container = prepareContainerForCopy("2"); dockerRule.getClient().copyArchiveToContainerCmd(container.getId()) - .withHostResource("src/test/resources/testReadFile") - .exec(); + .withHostResource(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()).toString()) + .exec(); assertFileCopied(container); } @Test public void copyStreamToContainerTwice() throws Exception { CreateContainerResponse container = prepareContainerForCopy("rerun"); - CopyArchiveToContainerCmd copyArchiveToContainerCmd=dockerRule.getClient().copyArchiveToContainerCmd(container.getId()) - .withHostResource("src/test/resources/testReadFile"); + CopyArchiveToContainerCmd copyArchiveToContainerCmd = dockerRule.getClient().copyArchiveToContainerCmd(container.getId()) + .withHostResource(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()).toString()); copyArchiveToContainerCmd.exec(); assertFileCopied(container); //run again to make sure no DockerClientException @@ -60,8 +60,8 @@ public void copyStreamToContainerTwice() throws Exception { private CreateContainerResponse prepareContainerForCopy(String method) { CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") - .withName("docker-java-itest-copyToContainer" + method) - .exec(); + .withName("docker-java-itest-copyToContainer" + method) + .exec(); LOG.info("Created container: {}", container); assertThat(container.getId(), not(isEmptyOrNullString())); dockerRule.getClient().startContainerCmd(container.getId()).exec(); @@ -72,18 +72,18 @@ private CreateContainerResponse prepareContainerForCopy(String method) { private void assertFileCopied(CreateContainerResponse container) throws IOException { try (InputStream response = dockerRule.getClient().copyArchiveFromContainerCmd(container.getId(), "testReadFile").exec()) { boolean bytesAvailable = response.read() != -1; - assertTrue( "The file was not copied to the container.", bytesAvailable); + assertTrue("The file was not copied to the container.", bytesAvailable); } } @Test(expected = NotFoundException.class) public void copyToNonExistingContainer() throws Exception { - - dockerRule.getClient().copyArchiveToContainerCmd("non-existing").withHostResource("src/test/resources/testReadFile").exec(); + dockerRule.getClient().copyArchiveToContainerCmd("non-existing") + .withHostResource(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()).toString()).exec(); } @Test - public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception{ + public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception { // create a temp dir Path localDir = Files.createTempDirectory(null); localDir.toFile().deleteOnExit(); @@ -96,25 +96,25 @@ public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception{ // create a test container CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") - .withCmd("sleep", "9999") - .exec(); + .withCmd("sleep", "9999") + .exec(); // start the container dockerRule.getClient().startContainerCmd(container.getId()).exec(); // copy data from local dir to container dockerRule.getClient().copyArchiveToContainerCmd(container.getId()) - .withHostResource(localDir.toString()) - .exec(); + .withHostResource(localDir.toString()) + .exec(); // cleanup dir FileUtils.deleteDirectory(localDir.toFile()); } - + @Test public void copyFileWithExecutePermission() throws Exception { // create script file, add permission to execute Path scriptPath = Files.createTempFile("run", ".sh"); boolean executable = scriptPath.toFile().setExecutable(true, false); - if (!executable){ + if (!executable) { throw new Exception("Execute permission on file not set!"); } String snippet = "Running script with execute permission."; @@ -125,20 +125,20 @@ public void copyFileWithExecutePermission() throws Exception { // script to be copied to the container's home dir and then executes it String containerCmd = "sleep 3; /home/" + scriptPath.getFileName().toString(); CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") - .withName("copyFileWithExecutivePerm") - .withCmd("/bin/sh", "-c", containerCmd) - .exec(); + .withName("copyFileWithExecutivePerm") + .withCmd("/bin/sh", "-c", containerCmd) + .exec(); // start the container dockerRule.getClient().startContainerCmd(container.getId()).exec(); // copy script to container home dir dockerRule.getClient().copyArchiveToContainerCmd(container.getId()) - .withRemotePath("/home") - .withHostResource(scriptPath.toString()) - .exec(); + .withRemotePath("/home") + .withHostResource(scriptPath.toString()) + .exec(); // await exid code int exitCode = dockerRule.getClient().waitContainerCmd(container.getId()) - .start() - .awaitStatusCode(); + .start() + .awaitStatusCode(); // check result assertThat(exitCode, equalTo(0)); } diff --git a/docker-java/src/test/java/com/github/dockerjava/utils/TestResources.java b/docker-java/src/test/java/com/github/dockerjava/utils/TestResources.java index 35ece680f..2a56333f1 100644 --- a/docker-java/src/test/java/com/github/dockerjava/utils/TestResources.java +++ b/docker-java/src/test/java/com/github/dockerjava/utils/TestResources.java @@ -1,5 +1,6 @@ package com.github.dockerjava.utils; +import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; @@ -8,7 +9,7 @@ public class TestResources { private TestResources() { } - public static Path getApiImagesLoadTestTarball() { - return Paths.get("src/test/resources/api/images/load/image.tar"); + public static Path getApiImagesLoadTestTarball() throws URISyntaxException { + return Paths.get(ClassLoader.getSystemResource("api/images/load/image.tar").toURI()); } } From aba4cfb0fdc1b6daaa5017160c19e1184b224e29 Mon Sep 17 00:00:00 2001 From: Matthias Wiedemann Date: Wed, 8 Mar 2023 23:46:28 +0100 Subject: [PATCH 2/3] reverted whitespace changes --- .../cmd/CopyArchiveToContainerCmdIT.java | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java index 01f8d00f2..b0c2c6247 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java @@ -31,9 +31,9 @@ public void copyFileToContainer() throws Exception { CompressArchiveUtil.tar(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()), temp, true, false); try (InputStream uploadStream = Files.newInputStream(temp)) { dockerRule.getClient() - .copyArchiveToContainerCmd(container.getId()) - .withTarInputStream(uploadStream) - .exec(); + .copyArchiveToContainerCmd(container.getId()) + .withTarInputStream(uploadStream) + .exec(); assertFileCopied(container); } } @@ -60,8 +60,8 @@ public void copyStreamToContainerTwice() throws Exception { private CreateContainerResponse prepareContainerForCopy(String method) { CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") - .withName("docker-java-itest-copyToContainer" + method) - .exec(); + .withName("docker-java-itest-copyToContainer" + method) + .exec(); LOG.info("Created container: {}", container); assertThat(container.getId(), not(isEmptyOrNullString())); dockerRule.getClient().startContainerCmd(container.getId()).exec(); @@ -72,7 +72,7 @@ private CreateContainerResponse prepareContainerForCopy(String method) { private void assertFileCopied(CreateContainerResponse container) throws IOException { try (InputStream response = dockerRule.getClient().copyArchiveFromContainerCmd(container.getId(), "testReadFile").exec()) { boolean bytesAvailable = response.read() != -1; - assertTrue("The file was not copied to the container.", bytesAvailable); + assertTrue( "The file was not copied to the container.", bytesAvailable); } } @@ -83,7 +83,7 @@ public void copyToNonExistingContainer() throws Exception { } @Test - public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception { + public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception{ // create a temp dir Path localDir = Files.createTempDirectory(null); localDir.toFile().deleteOnExit(); @@ -96,25 +96,25 @@ public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception { // create a test container CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") - .withCmd("sleep", "9999") - .exec(); + .withCmd("sleep", "9999") + .exec(); // start the container dockerRule.getClient().startContainerCmd(container.getId()).exec(); // copy data from local dir to container dockerRule.getClient().copyArchiveToContainerCmd(container.getId()) - .withHostResource(localDir.toString()) - .exec(); + .withHostResource(localDir.toString()) + .exec(); // cleanup dir FileUtils.deleteDirectory(localDir.toFile()); } - + @Test public void copyFileWithExecutePermission() throws Exception { // create script file, add permission to execute Path scriptPath = Files.createTempFile("run", ".sh"); boolean executable = scriptPath.toFile().setExecutable(true, false); - if (!executable) { + if (!executable){ throw new Exception("Execute permission on file not set!"); } String snippet = "Running script with execute permission."; @@ -125,20 +125,20 @@ public void copyFileWithExecutePermission() throws Exception { // script to be copied to the container's home dir and then executes it String containerCmd = "sleep 3; /home/" + scriptPath.getFileName().toString(); CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") - .withName("copyFileWithExecutivePerm") - .withCmd("/bin/sh", "-c", containerCmd) - .exec(); + .withName("copyFileWithExecutivePerm") + .withCmd("/bin/sh", "-c", containerCmd) + .exec(); // start the container dockerRule.getClient().startContainerCmd(container.getId()).exec(); // copy script to container home dir dockerRule.getClient().copyArchiveToContainerCmd(container.getId()) - .withRemotePath("/home") - .withHostResource(scriptPath.toString()) - .exec(); + .withRemotePath("/home") + .withHostResource(scriptPath.toString()) + .exec(); // await exid code int exitCode = dockerRule.getClient().waitContainerCmd(container.getId()) - .start() - .awaitStatusCode(); + .start() + .awaitStatusCode(); // check result assertThat(exitCode, equalTo(0)); } From dc3d8014f895cdf6b6964ff65a40fbee6fbcebfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 8 Mar 2023 17:51:30 -0600 Subject: [PATCH 3/3] Fix build and polish --- .../dockerjava/cmd/CopyArchiveToContainerCmdIT.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java index 54a5c1aed..bbd98932f 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/CopyArchiveToContainerCmdIT.java @@ -47,8 +47,8 @@ public void copyFileToContainer() throws Exception { public void copyStreamToContainer() throws Exception { CreateContainerResponse container = prepareContainerForCopy("2"); dockerRule.getClient().copyArchiveToContainerCmd(container.getId()) - .withHostResource(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()).toString()) - .exec(); + .withHostResource(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()).toString()) + .exec(); assertFileCopied(container); } @@ -56,7 +56,7 @@ public void copyStreamToContainer() throws Exception { public void copyStreamToContainerTwice() throws Exception { CreateContainerResponse container = prepareContainerForCopy("rerun"); CopyArchiveToContainerCmd copyArchiveToContainerCmd = dockerRule.getClient().copyArchiveToContainerCmd(container.getId()) - .withHostResource(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()).toString()); + .withHostResource(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()).toString()); copyArchiveToContainerCmd.exec(); assertFileCopied(container); //run again to make sure no DockerClientException @@ -82,9 +82,9 @@ private void assertFileCopied(CreateContainerResponse container) throws IOExcept } @Test(expected = NotFoundException.class) - public void copyToNonExistingContainer() { + public void copyToNonExistingContainer() throws Exception { dockerRule.getClient().copyArchiveToContainerCmd("non-existing") - .withHostResource(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()).toString()).exec(); + .withHostResource(Paths.get(ClassLoader.getSystemResource("testReadFile").toURI()).toString()).exec(); } @Test @@ -113,7 +113,7 @@ public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception{ // cleanup dir FileUtils.deleteDirectory(localDir.toFile()); } - + @Test public void copyFileWithExecutePermission() throws Exception { // create script file, add permission to execute