]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Resource.__eq__ allow resources in different stacks
authorSteve Baker <sbaker@redhat.com>
Wed, 5 Dec 2012 22:53:45 +0000 (11:53 +1300)
committerSteve Baker <sbaker@redhat.com>
Thu, 6 Dec 2012 01:17:00 +0000 (14:17 +1300)
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

heat/engine/resource.py
heat/tests/test_resource.py

index c3b149b46a8013a8d5f92a954ce6873016e0fbb1..fad416eb9076894414c5bb53a862244562aabb00 100644 (file)
@@ -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)
index cf0f4ae97b2f1f8817237119c93e9869f26f27a0..3eeadf301e0acd75d256aa1b16618d4529f894a4 100644 (file)
@@ -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')