]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Add test for Neutron object versions
authorMartin Hickey <martin.hickey@ie.ibm.com>
Tue, 15 Dec 2015 17:42:39 +0000 (17:42 +0000)
committerMartin Hickey <martin.hickey@ie.ibm.com>
Wed, 6 Jan 2016 10:34:03 +0000 (10:34 +0000)
Adds a test to Neutron to check object versions for any changes
to objects. It prompts the developer to update the version of the
changed object. It uses oslo.versionedobjects.

Change-Id: I99454b28ae0b5fa663354eeccdf709d4030a280b
Co-Authored-By: Ryan Rossiter <rlrossit@us.ibm.com>
neutron/objects/qos/policy.py
neutron/objects/qos/rule.py
neutron/objects/qos/rule_type.py
neutron/tests/unit/api/rpc/handlers/test_resources_rpc.py
neutron/tests/unit/objects/test_base.py
neutron/tests/unit/objects/test_objects.py [new file with mode: 0644]
tools/check_unit_test_structure.sh
tox.ini

index dfdc66848d7ba01d52e2299243917679310d6f00..110a921f6aa152910f0652691952abd57af95e8a 100644 (file)
@@ -27,6 +27,8 @@ from neutron.objects.qos import rule as rule_obj_impl
 
 @obj_base.VersionedObjectRegistry.register
 class QosPolicy(base.NeutronDbObject):
+    # Version 1.0: Initial version
+    VERSION = '1.0'
 
     db_model = qos_db_model.QosPolicy
 
index 2362fb7be99c90fd7e70f9b6d7805cefb9d17342..1cf90eb04edc9e22ab7547df0e5a19016d877dc1 100644 (file)
@@ -77,6 +77,8 @@ class QosRule(base.NeutronDbObject):
 
 @obj_base.VersionedObjectRegistry.register
 class QosBandwidthLimitRule(QosRule):
+    # Version 1.0: Initial version
+    VERSION = '1.0'
 
     db_model = qos_db_model.QosBandwidthLimitRule
 
index fb0754b9394f7e74b02062d1bb1ec60267663022..bb5d9bd740781efcd7db8d070925e394c7fd9ede 100644 (file)
@@ -28,6 +28,8 @@ class RuleTypeField(obj_fields.BaseEnumField):
 
 @obj_base.VersionedObjectRegistry.register
 class QosRuleType(base.NeutronObject):
