~ubuntu-branches/ubuntu/precise/maas/precise-updates

« back to all changes in this revision

Viewing changes to src/maasserver/testing/rabbit.py

Tags: 1.2+bzr1373+dfsg-0ubuntu1~12.04.4
* SECURITY UPDATE: failure to authenticate downloaded content (LP: #1039513)
  - debian/patches/CVE-2013-1058.patch: Authenticate downloaded files with
    GnuPG and MD5SUM files. Thanks to Julian Edwards.
  - CVE-2013-1058
* SECURITY UPDATE: configuration options may be loaded from current working
  directory (LP: #1158425)
  - debian/patches/CVE-2013-1057-1-2.patch: Do not load configuration
    options from the current working directory. Thanks to Julian Edwards.
  - CVE-2013-1057

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Helpers for testing with RabbitMQ."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = [
 
14
    "RabbitServerSettings",
 
15
    "use_rabbit_fixture",
 
16
    "uses_rabbit_fixture",
 
17
    ]
 
18
 
 
19
from functools import wraps
 
20
 
 
21
from fixtures import Fixture
 
22
from maastesting.rabbit import get_rabbit
 
23
from testtools.monkey import MonkeyPatcher
 
24
 
 
25
 
 
26
class RabbitServerSettings(Fixture):
 
27
    """
 
28
    This patches the active Django settings to point the application at the
 
29
    ephemeral RabbitMQ server specified by the given configuration.
 
30
    """
 
31
 
 
32
    def __init__(self, config):
 
33
        super(RabbitServerSettings, self).__init__()
 
34
        self.config = config
 
35
 
 
36
    def setUp(self):
 
37
        super(RabbitServerSettings, self).setUp()
 
38
        from django.conf import settings
 
39
        patcher = MonkeyPatcher()
 
40
        patcher.add_patch(
 
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)
 
48
        patcher.patch()
 
49
 
 
50
 
 
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.
 
55
    """
 
56
    config = get_rabbit().config
 
57
    fixture = RabbitServerSettings(config)
 
58
    test.useFixture(fixture)
 
59
 
 
60
 
 
61
def uses_rabbit_fixture(func):
 
62
    """Decorate a test function with `use_rabbit_fixture`."""
 
63
    @wraps(func)
 
64
    def wrapper(self):
 
65
        use_rabbit_fixture(self)
 
66
        return func(self)
 
67
    return wrapper