Fix bug that resources in attr_map may point to same object

The assignment of attr_map may cause resources' attr_map point
to a same object. So once one resource's attr_map update, it maybe
affecte others.

Change-Id: Ica2c3082f9579b297f8c6323e04f8fd17c4da222
Closes-Bug: #1443342
This commit is contained in:
Wei Wang 2015-04-13 18:25:11 +08:00
parent 83cac810f0
commit 75859529cf
2 changed files with 41 additions and 1 deletions

View File

@ -455,7 +455,7 @@ class ExtensionManager(object):
if attr_map.get(res, None):
attr_map[res].update(resource_attrs)
else:
attr_map[res] = resource_attrs
attr_map[res] = resource_attrs.copy()
except AttributeError:
LOG.exception(_LE("Error fetching extended attributes for "
"extension '%s'"), ext.get_name())

View File

@ -486,6 +486,46 @@ class ExtensionManagerTest(base.BaseTestCase):
self.assertIn('valid_extension', ext_mgr.extensions)
self.assertNotIn('invalid_extension', ext_mgr.extensions)
def test_assignment_of_attr_map(self):
class ExtendResourceExtension(object):
"""Generated Extended Resources.
This extension's extended resource will assign
to more than one resource.
"""
def get_name(self):
return "extension"
def get_alias(self):
return "extension"
def get_description(self):
return "Extension for test"
def get_updated(self):
return "2013-07-23T10:00:00-00:00"
def get_extended_resources(self, version):
EXTENDED_TIMESTAMP = {
'created_at': {'allow_post': False, 'allow_put': False,
'is_visible': True}}
EXTENDED_RESOURCES = ["ext1", "ext2"]
attrs = {}
for resources in EXTENDED_RESOURCES:
attrs[resources] = EXTENDED_TIMESTAMP
return attrs
ext_mgr = extensions.ExtensionManager('')
ext_mgr.add_extension(ExtendResourceExtension())
ext_mgr.add_extension(ext_stubs.StubExtension("ext1"))
ext_mgr.add_extension(ext_stubs.StubExtension("ext2"))
attr_map = {}
ext_mgr.extend_resources("2.0", attr_map)
self.assertNotEqual(id(attr_map['ext1']), id(attr_map['ext2']))
class PluginAwareExtensionManagerTest(base.BaseTestCase):