Resource.__eq__ allow resources in different stacks
Doing a stack update was incorrectly replacing identical resources because Refs in the new resource were evaluating to None in the new stack. This change allows an alternative template to be passed to parsed_template and modifies __eq__ to compare the two templates in the same stack. __eq__ now also checks that the name of both resources matches, since a stack could have multiple identical resources with different names. Change-Id: I7e09ee1238dd01490cdfe186c51b7fada9546a63
This commit is contained in:
@@ -136,9 +136,11 @@ class Resource(object):
|
||||
def __eq__(self, other):
|
||||
'''Allow == comparison of two resources'''
|
||||
# For the purposes of comparison, we declare two resource objects
|
||||
# equal if their parsed_templates are the same
|
||||
# equal if their names and parsed_templates are the same
|
||||
if isinstance(other, Resource):
|
||||
return self.parsed_template() == other.parsed_template()
|
||||
return (self.name == other.name) and (
|
||||
self.parsed_template() == self.parsed_template(
|
||||
template=other.t))
|
||||
return NotImplemented
|
||||
|
||||
def __ne__(self, other):
|
||||
@@ -156,14 +158,14 @@ class Resource(object):
|
||||
return identifier.ResourceIdentifier(resource_name=self.name,
|
||||
**self.stack.identifier())
|
||||
|
||||
def parsed_template(self, section=None, default={}):
|
||||
def parsed_template(self, section=None, default={}, template=None):
|
||||
'''
|
||||
Return the parsed template data for the resource. May be limited to
|
||||
only one section of the data, in which case a default value may also
|
||||
be supplied.
|
||||
'''
|
||||
if section is None:
|
||||
template = self.t
|
||||
template = template or self.t
|
||||
else:
|
||||
template = self.t.get(section, default)
|
||||
return self.stack.resolve_runtime_data(template)
|
||||
|
||||
@@ -95,6 +95,27 @@ class ResourceTest(unittest.TestCase):
|
||||
res = resource.GenericResource('test_resource', tmpl, self.stack)
|
||||
self.assertEqual(res.metadata, {})
|
||||
|
||||
def test_equals_different_stacks(self):
|
||||
tmpl1 = {'Type': 'Foo'}
|
||||
tmpl2 = {'Type': 'Foo'}
|
||||
tmpl3 = {'Type': 'Bar'}
|
||||
stack2 = parser.Stack(None, 'test_stack', parser.Template({}),
|
||||
stack_id=-1)
|
||||
res1 = resource.GenericResource('test_resource', tmpl1, self.stack)
|
||||
res2 = resource.GenericResource('test_resource', tmpl2, stack2)
|
||||
res3 = resource.GenericResource('test_resource2', tmpl3, stack2)
|
||||
|
||||
self.assertEqual(res1, res2)
|
||||
self.assertNotEqual(res1, res3)
|
||||
|
||||
def test_equals_names(self):
|
||||
tmpl1 = {'Type': 'Foo'}
|
||||
tmpl2 = {'Type': 'Foo'}
|
||||
res1 = resource.GenericResource('test_resource1', tmpl1, self.stack)
|
||||
res2 = resource.GenericResource('test_resource2', tmpl2, self.stack)
|
||||
|
||||
self.assertNotEqual(res1, res2)
|
||||
|
||||
|
||||
@attr(tag=['unit', 'resource'])
|
||||
@attr(speed='fast')
|
||||
|
||||
Reference in New Issue
Block a user