Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdi BEN ABDALLAH committed May 28, 2024
1 parent 57496c8 commit a72564a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
33 changes: 11 additions & 22 deletions modules/cosmosdb/testcontainers/cosmosdb/_emulator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import socket
import ssl
from collections.abc import Iterable
from distutils.util import strtobool
from urllib.error import HTTPError, URLError
from urllib.request import urlopen
Expand Down Expand Up @@ -37,16 +36,12 @@ def __init__(
"AZURE_COSMOS_EMULATOR_KEY",
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
),
bind_ports: bool = strtobool(os.getenv("AZURE_COSMOS_EMULATOR_BIND_PORTS", "true")),
endpoint_ports: Iterable[int] = [],
**other_kwargs,
):
super().__init__(image=image, **other_kwargs)
self.endpoint_ports = endpoint_ports
self.partition_count = partition_count
self.key = key
self.enable_data_persistence = enable_data_persistence
self.bind_ports = bind_ports

@property
def host(self) -> str:
Expand All @@ -65,17 +60,14 @@ def server_certificate_pem(self) -> bytes:
def start(self) -> Self:
self._configure()
super().start()
self._wait_until_ready()
self._wait_until_started()
self._cert_pem_bytes = self._download_cert()
self._wait_for_endpoint_ready()

return self

def _configure(self) -> None:
all_ports = {EMULATOR_PORT, *self.endpoint_ports}
if self.bind_ports:
for port in all_ports:
self.with_bind_ports(port, port)
else:
self.with_exposed_ports(*all_ports)
self.with_exposed_ports(EMULATOR_PORT)

(
self.with_env("AZURE_COSMOS_EMULATOR_PARTITION_COUNT", str(self.partition_count))
Expand All @@ -84,27 +76,24 @@ def _configure(self) -> None:
.with_env("AZURE_COSMOS_EMULATOR_KEY", str(self.key))
)

def _wait_until_ready(self) -> Self:
wait_for_logs(container=self, predicate="Started\\s*$")

if self.bind_ports:
self._wait_for_url(f"https://{self.host}:{EMULATOR_PORT}/_explorer/index.html")
self._wait_for_query_success()

return self

def _download_cert(self) -> bytes:
with grab.file(
self.get_wrapped_container(),
"/tmp/cosmos/appdata/.system/profiles/Client/AppData/Local/CosmosDBEmulator/emulator.pem",
) as cert:
return cert.read()

def _wait_until_started(self) -> Self:
wait_for_logs(container=self, predicate="Started\\s*$")
self._wait_for_url(f"https://{self.host}:{self.get_exposed_port(EMULATOR_PORT)}/_explorer/index.html")

return self

@wait_container_is_ready(HTTPError, URLError)
def _wait_for_url(self, url: str) -> Self:
with urlopen(url, context=ssl._create_unverified_context()) as response:
response.read()
return self

def _wait_for_query_success(self) -> None:
def _wait_for_endpoint_ready(self) -> None:
pass
10 changes: 6 additions & 4 deletions modules/cosmosdb/testcontainers/cosmosdb/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class CosmosDBMongoEndpointContainer(CosmosDBEmulatorContainer):
Example:
.. code-block:: python
.. doctest::
>>> from testcontainers.cosmosdb import CosmosDBMongoEndpointContainer
>>> with CosmosDBMongoEndpointContainer(mongodb_version="4.0") as emulator:
... print(f"Point your MongoDB client at {emulator.host}:{emulator.port} using key {emulator.key}")
... print(f"and eiher disable TLS server auth or trust the server's self signed cert (emulator.server_certificate_pem)")
... # Point your MongoDB client at {emulator.host}:{emulator.port} using key {emulator.key}
... # and eiher disable TLS server auth or trust the server's self signed cert (emulator.server_certificate_pem)
... pass
"""

Expand All @@ -31,7 +32,7 @@ def __init__(
),
**other_kwargs,
):
super().__init__(image=image, endpoint_ports=[ENDPOINT_PORT], **other_kwargs)
super().__init__(image=image, **other_kwargs)
assert mongodb_version is not None, "A MongoDB version is required to use the MongoDB Endpoint"
self.mongodb_version = mongodb_version

Expand All @@ -44,4 +45,5 @@ def port(self) -> str:

def _configure(self) -> None:
super()._configure()
self.with_exposed_ports(ENDPOINT_PORT)
self.with_env("AZURE_COSMOS_EMULATOR_ENABLE_MONGODB_ENDPOINT", self.mongodb_version)
12 changes: 8 additions & 4 deletions modules/cosmosdb/testcontainers/cosmosdb/nosql.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class CosmosDBNoSQLEndpointContainer(CosmosDBEmulatorContainer):
Example:
.. code-block:: python
.. doctest::
>>> from testcontainers.cosmosdb import CosmosDBNoSQLEndpointContainer
>>> with CosmosDBNoSQLEndpointContainer() as emulator:
... db = emulator.insecure_sync_client().create_database_if_not_exists("test")
.. code-block:: python
.. doctest::
>>> from testcontainers.cosmosdb import CosmosDBNoSQLEndpointContainer
>>> from azure.cosmos import CosmosClient
Expand All @@ -35,7 +35,7 @@ class CosmosDBNoSQLEndpointContainer(CosmosDBEmulatorContainer):
"""

