Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created method which copy file from repository to container #378

Conversation

lukidzi
Copy link
Contributor

@lukidzi lukidzi commented Jun 25, 2017

I am not sure if this function will be helpful. This is first concept I need to figure out how to check if path exist, how to get message if couldn't copy, and write some tests. I am tested it and I was able to copy file from my resource directory to container.

@@ -418,6 +418,8 @@ ExecResult execInContainer(Charset outputCharset, String... command)

InspectContainerResponse getContainerInfo();

void copyFileToContanier(String localPath, String containerPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using MountableFile instead of String localPath

@Override
public void copyFileToContanier(String localPath, String containerPath){

if (!isRunning()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since isRunning calls Docker API, I would remove this check and catch an exception from .exec()

@@ -851,6 +851,27 @@ public ExecResult execInContainer(String... command)
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move the JavaDoc to Container interface

@bsideup
Copy link
Member

bsideup commented Jun 25, 2017

Hi @lukidzi,

Thanks for your contribution! I added a few review notes from me :)

I would also like to see the tests for this change

@@ -418,6 +418,8 @@ ExecResult execInContainer(Charset outputCharset, String... command)

InspectContainerResponse getContainerInfo();

void copyFileToContanier(String localPath, String containerPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo as well

@lukidzi lukidzi changed the title First concept. Created method which copy file from repository which w… Created method which copy file from repository to container Jun 27, 2017
@bsideup
Copy link
Member

bsideup commented Jul 2, 2017

Hey @glebsts,

looks good, but could you please fix what Coday says? (press details to see what's wrong)

Thanks!


this.dockerClient
.copyArchiveToContainerCmd(this.containerId)
.withHostResource(mountableLocalFile.getDescription())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use getResolvedPath

* 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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it would be nice if you can replace it with try-with-resources version (see NetworkTest.WithoutRules). It helps to run individual tests without starting everything

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be removed now

@glebsts
Copy link
Contributor

glebsts commented Jul 2, 2017

@bsideup wrong mention, I guess?)

@bsideup
Copy link
Member

bsideup commented Jul 2, 2017

@glebsts ooops!

/cc @lukidzi

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small Javadoc change:
This method allows to copy a file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think it's a file inside the classpath and not inside the repository, correct?

@@ -120,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code wasn't commented in this PR.
Although +1 for removing this added space

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, you're right.

@rnorth
Copy link
Member

rnorth commented Oct 16, 2017

@lukidzi I think this would be a good thing to have if you're still interested in contributing. If you're stuck for time please let us know, though, as it doesn't look like thee's too much more to do.

Copy link
Member

@kiview kiview left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, sorry for not looking into this PR for some time. I've requested some minor Javadoc changes, and will merge ASAP afterwards.

@@ -392,6 +393,17 @@ ExecResult execInContainer(String... command)
ExecResult execInContainer(Charset outputCharset, String... command)
throws UnsupportedOperationException, IOException, InterruptedException;

/**
*
* Method allow to copy file which is inside classpath to docker container
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copies a file which resides inside the classpath to the 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file which is copied into the 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

destination path inside the container

@kiview
Copy link
Member

kiview commented Nov 16, 2017

@bsideup You're review is still pending, I think the changes are fine now?


try (
GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2")
.withCommand("sleep 9999")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use top instead of sleep 9999

final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt");
alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/");

try (final InputStream response = alpineCopyToContainer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it was requested a few times already, maybe you can add copyFileFromContainer() as well? :) We should also check the file content here

try (final InputStream response = this.dockerClient
.copyArchiveFromContainerCmd(this.containerId, containerPath)
.exec()) {
try (final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(response)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for the nested trys, just wrap .exec() result into TarArchiveInputStream

* @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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have IOUtils on classpath, please use IOUtils.copy()

@Test
public void copyToContainerTest() throws Exception {
//compare purpose
File outputFile = new File("src/test/resources/copy-from/test_copy_to_container.txt");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output file should not be placed in src/test, use a temporary directory instead

//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()){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temp dir will help to avoid this one as well

@@ -39,15 +42,14 @@
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you use something like Files.createTempDirectory()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, my bad

@bsideup
Copy link
Member

bsideup commented Nov 20, 2017

approved, thanks! @rnorth do you have something to add?

@rnorth
Copy link
Member

rnorth commented Nov 26, 2017

Sorry for being slow to respond - just noticed one more thing: because these methods only work when the container is created/running, we should add guards to check and log a suitable error message. I have the branch locally, so will add that.

@rnorth
Copy link
Member

rnorth commented Nov 26, 2017

Have rebased and squashed on a separate branch, so will merge from there.

@rnorth rnorth closed this in 524b957 Nov 26, 2017
@rnorth
Copy link
Member

rnorth commented Nov 26, 2017

Merged! Thank you for the contribution @lukidzi !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants