Skip to content

Commit

Permalink
Add test for docker agent bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin committed Jan 17, 2024
1 parent 77aab23 commit e0e179a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ strip = true
sdist-generator = "cargo"

[tool.pytest.ini_options]
addopts = "--tb=native --cov=binharness --cov-report lcov:.lcov --cov-report term-missing --mypy"
markers = ["linux", "darwin", "win32"]
addopts = "--cov=binharness --cov-report lcov:.lcov --cov-report term-missing --mypy"
markers = ["linux", "darwin", "win32", "docker"]

[tool.coverage.run]
branch = true
Expand Down Expand Up @@ -74,6 +74,7 @@ required-imports = ["from __future__ import annotations"]
[tool.ruff.per-file-ignores]
"python/tests/*" = [
"D",
"INP001",
"S101",
]
"**/*.pyi" = [
Expand Down
20 changes: 17 additions & 3 deletions python/binharness/bootstrap/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ def __init__(self: DockerAgent, container_id: str, port: int) -> None:

def __del__(self: DockerAgent) -> None:
"""__del__ is overridden to ensure that the docker client is closed."""
if self.container.status == "running":
self.container.stop()
self.container.remove()
self._client.close()

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


def _create_in_memory_tarfile(files: dict[str, str]) -> BinaryIO:
file_like_object = io.BytesIO()
Expand All @@ -46,10 +54,15 @@ def _create_in_memory_tarfile(files: dict[str, str]) -> BinaryIO:


def bootstrap_env_from_image(
agent_binary: str, image: str, port: int = 60162
agent_binary: str,
image: str,
port: int = 60162,
client: docker.DockerClient | None = None,
) -> DockerAgent:
"""Bootstraps an agent running in a docker container."""
client = docker.from_env()
user_client = client is not None
if client is None:
client = docker.from_env()
try:
# Setup container
client.images.pull(image)
Expand All @@ -67,4 +80,5 @@ def bootstrap_env_from_image(
# Build agent
return DockerAgent(container.id, port)
finally:
client.close()
if not user_client:
client.close()
22 changes: 22 additions & 0 deletions python/tests/bootstrap/test_docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

from pathlib import Path

from binharness.bootstrap.docker import DockerAgent, bootstrap_env_from_image
from binharness.types.executor import NullExecutor
from binharness.types.target import Target


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")

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)

proc.wait()
assert proc.returncode == 0
assert proc.stdout.read() == b"hello world\n"
33 changes: 33 additions & 0 deletions python/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
from __future__ import annotations

import platform
import sys
from pathlib import Path

import pytest

ALL = {"darwin", "linux", "win32"}


def pytest_runtest_setup(item: pytest.Item) -> None:
# Skip tests not marked as supported on the current platform
supported_platforms = ALL.intersection(mark.name for mark in item.iter_markers())
plat = sys.platform
if supported_platforms and plat not in supported_platforms:
pytest.skip(f"cannot run on platform {plat}")

# Skip docker tests if docker is not installed
if "docker" in list(item.iter_markers()):
try:
import docker
except ImportError:
pytest.skip("docker is not installed")
try:
docker.from_env()
except docker.errors.DockerException:
pytest.skip("docker is not running")


@pytest.fixture()
def agent_binary_linux_host_arch() -> str:
arch = platform.machine()
# Fixup arch for arm64
if arch == "arm64":
arch = "aarch64"

expected_path = (
Path(__file__).parent.parent.parent
/ "target"
/ f"{arch}-unknown-linux-musl"
/ "debug"
/ "bh_agent_server"
).absolute()
if not expected_path.exists():
pytest.skip("agent binary not found")
return str(expected_path)

0 comments on commit e0e179a

Please sign in to comment.