|
1 | 1 | import tempfile
|
2 | 2 | from pathlib import Path
|
3 | 3 |
|
4 |
| -from testcontainers.core.container import DockerContainer |
| 4 | +from testcontainers.core.container import DockerContainer, Transferrable |
5 | 5 |
|
6 | 6 |
|
7 | 7 | def test_garbage_collection_is_defensive():
|
@@ -46,3 +46,121 @@ def test_docker_container_with_env_file():
|
46 | 46 | assert "[email protected]" in output
|
47 | 47 | assert "ROOT_URL=example.org/app" in output
|
48 | 48 | print(output)
|
| 49 | + |
| 50 | + |
| 51 | +def test_copy_file_into_container_at_runtime(tmp_path: Path): |
| 52 | + # Given |
| 53 | + my_file = tmp_path / "my_file" |
| 54 | + my_file.write_text("hello world") |
| 55 | + destination_in_container = "/tmp/my_file" |
| 56 | + |
| 57 | + with DockerContainer("bash", command="sleep infinity") as container: |
| 58 | + # When |
| 59 | + container.copy_into_container(my_file, destination_in_container) |
| 60 | + result = container.exec(f"cat {destination_in_container}") |
| 61 | + |
| 62 | + # Then |
| 63 | + assert result.exit_code == 0 |
| 64 | + assert result.output == b"hello world" |
| 65 | + |
| 66 | + |
| 67 | +def test_copy_file_into_container_at_startup(tmp_path: Path): |
| 68 | + # Given |
| 69 | + my_file = tmp_path / "my_file" |
| 70 | + my_file.write_text("hello world") |
| 71 | + destination_in_container = "/tmp/my_file" |
| 72 | + |
| 73 | + container = DockerContainer("bash", command="sleep infinity") |
| 74 | + container.with_copy_into_container(my_file, destination_in_container) |
| 75 | + |
| 76 | + with container: |
| 77 | + # When |
| 78 | + result = container.exec(f"cat {destination_in_container}") |
| 79 | + |
| 80 | + # Then |
| 81 | + assert result.exit_code == 0 |
| 82 | + assert result.output == b"hello world" |
| 83 | + |
| 84 | + |
| 85 | +def test_copy_file_into_container_via_initializer(tmp_path: Path): |
| 86 | + # Given |
| 87 | + my_file = tmp_path / "my_file" |
| 88 | + my_file.write_text("hello world") |
| 89 | + destination_in_container = "/tmp/my_file" |
| 90 | + |
| 91 | + with DockerContainer( |
| 92 | + "bash", command="sleep infinity", transferrables=(Transferrable(my_file, destination_in_container),) |
| 93 | + ) as container: |
| 94 | + # When |
| 95 | + result = container.exec(f"cat {destination_in_container}") |
| 96 | + |
| 97 | + # Then |
| 98 | + assert result.exit_code == 0 |
| 99 | + assert result.output == b"hello world" |
| 100 | + |
| 101 | + |
| 102 | +def test_copy_bytes_to_container_at_runtime(): |
| 103 | + # Given |
| 104 | + file_content = b"hello world" |
| 105 | + destination_in_container = "/tmp/my_file" |
| 106 | + |
| 107 | + with DockerContainer("bash", command="sleep infinity") as container: |
| 108 | + # When |
| 109 | + container.copy_into_container(file_content, destination_in_container) |
| 110 | + |
| 111 | + # Then |
| 112 | + result = container.exec(f"cat {destination_in_container}") |
| 113 | + |
| 114 | + assert result.exit_code == 0 |
| 115 | + assert result.output == b"hello world" |
| 116 | + |
| 117 | + |
| 118 | +def test_copy_bytes_to_container_at_startup(): |
| 119 | + # Given |
| 120 | + file_content = b"hello world" |
| 121 | + destination_in_container = "/tmp/my_file" |
| 122 | + |
| 123 | + container = DockerContainer("bash", command="sleep infinity") |
| 124 | + container.with_copy_into_container(file_content, destination_in_container) |
| 125 | + |
| 126 | + with container: |
| 127 | + # When |
| 128 | + result = container.exec(f"cat {destination_in_container}") |
| 129 | + |
| 130 | + # Then |
| 131 | + assert result.exit_code == 0 |
| 132 | + assert result.output == b"hello world" |
| 133 | + |
| 134 | + |
| 135 | +def test_copy_bytes_to_container_via_initializer(): |
| 136 | + # Given |
| 137 | + file_content = b"hello world" |
| 138 | + destination_in_container = "/tmp/my_file" |
| 139 | + |
| 140 | + with DockerContainer( |
| 141 | + "bash", command="sleep infinity", transferrables=(Transferrable(file_content, destination_in_container),) |
| 142 | + ) as container: |
| 143 | + # When |
| 144 | + result = container.exec(f"cat {destination_in_container}") |
| 145 | + |
| 146 | + # Then |
| 147 | + assert result.exit_code == 0 |
| 148 | + assert result.output == b"hello world" |
| 149 | + |
| 150 | + |
| 151 | +def test_copy_file_from_container(tmp_path: Path): |
| 152 | + # Given |
| 153 | + file_in_container = "/tmp/foo.txt" |
| 154 | + destination_on_host = tmp_path / "foo.txt" |
| 155 | + assert not destination_on_host.is_file() |
| 156 | + |
| 157 | + with DockerContainer("bash", command="sleep infinity") as container: |
| 158 | + result = container.exec(f'bash -c "echo -n hello world > {file_in_container}"') |
| 159 | + assert result.exit_code == 0 |
| 160 | + |
| 161 | + # When |
| 162 | + container.copy_from_container(file_in_container, destination_on_host) |
| 163 | + |
| 164 | + # Then |
| 165 | + assert destination_on_host.is_file() |
| 166 | + assert destination_on_host.read_text() == "hello world" |
0 commit comments