3
3
import ssl
4
4
from collections .abc import Iterable
5
5
from enum import Enum , auto
6
- from typing import Callable
6
+ from typing import Callable , Optional
7
7
from urllib .error import HTTPError , URLError
8
8
from urllib .request import urlopen
9
9
@@ -26,8 +26,6 @@ class Endpoints(Enum):
26
26
Cassandra = auto ()
27
27
28
28
29
- ALL_ENDPOINTS = set (Endpoints )
30
-
31
29
# Ports mostly derived from https://docs.microsoft.com/en-us/azure/cosmos-db/emulator-command-line-parameters
32
30
EMULATOR_PORT = 8081
33
31
endpoint_ports = {
@@ -61,8 +59,8 @@ class CosmosDBEmulatorContainer(DockerContainer):
61
59
62
60
.. doctest::
63
61
>>> 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))) }")
66
64
"""
67
65
68
66
def __init__ (
@@ -80,6 +78,7 @@ def __init__(
80
78
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" ,
81
79
),
82
80
endpoints : Iterable [Endpoints ] = [], # the emulator image does not support host-container port mapping
81
+ mongodb_version : Optional [str ] = None ,
83
82
** docker_client_kw ,
84
83
):
85
84
super ().__init__ (image = image , ** docker_client_kw )
@@ -88,6 +87,10 @@ def __init__(
88
87
self .enable_data_persistence = enable_data_persistence
89
88
self .endpoints = frozenset (endpoints )
90
89
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
91
94
92
95
def start (self ) -> Self :
93
96
self ._configure ()
@@ -141,6 +144,9 @@ def _configure(self) -> None:
141
144
.with_env ("AZURE_COSMOS_EMULATOR_KEY" , str (self .key ))
142
145
)
143
146
147
+ if Endpoints .MongoDB in self .endpoints :
148
+ self .with_env ("AZURE_COSMOS_EMULATOR_ENABLE_MONGODB_ENDPOINT" , self .mongodb_version )
149
+
144
150
def _wait_until_ready (self ) -> Self :
145
151
"""
146
152
Waits until the CosmosDB Emulator image is ready to be used.
0 commit comments