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

22 changes: 22 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,27 @@ ExecResult execInContainer(String... command)
ExecResult execInContainer(Charset outputCharset, String... command)
throws UnsupportedOperationException, IOException, InterruptedException;

/**
*
* Copies a file which resides inside the classpath to the container.
*
* @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
*/
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<Integer> getExposedPorts();

List<String> getPortBindings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,8 +32,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 +851,60 @@ 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}
*/
@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)) {
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

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)) {
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()

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}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
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;
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;
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 @@ -320,6 +323,68 @@ 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");
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

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

outputFile.delete();
}

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

//clean up
if(outputFile.exists()){
outputFile.delete();
}
}
}

@Test(expected = NotFoundException.class)
public void copyFromContainerShouldFailBecauseNoFileTest() throws NotFoundException, IOException, InterruptedException {

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 {

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, () -> {
Expand Down
1 change: 1 addition & 0 deletions core/src/test/resources/copy-from/info
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file purpose is to keep folder created
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