Skip to content

Commit f750da8

Browse files
Mehdi BEN ABDALLAHmbenabda
authored andcommitted
linting
1 parent 7e503fd commit f750da8

File tree

7 files changed

+44
-33
lines changed

7 files changed

+44
-33
lines changed

modules/cosmosdb/testcontainers/cosmosdb/_emulator.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
import socket
33
import ssl
44
from collections.abc import Iterable
5+
from distutils.util import strtobool
6+
from urllib.error import HTTPError, URLError
7+
from urllib.request import urlopen
8+
59
from typing_extensions import Self
10+
611
from testcontainers.core.container import DockerContainer
712
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
13+
814
from . 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

1518
EMULATOR_PORT = 8081
1619

20+
1721
class 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
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
2-
from pathlib import Path
3-
from os import path
41
import tarfile
52
import tempfile
63
from contextlib import contextmanager
4+
from os import path
5+
from pathlib import Path
6+
77
from docker.models.containers import Container
88

9+
910
@contextmanager
1011
def file(container: Container, target: str):
11-
target_path = Path(target)
12-
assert target_path.is_absolute(), "target must be an absolute path"
13-
14-
with tempfile.TemporaryDirectory() as tmp:
15-
archive = Path(tmp) / 'grabbed.tar'
12+
target_path = Path(target)
13+
assert target_path.is_absolute(), "target must be an absolute path"
1614

17-
# download from container as tar archive
18-
with open(archive, 'wb') as f:
19-
tar_bits, _ = container.get_archive(target)
20-
for chunk in tar_bits:
21-
f.write(chunk)
15+
with tempfile.TemporaryDirectory() as tmp:
16+
archive = Path(tmp) / "grabbed.tar"
2217

23-
# extract target file from tar archive
24-
with tarfile.TarFile(archive) as tar:
25-
yield tar.extractfile(path.basename(target))
18+
# download from container as tar archive
19+
with open(archive, "wb") as f:
20+
tar_bits, _ = container.get_archive(target)
21+
for chunk in tar_bits:
22+
f.write(chunk)
2623

24+
# extract target file from tar archive
25+
with tarfile.TarFile(archive) as tar:
26+
yield tar.extractfile(path.basename(target))

modules/cosmosdb/testcontainers/cosmosdb/mongodb.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os
2+
23
from ._emulator import CosmosDBEmulatorContainer
34

45
__all__ = ["CosmosDBMongoEndpointContainer"]
56

67
ENDPOINT_PORT = 10255
78

9+
810
class CosmosDBMongoEndpointContainer(CosmosDBEmulatorContainer):
911
"""
1012
CosmosDB MongoDB enpoint Emulator.
@@ -21,7 +23,7 @@ class CosmosDBMongoEndpointContainer(CosmosDBEmulatorContainer):
2123

2224
def __init__(
2325
self,
24-
mongodb_version: str = None,
26+
mongodb_version: str,
2527
image: str = os.getenv(
2628
"AZURE_COSMOS_EMULATOR_IMAGE", "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:mongodb"
2729
),
@@ -37,7 +39,7 @@ def port(self) -> str:
3739
The exposed port to the MongoDB endpoint
3840
"""
3941
return self.get_exposed_port(ENDPOINT_PORT)
40-
42+
4143
def _configure(self) -> None:
4244
super()._configure()
4345
self.with_env("AZURE_COSMOS_EMULATOR_ENABLE_MONGODB_ENDPOINT", self.mongodb_version)

modules/cosmosdb/testcontainers/cosmosdb/nosql.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
from azure.core.exceptions import ServiceRequestError
22
from azure.cosmos import CosmosClient as SyncCosmosClient
33
from azure.cosmos.aio import CosmosClient as AsyncCosmosClient
4+
45
from testcontainers.core.waiting_utils import wait_container_is_ready
56

67
from ._emulator import CosmosDBEmulatorContainer
8+
79
__all__ = ["CosmosDBNoSQLEndpointContainer"]
810

911
NOSQL_PORT = 8081
1012

13+
1114
class CosmosDBNoSQLEndpointContainer(CosmosDBEmulatorContainer):
1215
"""
1316
CosmosDB NoSQL enpoint Emulator.

modules/cosmosdb/tests/test_emulator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from testcontainers.cosmosdb._emulator import CosmosDBEmulatorContainer
33

4+
45
def test_runs():
56
with CosmosDBEmulatorContainer(partition_count=1, bind_ports=False) as emulator:
67
assert emulator.server_certificate_pem is not None

modules/cosmosdb/tests/test_mongodb.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import pytest
22
from testcontainers.cosmosdb import CosmosDBMongoEndpointContainer
33

4+
45
def test_requires_a_version():
56
with pytest.raises(AssertionError, match="A MongoDB version is required"):
6-
CosmosDBMongoEndpointContainer()
7+
CosmosDBMongoEndpointContainer(mongodb_version=None)
78

89
# instanciates
910
CosmosDBMongoEndpointContainer(mongodb_version="4.0")
1011

12+
1113
def test_runs():
1214
with CosmosDBMongoEndpointContainer(mongodb_version="4.0", partition_count=1, bind_ports=False) as emulator:
1315
assert emulator.env["AZURE_COSMOS_EMULATOR_ENABLE_MONGODB_ENDPOINT"] == "4.0"

modules/cosmosdb/tests/test_nosql.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from testcontainers.cosmosdb import CosmosDBNoSQLEndpointContainer
33

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

0 commit comments

Comments
 (0)