1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Helpers for testing with RabbitMQ."""
6
from __future__ import (
14
"RabbitServerSettings",
16
"uses_rabbit_fixture",
19
from functools import wraps
21
from fixtures import Fixture
22
from maastesting.rabbit import get_rabbit
23
from testtools.monkey import MonkeyPatcher
26
class RabbitServerSettings(Fixture):
28
This patches the active Django settings to point the application at the
29
ephemeral RabbitMQ server specified by the given configuration.
32
def __init__(self, config):
33
super(RabbitServerSettings, self).__init__()
37
super(RabbitServerSettings, self).setUp()
38
from django.conf import settings
39
patcher = MonkeyPatcher()
41
settings, "RABBITMQ_HOST",
42
"%s:%d" % (self.config.hostname, self.config.port))
43
patcher.add_patch(settings, "RABBITMQ_USERID", "guest")
44
patcher.add_patch(settings, "RABBITMQ_PASSWORD", "guest")
45
patcher.add_patch(settings, "RABBITMQ_VIRTUAL_HOST", "/")
46
patcher.add_patch(settings, "RABBITMQ_PUBLISH", True)
47
self.addCleanup(patcher.restore)
51
def use_rabbit_fixture(test):
52
"""Ensure that a :class:`RabbitServer` is started, and Django's setting
53
updated to point to it, and that Django's settings are returned to their
54
original values at the end.
56
config = get_rabbit().config
57
fixture = RabbitServerSettings(config)
58
test.useFixture(fixture)
61
def uses_rabbit_fixture(func):
62
"""Decorate a test function with `use_rabbit_fixture`."""
65
use_rabbit_fixture(self)