Skip to content
Closed
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
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
'mongo': ['pymongo'],
'redis': ['redis'],
'mssqlserver': ['pyodbc'],
'neo4j': ['neo4j']
'neo4j': ['neo4j'],
'rabbitmq': ['pika']
},
long_description_content_type="text/x-rst",
long_description=long_description,
Expand Down
76 changes: 76 additions & 0 deletions testcontainers/rabbitmq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from .core.container import DockerContainer
from .core.waiting_utils import wait_container_is_ready
import os
import pika


class RabbitmqContainer(DockerContainer):
RABBITMQ_DEFAULT_USER = os.environ.get("RABBITMQ_DEFAULT_USER", "guest")
RABBITMQ_DEFAULT_PASS = os.environ.get("RABBITMQ_DEFAULT_PASS", "guest")
SERVICE_STARTED = r"Server startup complete;"

def __init__(self, image="rabbitmq:latest"):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rabbitmq:latest does not have rabbitmqadmin, so all methods below which are using it do not work

super(RabbitmqContainer, self).__init__(image=image)
self.port_to_expose = 5672
self.with_exposed_ports(self.port_to_expose)
self.queues = list()

def _configure(self):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is not called at all, nowhere

self.with_env('RABBITMQ_DEFAULT_USER', 'guest')
self.with_env('RABBITMQ_DEFAULT_PASS', 'guest')

@wait_container_is_ready()
def get_connection_parameters(self):
credentials = pika.PlainCredentials(self.RABBITMQ_DEFAULT_USER, self.RABBITMQ_DEFAULT_PASS)
parameters = pika.ConnectionParameters(self.get_container_host_ip(), self.port_to_expose,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.port_to_expose -> self.get_exposed_port(self.port_to_expose)

'/', credentials)
return parameters

@wait_container_is_ready()
def get_connection(self):
return self.get_container_host_ip(), self.port_to_expose

@wait_container_is_ready()
def declare_queue(self, queue):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a duplicate (in terms of signature) of the function in line 48. Should this be removed?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to the logic - this method is supposed to declare multiple queues

a = list()
for q in queue:
a.append(self.declare_queue(q))
return a

@wait_container_is_ready()
def declare_queue(self, queue):
cmd = "rabbitmqadmin declare queue name={}".format(queue)
return self.exec(cmd)

@wait_container_is_ready()
def declare_binding(self, src, dest):
cmd = "rabbitmqadmin declare binding source={} destination= {}".format(src, dest)
return self.exec(cmd)

@wait_container_is_ready()
def declare_exchange(self, name, type):
cmd = "rabbitmqadmin declare exchange name={} type={}".format(name, type)
return self.exec(cmd)

@wait_container_is_ready()
def declare_vhost(self, vhost):
cmd = "rabbitmqadmin declare exchange vhost={} type={}".format(vhost)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type is missing

return self.exec(cmd)

@wait_container_is_ready()
def declare_parameter(self, component, name, value):
cmd = "rabbitmqadmin declare parameter component={} name={} value={}".format(component, name, value)
return self.exec(cmd)

30 changes: 30 additions & 0 deletions tests/test_rabbitmq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import time

from testcontainers.core.waiting_utils import wait_for_logs
from testcontainers.rabbitmq import RabbitmqContainer


def test_declare_queue():
rabbitmq_container = RabbitmqContainer("rabbitmq:lastest")
queue_name = "test"
with rabbitmq_container as rabbit:
wait_for_logs(rabbit, RabbitmqContainer.SERVICE_STARTED, timeout=30, interval=1)
time.sleep(30)
assert rabbit.declare_queue(queue_name)[1] == b'queue declared\n'


def test_connection():
rabbitmq_container = RabbitmqContainer("rabbitmq:lastest")
with rabbitmq_container as rabbit:
wait_for_logs(rabbit, RabbitmqContainer.SERVICE_STARTED, timeout=30, interval=1)
_, exposed_port = rabbit.get_connection()
assert exposed_port == 5672


def test_declare_vhost():
rabbitmq_container = RabbitmqContainer("rabbitmq:lastest")
vhost_name = "test"
with rabbitmq_container as rabbit:
wait_for_logs(rabbit, RabbitmqContainer.SERVICE_STARTED, timeout=30, interval=1)
time.sleep(30)
Comment thread
dabrign marked this conversation as resolved.
Resolved
assert rabbit.declare_vhost(vhost_name)[1] == b'queue declared\n'