Skip to content

Commit cf6c585

Browse files
committed
Rename port_to_expose to port and use f-strings in tests.
1 parent 14761b1 commit cf6c585

File tree

12 files changed

+69
-52
lines changed

12 files changed

+69
-52
lines changed

arangodb/testcontainers/arangodb/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ class ArangoDbContainer(DbContainer):
3636

3737
def __init__(self,
3838
image: str = "arangodb:latest",
39-
port_to_expose: int = 8529,
39+
port: int = 8529,
4040
arango_root_password: str = "passwd",
4141
arango_no_auth: typing.Optional[bool] = None,
4242
arango_random_root_password: typing.Optional[bool] = None,
43+
port_to_expose: None = None,
4344
**kwargs) -> None:
4445
"""
4546
Args:
4647
image: Actual docker image/tag to pull.
47-
port_to_expose: Port the container needs to expose.
48+
port: Port the container needs to expose.
4849
arango_root_password: Start ArangoDB with the given password for root. Defaults to the
4950
environment variable `ARANGO_ROOT_PASSWORD` if `None`.
5051
arango_no_auth: Disable authentication completely. Defaults to the environment variable
@@ -53,9 +54,11 @@ def __init__(self,
5354
the environment variable `ARANGO_NO_AUTH` if `None` or `False` if the environment
5455
variable is not available.
5556
"""
57+
if port_to_expose:
58+
raise ValueError("use `port` instead of `port_to_expose`")
5659
super().__init__(image=image, **kwargs)
57-
self.port_to_expose = port_to_expose
58-
self.with_exposed_ports(self.port_to_expose)
60+
self.port = port
61+
self.with_exposed_ports(self.port)
5962

6063
# See https://www.arangodb.com/docs/stable/deployment-single-instance-manual-start.html for
6164
# details. We convert to int then to bool because Arango uses the string literal "1" to
@@ -77,7 +80,7 @@ def _configure(self) -> None:
7780
self.with_env("ARANGO_RANDOM_ROOT_PASSWORD", "1")
7881

7982
def get_connection_url(self) -> str:
80-
port = self.get_exposed_port(self.port_to_expose)
83+
port = self.get_exposed_port(self.port)
8184
return f"http://{self.get_container_host_ip()}:{port}"
8285

8386
def _connect(self) -> None:

kafka/testcontainers/kafka/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ class KafkaContainer(DockerContainer):
2525
"""
2626
TC_START_SCRIPT = '/tc-start.sh'
2727

28-
def __init__(self, image: str = "confluentinc/cp-kafka:5.4.3", port_to_expose: int = 9093,
29-
**kwargs) -> None:
28+
def __init__(self, image: str = "confluentinc/cp-kafka:5.4.3", port: int = 9093,
29+
port_to_expose: None = None, **kwargs) -> None:
30+
if port_to_expose:
31+
raise ValueError("use `port` instead of `port_to_expose`")
3032
super(KafkaContainer, self).__init__(image, **kwargs)
31-
self.port_to_expose = port_to_expose
32-
self.with_exposed_ports(self.port_to_expose)
33-
listeners = f'PLAINTEXT://0.0.0.0:{port_to_expose},BROKER://0.0.0.0:9092'
33+
self.port = port
34+
self.with_exposed_ports(self.port)
35+
listeners = f'PLAINTEXT://0.0.0.0:{self.port},BROKER://0.0.0.0:9092'
3436
self.with_env('KAFKA_LISTENERS', listeners)
3537
self.with_env('KAFKA_LISTENER_SECURITY_PROTOCOL_MAP',
3638
'BROKER:PLAINTEXT,PLAINTEXT:PLAINTEXT')
@@ -44,7 +46,7 @@ def __init__(self, image: str = "confluentinc/cp-kafka:5.4.3", port_to_expose: i
4446

4547
def get_bootstrap_server(self) -> str:
4648
host = self.get_container_host_ip()
47-
port = self.get_exposed_port(self.port_to_expose)
49+
port = self.get_exposed_port(self.port)
4850
return f'{host}:{port}'
4951

5052
@wait_container_is_ready(UnrecognizedBrokerVersion, NoBrokersAvailable, KafkaError, ValueError)
@@ -56,7 +58,7 @@ def _connect(self) -> None:
5658

5759
def tc_start(self) -> None:
5860
host = self.get_container_host_ip()
59-
port = self.get_exposed_port(self.port_to_expose)
61+
port = self.get_exposed_port(self.port)
6062
listeners = f'PLAINTEXT://{host}:{port},BROKER://$(hostname -i):9092'
6163
data = (
6264
dedent(

kafka/tests/test_kafka.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def test_kafka_producer_consumer():
88

99

1010
def test_kafka_producer_consumer_custom_port():
11-
with KafkaContainer(port_to_expose=9888) as container:
12-
assert container.port_to_expose == 9888
11+
with KafkaContainer(port=9888) as container:
12+
assert container.port == 9888
1313
produce_and_consume_kafka_message(container)
1414

1515

keycloak/tests/test_keycloak.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from testcontainers.keycloak import KeycloakContainer
44

55

6-
@pytest.mark.parametrize(["version"], [("16.1.1", )])
7-
def test_docker_run_keycloak(version):
8-
with KeycloakContainer('jboss/keycloak:{}'.format(version)) as kc:
6+
@pytest.mark.parametrize("version", ["16.1.1"])
7+
def test_docker_run_keycloak(version: str):
8+
with KeycloakContainer(f'jboss/keycloak:{version}') as kc:
99
kc.get_client().users_count()

localstack/tests/test_localstack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def test_docker_run_localstack():
88
with LocalStackContainer() as localstack:
9-
resp = urllib.request.urlopen('{}/health'.format(localstack.get_url()))
9+
resp = urllib.request.urlopen(f'{localstack.get_url()}/health')
1010
services = json.loads(resp.read().decode())['services']
1111

1212
# Check that all services are running

minio/testcontainers/minio/__init__.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,26 @@ class MinioContainer(DockerContainer):
3535
"""
3636