+    # Version 1.0: Initial version
+    VERSION = '1.0'
 
     fields = {
         'type': RuleTypeField(),
index 179bf5150cffb94ba531eca47ac85e13c4af3d40..bd32fe01bd143b87fa0ddab1292955c1942bf8fe 100755 (executable)
@@ -13,6 +13,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import copy
+
 import mock
 from oslo_versionedobjects import base as obj_base
 from oslo_versionedobjects import fields as obj_fields
@@ -38,8 +40,9 @@ def _create_test_resource(context=None):
     return resource
 
 
-@obj_base.VersionedObjectRegistry.register
 class FakeResource(objects_base.NeutronObject):
+    # Version 1.0: Initial version
+    VERSION = '1.0'
 
     fields = {
         'id': obj_fields.UUIDField(),
@@ -55,8 +58,22 @@ class ResourcesRpcBaseTestCase(base.BaseTestCase):
 
     def setUp(self):
         super(ResourcesRpcBaseTestCase, self).setUp()
+
+        # TODO(mhickey) This is using temp registry pattern. The
+        # pattern solution is to backup the object registry, register
+        # a class locally, and then restore the original registry.
+        # Refer to https://review.openstack.org/#/c/263800/ for more
+        # details. This code should be updated when the patch is merged.
+        self._base_test_backup = copy.copy(
+            obj_base.VersionedObjectRegistry._registry._obj_classes)
+        self.addCleanup(self._restore_obj_registry)
+
         self.context = context.get_admin_context()
 
+    def _restore_obj_registry(self):
+        obj_base.VersionedObjectRegistry._registry._obj_classes = (
+            self._base_test_backup)
+
 
 class _ValidateResourceTypeTestCase(base.BaseTestCase):
     def setUp(self):
@@ -103,6 +120,7 @@ class ResourcesPullRpcApiTestCase(ResourcesRpcBaseTestCase):
         self.assertIs(self.rpc, resources_rpc.ResourcesPullRpcApi())
 
     def test_pull(self):
+        obj_base.VersionedObjectRegistry.register(FakeResource)
         expected_obj = _create_test_resource(self.context)
         resource_id = expected_obj.id
         self.cctxt_mock.call.return_value = expected_obj.obj_to_primitive()
@@ -128,6 +146,7 @@ class ResourcesPullRpcCallbackTestCase(ResourcesRpcBaseTestCase):
 
     def setUp(self):
         super(ResourcesPullRpcCallbackTestCase, self).setUp()
+        obj_base.VersionedObjectRegistry.register(FakeResource)
         self.callbacks = resources_rpc.ResourcesPullRpcCallback()
         self.resource_obj = _create_test_resource(self.context)
 
@@ -207,6 +226,7 @@ class ResourcesPushRpcCallbackTestCase(ResourcesRpcBaseTestCase):
 
     @mock.patch.object(resources_rpc.cons_registry, 'push')
     def test_push(self, reg_push_mock):
+        obj_base.VersionedObjectRegistry.register(FakeResource)
         self.callbacks.push(self.context, self.resource_prim, 'TYPE')
         reg_push_mock.assert_called_once_with(self.resource_obj.obj_name(),
                                               self.resource_obj, 'TYPE')
index 00afb8a62ee8ba186ba00f85bcd3ef1ee9c36e1b..34743ac5f942cc916e9c6c00d2708a517f64695e 100644 (file)
@@ -36,8 +36,10 @@ class FakeModel(object):
         pass
 
 
-@obj_base.VersionedObjectRegistry.register
+@obj_base.VersionedObjectRegistry.register_if(False)
 class FakeNeutronObject(base.NeutronDbObject):
+    # Version 1.0: Initial version
+    VERSION = '1.0'
 
     db_model = FakeModel
 
diff --git a/neutron/tests/unit/objects/test_objects.py b/neutron/tests/unit/objects/test_objects.py
new file mode 100644 (file)
index 0000000..341fc0f
--- /dev/null
@@ -0,0 +1,48 @@
+# Copyright 2015 IBM Corp.
+#
+#    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. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+import os
+import pprint
+
+from oslo_versionedobjects import base as obj_base
+from oslo_versionedobjects import fixture
+
+from neutron.tests import base as test_base
+
+
+# NOTE: The hashes in this list should only be changed if they come with a
+# corresponding version bump in the affected objects.
+object_data = {
+    'QosBandwidthLimitRule': '1.0-4e44a8f5c2895ab1278399f87b40a13d',
+    'QosRuleType': '1.0-d0df298d49eeffab91af18d1a4cf7eaf',
+    'QosPolicy': '1.0-721fa60ea8f0e8f15d456d6e917dfe59',
+}
+
+
+class TestObjectVersions(test_base.BaseTestCase):
+
+    def test_versions(self):
+        checker = fixture.ObjectVersionChecker(
+            obj_base.VersionedObjectRegistry.obj_classes())
+        fingerprints = checker.get_hashes()
+
+        if os.getenv('GENERATE_HASHES'):
+            file('object_hashes.txt', 'w').write(
+                pprint.pformat(fingerprints))
+
+        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 in the object_data map in this test module.')
index 9ada2e3bb94b815ce9bbb17b9b41fe4b6042e9cf..d47eba8103da822d110123b30ef1ed99cdf02bf7 100755 (executable)
@@ -14,6 +14,8 @@ ignore_regexes=(
     # The following vendor plugins are not required to confrm to the
     # structural requirements.
     "^plugins/ibm.*$"
+    # The following test is required for oslo.versionedobjects
+    "^objects/test_objects.py$"
     # The following open source plugin tests are not actually unit
     # tests and are ignored pending their relocation to the functional
     # test tree.
diff --git a/tox.ini b/tox.ini
index 9710cb9a521876644d96ebbd82f49857e3fed9f7..879122a52586187983b9df8ceb3dbe3f2c8f8b65 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -5,7 +5,7 @@ skipsdist = True
 
 [testenv]
 setenv = VIRTUAL_ENV={envdir}
-passenv = TRACE_FAILONLY
+passenv = TRACE_FAILONLY GENERATE_HASHES
 usedevelop = True
 install_command =
                   constraints: {[testenv:common-constraints]install_command}