1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for the Rabbit fixture."""
10
from textwrap import dedent
12
from amqplib import client_0_8 as amqp
13
from fixtures import EnvironmentVariableFixture
14
from testtools.content import Content
16
from lp.services.rabbit.testing.server import RabbitServer
17
from lp.testing import TestCase
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.
26
def copy_content(content_object):
27
"""Make a copy of the given content object.
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.
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.
37
content_bytes = list(content_object.iter_bytes())
38
content_callback = lambda: content_bytes
39
return Content(content_object.content_type, content_callback)
42
def gather_details(source, target):
43
"""Merge the details from `source` into `target`.
45
:param source: A *detailed* object from which details will be gathered.
46
:param target: A *detailed* object into which details will be gathered.
48
source_details = source.getDetails()
49
target_details = target.getDetails()
50
for name, content_object in source_details.items():
52
disambiguator = itertools.count(1)
53
while new_name in target_details:
54
new_name = '%s-%d' % (name, next(disambiguator))
56
target.addDetail(name, copy_content(content_object))
59
class TestRabbitFixture(TestCase):
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'))
67
fixture = RabbitServer()
73
"host": 'localhost:%s' % fixture.config.port,
74
"userid": "guest", "password": "guest",
75
"virtual_host": "/", "insist": False,
77
amqp.Connection(**connect_arguments).close()
79
log = fixture.runner.getDetails()["server.log"]
80
# Which shouldn't blow up on iteration.
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("""\
91
""" % fixture.config.port)
92
self.assertEqual(expected, fixture.config.service_config)
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)
103
# The daemon should be closed now.
104
self.assertRaises(socket.error, amqp.Connection, **connect_arguments)