From 62bd5f26a5fd0b6aa9f93dfebb3224157a63897d Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: 2015年11月10日 11:48:35 -0800 Subject: [PATCH] Use ObjectVersionChecker fixture from oslo.versionedobjects The fixture in the o.vo library allows us to remove duplicate test code in Nova, so let's use that. Change-Id: I896e7ee53856955e0338e7c83cd112d43b27367d --- nova/tests/unit/objects/test_objects.py | 74 ++----------------------- tox.ini | 3 + 2 files changed, 7 insertions(+), 70 deletions(-) diff --git a/nova/tests/unit/objects/test_objects.py b/nova/tests/unit/objects/test_objects.py index 83d70e9ac5c3..ca743ef22475 100644 --- a/nova/tests/unit/objects/test_objects.py +++ b/nova/tests/unit/objects/test_objects.py @@ -12,11 +12,9 @@ # License for the specific language governing permissions and limitations # under the License. -from collections import OrderedDict import contextlib import copy import datetime -import hashlib import inspect import os import pprint @@ -1284,46 +1282,6 @@ class TestObjectVersions(test.NoDBTestCase): [x.encode('utf-8') for x in field._type._valid_values]) - def _get_fingerprint(self, obj_class): - fields = list(obj_class.fields.items()) - # NOTE(danms): We store valid_values in the enum as strings, - # but oslo is working to make these coerced to unicode (which - # is the right thing to do). The functionality will be - # unchanged, but the repr() result that we use for calculating - # the hashes will be different. This helper method coerces all - # Enum valid_values elements to UTF-8 string before we make the - # repr() call so that it is consistent before and after the - # unicode change, and on py2 and py3. - if six.PY2: - self._un_unicodify_enum_valid_values(fields) - - fields.sort() - methods = [] - for name in dir(obj_class): - thing = getattr(obj_class, name) - if self._is_method(thing) or isinstance(thing, classmethod): - method = self._find_remotable_method(obj_class, thing) - if method: - methods.append((name, inspect.getargspec(method))) - methods.sort() - # NOTE(danms): Things that need a version bump are any fields - # and their types, or the signatures of any remotable methods. - # Of course, these are just the mechanical changes we can detect, - # but many other things may require a version bump (method behavior - # and return value changes, for example). - if hasattr(obj_class, 'child_versions'): - relevant_data = (fields, methods, - OrderedDict( - sorted(obj_class.child_versions.items()))) - else: - relevant_data = (fields, methods) - relevant_data = repr(relevant_data) - if six.PY3: - relevant_data = relevant_data.encode('utf-8') - fingerprint = '%s-%s' % ( - obj_class.VERSION, hashlib.md5(relevant_data).hexdigest()) - return fingerprint - def test_find_remotable_method(self): class MyObject(object): @base.remotable @@ -1334,18 +1292,9 @@ class TestObjectVersions(test.NoDBTestCase): self.assertIsNotNone(thing) def test_versions(self): - fingerprints = {} - obj_classes = base.NovaObjectRegistry.obj_classes() - for obj_name in sorted(obj_classes, key=lambda x: x[0]): - index = 0 - for version_cls in obj_classes[obj_name]: - if len(obj_classes[obj_name])> 1 and index != 0: - name = '%s%s' % (obj_name, - version_cls.VERSION.split('.')[0]) - else: - name = obj_name - fingerprints[name] = self._get_fingerprint(version_cls) - index += 1 + checker = fixture.ObjectVersionChecker( + base.NovaObjectRegistry.obj_classes()) + fingerprints = checker.get_hashes() if os.getenv('GENERATE_HASHES'): file('object_hashes.txt', 'w').write( @@ -1353,27 +1302,12 @@ class TestObjectVersions(test.NoDBTestCase): raise test.TestingException( 'Generated hashes in object_hashes.txt') - stored = set(object_data.items()) - computed = set(fingerprints.items()) - changed = stored.symmetric_difference(computed) - expected = {} - actual = {} - for name, hash in changed: - expected[name] = object_data.get(name) - actual[name] = fingerprints.get(name) - + expected, actual = checker.test_hashes(object_data) self.assertEqual(expected, actual, 'Some objects have changed; please make sure the ' 'versions have been bumped, and then update their ' 'hashes here.') - def _get_object_field_name(self, field): - if isinstance(field._type, fields.Object): - return field._type._obj_name - if isinstance(field, fields.ListOfObjectsField): - return field._type._element_type._type._obj_name - return None - def test_obj_make_compatible(self): # Iterate all object classes and verify that we can run # obj_make_compatible with every older version than current. diff --git a/tox.ini b/tox.ini index c1388cd4923d..13190d1ac28e 100644 --- a/tox.ini +++ b/tox.ini @@ -16,7 +16,10 @@ setenv = VIRTUAL_ENV={envdir} OS_TEST_PATH=./nova/tests/unit LANGUAGE=en_US LC_ALL=en_US.utf-8 +# TODO(mriedem): Move oslo.versionedobjects[fixtures] to test-requirements.txt +# after I937823ffeb95725f0b55e298ebee1857d6482883 lands. deps = -r{toxinidir}/test-requirements.txt + oslo.versionedobjects[fixtures] commands = find . -type f -name "*.pyc" -delete bash tools/pretty_tox.sh '{posargs}'

AltStyle によって変換されたページ (->オリジナル) /