Skip to content

Commit fa2081a

Browse files
authored
fix(rabbitmq): add vhost as parameter to RabbitMqContainer (#656)
Adds a `vhost` parameter to the RabbitMQContainer constructor that allows the `RABBITMQ_DEFAULT_VHOST` [environment variable](https://www.rabbitmq.com/docs/configure#supported-environment-variables) to be modified. Subsequently `vhost` is then also used inside the `get_connection_params` method for the `pika` connection parameters, which is used to test if the container is ready.
1 parent 9161cb6 commit fa2081a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

modules/rabbitmq/testcontainers/rabbitmq/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(
3131
port: Optional[int] = None,
3232
username: Optional[str] = None,
3333
password: Optional[str] = None,
34+
vhost: Optional[str] = None,
3435
**kwargs,
3536
) -> None:
3637
"""Initialize the RabbitMQ test container.
@@ -45,11 +46,13 @@ def __init__(
4546
self.port = port or int(os.environ.get("RABBITMQ_NODE_PORT", 5672))
4647
self.username = username or os.environ.get("RABBITMQ_DEFAULT_USER", "guest")
4748
self.password = password or os.environ.get("RABBITMQ_DEFAULT_PASS", "guest")
49+
self.vhost = vhost or os.environ.get("RABBITMQ_DEFAULT_VHOST", "/")
4850

4951
self.with_exposed_ports(self.port)
5052
self.with_env("RABBITMQ_NODE_PORT", self.port)
5153
self.with_env("RABBITMQ_DEFAULT_USER", self.username)
5254
self.with_env("RABBITMQ_DEFAULT_PASS", self.password)
55+
self.with_env("RABBITMQ_DEFAULT_VHOST", self.vhost)
5356

5457
@wait_container_is_ready(pika.exceptions.IncompatibleProtocolError, pika.exceptions.AMQPConnectionError)
5558
def readiness_probe(self) -> bool:
@@ -71,6 +74,7 @@ def get_connection_params(self) -> pika.ConnectionParameters:
7174
return pika.ConnectionParameters(
7275
host=self.get_container_host_ip(),
7376
port=self.get_exposed_port(self.port),
77+
virtual_host=self.vhost,
7478
credentials=credentials,
7579
)
7680

modules/rabbitmq/tests/test_rabbitmq.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313

1414

1515
@pytest.mark.parametrize(
16-
argnames=["port", "username", "password"],
16+
argnames=["port", "username", "password", "vhost"],
1717
argvalues=[
18-
[None, None, None], # use the defaults
19-
[5673, None, None], # test with custom port
20-
[None, "my_test_user", "my_secret_password"], # test with custom credentials
18+
[None, None, None, None], # use the defaults
19+
[5673, None, None, None], # test with custom port
20+
[None, "my_test_user", "my_secret_password", None], # test with custom credentials
21+
[None, None, None, "vhost"], # test with custom vhost
2122
],
2223
)
23-
def test_docker_run_rabbitmq(port: Optional[int], username: Optional[str], password: Optional[str]):
24+
def test_docker_run_rabbitmq(
25+
port: Optional[int], username: Optional[str], password: Optional[str], vhost: Optional[str]
26+
):
2427
"""Run rabbitmq test container and use it to deliver a simple message."""
2528
kwargs = {}
2629
if port is not None:
@@ -29,6 +32,8 @@ def test_docker_run_rabbitmq(port: Optional[int], username: Optional[str], passw
2932
kwargs["username"] = username
3033
if password is not None:
3134
kwargs["password"] = password
35+
if vhost is not None:
36+
kwargs["vhost"] = vhost
3237

3338
rabbitmq_container = RabbitMqContainer("rabbitmq:latest", **kwargs)
3439
with rabbitmq_container as rabbitmq:

0 commit comments

Comments
 (0)