From 7ccb2994b8cd53317961c7dc4a251eeaa965c1d2 Mon Sep 17 00:00:00 2001 From: John Perkins Date: Thu, 18 Aug 2016 13:54:49 +0100 Subject: [PATCH] Raise exception when synthetic field invalid A check is introduced in the ``__init__`` method of ``NeutronObject`` class. This check ensures synthetic fields are valid in any oslo versioned object. Reference: I3c565f83098a03d210616f51d359ca45388aaf60 Depends-On: I12c7a330555966d30e44418cbd500958fe844462 Co-Authored-By: Rodolfo Alonso Hernandez Closes-Bug: #1614478 Change-Id: I33c41fbd4dd40ba292ebe67ac497372d4354f260 --- neutron/objects/base.py | 5 +++++ neutron/tests/unit/objects/test_base.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/neutron/objects/base.py b/neutron/objects/base.py index 3124e51134b..547667d0725 100644 --- a/neutron/objects/base.py +++ b/neutron/objects/base.py @@ -290,6 +290,11 @@ class DeclarativeObject(abc.ABCMeta): if 'project_id' in cls.fields and 'tenant_id' not in cls.fields: cls.extra_filter_names.add('tenant_id') + invalid_fields = [f for f in cls.synthetic_fields + if f not in cls.fields] + if invalid_fields: + raise o_exc.NeutronObjectValidatorException(fields=invalid_fields) + @six.add_metaclass(DeclarativeObject) class NeutronDbObject(NeutronObject): diff --git a/neutron/tests/unit/objects/test_base.py b/neutron/tests/unit/objects/test_base.py index 1277f4e1095..ec02fcc40d6 100644 --- a/neutron/tests/unit/objects/test_base.py +++ b/neutron/tests/unit/objects/test_base.py @@ -1991,3 +1991,24 @@ class OperationOnStringAndJsonTestCase(test_base.BaseTestCase): res = base.NeutronDbObject.filter_to_json_str(field_val, default_val) self.assertEqual(default_val, res) + + +class NeutronObjectValidatorTestCase(test_base.BaseTestCase): + + def test_load_wrong_synthetic_fields(self): + try: + @obj_base.VersionedObjectRegistry.register_if(False) + class FakeNeutronObjectSyntheticFieldWrong(base.NeutronDbObject): + # Version 1.0: Initial version + VERSION = '1.0' + + db_model = FakeModel + + fields = { + 'id': common_types.UUIDField(), + 'obj_field': common_types.UUIDField() + } + + synthetic_fields = ['obj_field', 'wrong_synthetic_field_name'] + except o_exc.NeutronObjectValidatorException as exc: + self.assertIn('wrong_synthetic_field_name', str(exc))