Merge lp:~allenap/rabbitfixture/settable-resources into lp:rabbitfixture

Proposed by Gavin Panella
Status: Merged
Approved by: Gavin Panella
Approved revision: 21
Merged at revision: 18
Proposed branch: lp:~allenap/rabbitfixture/settable-resources
Merge into: lp:rabbitfixture
Diff against target: 194 lines (+75/-43)
3 files modified
.bzrignore (+2/-0)
rabbitfixture/server.py (+29/-40)
rabbitfixture/tests/test_server.py (+44/-3)
To merge this branch: bzr merge lp:~allenap/rabbitfixture/settable-resources
Reviewer Review Type Date Requested Status
Gavin Panella Approve
Review via email: mp+66964@code.launchpad.net

Commit message

Make RabbitServerResources configurable so that users have more control over the resources that the fixture makes use of.

Description of the change

In Launchpad, so that Rabbit can be brought up as a developer service like the librarian, we need to have some ability to configure what resources the fixture uses, like port.

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '.bzrignore'
2--- .bzrignore 2011-06-28 14:26:33 +0000
3+++ .bzrignore 2011-07-05 20:14:42 +0000
4@@ -1,5 +1,6 @@
5 *.egg-info
6 ./.installed.cfg
7+./TAGS
8 ./bin
9 ./build
10 ./develop-eggs
11@@ -7,3 +8,4 @@
12 ./download-cache/dist
13 ./eggs
14 ./parts
15+./tags
16
17=== modified file 'rabbitfixture/server.py'
18--- rabbitfixture/server.py 2011-07-05 13:45:06 +0000
19+++ rabbitfixture/server.py 2011-07-05 20:14:42 +0000
20@@ -7,6 +7,7 @@
21
22 __all__ = [
23 "RabbitServer",
24+ "RabbitServerResources",
25 ]
26
27 import os
28@@ -34,39 +35,6 @@
29 RABBITBIN = "/usr/lib/rabbitmq/bin"
30
31
32-# def setup_exchange(conf, port):
33-# """ create an exchange """
34-# # Not ported yet.
35-# conn = _get_connection(conf, port)
36-# # see if we already have the exchange
37-# must_create = False
38-# chan = conn.channel()
39-# try:
40-# chan.exchange_declare(exchange=conf.exchange_name + BRANCH_NICK,
41-# type=conf.exchange_type, passive=True)
42-# except (amqp.AMQPConnectionException, amqp.AMQPChannelException), e:
43-# if e.amqp_reply_code == 404:
44-# must_create = True
45-# # amqplib kills the channel on error.... we dispose of it too
46-# chan.close()
47-# chan = conn.channel()
48-# else:
49-# raise
50-# # now create the exchange if needed
51-# if must_create:
52-# chan.exchange_declare(exchange=conf.exchange_name + BRANCH_NICK,
53-# type=conf.exchange_type,
54-# durable=True, auto_delete=False,)
55-# print "Created new exchange %s (%s)" % (
56-# conf.exchange_name + BRANCH_NICK, conf.exchange_type)
57-# else:
58-# print "Exchange %s (%s) is already declared" % (
59-# conf.exchange_name + BRANCH_NICK, conf.exchange_type)
60-# chan.close()
61-# conn.close()
62-# return True
63-
64-
65 def preexec_fn():
66 # Revert Python's handling of SIGPIPE. See
67 # http://bugs.python.org/issue1652 for more info.
68@@ -102,14 +70,30 @@
69 :ivar nodename: The name of the node.
70 """
71
72+ def __init__(self, hostname=None, port=None, homedir=None,
73+ mnesiadir=None, logfile=None, nodename=None):
74+ super(RabbitServerResources, self).__init__()
75+ self.hostname = hostname
76+ self.port = port
77+ self.homedir = homedir
78+ self.mnesiadir = mnesiadir
79+ self.logfile = logfile
80+ self.nodename = nodename
81+
82 def setUp(self):
83 super(RabbitServerResources, self).setUp()
84- self.hostname = 'localhost'
85- self.port = allocate_ports()[0]
86- self.homedir = self.useFixture(TempDir()).path
87- self.mnesiadir = self.useFixture(TempDir()).path
88- self.logfile = os.path.join(self.homedir, 'server.log')
89- self.nodename = os.path.basename(self.useFixture(TempDir()).path)
90+ if self.hostname is None:
91+ self.hostname = 'localhost'
92+ if self.port is None:
93+ [self.port] = allocate_ports(1)
94+ if self.homedir is None:
95+ self.homedir = self.useFixture(TempDir()).path
96+ if self.mnesiadir is None:
97+ self.mnesiadir = self.useFixture(TempDir()).path
98+ if self.logfile is None:
99+ self.logfile = os.path.join(self.homedir, 'server.log')
100+ if self.nodename is None:
101+ self.nodename = os.path.basename(self.useFixture(TempDir()).path)
102
103 @property
104 def fq_nodename(self):
105@@ -354,9 +338,14 @@
106 :ivar runner: The `RabbitServerRunner` that bootstraps the server.
107 """
108
109+ def __init__(self, config=None):
110+ super(RabbitServer, self).__init__()
111+ self.config = config
112+
113 def setUp(self):
114 super(RabbitServer, self).setUp()
115- self.config = RabbitServerResources()
116+ if self.config is None:
117+ self.config = RabbitServerResources()
118 self.useFixture(self.config)
119 self.runner = RabbitServerRunner(self.config)
120 self.useFixture(self.runner)
121
122=== modified file 'rabbitfixture/tests/test_server.py'
123--- rabbitfixture/tests/test_server.py 2011-07-05 13:45:06 +0000
124+++ rabbitfixture/tests/test_server.py 2011-07-05 20:14:42 +0000
125@@ -6,21 +6,27 @@
126 __metaclass__ = type
127
128 import socket
129+from socket import gethostname
130
131 from amqplib import client_0_8 as amqp
132 from fixtures import EnvironmentVariableFixture
133-from rabbitfixture.server import RabbitServer
134+from rabbitfixture.server import (
135+ RabbitServer,
136+ RabbitServerResources,
137+ )
138 from testtools import TestCase
139
140
141 class TestRabbitFixture(TestCase):
142
143- def test_start_check_shutdown(self):
144+ def setUp(self):
145+ super(TestRabbitFixture, self).setUp()
146 # Rabbit needs to fully isolate itself: an existing per user
147- # .erlange.cookie has to be ignored, and ditto bogus HOME if other
148+ # .erlang.cookie has to be ignored, and ditto bogus HOME if other
149 # tests fail to cleanup.
150 self.useFixture(EnvironmentVariableFixture('HOME', '/nonsense/value'))
151
152+ def test_start_check_shutdown(self):
153 fixture = self.useFixture(RabbitServer())
154
155 # We can connect.
156@@ -39,3 +45,38 @@
157
158 # The daemon should be closed now.
159 self.assertRaises(socket.error, amqp.Connection, **connect_arguments)
160+
161+ def test_config(self):
162+ # The configuration can be passed in.
163+ config = RabbitServerResources()
164+ fixture = self.useFixture(RabbitServer(config))
165+ self.assertIs(config, fixture.config)
166+ self.assertIs(config, fixture.runner.config)
167+ self.assertIs(config, fixture.runner.environment.config)
168+
169+
170+class TestRabbitServerResources(TestCase):
171+
172+ def test_defaults(self):
173+ with RabbitServerResources() as resources:
174+ self.assertEqual("localhost", resources.hostname)
175+ self.assertIsInstance(resources.port, int)
176+ self.assertIsInstance(resources.homedir, (str, unicode))
177+ self.assertIsInstance(resources.mnesiadir, (str, unicode))
178+ self.assertIsInstance(resources.logfile, (str, unicode))
179+ self.assertIsInstance(resources.nodename, (str, unicode))
180+
181+ def test_passed_to_init(self):
182+ args = dict(
183+ hostname="hostname", port=1234,
184+ homedir="homedir", mnesiadir="mnesiadir",
185+ logfile="logfile", nodename="nodename")
186+ with RabbitServerResources(**args) as resources:
187+ for key, value in args.iteritems():
188+ self.assertEqual(value, getattr(resources, key))
189+
190+ def test_fq_nodename(self):
191+ with RabbitServerResources(nodename="nibbles") as resources:
192+ self.assertEqual(
193+ "nibbles@%s" % gethostname(),
194+ resources.fq_nodename)

Subscribers

People subscribed via source and target branches

to all changes: