fix fakeldap so it can use redis keeper

This commit is contained in:
Vishvananda Ishaya
2010-06-24 04:11:59 +01:00
committed by andy
parent 70b9d6b2df
commit d171bed950
3 changed files with 24 additions and 23 deletions

View File

@@ -40,7 +40,7 @@ def initialize(uri):
class FakeLDAP(object): class FakeLDAP(object):
def __init__(self, _uri): def __init__(self, _uri):
self.keeper = datastore.SqliteKeeper('fakeldap') #Redis keeper never works here... self.keeper = datastore.Keeper('fakeldap')
if self.keeper['objects'] is None: if self.keeper['objects'] is None:
self.keeper['objects'] = {} self.keeper['objects'] = {}

View File

@@ -1,12 +1,12 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4 # vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright [2010] [Anso Labs, LLC] # Copyright [2010] [Anso Labs, LLC]
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,7 +15,7 @@
""" """
Base classes for our unit tests. Base classes for our unit tests.
Allows overriding of flags for use of fakes, Allows overriding of flags for use of fakes,
and some black magic for inline callbacks. and some black magic for inline callbacks.
""" """
@@ -47,13 +47,13 @@ def skip_if_fake(f):
raise trial_unittest.SkipTest('Test cannot be run in fake mode') raise trial_unittest.SkipTest('Test cannot be run in fake mode')
else: else:
return f(*args, **kw) return f(*args, **kw)
_skipper.func_name = f.func_name _skipper.func_name = f.func_name
return _skipper return _skipper
class TrialTestCase(trial_unittest.TestCase): class TrialTestCase(trial_unittest.TestCase):
flush_db = True
def setUp(self): def setUp(self):
super(TrialTestCase, self).setUp() super(TrialTestCase, self).setUp()
@@ -62,11 +62,6 @@ class TrialTestCase(trial_unittest.TestCase):
self.mox = mox.Mox() self.mox = mox.Mox()
self.stubs = stubout.StubOutForTesting() self.stubs = stubout.StubOutForTesting()
self.flag_overrides = {} self.flag_overrides = {}
self.flags(redis_db=8)
if self.flush_db:
logging.info("Flushing redis datastore")
r = datastore.Redis.instance()
r.flushdb()
def tearDown(self): def tearDown(self):
super(TrialTestCase, self).tearDown() super(TrialTestCase, self).tearDown()
@@ -78,7 +73,7 @@ class TrialTestCase(trial_unittest.TestCase):
if FLAGS.fake_rabbit: if FLAGS.fake_rabbit:
fakerabbit.reset_all() fakerabbit.reset_all()
# attempt to wipe all keepers # attempt to wipe all keepers
#keeper = datastore.Keeper() #keeper = datastore.Keeper()
#keeper.clear_all() #keeper.clear_all()
@@ -96,7 +91,7 @@ class TrialTestCase(trial_unittest.TestCase):
for k, v in self.flag_overrides.iteritems(): for k, v in self.flag_overrides.iteritems():
setattr(FLAGS, k, v) setattr(FLAGS, k, v)
class BaseTestCase(TrialTestCase): class BaseTestCase(TrialTestCase):
def setUp(self): def setUp(self):
@@ -105,7 +100,7 @@ class BaseTestCase(TrialTestCase):
# the injected listeners... this is fine for now though # the injected listeners... this is fine for now though
self.injected = [] self.injected = []
self.ioloop = ioloop.IOLoop.instance() self.ioloop = ioloop.IOLoop.instance()
self._waiting = None self._waiting = None
self._doneWaiting = False self._doneWaiting = False
self._timedOut = False self._timedOut = False
@@ -124,8 +119,6 @@ class BaseTestCase(TrialTestCase):
if FLAGS.fake_rabbit: if FLAGS.fake_rabbit:
fakerabbit.reset_all() fakerabbit.reset_all()
self.tear_down() self.tear_down()
r = datastore.Redis.instance()
r.flushdb()
def _waitForTest(self, timeout=60): def _waitForTest(self, timeout=60):
""" Push the ioloop along to wait for our test to complete. """ """ Push the ioloop along to wait for our test to complete. """
@@ -152,10 +145,10 @@ class BaseTestCase(TrialTestCase):
pass pass
self._waiting = None self._waiting = None
self._doneWaiting = True self._doneWaiting = True
def _maybeInlineCallbacks(self, f): def _maybeInlineCallbacks(self, f):
""" If we're doing async calls in our tests, wait on them. """ If we're doing async calls in our tests, wait on them.
This is probably the most complicated hunk of code we have so far. This is probably the most complicated hunk of code we have so far.
First up, if the function is normal (not async) we just act normal First up, if the function is normal (not async) we just act normal
@@ -166,7 +159,7 @@ class BaseTestCase(TrialTestCase):
of making epic callback chains. of making epic callback chains.
Example (callback chain, ugly): Example (callback chain, ugly):
d = self.node.terminate_instance(instance_id) # a Deferred instance d = self.node.terminate_instance(instance_id) # a Deferred instance
def _describe(_): def _describe(_):
d_desc = self.node.describe_instances() # another Deferred instance d_desc = self.node.describe_instances() # another Deferred instance
@@ -177,7 +170,7 @@ class BaseTestCase(TrialTestCase):
d.addCallback(_checkDescribe) d.addCallback(_checkDescribe)
d.addCallback(lambda x: self._done()) d.addCallback(lambda x: self._done())
self._waitForTest() self._waitForTest()
Example (inline callbacks! yay!): Example (inline callbacks! yay!):
yield self.node.terminate_instance(instance_id) yield self.node.terminate_instance(instance_id)
@@ -194,11 +187,11 @@ class BaseTestCase(TrialTestCase):
if not hasattr(g, 'send'): if not hasattr(g, 'send'):
self._done() self._done()
return defer.succeed(g) return defer.succeed(g)
inlined = defer.inlineCallbacks(f) inlined = defer.inlineCallbacks(f)
d = inlined() d = inlined()
return d return d
def _catchExceptions(self, result, failure): def _catchExceptions(self, result, failure):
exc = (failure.type, failure.value, failure.getTracebackObject()) exc = (failure.type, failure.value, failure.getTracebackObject())
if isinstance(failure.value, self.failureException): if isinstance(failure.value, self.failureException):

View File

@@ -60,6 +60,8 @@ from nova.tests.validator_unittest import *
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
flags.DEFINE_bool('flush_db', True,
'Flush the database before running fake tests')
if __name__ == '__main__': if __name__ == '__main__':
OptionsClass = twistd.WrapTwistedOptions(trial_script.Options) OptionsClass = twistd.WrapTwistedOptions(trial_script.Options)
@@ -71,6 +73,12 @@ if __name__ == '__main__':
# TODO(termie): these should make a call instead of doing work on import # TODO(termie): these should make a call instead of doing work on import
if FLAGS.fake_tests: if FLAGS.fake_tests:
from nova.tests.fake_flags import * from nova.tests.fake_flags import *
# use db 8 for fake tests
FLAGS.redis_db = 8
if FLAGS.flush_db:
logging.info("Flushing redis datastore")
r = datastore.Redis.instance()
r.flushdb()
else: else:
from nova.tests.real_flags import * from nova.tests.real_flags import *