Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rabbitmq): add vhost as parameter to RabbitMqContainer #656

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/rabbitmq/testcontainers/rabbitmq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(
port: Optional[int] = None,
username: Optional[str] = None,
password: Optional[str] = None,
vhost: Optional[str] = None,
**kwargs,
) -> None:
"""Initialize the RabbitMQ test container.
Expand All @@ -45,11 +46,13 @@ def __init__(
self.port = port or int(os.environ.get("RABBITMQ_NODE_PORT", 5672))
self.username = username or os.environ.get("RABBITMQ_DEFAULT_USER", "guest")
self.password = password or os.environ.get("RABBITMQ_DEFAULT_PASS", "guest")
self.vhost = vhost or os.environ.get("RABBITMQ_DEFAULT_VHOST", "/")

self.with_exposed_ports(self.port)
self.with_env("RABBITMQ_NODE_PORT", self.port)
self.with_env("RABBITMQ_DEFAULT_USER", self.username)
self.with_env("RABBITMQ_DEFAULT_PASS", self.password)
self.with_env("RABBITMQ_DEFAULT_VHOST", self.vhost)

@wait_container_is_ready(pika.exceptions.IncompatibleProtocolError, pika.exceptions.AMQPConnectionError)
def readiness_probe(self) -> bool:
Expand All @@ -71,6 +74,7 @@ def get_connection_params(self) -> pika.ConnectionParameters:
return pika.ConnectionParameters(
host=self.get_container_host_ip(),
port=self.get_exposed_port(self.port),
virtual_host=self.vhost,
credentials=credentials,
)

Expand Down
15 changes: 10 additions & 5 deletions modules/rabbitmq/tests/test_rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@


@pytest.mark.parametrize(
argnames=["port", "username", "password"],
argnames=["port", "username", "password", "vhost"],
argvalues=[
[None, None, None], # use the defaults
[5673, None, None], # test with custom port
[None, "my_test_user", "my_secret_password"], # test with custom credentials
[None, None, None, None], # use the defaults
[5673, None, None, None], # test with custom port
[None, "my_test_user", "my_secret_password", None], # test with custom credentials
[None, None, None, "vhost"], # test with custom vhost
],
)
def test_docker_run_rabbitmq(port: Optional[int], username: Optional[str], password: Optional[str]):
def test_docker_run_rabbitmq(
port: Optional[int], username: Optional[str], password: Optional[str], vhost: Optional[str]
):
"""Run rabbitmq test container and use it to deliver a simple message."""
kwargs = {}
if port is not None:
Expand All @@ -29,6 +32,8 @@ def test_docker_run_rabbitmq(port: Optional[int], username: Optional[str], passw
kwargs["username"] = username
if password is not None:
kwargs["password"] = password
if vhost is not None:
kwargs["vhost"] = vhost

rabbitmq_container = RabbitMqContainer("rabbitmq:latest", **kwargs)
with rabbitmq_container as rabbitmq:
Expand Down