From 75859529cf878dcc1b59768fac2b8a2ad14822df Mon Sep 17 00:00:00 2001 From: Wei Wang Date: Mon, 13 Apr 2015 18:25:11 +0800 Subject: [PATCH] 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 --- neutron/api/extensions.py | 2 +- neutron/tests/unit/api/test_extensions.py | 40 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/neutron/api/extensions.py b/neutron/api/extensions.py index 01bdf4512..8c2e4e2b0 100644 --- a/neutron/api/extensions.py +++ b/neutron/api/extensions.py @@ -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()) diff --git a/neutron/tests/unit/api/test_extensions.py b/neutron/tests/unit/api/test_extensions.py index 0730f3e32..cc3f71c05 100644 --- a/neutron/tests/unit/api/test_extensions.py +++ b/neutron/tests/unit/api/test_extensions.py @@ -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): -- 2.45.2