Merge lp:~soren/nova/redisectomy into lp:~hudson-openstack/nova/trunk

Proposed by Soren Hansen
Status: Merged
Approved by: Jay Pipes
Approved revision: 313
Merged at revision: 355
Proposed branch: lp:~soren/nova/redisectomy
Merge into: lp:~hudson-openstack/nova/trunk
Diff against target: 211 lines (+46/-69)
5 files modified
nova/auth/fakeldap.py (+30/-6)
nova/auth/manager.py (+1/-1)
nova/datastore.py (+0/-53)
nova/tests/auth_unittest.py (+15/-2)
run_tests.py (+0/-7)
To merge this branch: bzr merge lp:~soren/nova/redisectomy
Reviewer Review Type Date Requested Status
Jay Pipes (community) Approve
Devin Carlen (community) Approve
Review via email: mp+38455@code.launchpad.net

Description of the change

Make Redis completely optional:

 * Move Redis code into fakeldap, since it's the only thing that still uses it.
 * Adjust auth unittests to skip fakeldap tests if Redis isn't around.
 * Adjust auth unittests to actually run the fakeldap tests if Redis /is/ around.

To post a comment you must log in.
Revision history for this message
Devin Carlen (devcamcar) wrote :

lgtm

review: Approve
Revision history for this message
Jay Pipes (jaypipes) wrote :

