Skip to content

Commit 58ebe8f

Browse files
author
Mehdi BEN ABDALLAH
committed
actually expose the MongoDB endpoint when requested
1 parent 62eac8c commit 58ebe8f

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

modules/cosmosdb/testcontainers/cosmosdb/__init__.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import ssl
44
from collections.abc import Iterable
55
from enum import Enum, auto
6-
from typing import Callable
6+
from typing import Callable, Optional
77
from urllib.error import HTTPError, URLError
88
from urllib.request import urlopen
99

@@ -26,8 +26,6 @@ class Endpoints(Enum):
2626
Cassandra = auto()
2727

2828

29-
ALL_ENDPOINTS = set(Endpoints)
30-
3129
# Ports mostly derived from https://docs.microsoft.com/en-us/azure/cosmos-db/emulator-command-line-parameters
3230
EMULATOR_PORT = 8081
3331
endpoint_ports = {
@@ -61,8 +59,8 @@ class CosmosDBEmulatorContainer(DockerContainer):
6159
6260
.. doctest::
6361
>>> from testcontainers.cosmosdb import CosmosDBEmulatorContainer, Endpoints
64-
>>> with CosmosDBEmulatorContainer(endpoints=[Endpoints.MongoDB]) as emulator:
65-
... print(f"Point yout MongoDB client to {emulator.host}:{emulator.ports(Endpoints.MongoDB)[0]}")
62+
>>> with CosmosDBEmulatorContainer(endpoints=[Endpoints.MongoDB], mongodb_version="4.0") as emulator:
63+
... print(f"Point yout MongoDB client to {emulator.host}:{next(iter(emulator.ports(Endpoints.MongoDB)))}")
6664
"""
6765

6866
def __init__(
@@ -80,6 +78,7 @@ def __init__(
8078
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
8179
),
8280
endpoints: Iterable[Endpoints] = [], # the emulator image does not support host-container port mapping
81+
mongodb_version: Optional[str] = None,
8382
**docker_client_kw,
8483
):
8584
super().__init__(image=image, **docker_client_kw)
@@ -88,6 +87,10 @@ def __init__(
8887
self.enable_data_persistence = enable_data_persistence
8988
self.endpoints = frozenset(endpoints)
9089
self.bind_ports = bind_ports
90+
assert (Endpoints.MongoDB not in self.endpoints) or (
91+
mongodb_version is not None
92+
), "A MongoDB version is required to use the MongoDB Endpoint"
93+
self.mongodb_version = mongodb_version
9194

9295
def start(self) -> Self:
9396
self._configure()
@@ -141,6 +144,9 @@ def _configure(self) -> None:
141144
.with_env("AZURE_COSMOS_EMULATOR_KEY", str(self.key))
142145
)
143146

147+
if Endpoints.MongoDB in self.endpoints:
148+
self.with_env("AZURE_COSMOS_EMULATOR_ENABLE_MONGODB_ENDPOINT", self.mongodb_version)
149+
144150
def _wait_until_ready(self) -> Self:
145151
"""
146152
Waits until the CosmosDB Emulator image is ready to be used.
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import pytest
2-
from testcontainers.cosmosdb import CosmosDBEmulatorContainer
2+
from testcontainers.cosmosdb import CosmosDBEmulatorContainer, Endpoints
33

44

55
def test_docker_run():
66
with CosmosDBEmulatorContainer(partition_count=1) as cosmosdb:
77
list(cosmosdb.sync_client().list_databases())
8+
9+
10+
def test_enabling_mondogb_endpoint_requires_a_version():
11+
with pytest.raises(AssertionError, match="A MongoDB version is required to use the MongoDB Endpoint"):
12+
CosmosDBEmulatorContainer(endpoints=[Endpoints.MongoDB])
13+
14+
# instanciates
15+
CosmosDBEmulatorContainer(endpoints=[Endpoints.MongoDB], mongodb_version="4.0")
16+
17+
18+
def test_enables_mondogb_endpoint():
19+
with CosmosDBEmulatorContainer(partition_count=1, endpoints=[Endpoints.MongoDB], mongodb_version="4.0") as emulator:
20+
assert emulator.env["AZURE_COSMOS_EMULATOR_ENABLE_MONGODB_ENDPOINT"] == "4.0"
21+
assert emulator.get_exposed_port(10255) is not None, "The MongoDB endpoint's port should be exposed"

0 commit comments

Comments
 (0)