Skip to content

Commit

Permalink
Skip docker tests if not available (#67)
Browse files Browse the repository at this point in the history
* Skip docker tests if not available

* Fix rename
  • Loading branch information
twizmwazin authored Jan 20, 2024
1 parent f4c8bcf commit 80a9fc2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ sdist-generator = "cargo"

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

[tool.coverage.run]
branch = true
Expand Down
14 changes: 7 additions & 7 deletions python/binharness/bootstrap/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,20 @@ def bootstrap_env_from_image(
agent_binary: str,
image: str,
port: int = 60162,
client: docker.DockerClient | None = None,
docker_client: docker.DockerClient | None = None,
agent_log: str | None = None,
) -> DockerAgent:
"""Bootstraps an agent running in a docker container."""
user_client = client is not None
if client is None:
client = docker.from_env()
user_client = docker_client is not None
if docker_client is None:
docker_client = docker.from_env()
try:
# Setup container
client.images.pull(image)
docker_client.images.pull(image)
environment = {}
if agent_log is not None:
environment["RUST_LOG"] = agent_log
container = client.containers.create(
container = docker_client.containers.create(
image,
command=["/agent", "0.0.0.0", str(port)], # noqa: S104
environment=environment,
Expand All @@ -83,4 +83,4 @@ def bootstrap_env_from_image(
return DockerAgent(container.id, port)
finally:
if not user_client:
client.close()
docker_client.close()
12 changes: 10 additions & 2 deletions python/tests/bootstrap/test_docker.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

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

if TYPE_CHECKING:
import docker

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

def test_bootstrap_env_from_image(
docker_client: docker.APIClient, agent_binary_linux_host_arch: str
) -> None:
agent = bootstrap_env_from_image(
agent_binary_linux_host_arch, "ubuntu:22.04", docker_client=docker_client
)
try:
assert isinstance(agent, DockerAgent)
assert agent.get_environment_ids() == [0]
Expand Down
16 changes: 16 additions & 0 deletions python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

import pytest

try:
import docker
except ImportError:
docker = None


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


Expand Down Expand Up @@ -45,3 +51,13 @@ def agent_binary_linux_host_arch() -> str:
if not expected_path.exists():
pytest.skip("agent binary not found")
return str(expected_path)


@pytest.fixture()
def docker_client() -> docker.DockerClient:
if docker is None:
pytest.skip("docker is not installed")
try:
return docker.from_env()
except docker.errors.DockerException:
pytest.skip("docker is not running")

0 comments on commit 80a9fc2

Please sign in to comment.