3737
def __init__(self, image: str = "minio/minio:RELEASE.2022-12-02T19-19-22Z",
38-
port_to_expose: int = 9000, access_key: str = "minioadmin",
39-
secret_key: str = "minioadmin", **kwargs) -> None:
38+
port: int = 9000, access_key: str = "minioadmin",
39+
secret_key: str = "minioadmin", port_to_expose: None = None, **kwargs) -> None:
4040
"""
4141
Args:
4242
image: Docker image to use for the MinIO container.
43-
port_to_expose: Port to expose on the container.
43+
port: Port to expose on the container.
4444
access_key: Access key for client connections.
4545
secret_key: Secret key for client connections.
4646
"""
47+
if port_to_expose:
48+
raise ValueError("use `port` instead of `port_to_expose`")
4749
super(MinioContainer, self).__init__(image, **kwargs)
48-
self.port_to_expose = port_to_expose
50+
self.port = port
4951
self.access_key = access_key
5052
self.secret_key = secret_key
5153

52-
self.with_exposed_ports(self.port_to_expose)
54+
self.with_exposed_ports(self.port)
5355
self.with_env("MINIO_ACCESS_KEY", self.access_key)
5456
self.with_env("MINIO_SECRET_KEY", self.secret_key)
55-
self.with_command(f"server /data --address :{self.port_to_expose}")
57+
self.with_command(f"server /data --address :{self.port}")
5658

5759
def get_client(self, **kwargs) -> Minio:
5860
"""Returns a Minio client to connect to the container.
@@ -62,7 +64,7 @@ def get_client(self, **kwargs) -> Minio:
6264
https://min.io/docs/minio/linux/developers/python/API.html
6365
"""
6466
host_ip = self.get_container_host_ip()
65-
exposed_port = self.get_exposed_port(self.port_to_expose)
67+
exposed_port = self.get_exposed_port(self.port)
6668
return Minio(
6769
f"{host_ip}:{exposed_port}",
6870
access_key=self.access_key,
@@ -79,7 +81,7 @@ def get_config(self) -> dict:
7981
dict: {`endpoint`: str, `access_key`: str, `secret_key`: str}
8082
"""
8183
host_ip = self.get_container_host_ip()
82-
exposed_port = self.get_exposed_port(self.port_to_expose)
84+
exposed_port = self.get_exposed_port(self.port)
8385
return {
8486
"endpoint": f"{host_ip}:{exposed_port}",
8587
"access_key": self.access_key,

mongodb/testcontainers/mongodb/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ class MongoDbContainer(DbContainer):
4747
... # Find the restaurant document
4848
... cursor = db.restaurants.find({"borough": "Manhattan"})
4949
"""
50-
def __init__(self, image: str = "mongo:latest", port_to_expose: int = 27017,
50+
def __init__(self, image: str = "mongo:latest", port: int = 27017,
5151
username: Optional[str] = None, password: Optional[str] = None,
52-
dbname: Optional[str] = None, **kwargs) -> None:
52+
dbname: Optional[str] = None, port_to_expose: None = None, **kwargs) -> None:
53+
if port_to_expose:
54+
raise ValueError("use `port` instead of `port_to_expose`")
5355
super(MongoDbContainer, self).__init__(image=image, **kwargs)
5456
self.username = username or os.environ.get("MONGO_INITDB_ROOT_USERNAME", "test")
5557
self.password = password or os.environ.get("MONGO_INITDB_ROOT_PASSWORD", "test")
5658
self.dbname = dbname or os.environ.get("MONGO_DB", "test")
57-
self.command = "mongo"
58-
self.port_to_expose = port_to_expose
59-
self.with_exposed_ports(self.port_to_expose)
59+
self.port = port
60+
self.with_exposed_ports(self.port)
61+
self.with_command("mongo")
6062

6163
def _configure(self) -> None:
6264
self.with_env("MONGO_INITDB_ROOT_USERNAME", self.username)
@@ -68,7 +70,7 @@ def get_connection_url(self) -> str:
6870
dialect='mongodb',
6971
username=self.username,
7072
password=self.password,
71-
port=self.port_to_expose,
73+
port=self.port,
7274
)
7375

