Skip to content

Commit

Permalink
fixup! Update to Atoti Python API 0.8.9
Browse files Browse the repository at this point in the history
  • Loading branch information
tibdex committed Feb 14, 2024
1 parent faede10 commit 6191879
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 50 deletions.
11 changes: 0 additions & 11 deletions tests/docker/_run_command.py

This file was deleted.

57 changes: 18 additions & 39 deletions tests/docker/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import re
from collections.abc import Generator
from datetime import timedelta
from shutil import which
Expand All @@ -9,18 +8,14 @@
import atoti as tt
import docker
import pytest
from docker.models.containers import Container

from ._docker_container import docker_container as _docker_container
from ._run_command import run_command
from ._timeout import Timeout


@pytest.fixture(name="docker_executable_path", scope="session")
def docker_executable_path_fixture() -> str:
docker_executable_path = which("docker")
assert docker_executable_path
return docker_executable_path
@pytest.fixture(name="docker_client", scope="session")
def docker_client_fixture() -> docker.DockerClient:
return docker.from_env()


@pytest.fixture(name="poetry_executable_path", scope="session")
Expand All @@ -32,32 +27,21 @@ def poetry_executable_path_fixture() -> str:

@pytest.fixture(name="docker_image_name", scope="session")
def docker_image_name_fixture(
docker_executable_path: str, project_name: str
docker_client: docker.DockerClient, project_name: str
) -> Generator[str, None, None]:
tag = f"{project_name}:{uuid4()}"
build_image_output = run_command(
[docker_executable_path, "build", "--tag", tag, "."]
)
assert f"naming to docker.io/library/{tag}" in build_image_output
docker_client.images.build(path=".", rm=True, tag=tag)
yield tag
remove_image_output = run_command([docker_executable_path, "image", "rm", tag])
assert re.match("(Deleted|Untagged)", remove_image_output)


@pytest.fixture(name="docker_client", scope="session")
def docker_client_fixture() -> docker.DockerClient:
return docker.from_env()
docker_client.images.remove(tag)


@pytest.fixture(
name="docker_container",
# Don't use this fixture in tests mutating the container or its underlying app.
name="query_session_inside_docker_container",
scope="session",
)
def docker_container_fixture(
docker_client: docker.DockerClient,
docker_image_name: str,
) -> Generator[Container, None, None]:
def query_session_inside_docker_container_fixture(
docker_client: docker.DockerClient, docker_image_name: str
) -> Generator[tt.QuerySession, None, None]:
timeout = Timeout(timedelta(minutes=1))

with _docker_container(docker_image_name, client=docker_client) as container:
Expand All @@ -67,17 +51,12 @@ def docker_container_fixture(
if timeout.timed_out:
raise RuntimeError(f"Session start timed out:\n{container.logs()}")

yield container


@pytest.fixture(name="host_port", scope="session")
def host_port_fixture(docker_executable_path: str, docker_container: Container) -> int:
container_port_output = run_command(
[docker_executable_path, "container", "port", docker_container.name]
)
return int(container_port_output.rsplit(":", maxsplit=1)[-1].strip())

container.reload() # Refresh `attrs` to get its `HostPort`.
host_port = int(
next(iter(container.attrs["NetworkSettings"]["Ports"].values()))[0][
"HostPort"
]
)
query_session = tt.QuerySession(f"http://localhost:{host_port}")

@pytest.fixture(name="query_session_inside_docker_container", scope="session")
def query_session_inside_docker_container_fixture(host_port: int) -> tt.QuerySession:
return tt.QuerySession(f"http://localhost:{host_port}")
yield query_session

0 comments on commit 6191879

Please sign in to comment.