def __init__(self, **kwargs):
super().__init__(endpoint_ports=[NOSQL_PORT], **kwargs)
super().__init__(**kwargs)

@property
def port(self) -> str:
Expand Down Expand Up @@ -63,7 +63,11 @@ def insecure_sync_client(self):
"""
return SyncCosmosClient(url=self.url, credential=self.key, connection_verify=False)

def _configure(self) -> None:
super()._configure()
self.with_exposed_ports(NOSQL_PORT)

@wait_container_is_ready(ServiceRequestError)
def _wait_for_query_success(self) -> None:
def _wait_for_endpoint_ready(self) -> None:
with self.insecure_sync_client() as c:
list(c.list_databases())
8 changes: 7 additions & 1 deletion modules/cosmosdb/tests/test_emulator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import pytest
from testcontainers.cosmosdb._emulator import CosmosDBEmulatorContainer
from testcontainers.core.config import testcontainers_config


@pytest.fixture(scope="session", autouse=True)
def config_testcontatiners():
testcontainers_config.max_tries = 300


def test_runs():
with CosmosDBEmulatorContainer(partition_count=1, bind_ports=False) as emulator:
with CosmosDBEmulatorContainer(partition_count=1) as emulator:
assert emulator.server_certificate_pem is not None
assert emulator.get_exposed_port(8081) is not None
8 changes: 7 additions & 1 deletion modules/cosmosdb/tests/test_mongodb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import pytest
from testcontainers.cosmosdb import CosmosDBMongoEndpointContainer
from testcontainers.core.config import testcontainers_config


@pytest.fixture(scope="session", autouse=True)
def config_testcontatiners():
testcontainers_config.max_tries = 300


def test_requires_a_version():
Expand All @@ -11,6 +17,6 @@ def test_requires_a_version():


def test_runs():
with CosmosDBMongoEndpointContainer(mongodb_version="4.0", partition_count=1, bind_ports=False) as emulator:
with CosmosDBMongoEndpointContainer(mongodb_version="4.0", partition_count=1) as emulator:
assert emulator.env["AZURE_COSMOS_EMULATOR_ENABLE_MONGODB_ENDPOINT"] == "4.0"
assert emulator.get_exposed_port(10255) is not None, "The MongoDB endpoint's port should be exposed"
10 changes: 8 additions & 2 deletions modules/cosmosdb/tests/test_nosql.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import pytest
from testcontainers.cosmosdb import CosmosDBNoSQLEndpointContainer
from testcontainers.core.config import testcontainers_config


@pytest.fixture(scope="session", autouse=True)
def config_testcontatiners():
testcontainers_config.max_tries = 300


def test_runs():
with CosmosDBNoSQLEndpointContainer(partition_count=1, bind_ports=False) as emulator:
assert emulator.get_exposed_port(8081) is not None, "The NoSQL endpoint's port should be exposed"
with CosmosDBNoSQLEndpointContainer(partition_count=1) as emulator:
assert emulator.port is not None, "The NoSQL endpoint's port should be exposed"

0 comments on commit a72564a

Please sign in to comment.