~launchpad-pqm/launchpad/devel

12412.3.1 by Robert Collins
Import rabbitmq support code from u1 (approved) after converting the core interface to fixtures for use inside test processes.
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Tests for the Rabbit fixture."""
5
6
__metaclass__ = type
7
13119.1.1 by Gavin Panella
Rip out RabbitFixture; it didn't work as intended. Replace with some functions copied from an unlanded testtools branch (with instructions to remove when/if that branch does land and is released).
8
import itertools
12412.3.1 by Robert Collins
Import rabbitmq support code from u1 (approved) after converting the core interface to fixtures for use inside test processes.
9
import socket
10
13047.3.11 by Gavin Panella
Use amqp.Connection in the test, and capture details from all fixtures if there's a failure.
11
from amqplib import client_0_8 as amqp
12412.3.3 by Robert Collins
Isolate HOME from the rabbit test instance as well, because of .erlang.cookie.
12
from fixtures import EnvironmentVariableFixture
13119.1.1 by Gavin Panella
Rip out RabbitFixture; it didn't work as intended. Replace with some functions copied from an unlanded testtools branch (with instructions to remove when/if that branch does land and is released).
13
from testtools.content import Content
12412.3.1 by Robert Collins
Import rabbitmq support code from u1 (approved) after converting the core interface to fixtures for use inside test processes.
14
13047.3.2 by Gavin Panella
Rename get_connection to getConnection, and provide convenient ways to get at it.
15
from lp.services.rabbit.testing.server import RabbitServer
12412.3.1 by Robert Collins
Import rabbitmq support code from u1 (approved) after converting the core interface to fixtures for use inside test processes.
16
from lp.testing import TestCase
17
13119.1.1 by Gavin Panella
Rip out RabbitFixture; it didn't work as intended. Replace with some functions copied from an unlanded testtools branch (with instructions to remove when/if that branch does land and is released).
18
#
19
# copy_content() and gather_details() have been copied from
20
# lp:~allenap/testtools/gather-details. If/when that branch lands the copies
21
# here should be removed.
22
#
23
24
25
def copy_content(content_object):
26
    """Make a copy of the given content object.
27
28
    The content within `content_object` is iterated and saved. This is useful
29
    when the source of the content is volatile, a log file in a temporary
30
    directory for example.
31
32
    :param content_object: A `content.Content` instance.
33
    :return: A `content.Content` instance with the same mime-type as
34
        `content_object` and a non-volatile copy of its content.
35
    """
36
    content_bytes = list(content_object.iter_bytes())
37
    content_callback = lambda: content_bytes
38
    return Content(content_object.content_type, content_callback)
39
40
41
def gather_details(source, target):
42
    """Merge the details from `source` into `target`.
43
44
    :param source: A *detailed* object from which details will be gathered.
45
    :param target: A *detailed* object into which details will be gathered.
46
    """
47
    source_details = source.getDetails()
48
    target_details = target.getDetails()
49
    for name, content_object in source_details.items():
50
        new_name = name
51
        disambiguator = itertools.count(1)
52
        while new_name in target_details:
53
            new_name = '%s-%d' % (name, next(disambiguator))
54
        name = new_name
55
        target.addDetail(name, copy_content(content_object))
56
12412.3.1 by Robert Collins
Import rabbitmq support code from u1 (approved) after converting the core interface to fixtures for use inside test processes.
57
58
class TestRabbitFixture(TestCase):
59
60
    def test_start_check_shutdown(self):
13119.1.6 by Gavin Panella
Disable test_start_check_shutdown.
61
        # XXX: GavinPanella 2011-05-26 bug=788557 : Disabled due to spurious
62
        # failures (cannot create cookie file).
63
        self.skip("Disabled (bug 788557)")
64
12412.3.3 by Robert Collins
Isolate HOME from the rabbit test instance as well, because of .erlang.cookie.
65
        # Rabbit needs to fully isolate itself: an existing per user
66
        # .erlange.cookie has to be ignored, and ditto bogus HOME if other
67
        # tests fail to cleanup.
68
        self.useFixture(EnvironmentVariableFixture('HOME', '/nonsense/value'))
13047.3.11 by Gavin Panella
Use amqp.Connection in the test, and capture details from all fixtures if there's a failure.
69
12412.3.1 by Robert Collins
Import rabbitmq support code from u1 (approved) after converting the core interface to fixtures for use inside test processes.
70
        fixture = RabbitServer()
13047.3.11 by Gavin Panella
Use amqp.Connection in the test, and capture details from all fixtures if there's a failure.
71
13119.1.1 by Gavin Panella
Rip out RabbitFixture; it didn't work as intended. Replace with some functions copied from an unlanded testtools branch (with instructions to remove when/if that branch does land and is released).
72
        try:
73
            with fixture:
74
                # We can connect.
75
                connect_arguments = {
76
                    "host": 'localhost:%s' % fixture.config.port,
77
                    "userid": "guest", "password": "guest",
78
                    "virtual_host": "/", "insist": False,
79
                    }
80
                amqp.Connection(**connect_arguments).close()
81
                # And get a log file.
82
                log = fixture.runner.getDetails()["rabbit.log"]
83
                # Which shouldn't blow up on iteration.
84
                list(log.iter_text())
85
        except:
86
            # Work around failures-in-setup-not-attaching-details (if they did
87
            # we could use self.useFixture).
88
            gather_details(fixture.runner.environment, fixture.runner)
89
            gather_details(fixture.runner, fixture)
90
            gather_details(fixture.config, fixture)
91
            gather_details(fixture, self)
92
            raise
13047.3.11 by Gavin Panella
Use amqp.Connection in the test, and capture details from all fixtures if there's a failure.
93
12412.3.1 by Robert Collins
Import rabbitmq support code from u1 (approved) after converting the core interface to fixtures for use inside test processes.
94
        # The daemon should be closed now.
13047.3.11 by Gavin Panella
Use amqp.Connection in the test, and capture details from all fixtures if there's a failure.
95
        self.assertRaises(socket.error, amqp.Connection, **connect_arguments)