7476
@wait_container_is_ready()

mongodb/tests/test_mongodb.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
def test_docker_generic_db():
1010
with DockerContainer("mongo:latest").with_bind_ports(27017, 27017) as mongo_container:
1111
def connect():
12-
return MongoClient("mongodb://{}:{}".format(mongo_container.get_container_host_ip(),
13-
mongo_container.get_exposed_port(27017)))
12+
host = mongo_container.get_container_host_ip()
13+
port = mongo_container.get_exposed_port(27017)
14+
return MongoClient(f"mongodb://{host}:{port}")
1415

1516
db = wait_for(connect).primer
1617
result = db.restaurants.insert_one(
@@ -55,8 +56,8 @@ def test_docker_run_mongodb():
5556

5657
def test_docker_run_mongodb_connect_without_credentials():
5758
with MongoDbContainer() as mongo:
58-
connection_url = "mongodb://{}:{}".format(mongo.get_container_host_ip(),
59-
mongo.get_exposed_port(mongo.port_to_expose))
59+
connection_url = f"mongodb://{mongo.get_container_host_ip()}:" \
60+
f"{mongo.get_exposed_port(mongo.port)}"
6061
db = MongoClient(connection_url).test
6162
with pytest.raises(OperationFailure):
6263
db.restaurants.insert_one({})

neo4j/testcontainers/neo4j/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,23 @@ class Neo4jContainer(DbContainer):
3737
... result = session.run("MATCH (n) RETURN n LIMIT 1")
3838
... record = result.single()
3939
"""
40-
def __init__(self, image: str = "neo4j:latest", *, bolt_port: int = 7687,
41-
password: Optional[str] = None, username: Optional[str] = None, **kwargs) -> None:
40+
def __init__(self, image: str = "neo4j:latest", *, port: int = 7687,
41+
password: Optional[str] = None, username: Optional[str] = None,
42+
bolt_port: None = None, **kwargs) -> None:
43+
if bolt_port:
44+
raise ValueError("use `port` instead of `bolt_port`")
4245
super(Neo4jContainer, self).__init__(image, **kwargs)
4346
self.username = username or os.environ.get("NEO4J_USER", "neo4j")
4447
self.password = password or os.environ.get("NEO4J_PASSWORD", "password")
45-
self.bolt_port = bolt_port
46-
self.with_exposed_ports(self.bolt_port)
48+
self.port = port
49+
self.with_exposed_ports(self.port)
4750
self._driver = None
4851

4952
def _configure(self) -> None:
5053
self.with_env("NEO4J_AUTH", f"neo4j/{self.password}")
5154

5255
def get_connection_url(self) -> str:
53-
return f"bolt://{self.get_container_host_ip()}:{self.get_exposed_port(self.bolt_port)}"
56+
return f"bolt://{self.get_container_host_ip()}:{self.get_exposed_port(self.port)}"
5457

5558
@wait_container_is_ready()
5659
def _connect(self) -> None:

nginx/testcontainers/nginx/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515

1616
class NginxContainer(DockerContainer):
17-
def __init__(self, image: str = "nginx:latest", port_to_expose: int = 80, **kwargs) -> None:
17+
def __init__(self, image: str = "nginx:latest", port: int = 80, port_to_expose: None = None,
18+
**kwargs) -> None:
19+
if port_to_expose:
20+
raise ValueError("use `port` instead of `port_to_expose`")
1821
super(NginxContainer, self).__init__(image, **kwargs)
19-
self.port_to_expose = port_to_expose
20-
self.with_exposed_ports(self.port_to_expose)
22+
self.port = port
23+
self.with_exposed_ports(self.port)

0 commit comments

Comments
 (0)