1
1
import time
2
2
from io import BytesIO
3
3
from tarfile import TarFile , TarInfo
4
- from typing import Optional
4
+ from typing import TYPE_CHECKING , Optional
5
5
6
6
import bcrypt
7
- from requests import Response , get
7
+ from requests import get
8
8
from requests .auth import HTTPBasicAuth
9
9
from requests .exceptions import ConnectionError , ReadTimeout
10
+
10
11
from testcontainers .core .container import DockerContainer
11
12
from testcontainers .core .waiting_utils import wait_container_is_ready
12
13
14
+ if TYPE_CHECKING :
15
+ from requests import Response
16
+
17
+
13
18
class DockerRegistryContainer (DockerContainer ):
14
19
# https://docs.docker.com/registry/
15
20
credentials_path : str = "/htpasswd/credentials.txt"
@@ -18,8 +23,8 @@ def __init__(
18
23
self ,
19
24
image : str = "registry:2" ,
20
25
port : int = 5000 ,
21
- username : str = None ,
22
- password : str = None ,
26
+ username : Optional [ str ] = None ,
27
+ password : Optional [ str ] = None ,
23
28
** kwargs ,
24
29
) -> None :
25
30
super ().__init__ (image = image , ** kwargs )
@@ -34,11 +39,9 @@ def _copy_credentials(self) -> None:
34
39
self .password .encode ("utf-8" ),
35
40
bcrypt .gensalt (rounds = 12 , prefix = b"2a" ),
36
41
).decode ("utf-8" )
37
- content = f"{ self .username } :{ hashed_password } " .encode ("utf-8" )
42
+ content : bytes = f"{ self .username } :{ hashed_password } " .encode ("utf-8" ) # noqa: UP012
38
43
39
- with BytesIO () as tar_archive_object , TarFile (
40
- fileobj = tar_archive_object , mode = "w"
41
- ) as tmp_tarfile :
44
+ with BytesIO () as tar_archive_object , TarFile (fileobj = tar_archive_object , mode = "w" ) as tmp_tarfile :
42
45
tarinfo : TarInfo = TarInfo (name = self .credentials_path )
43
46
tarinfo .size = len (content )
44
47
tarinfo .mtime = time .time ()
@@ -65,7 +68,7 @@ def start(self):
65
68
else :
66
69
super ().start ()
67
70
68
- self ._readiness_probe ()
71
+ self ._readiness_probe ()
69
72
return self
70
73
71
74
def get_registry (self ) -> str :
0 commit comments