|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | +from testcontainers.core.container import DockerContainer |
| 5 | +from testcontainers.core.transferable import Transferable, TransferSpec, build_transfer_tar |
| 6 | + |
| 7 | +import io |
| 8 | +import tarfile |
| 9 | +from typing import Any |
| 10 | + |
| 11 | + |
| 12 | +def test_build_transfer_tar_from_bytes(): |
| 13 | + data = b"hello world" |
| 14 | + tar_bytes = build_transfer_tar(data, "/tmp/my_file") |
| 15 | + |
| 16 | + with tarfile.open(fileobj=io.BytesIO(tar_bytes)) as tar: |
| 17 | + members = tar.getmembers() |
| 18 | + assert len(members) == 1 |
| 19 | + assert members[0].name == "/tmp/my_file" |
| 20 | + assert members[0].size == len(data) |
| 21 | + assert members[0].mode == 0o644 |
| 22 | + extracted = tar.extractfile(members[0]) |
| 23 | + assert extracted is not None |
| 24 | + assert extracted.read() == data |
| 25 | + |
| 26 | + |
| 27 | +def test_build_transfer_tar_from_file(tmp_path: Path): |
| 28 | + my_file = tmp_path / "my_file" |
| 29 | + my_file.write_bytes(b"file content") |
| 30 | + |
| 31 | + tar_bytes = build_transfer_tar(my_file, "/dest/my_file", mode=0o755) |
| 32 | + |
| 33 | + with tarfile.open(fileobj=io.BytesIO(tar_bytes)) as tar: |
| 34 | + members = tar.getmembers() |
| 35 | + assert len(members) == 1 |
| 36 | + assert members[0].name == "/dest/my_file" |
| 37 | + assert members[0].mode == 0o755 |
| 38 | + extracted = tar.extractfile(members[0]) |
| 39 | + assert extracted is not None |
| 40 | + assert extracted.read() == b"file content" |
| 41 | + |
| 42 | + |
| 43 | +def test_build_transfer_tar_from_directory(tmp_path: Path): |
| 44 | + source_dir = tmp_path / "my_dir" |
| 45 | + source_dir.mkdir() |
| 46 | + (source_dir / "a.txt").write_bytes(b"aaa") |
| 47 | + |
| 48 | + tar_bytes = build_transfer_tar(source_dir, "/dest") |
| 49 | + |
| 50 | + with tarfile.open(fileobj=io.BytesIO(tar_bytes)) as tar: |
| 51 | + names = tar.getnames() |
| 52 | + assert any("my_dir" in n for n in names) |
| 53 | + assert any("a.txt" in n for n in names) |
| 54 | + |
| 55 | + |
| 56 | +def test_build_transfer_tar_rejects_invalid_type(): |
| 57 | + with pytest.raises(TypeError, match="source must be bytes or Path"): |
| 58 | + invalid: Any = 123 |
| 59 | + build_transfer_tar(invalid, "/tmp/bad") |
| 60 | + |
| 61 | + |
| 62 | +def test_build_transfer_tar_rejects_nonexistent_path(tmp_path: Path): |
| 63 | + bad_path = tmp_path / "does_not_exist" |
| 64 | + with pytest.raises(TypeError, match="neither a file nor directory"): |
| 65 | + build_transfer_tar(bad_path, "/tmp/bad") |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture(name="transferable", params=(bytes, Path)) |
| 69 | +def copy_sources_fixture(request, tmp_path: Path): |
| 70 | + """ |
| 71 | + Provide source argument for tests of copy_into_container |
| 72 | + """ |
| 73 | + raw_data = b"hello world" |
| 74 | + if request.param is bytes: |
| 75 | + return raw_data |
| 76 | + elif request.param is Path: |
| 77 | + my_file = tmp_path / "my_file" |
| 78 | + my_file.write_bytes(raw_data) |
| 79 | + return my_file |
| 80 | + pytest.fail("Invalid type") |
| 81 | + |
| 82 | + |
| 83 | +def test_copy_into_container_at_runtime(transferable: Transferable): |
| 84 | + destination_in_container = "/tmp/my_file" |
| 85 | + |
| 86 | + with DockerContainer("bash", command="sleep infinity") as container: |
| 87 | + container.copy_into_container(transferable, destination_in_container) |
| 88 | + result = container.exec(f"cat {destination_in_container}") |
| 89 | + |
| 90 | + assert result.exit_code == 0 |
| 91 | + assert result.output == b"hello world" |
| 92 | + |
| 93 | + |
| 94 | +def test_copy_into_container_at_startup(transferable: Transferable): |
| 95 | + destination_in_container = "/tmp/my_file" |
| 96 | + |
| 97 | + container = DockerContainer("bash", command="sleep infinity") |
| 98 | + container.with_copy_into_container(transferable, destination_in_container) |
| 99 | + |
| 100 | + with container: |
| 101 | + result = container.exec(f"cat {destination_in_container}") |
| 102 | + |
| 103 | + assert result.exit_code == 0 |
| 104 | + assert result.output == b"hello world" |
| 105 | + |
| 106 | + |
| 107 | +def test_copy_into_container_via_initializer(transferable: Transferable): |
| 108 | + destination_in_container = "/tmp/my_file" |
| 109 | + transferables: list[TransferSpec] = [(transferable, destination_in_container, 0o644)] |
| 110 | + |
| 111 | + with DockerContainer("bash", command="sleep infinity", transferables=transferables) as container: |
| 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_file_from_container(tmp_path: Path): |
| 119 | + file_in_container = "/tmp/foo.txt" |
| 120 | + destination_on_host = tmp_path / "foo.txt" |
| 121 | + assert not destination_on_host.is_file() |
| 122 | + |
| 123 | + with DockerContainer("bash", command="sleep infinity") as container: |
| 124 | + result = container.exec(f'bash -c "echo -n hello world > {file_in_container}"') |
| 125 | + assert result.exit_code == 0 |
| 126 | + container.copy_from_container(file_in_container, destination_on_host) |
| 127 | + |
| 128 | + assert destination_on_host.is_file() |
| 129 | + assert destination_on_host.read_text() == "hello world" |
| 130 | + |
| 131 | + |
| 132 | +def test_copy_directory_into_container(tmp_path: Path): |
| 133 | + source_dir = tmp_path / "my_directory" |
| 134 | + source_dir.mkdir() |
| 135 | + my_file = source_dir / "my_file" |
| 136 | + my_file.write_bytes(b"hello world") |
| 137 | + |
| 138 | + destination_in_container = "/tmp/my_destination_directory" |
| 139 | + |
| 140 | + with DockerContainer("bash", command="sleep infinity") as container: |
| 141 | + container.copy_into_container(source_dir, destination_in_container) |
| 142 | + result = container.exec(f"ls {destination_in_container}") |
| 143 | + |
| 144 | + assert result.exit_code == 0 |
| 145 | + assert result.output == b"my_directory\n" |
| 146 | + |
| 147 | + result = container.exec(f"ls {destination_in_container}/my_directory") |
| 148 | + assert result.exit_code == 0 |
| 149 | + assert result.output == b"my_file\n" |
| 150 | + |
| 151 | + result = container.exec(f"cat {destination_in_container}/my_directory/my_file") |
| 152 | + assert result.exit_code == 0 |
| 153 | + assert result.output == b"hello world" |
0 commit comments