Handle unregistered nested objects

The 'obj_tree_get_versions' function is used by e.g. the
'test_compatibility_routines' fixture to sanity check object versioning
and we don't do things like remove objects that are referenced
elsewhere. Unfortunately if we do this exact thing, an unhelpful
'IndexError' is raised. Handle things better and raise a specific
exception with hints towards what may be the root cause.

Change-Id: I7c05c648a9efb018ef9718fa31a9cc075482db59
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Closes-bug: #1860652
This commit is contained in:
Stephen Finucane 2020-01-23 11:17:09 +00:00
parent a4b7ef7b56
commit bbceb088eb
3 changed files with 26 additions and 1 deletions

View File

@ -1172,7 +1172,11 @@ def obj_tree_get_versions(objname, tree=None):
else:
continue
obj_tree_get_versions(child_cls, tree=tree)
try:
obj_tree_get_versions(child_cls, tree=tree)
except IndexError:
raise exception.UnregisteredSubobject(
child_objname=child_cls, parent_objname=objname)
return tree

View File

@ -189,3 +189,8 @@ class InvalidTargetVersion(VersionedObjectsException):
class TargetBeforeSubobjectExistedException(VersionedObjectsException):
msg_fmt = _("No subobject existed at version %(target_version)s")
class UnregisteredSubobject(VersionedObjectsException):
msg_fmt = _("%(child_objname)s is referenced by %(parent_objname)s but "
"is not registered")

View File

@ -2354,6 +2354,22 @@ class TestUtilityMethods(test.TestCase):
'TestChildTwo': '4.56'},
tree)
def test_missing_referenced(self):
"""Ensure a missing child object is highlighted."""
@base.VersionedObjectRegistry.register
class TestObjectFoo(base.VersionedObject):
VERSION = '1.23'
fields = {
# note that this object does not exist
'child': fields.ObjectField('TestChildBar'),
}
exc = self.assertRaises(exception.UnregisteredSubobject,
base.obj_tree_get_versions,
'TestObjectFoo')
self.assertIn('TestChildBar is referenced by TestObjectFoo',
exc.format_message())
class TestListObjectConcat(test.TestCase):
def test_list_object_concat(self):