Skip to content

Commit

Permalink
Cleanup docker container in test case (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin authored Jan 18, 2024
1 parent de2dc8e commit be965c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
10 changes: 5 additions & 5 deletions python/binharness/bootstrap/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class DockerAgent(AgentConnection):
it allows managing the agent in a docker container.
"""

_client: docker.DockerClient
_docker_client: docker.DockerClient
_container_id: str

def __init__(self: DockerAgent, container_id: str, port: int) -> None:
"""Initialize a DockerAgent."""
self._client = docker.from_env()
self._docker_client = docker.from_env()
self._container_id = container_id

container = self._client.containers.get(container_id)
container = self._docker_client.containers.get(container_id)
ip_address = container.attrs["NetworkSettings"]["IPAddress"]
super().__init__(ip_address, port)

Expand All @@ -34,12 +34,12 @@ def __del__(self: DockerAgent) -> None:
if self.container.status == "running":
self.container.stop()
self.container.remove()
self._client.close()
self._docker_client.close()

@property
def container(self: DockerAgent) -> docker.models.containers.Container:
"""Return the docker container."""
return self._client.containers.get(self._container_id)
return self._docker_client.containers.get(self._container_id)


def _create_in_memory_tarfile(files: dict[str, str]) -> BinaryIO:
Expand Down
20 changes: 11 additions & 9 deletions python/tests/bootstrap/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

def test_bootstrap_env_from_image(agent_binary_linux_host_arch: str) -> None:
agent = bootstrap_env_from_image(agent_binary_linux_host_arch, "ubuntu:22.04")
try:
assert isinstance(agent, DockerAgent)
assert agent.get_environment_ids() == [0]

assert isinstance(agent, DockerAgent)
assert agent.get_environment_ids() == [0]
env = agent.get_environment(0)
target = Target(env, Path("/usr/bin/echo"), args=["hello world"])
proc = NullExecutor().run_target(target)

env = agent.get_environment(0)
target = Target(env, Path("/usr/bin/echo"), args=["hello world"])
proc = NullExecutor().run_target(target)

proc.wait()
assert proc.returncode == 0
assert proc.stdout.read() == b"hello world\n"
proc.wait()
assert proc.returncode == 0
assert proc.stdout.read() == b"hello world\n"
finally:
agent.container.stop()

0 comments on commit be965c0

Please sign in to comment.