~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/rabbit/tests/test_fixture.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-06-30 16:25:34 UTC
  • mfrom: (13335.1.1 use-rabbitfixture)
  • Revision ID: launchpad@pqm.canonical.com-20110630162534-w8ggb0kn49xccnem
[r=wgrant][no-qa] Replace lp.services.rabbit with rabbitfixture.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
8
 
import itertools
9
 
import socket
10
 
from textwrap import dedent
11
 
 
12
 
from amqplib import client_0_8 as amqp
13
 
from fixtures import EnvironmentVariableFixture
14
 
from testtools.content import Content
15
 
 
16
 
from lp.services.rabbit.testing.server import RabbitServer
17
 
from lp.testing import TestCase
18
 
 
19
 
#
20
 
# copy_content() and gather_details() have been copied from
21
 
# lp:~allenap/testtools/gather-details. If/when that branch lands the copies
22
 
# here should be removed.
23
 
#
24
 
 
25
 
 
26
 
def copy_content(content_object):
27
 
    """Make a copy of the given content object.
28
 
 
29
 
    The content within `content_object` is iterated and saved. This is useful
30
 
    when the source of the content is volatile, a log file in a temporary
31
 
    directory for example.
32
 
 
33
 
    :param content_object: A `content.Content` instance.
34
 
    :return: A `content.Content` instance with the same mime-type as
35
 
        `content_object` and a non-volatile copy of its content.
36
 
    """
37
 
    content_bytes = list(content_object.iter_bytes())
38
 
    content_callback = lambda: content_bytes
39
 
    return Content(content_object.content_type, content_callback)
40
 
 
41
 
 
42
 
def gather_details(source, target):
43
 
    """Merge the details from `source` into `target`.
44
 
 
45
 
    :param source: A *detailed* object from which details will be gathered.
46
 
    :param target: A *detailed* object into which details will be gathered.
47
 
    """
48
 
    source_details = source.getDetails()
49
 
    target_details = target.getDetails()
50
 
    for name, content_object in source_details.items():
51
 
        new_name = name
52
 
        disambiguator = itertools.count(1)
53
 
        while new_name in target_details:
54
 
            new_name = '%s-%d' % (name, next(disambiguator))
55
 
        name = new_name
56
 
        target.addDetail(name, copy_content(content_object))
57
 
 
58
 
 
59
 
class TestRabbitFixture(TestCase):
60
 
 
61
 
    def test_start_check_shutdown(self):
62
 
        # Rabbit needs to fully isolate itself: an existing per user
63
 
        # .erlange.cookie has to be ignored, and ditto bogus HOME if other
64
 
        # tests fail to cleanup.
65
 
        self.useFixture(EnvironmentVariableFixture('HOME', '/nonsense/value'))
66
 
 
67
 
        fixture = RabbitServer()
68
 
 
69
 
        try:
70
 
            with fixture:
71
 
                # We can connect.
72
 
                connect_arguments = {
73
 
                    "host": 'localhost:%s' % fixture.config.port,
74
 
                    "userid": "guest", "password": "guest",
75
 
                    "virtual_host": "/", "insist": False,
76
 
                    }
77
 
                amqp.Connection(**connect_arguments).close()
78
 
                # And get a log file.
79
 
                log = fixture.runner.getDetails()["server.log"]
80
 
                # Which shouldn't blow up on iteration.
81
 
                list(log.iter_text())
82
 
 
83
 
            # There is a (launchpad specific) config fixture. (This could be a
84
 
            # separate class if we make the fixture external in the future).
85
 
            expected = dedent("""\
86
 
                [rabbitmq]
87
 
                host: localhost:%d
88
 
                userid: guest
89
 
                password: guest
90
 
                virtual_host: /
91
 
                """ % fixture.config.port)
92
 
            self.assertEqual(expected, fixture.config.service_config)
93
 
 
94
 
        except:
95
 
            # Work around failures-in-setup-not-attaching-details (if they did
96
 
            # we could use self.useFixture).
97
 
            gather_details(fixture.runner.environment, fixture.runner)
98
 
            gather_details(fixture.runner, fixture)
99
 
            gather_details(fixture.config, fixture)
100
 
            gather_details(fixture, self)
101
 
            raise
102
 
 
103
 
        # The daemon should be closed now.
104
 
        self.assertRaises(socket.error, amqp.Connection, **connect_arguments)