From d171bed950dfdd7b762b702235eb3edeca6ce12d Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 24 Jun 2010 04:11:59 +0100 Subject: [PATCH] fix fakeldap so it can use redis keeper --- nova/auth/fakeldap.py | 2 +- nova/test.py | 37 +++++++++++++++---------------------- run_tests.py | 8 ++++++++ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/nova/auth/fakeldap.py b/nova/auth/fakeldap.py index 33003f057..feb2ac14a 100644 --- a/nova/auth/fakeldap.py +++ b/nova/auth/fakeldap.py @@ -40,7 +40,7 @@ def initialize(uri): class FakeLDAP(object): 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: self.keeper['objects'] = {} diff --git a/nova/test.py b/nova/test.py index 05cb5b504..4b9a9f8a9 100644 --- a/nova/test.py +++ b/nova/test.py @@ -1,12 +1,12 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright [2010] [Anso Labs, LLC] -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,7 +15,7 @@ """ 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. """ @@ -47,13 +47,13 @@ def skip_if_fake(f): raise trial_unittest.SkipTest('Test cannot be run in fake mode') else: return f(*args, **kw) - + _skipper.func_name = f.func_name return _skipper class TrialTestCase(trial_unittest.TestCase): - flush_db = True + def setUp(self): super(TrialTestCase, self).setUp() @@ -62,11 +62,6 @@ class TrialTestCase(trial_unittest.TestCase): self.mox = mox.Mox() self.stubs = stubout.StubOutForTesting() 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): super(TrialTestCase, self).tearDown() @@ -78,7 +73,7 @@ class TrialTestCase(trial_unittest.TestCase): if FLAGS.fake_rabbit: fakerabbit.reset_all() - + # attempt to wipe all keepers #keeper = datastore.Keeper() #keeper.clear_all() @@ -96,7 +91,7 @@ class TrialTestCase(trial_unittest.TestCase): for k, v in self.flag_overrides.iteritems(): setattr(FLAGS, k, v) - + class BaseTestCase(TrialTestCase): def setUp(self): @@ -105,7 +100,7 @@ class BaseTestCase(TrialTestCase): # the injected listeners... this is fine for now though self.injected = [] self.ioloop = ioloop.IOLoop.instance() - + self._waiting = None self._doneWaiting = False self._timedOut = False @@ -124,8 +119,6 @@ class BaseTestCase(TrialTestCase): if FLAGS.fake_rabbit: fakerabbit.reset_all() self.tear_down() - r = datastore.Redis.instance() - r.flushdb() def _waitForTest(self, timeout=60): """ Push the ioloop along to wait for our test to complete. """ @@ -152,10 +145,10 @@ class BaseTestCase(TrialTestCase): pass self._waiting = None self._doneWaiting = True - + def _maybeInlineCallbacks(self, f): """ 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. 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. Example (callback chain, ugly): - + d = self.node.terminate_instance(instance_id) # a Deferred instance def _describe(_): d_desc = self.node.describe_instances() # another Deferred instance @@ -177,7 +170,7 @@ class BaseTestCase(TrialTestCase): d.addCallback(_checkDescribe) d.addCallback(lambda x: self._done()) self._waitForTest() - + Example (inline callbacks! yay!): yield self.node.terminate_instance(instance_id) @@ -194,11 +187,11 @@ class BaseTestCase(TrialTestCase): if not hasattr(g, 'send'): self._done() return defer.succeed(g) - + inlined = defer.inlineCallbacks(f) d = inlined() return d - + def _catchExceptions(self, result, failure): exc = (failure.type, failure.value, failure.getTracebackObject()) if isinstance(failure.value, self.failureException): diff --git a/run_tests.py b/run_tests.py index 957e5d208..d03dbb987 100644 --- a/run_tests.py +++ b/run_tests.py @@ -60,6 +60,8 @@ from nova.tests.validator_unittest import * FLAGS = flags.FLAGS +flags.DEFINE_bool('flush_db', True, + 'Flush the database before running fake tests') if __name__ == '__main__': 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 if FLAGS.fake_tests: 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: from nova.tests.real_flags import *