-
Notifications
You must be signed in to change notification settings - Fork 347
feat: Transferable #852
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
Open
rhoban13
wants to merge
9
commits into
testcontainers:main
Choose a base branch
from
rhoban13:copy_file_tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−0
Open
feat: Transferable #852
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c2cda80
Tests demonstrating copy file interfaces
rhoban13 631740e
spelling and split out dataclass
rhoban13 8c29c86
fixes
rhoban13 a78ce9f
Merge branch 'main' into copy_file_tests
rhoban13 e989f8c
Merge branch 'main' into copy_file_tests
Tranquility2 612e30f
parametrize tests to simplify a little
rhoban13 3d5e078
Make Transferable simply a Union
rhoban13 1fd9831
Implement & test copying a directory
rhoban13 847c42e
Merge branch 'main' into copy_file_tests
rhoban13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import dataclasses | ||
| import pathlib | ||
| from typing import Union | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class Transferable: | ||
| """ | ||
| Wrapper class enabling copying files into a container | ||
| """ | ||
|
|
||
| source: Union[bytes, pathlib.Path] | ||
| destination_in_container: str | ||
| mode: int = 0o644 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| from pathlib import Path | ||
|
|
||
| from testcontainers.core.container import DockerContainer | ||
| from testcontainers.core.transferable import Transferable | ||
|
|
||
|
|
||
| def test_garbage_collection_is_defensive(): | ||
|
|
@@ -46,3 +47,119 @@ def test_docker_container_with_env_file(): | |
| assert "[email protected]" in output | ||
| assert "ROOT_URL=example.org/app" in output | ||
| print(output) | ||
|
|
||
|
|
||
| def test_copy_file_into_container_at_runtime(tmp_path: Path): | ||
| # Given | ||
| my_file = tmp_path / "my_file" | ||
| my_file.write_text("hello world") | ||
| destination_in_container = "/tmp/my_file" | ||
|
|
||
| with DockerContainer("bash", command="sleep infinity") as container: | ||
| # When | ||
| container.copy_into_container(my_file, destination_in_container) | ||
| result = container.exec(f"cat {destination_in_container}") | ||
|
|
||
| # Then | ||
| assert result.exit_code == 0 | ||
| assert result.output == b"hello world" | ||
|
|
||
|
|
||
| def test_copy_file_into_container_at_startup(tmp_path: Path): | ||
| # Given | ||
| my_file = tmp_path / "my_file" | ||
| my_file.write_text("hello world") | ||
| destination_in_container = "/tmp/my_file" | ||
|
|
||
| container = DockerContainer("bash", command="sleep infinity") | ||
| container.with_copy_into_container(my_file, destination_in_container) | ||
|
|
||
| with container: | ||
| # When | ||
| result = container.exec(f"cat {destination_in_container}") | ||
|
|
||
| # Then | ||
| assert result.exit_code == 0 | ||
| assert result.output == b"hello world" | ||
|
|
||
|
|
||
| def test_copy_file_into_container_via_initializer(tmp_path: Path): | ||
| # Given | ||
| my_file = tmp_path / "my_file" | ||
| my_file.write_text("hello world") | ||
| destination_in_container = "/tmp/my_file" | ||
| transferables = [Transferable(my_file, destination_in_container)] | ||
|
|
||
| with DockerContainer("bash", command="sleep infinity", transferables=transferables) as container: | ||
| # When | ||
| result = container.exec(f"cat {destination_in_container}") | ||
|
|
||
| # Then | ||
| assert result.exit_code == 0 | ||
| assert result.output == b"hello world" | ||
|
|
||
|
|
||
| def test_copy_bytes_to_container_at_runtime(): | ||
| # Given | ||
| file_content = b"hello world" | ||
| destination_in_container = "/tmp/my_file" | ||
|
|
||
| with DockerContainer("bash", command="sleep infinity") as container: | ||
| # When | ||
| container.copy_into_container(file_content, destination_in_container) | ||
|
|
||
| # Then | ||
| result = container.exec(f"cat {destination_in_container}") | ||
|
|
||
| assert result.exit_code == 0 | ||
| assert result.output == b"hello world" | ||
|
|
||
|
|
||
| def test_copy_bytes_to_container_at_startup(): | ||
| # Given | ||
| file_content = b"hello world" | ||
| destination_in_container = "/tmp/my_file" | ||
|
|
||
| container = DockerContainer("bash", command="sleep infinity") | ||
| container.with_copy_into_container(file_content, destination_in_container) | ||
|
|
||
| with container: | ||
| # When | ||
| result = container.exec(f"cat {destination_in_container}") | ||
|
|
||
| # Then | ||
| assert result.exit_code == 0 | ||
| assert result.output == b"hello world" | ||
|
|
||
|
|
||
| def test_copy_bytes_to_container_via_initializer(): | ||
| # Given | ||
| file_content = b"hello world" | ||
| destination_in_container = "/tmp/my_file" | ||
| transferables = [Transferable(file_content, destination_in_container)] | ||
|
|
||
| with DockerContainer("bash", command="sleep infinity", transferables=transferables) as container: | ||
| # When | ||
| result = container.exec(f"cat {destination_in_container}") | ||
|
|
||
| # Then | ||
| assert result.exit_code == 0 | ||
| assert result.output == b"hello world" | ||
|
|
||
|
|
||
| def test_copy_file_from_container(tmp_path: Path): | ||
| # Given | ||
| file_in_container = "/tmp/foo.txt" | ||
| destination_on_host = tmp_path / "foo.txt" | ||
| assert not destination_on_host.is_file() | ||
|
|
||
| with DockerContainer("bash", command="sleep infinity") as container: | ||
| result = container.exec(f'bash -c "echo -n hello world > {file_in_container}"') | ||
| assert result.exit_code == 0 | ||
|
|
||
| # When | ||
| container.copy_from_container(file_in_container, destination_on_host) | ||
|
|
||
| # Then | ||
| assert destination_on_host.is_file() | ||
| assert destination_on_host.read_text() == "hello world" | ||
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.
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.
Maybe the name
file_contenthere isn't great? Maybesourceorcopy_source?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.
In java they all just take transferable and destination I think
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.
I refined the initial definition of
Transferableto now simply the be the union of those types. I kinda like that the signatures all just containtransferable: Transferable. It was an easy update except for the case of passing args to the initializer, which I changed to more closely mirror the type passed tovolumes.