merge with trey
This commit is contained in:
30
nova/test.py
30
nova/test.py
@@ -23,7 +23,6 @@ inline callbacks.
|
||||
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import functools
|
||||
import os
|
||||
import shutil
|
||||
@@ -36,14 +35,14 @@ import shutil
|
||||
import stubout
|
||||
from eventlet import greenthread
|
||||
|
||||
from nova import context
|
||||
from nova import db
|
||||
from nova import fakerabbit
|
||||
from nova import flags
|
||||
from nova import log
|
||||
from nova import rpc
|
||||
from nova import utils
|
||||
from nova import service
|
||||
from nova import wsgi
|
||||
from nova.virt import fake
|
||||
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
@@ -89,7 +88,7 @@ class TestCase(unittest.TestCase):
|
||||
# NOTE(vish): We need a better method for creating fixtures for tests
|
||||
# now that we have some required db setup for the system
|
||||
# to work properly.
|
||||
self.start = datetime.datetime.utcnow()
|
||||
self.start = utils.utcnow()
|
||||
shutil.copyfile(os.path.join(FLAGS.state_path, FLAGS.sqlite_clean_db),
|
||||
os.path.join(FLAGS.state_path, FLAGS.sqlite_db))
|
||||
|
||||
@@ -103,6 +102,7 @@ class TestCase(unittest.TestCase):
|
||||
self._monkey_patch_attach()
|
||||
self._monkey_patch_wsgi()
|
||||
self._original_flags = FLAGS.FlagValuesDict()
|
||||
rpc.ConnectionPool = rpc.Pool(max_size=FLAGS.rpc_conn_pool_size)
|
||||
|
||||
def tearDown(self):
|
||||
"""Runs after each test method to tear down test environment."""
|
||||
@@ -117,6 +117,10 @@ class TestCase(unittest.TestCase):
|
||||
if FLAGS.fake_rabbit:
|
||||
fakerabbit.reset_all()
|
||||
|
||||
if FLAGS.connection_type == 'fake':
|
||||
if hasattr(fake.FakeConnection, '_instance'):
|
||||
del fake.FakeConnection._instance
|
||||
|
||||
# Reset any overriden flags
|
||||
self.reset_flags()
|
||||
|
||||
@@ -199,7 +203,7 @@ class TestCase(unittest.TestCase):
|
||||
wsgi.Server.start = _wrapped_start
|
||||
|
||||
# Useful assertions
|
||||
def assertDictMatch(self, d1, d2):
|
||||
def assertDictMatch(self, d1, d2, approx_equal=False, tolerance=0.001):
|
||||
"""Assert two dicts are equivalent.
|
||||
|
||||
This is a 'deep' match in the sense that it handles nested
|
||||
@@ -230,15 +234,26 @@ class TestCase(unittest.TestCase):
|
||||
for key in d1keys:
|
||||
d1value = d1[key]
|
||||
d2value = d2[key]
|
||||
try:
|
||||
error = abs(float(d1value) - float(d2value))
|
||||
within_tolerance = error <= tolerance
|
||||
except (ValueError, TypeError):
|
||||
# If both values aren't convertable to float, just ignore
|
||||
# ValueError if arg is a str, TypeError if it's something else
|
||||
# (like None)
|
||||
within_tolerance = False
|
||||
|
||||
if hasattr(d1value, 'keys') and hasattr(d2value, 'keys'):
|
||||
self.assertDictMatch(d1value, d2value)
|
||||
elif 'DONTCARE' in (d1value, d2value):
|
||||
continue
|
||||
elif approx_equal and within_tolerance:
|
||||
continue
|
||||
elif d1value != d2value:
|
||||
raise_assertion("d1['%(key)s']=%(d1value)s != "
|
||||
"d2['%(key)s']=%(d2value)s" % locals())
|
||||
|
||||
def assertDictListMatch(self, L1, L2):
|
||||
def assertDictListMatch(self, L1, L2, approx_equal=False, tolerance=0.001):
|
||||
"""Assert a list of dicts are equivalent."""
|
||||
def raise_assertion(msg):
|
||||
L1str = str(L1)
|
||||
@@ -254,4 +269,5 @@ class TestCase(unittest.TestCase):
|
||||
'len(L2)=%(L2count)d' % locals())
|
||||
|
||||
for d1, d2 in zip(L1, L2):
|
||||
self.assertDictMatch(d1, d2)
|
||||
self.assertDictMatch(d1, d2, approx_equal=approx_equal,
|
||||
tolerance=tolerance)
|
||||
|
||||
Reference in New Issue
Block a user