-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 5 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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
* | ||
* @param mountableLocalFile path to file which we would like to place in container | ||
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.
|
||
* @param containerPath path where we want to copy file | ||
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.
|
||
* @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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -850,6 +849,19 @@ public ExecResult execInContainer(String... command) | |
return execInContainer(UTF8, command); | ||
} | ||
|
||
/** | ||
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. please move the JavaDoc to |
||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) throws IOException, InterruptedException { | ||
|
||
this.dockerClient | ||
.copyArchiveToContainerCmd(this.containerId) | ||
.withHostResource(mountableLocalFile.getResolvedPath()) | ||
.withRemotePath(containerPath) | ||
.exec(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
|
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; | ||
|
@@ -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.*; | ||
|
@@ -320,6 +322,47 @@ public void createContainerCmdHookTest() { | |
} | ||
} | ||
|
||
@Test | ||
public void copyToContainerTest() throws Exception { | ||
|
||
try ( | ||
GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") | ||
.withCommand("sleep 9999") | ||
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. please use |
||
){ | ||
alpineCopyToContainer.start(); | ||
|
||
final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); | ||
alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); | ||
|
||
try (final InputStream response = alpineCopyToContainer | ||
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. Since it was requested a few times already, maybe you can add |
||
.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, () -> { | ||
|
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.
Copies a file which resides inside the classpath to the container.