22import socket
33import ssl
44from collections .abc import Iterable
5+ from distutils .util import strtobool
6+ from urllib .error import HTTPError , URLError
7+ from urllib .request import urlopen
8+
59from typing_extensions import Self
10+
611from testcontainers .core .container import DockerContainer
712from testcontainers .core .waiting_utils import wait_container_is_ready , wait_for_logs
13+
814from . import _grab as grab
9- from distutils .util import strtobool
10- from urllib .error import HTTPError , URLError
11- from urllib .request import urlopen
1215
1316__all__ = ["CosmosDBEmulatorContainer" ]
1417
1518EMULATOR_PORT = 8081
1619
20+
1721class CosmosDBEmulatorContainer (DockerContainer ):
1822 """
1923 Abstract class for CosmosDB Emulator endpoints.
@@ -28,9 +32,7 @@ def __init__(
2832 "AZURE_COSMOS_EMULATOR_IMAGE" , "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest"
2933 ),
3034 partition_count : int = os .getenv ("AZURE_COSMOS_EMULATOR_PARTITION_COUNT" , None ),
31- enable_data_persistence : bool = strtobool (
32- os .getenv ("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE" , "false" )
33- ),
35+ enable_data_persistence : bool = strtobool (os .getenv ("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE" , "false" )),
3436 key : str = os .getenv (
3537 "AZURE_COSMOS_EMULATOR_KEY" ,
3638 "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" ,
@@ -52,7 +54,7 @@ def host(self) -> str:
5254 Emulator host
5355 """
5456 return self .get_container_host_ip ()
55-
57+
5658 @property
5759 def server_certificate_pem (self ) -> bytes :
5860 """
@@ -66,18 +68,17 @@ def start(self) -> Self:
6668 self ._wait_until_ready ()
6769 self ._cert_pem_bytes = self ._download_cert ()
6870 return self
69-
71+
7072 def _configure (self ) -> None :
71- all_ports = set ([ EMULATOR_PORT ] + self .endpoint_ports )
73+ all_ports = { EMULATOR_PORT , * self .endpoint_ports }
7274 if self .bind_ports :
7375 for port in all_ports :
7476 self .with_bind_ports (port , port )
7577 else :
7678 self .with_exposed_ports (* all_ports )
7779
7880 (
79- self
80- .with_env ("AZURE_COSMOS_EMULATOR_PARTITION_COUNT" , str (self .partition_count ))
81+ self .with_env ("AZURE_COSMOS_EMULATOR_PARTITION_COUNT" , str (self .partition_count ))
8182 .with_env ("AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE" , socket .gethostbyname (socket .gethostname ()))
8283 .with_env ("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE" , str (self .enable_data_persistence ))
8384 .with_env ("AZURE_COSMOS_EMULATOR_KEY" , str (self .key ))
@@ -89,12 +90,13 @@ def _wait_until_ready(self) -> Self:
8990 if self .bind_ports :
9091 self ._wait_for_url (f"https://{ self .host } :{ EMULATOR_PORT } /_explorer/index.html" )
9192 self ._wait_for_query_success ()
92-
93+
9394 return self
9495
9596 def _download_cert (self ) -> bytes :
9697 with grab .file (
97- self .get_wrapped_container (), "/tmp/cosmos/appdata/.system/profiles/Client/AppData/Local/CosmosDBEmulator/emulator.pem"
98+ self .get_wrapped_container (),
99+ "/tmp/cosmos/appdata/.system/profiles/Client/AppData/Local/CosmosDBEmulator/emulator.pem" ,
98100 ) as cert :
99101 return cert .read ()
100102
@@ -103,6 +105,6 @@ def _wait_for_url(self, url: str) -> Self:
103105 with urlopen (url , context = ssl ._create_unverified_context ()) as response :
104106 response .read ()
105107 return self
106-
108+
107109 def _wait_for_query_success (self ) -> None :
108110 pass
0 commit comments