yup, rock n roll.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'nova/auth/fakeldap.py'
2--- nova/auth/fakeldap.py 2010-09-10 11:52:48 +0000
3+++ nova/auth/fakeldap.py 2010-10-14 20:21:03 +0000
4@@ -24,8 +24,30 @@
5 """
6
7 import json
8-
9-from nova import datastore
10+import redis
11+
12+from nova import flags
13+
14+FLAGS = flags.FLAGS
15+flags.DEFINE_string('redis_host', '127.0.0.1',
16+ 'Host that redis is running on.')
17+flags.DEFINE_integer('redis_port', 6379,
18+ 'Port that redis is running on.')
19+flags.DEFINE_integer('redis_db', 0, 'Multiple DB keeps tests away')
20+
21+class Redis(object):
22+ def __init__(self):
23+ if hasattr(self.__class__, '_instance'):
24+ raise Exception('Attempted to instantiate singleton')
25+
26+ @classmethod
27+ def instance(cls):
28+ if not hasattr(cls, '_instance'):
29+ inst = redis.Redis(host=FLAGS.redis_host,
30+ port=FLAGS.redis_port,
31+ db=FLAGS.redis_db)
32+ cls._instance = inst
33+ return cls._instance
34
35
36 SCOPE_BASE = 0
37@@ -164,11 +186,11 @@
38 key = "%s%s" % (self.__redis_prefix, dn)
39
40 value_dict = dict([(k, _to_json(v)) for k, v in attr])
41- datastore.Redis.instance().hmset(key, value_dict)
42+ Redis.instance().hmset(key, value_dict)
43
44 def delete_s(self, dn):
45 """Remove the ldap object at specified dn."""
46- datastore.Redis.instance().delete("%s%s" % (self.__redis_prefix, dn))
47+ Redis.instance().delete("%s%s" % (self.__redis_prefix, dn))
48
49 def modify_s(self, dn, attrs):
50 """Modify the object at dn using the attribute list.
51@@ -179,7 +201,7 @@
52 ([MOD_ADD | MOD_DELETE | MOD_REPACE], attribute, value)
53
54 """
55- redis = datastore.Redis.instance()
56+ redis = Redis.instance()
57 key = "%s%s" % (self.__redis_prefix, dn)
58
59 for cmd, k, v in attrs:
60@@ -204,7 +226,7 @@
61 """
62 if scope != SCOPE_BASE and scope != SCOPE_SUBTREE:
63 raise NotImplementedError(str(scope))
64- redis = datastore.Redis.instance()
65+ redis = Redis.instance()
66 if scope == SCOPE_BASE:
67 keys = ["%s%s" % (self.__redis_prefix, dn)]
68 else:
69@@ -232,3 +254,5 @@
70 def __redis_prefix(self): # pylint: disable-msg=R0201
71 """Get the prefix to use for all redis keys."""
72 return 'ldap:'
73+
74+
75
76=== modified file 'nova/auth/manager.py'
77--- nova/auth/manager.py 2010-10-13 02:00:34 +0000
78+++ nova/auth/manager.py 2010-10-14 20:21:03 +0000
79@@ -201,7 +201,7 @@
80
81 def __new__(cls, *args, **kwargs):
82 """Returns the AuthManager singleton"""
83- if not cls._instance:
84+ if not cls._instance or ('new' in kwargs and kwargs['new']):
85 cls._instance = super(AuthManager, cls).__new__(cls)
86 return cls._instance
87
88
89=== removed file 'nova/datastore.py'
90--- nova/datastore.py 2010-08-15 01:31:23 +0000
91+++ nova/datastore.py 1970-01-01 00:00:00 +0000
92@@ -1,53 +0,0 @@
93-# vim: tabstop=4 shiftwidth=4 softtabstop=4
94-
95-# Copyright 2010 United States Government as represented by the
96-# Administrator of the National Aeronautics and Space Administration.
97-# All Rights Reserved.
98-#
99-# Licensed under the Apache License, Version 2.0 (the "License"); you may
100-# not use this file except in compliance with the License. You may obtain
101-# a copy of the License at
102-#
103-# http://www.apache.org/licenses/LICENSE-2.0
104-#
105-# Unless required by applicable law or agreed to in writing, software
106-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
107-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
108-# License for the specific language governing permissions and limitations
109-# under the License.
110-
111-"""
112-Datastore:
113-
114-MAKE Sure that ReDIS is running, and your flags are set properly,
115-before trying to run this.
116-"""
117-
118-import logging
119-import redis
120-
121-from nova import flags
122-
123-FLAGS = flags.FLAGS
124-flags.DEFINE_string('redis_host', '127.0.0.1',
125- 'Host that redis is running on.')
126-flags.DEFINE_integer('redis_port', 6379,
127- 'Port that redis is running on.')
128-flags.DEFINE_integer('redis_db', 0, 'Multiple DB keeps tests away')
129-
130-
131-class Redis(object):
132- def __init__(self):
133- if hasattr(self.__class__, '_instance'):
134- raise Exception('Attempted to instantiate singleton')
135-
136- @classmethod
137- def instance(cls):
138- if not hasattr(cls, '_instance'):
139- inst = redis.Redis(host=FLAGS.redis_host,
140- port=FLAGS.redis_port,
141- db=FLAGS.redis_db)
142- cls._instance = inst
143- return cls._instance
144-
145-
146
147=== modified file 'nova/tests/auth_unittest.py'
148--- nova/tests/auth_unittest.py 2010-09-30 22:42:09 +0000
149+++ nova/tests/auth_unittest.py 2010-10-14 20:21:03 +0000
150@@ -80,7 +80,7 @@
151 FLAGS.auth_driver = self.auth_driver
152 super(AuthManagerTestCase, self).setUp()
153 self.flags(connection_type='fake')
154- self.manager = manager.AuthManager()
155+ self.manager = manager.AuthManager(new=True)
156
157 def test_create_and_find_user(self):
158 with user_generator(self.manager):
159@@ -117,7 +117,7 @@
160 self.assert_(filter(lambda u: u.id == 'test1', users))
161 self.assert_(filter(lambda u: u.id == 'test2', users))
162 self.assert_(not filter(lambda u: u.id == 'test3', users))
163-
164+
165 def test_can_add_and_remove_user_role(self):
166 with user_generator(self.manager):
167 self.assertFalse(self.manager.has_role('test1', 'itsec'))
168@@ -324,6 +324,19 @@
169 class AuthManagerLdapTestCase(AuthManagerTestCase, test.TrialTestCase):
170 auth_driver = 'nova.auth.ldapdriver.FakeLdapDriver'
171
172+ def __init__(self, *args, **kwargs):
173+ AuthManagerTestCase.__init__(self)
174+ test.TrialTestCase.__init__(self, *args, **kwargs)
175+ import nova.auth.fakeldap as fakeldap
176+ FLAGS.redis_db = 8
177+ if FLAGS.flush_db:
178+ logging.info("Flushing redis datastore")
179+ try:
180+ r = fakeldap.Redis.instance()
181+ r.flushdb()
182+ except:
183+ self.skip = True
184+
185 class AuthManagerDbTestCase(AuthManagerTestCase, test.TrialTestCase):
186 auth_driver = 'nova.auth.dbdriver.DbDriver'
187
188
189=== modified file 'run_tests.py'
190--- run_tests.py 2010-10-12 20:04:46 +0000
191+++ run_tests.py 2010-10-14 20:21:03 +0000
192@@ -45,7 +45,6 @@
193
194 from twisted.scripts import trial as trial_script
195
196-from nova import datastore
197 from nova import flags
198 from nova import twistd
199
200@@ -86,12 +85,6 @@
201 # TODO(termie): these should make a call instead of doing work on import
202 if FLAGS.fake_tests:
203 from nova.tests.fake_flags import *
204- # use db 8 for fake tests
205- FLAGS.redis_db = 8
206- if FLAGS.flush_db:
207- logging.info("Flushing redis datastore")
208- r = datastore.Redis.instance()
209- r.flushdb()
210 else:
211 from nova.tests.real_flags import *
212