Add support for copying directories from containers#9476
Open
Jozott00 wants to merge 3 commits intotestcontainers:mainfrom
Open
Add support for copying directories from containers#9476Jozott00 wants to merge 3 commits intotestcontainers:mainfrom
Jozott00 wants to merge 3 commits intotestcontainers:mainfrom
Conversation
…FileFromContainer While the copyFileFromContainer allows consuming a generic InputStream of the Archives' first entry, it does not support directories. The new copyArchiveFromContainer method allows users to process the raw TAR archive InputStream.
Introduces a new `copyPathFromContainer` method in `ContainerState.java` that enables copying files or directories from a Docker container to the host. This method handles both file and directory copying, ensuring proper directory structure on the host. It also includes error handling for various states such as existing files conflicting with directories.
These tests cover various scenarios such as copying single files, nested directories, and handling conflicts and errors.
Author
|
@eddumelendez Is this going to be reviewed? |
|
@eddumelendez I would also like this PR merged, thx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR attempts to improve the API for transferring content from the container to the host system.
The problem with the current API
Currently
testcontainers-javahas the methodContainerState#copyFileFromContainer(String containerPath, String hostPath)which only supports copying a single file. If you try to copy a directory, the method will create an empty file in the file system of the host.The second method is the
copyFileFromContainer(String containerPath, ThrowingFunction<InputStream, T> function)method, which allows you to use a genericInputStream. Internally, however, this input stream is aTarArchiveInputStreamthat has been advanced to the first entry. This means that when reading from the input stream, only this first entry is read. It is therefore not possible to read the entire tar archive with this API. If you try to copy a directory, it will read 0 bytes.The added API
This PR adds two new methods and doesn’t change the signature of the two existing ones.
The first method is
copyArchiveFromContainer(String containerPath, ThrowingFunction<InputStream, T> function), which simply passes the raw archive stream fromdockerClientto the consumer function. This allows users to implement their own consumers for handling the tar archive.The second method is
copyPathFromContainer(String containerPath, String hostPath), which copies all files and directories from the specified container path to the specified host path.If the
containerPathpoints to a file, the behaviour is identical to that of thecopyFileFromContainermethod. However, if the file points to a directory, the file/directory structure within this directory is mirrored to the host file system within the specifiedhostPathdirectory. All required directories on the host system are created automatically.This would also provide a real solution to #1647.