Skip to content

Commit 4b7db74

Browse files
basic transferable demo
1 parent 2bd20c6 commit 4b7db74

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

core/testcontainers/core/docker_client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from docker.models.containers import Container, ContainerCollection
1717
import functools as ft
1818
import os
19-
from typing import List, Optional, Union
19+
from typing import Any, Dict, List, Optional, Union
2020
import urllib
2121

2222
from .utils import default_gateway_ip, inside_container, setup_logger
@@ -65,7 +65,7 @@ def port(self, container_id: str, port: int) -> int:
6565
'not available')
6666
return port_mappings[0]["HostPort"]
6767

68-
def get_container(self, container_id: str) -> Container:
68+
def get_container(self, container_id: str) -> Dict[str, Any]:
6969
"""
7070
Get the container with a given identifier.
7171
"""
@@ -110,3 +110,10 @@ def host(self) -> str:
110110
if ip_address:
111111
return ip_address
112112
return "localhost"
113+
114+
def status(self, container_id: str) -> str:
115+
"""
116+
Get the status of a container. (running, stopped, etc)
117+
"""
118+
container = self.get_container(container_id)
119+
return container['State']

core/testcontainers/core/transferable.py

Whitespace-only changes.

core/testcontainers/core/waiting_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,11 @@ def wait_for_logs(container: "DockerContainer", predicate: Union[Callable, str],
105105
raise TimeoutError(f"Container did not emit logs satisfying predicate in {timeout:.3f} "
106106
"seconds")
107107
time.sleep(interval)
108+
109+
110+
def wait_for_status(container: "DockerContainer", status: str = 'running') -> None:
111+
start = time.time()
112+
while config.TIMEOUT > time.time() - start:
113+
if status == container.get_docker_client().status(container.get_wrapped_container().id):
114+
break
115+
time.sleep(config.SLEEP_TIME)

core/tests/test_transferable.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from pathlib import Path
2+
from tarfile import open as tarfile_open, TarInfo
3+
from io import BytesIO
4+
5+
from testcontainers.core.container import DockerContainer
6+
from testcontainers.core.docker_client import DockerClient
7+
from testcontainers.core.transferable import *
8+
from testcontainers.core.waiting_utils import *
9+
from docker.models.containers import Container
10+
11+
12+
def test_docker_custom_image():
13+
alpine = DockerContainer('alpine')
14+
alpine.with_command('sleep 1000000')
15+
alpine.start()
16+
wait_for_status(alpine)
17+
18+
def put_file(dest: str, content: str):
19+
alpine.exec(f'touch "{dest}"')
20+
21+
# setup tar info
22+
content_encode = content.encode()
23+
content_bytes = BytesIO(content_encode)
24+
into = TarInfo(name=Path(dest).name)
25+
# into = TarInfo(name=dest)
26+
into.size = len(content_encode)
27+
28+
# setup tar
29+
output_bytes = BytesIO()
30+
with tarfile_open(fileobj=output_bytes, mode='w:gz') as tar:
31+
tar.addfile(into, content_bytes)
32+
33+
# create tar
34+
data = output_bytes.getvalue()
35+
36+
# peform docker api call
37+
alpine.get_wrapped_container() \
38+
.put_archive(str(Path(dest).parent), data)
39+
40+
put_file('/etc/stuff', 'content')
41+
42+
print(alpine.exec('cat /etc/stuff'))

0 commit comments

Comments
 (0)