Add some typing to tests

This commit is contained in:
Ayaz Salikhov
2022-01-18 19:13:17 +03:00
parent ee77b7831e
commit 2a1316c9ac
24 changed files with 211 additions and 141 deletions

View File

@@ -2,6 +2,7 @@
# Distributed under the terms of the Modified BSD License.
import os
import logging
import typing
import docker
import pytest
@@ -15,7 +16,7 @@ LOGGER = logging.getLogger(__name__)
@pytest.fixture(scope="session")
def http_client():
def http_client() -> requests.Session:
"""Requests session with retries and backoff."""
s = requests.Session()
retries = Retry(total=5, backoff_factor=1)
@@ -25,13 +26,13 @@ def http_client():
@pytest.fixture(scope="session")
def docker_client():
def docker_client() -> docker.DockerClient:
"""Docker client configured based on the host environment"""
return docker.from_env()
@pytest.fixture(scope="session")
def image_name():
def image_name() -> str:
"""Image name to test"""
return os.getenv("TEST_IMAGE")
@@ -50,13 +51,15 @@ class TrackedContainer:
Default keyword arguments to pass to docker.DockerClient.containers.run
"""
def __init__(self, docker_client, image_name, **kwargs):
def __init__(
self, docker_client: docker.DockerClient, image_name: str, **kwargs: typing.Any
):
self.container = None
self.docker_client = docker_client
self.image_name = image_name
self.kwargs = kwargs
def run(self, **kwargs):
def run(self, **kwargs: typing.Any):
"""Runs a docker container using the preconfigured image name
and a mix of the preconfigured container options and those passed
to this method.
@@ -74,9 +77,7 @@ class TrackedContainer:
-------
docker.Container
"""
all_kwargs = {}
all_kwargs.update(self.kwargs)
all_kwargs.update(kwargs)
all_kwargs = self.kwargs | kwargs
LOGGER.info(f"Running {self.image_name} with args {all_kwargs} ...")
self.container = self.docker_client.containers.run(
self.image_name,
@@ -91,7 +92,7 @@ class TrackedContainer:
@pytest.fixture(scope="function")
def container(docker_client, image_name):
def container(docker_client: docker.DockerClient, image_name: str):
"""Notebook container with initial configuration appropriate for testing
(e.g., HTTP port exposed to the host for HTTP calls).