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