2
2
# Distributed under the terms of the Modified BSD License.
3
3
import os
4
4
import logging
5
- import typing
5
+ from typing import Any , Optional
6
6
7
7
import docker
8
8
from docker .models .containers import Container
9
- import pytest
9
+ import pytest # type: ignore
10
10
import requests
11
11
12
12
from requests .packages .urllib3 .util .retry import Retry
@@ -35,7 +35,7 @@ def docker_client() -> docker.DockerClient:
35
35
@pytest .fixture (scope = "session" )
36
36
def image_name () -> str :
37
37
"""Image name to test"""
38
- return os .getenv ( "TEST_IMAGE" )
38
+ return os .environ [ "TEST_IMAGE" ]
39
39
40
40
41
41
class TrackedContainer :
@@ -56,14 +56,14 @@ def __init__(
56
56
self ,
57
57
docker_client : docker .DockerClient ,
58
58
image_name : str ,
59
- ** kwargs : typing . Any ,
59
+ ** kwargs : Any ,
60
60
):
61
- self .container = None
62
- self .docker_client = docker_client
63
- self .image_name = image_name
64
- self .kwargs = kwargs
61
+ self .container : Optional [ Container ] = None
62
+ self .docker_client : docker . DockerClient = docker_client
63
+ self .image_name : str = image_name
64
+ self .kwargs : Any = kwargs
65
65
66
- def run_detached (self , ** kwargs : typing . Any ) -> Container :
66
+ def run_detached (self , ** kwargs : Any ) -> Container :
67
67
"""Runs a docker container using the preconfigured image name
68
68
and a mix of the preconfigured container options and those passed
69
69
to this method.
@@ -94,11 +94,12 @@ def run_and_wait(
94
94
timeout : int ,
95
95
no_warnings : bool = True ,
96
96
no_errors : bool = True ,
97
- ** kwargs : typing . Any ,
97
+ ** kwargs : Any ,
98
98
) -> str :
99
99
running_container = self .run_detached (** kwargs )
100
100
rv = running_container .wait (timeout = timeout )
101
101
logs = running_container .logs ().decode ("utf-8" )
102
+ assert isinstance (logs , str )
102
103
LOGGER .debug (logs )
103
104
if no_warnings :
104
105
assert not self .get_warnings (logs )
@@ -119,14 +120,14 @@ def get_warnings(logs: str) -> list[str]:
119
120
def _lines_starting_with (logs : str , pattern : str ) -> list [str ]:
120
121
return [line for line in logs .splitlines () if line .startswith (pattern )]
121
122
122
- def remove (self ):
123
+ def remove (self ) -> None :
123
124
"""Kills and removes the tracked docker container."""
124
125
if self .container :
125
126
self .container .remove (force = True )
126
127
127
128
128
129
@pytest .fixture (scope = "function" )
129
- def container (docker_client : docker .DockerClient , image_name : str ):
130
+ def container (docker_client : docker .DockerClient , image_name : str ) -> Container :
130
131
"""Notebook container with initial configuration appropriate for testing
131
132
(e.g., HTTP port exposed to the host for HTTP calls).
132
133
0 commit comments