-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from 7 commits
3902ead
6d34017
7b6b1f7
eaf07e5
01fed46
4dd3f44
3d028fc
e2f7df4
752ef0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -850,6 +851,60 @@ public ExecResult execInContainer(String... command) | |
return execInContainer(UTF8, command); | ||
} | ||
|
||
/** | ||
* {@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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for the nested |
||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have IOUtils on classpath, please use |
||
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} | ||
*/ | ||
|
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.*; | ||
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. output file should not be placed in |
||
File currentFile = new File("src/test/resources/test_copy_to_container.txt"); | ||
if(outputFile.exists()){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, () -> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This file purpose is to keep folder created |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Some Test | ||
Message |
There was a problem hiding this comment.
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