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

12 changes: 12 additions & 0 deletions core/src/main/java/org/testcontainers/containers/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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?

*
* @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

* @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

* @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<Integer> getExposedPorts();

List<String> getPortBindings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -850,6 +849,19 @@ public ExecResult execInContainer(String... command)
return execInContainer(UTF8, 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

* {@inheritDoc}
*/
@Override
public void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) throws IOException, InterruptedException {

this.dockerClient
.copyArchiveToContainerCmd(this.containerId)
.withHostResource(mountableLocalFile.getResolvedPath())
.withRemotePath(containerPath)
.exec();
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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.

// public void simpleRedisTest() {
// String ipAddress = redis.getContainerIpAddress();
Expand Down Expand Up @@ -320,6 +322,47 @@ public void createContainerCmdHookTest() {
}
}

@Test
public void copyToContainerTest() throws Exception {

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

){
alpineCopyToContainer.start();

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

.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 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);
}
}
}

private BufferedReader getReaderForContainerPort80(GenericContainer container) {

return Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> {
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/resources/test_copy_to_container.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Some Test
Message