]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Add a test for customizing AWS::EC2::Instance
authorChristopher Armstrong <chris.armstrong@rackspace.com>
Tue, 16 Jul 2013 19:57:55 +0000 (19:57 +0000)
committerChristopher Armstrong <chris.armstrong@rackspace.com>
Wed, 17 Jul 2013 01:06:41 +0000 (20:06 -0500)
InstanceGroup explicitly supports "custom" versions of AWS::EC2::Instance
(instead of directly using heat.engine.resources.instance.Instance)
but there was no unit test for that feature. Here we add one.

Change-Id: I490675c18734a61d27625ed3c76fb228e3fd1a9d

heat/engine/resources/autoscaling.py
heat/tests/test_instance_group.py

index da937f11079302dd3b9d3c5933fa1db8bd662600..94edf816fd072854d28e5b90364163bb1dcb5e6c 100644 (file)
@@ -113,14 +113,16 @@ class InstanceGroup(resource.Resource):
                     self._wait_for_activation(creator)
 
     def _make_instance(self, name):
-
+        # We look up and subclass the class for AWS::EC2::Instance instead of
+        # just importing Instance, so that if someone overrides that resource
+        # we'll use the custom one.
         Instance = resource.get_class('AWS::EC2::Instance',
                                       resource_name=name,
                                       environment=self.stack.env)
 
         class GroupedInstance(Instance):
             '''
-            Subclass instance.Instance to supress event transitions, since the
+            Subclass Instance to suppress event transitions, since the
             scaling-group instances are not "real" resources, ie defined in the
             template, which causes problems for event handling since we can't
             look up the resources via parser.Stack
index 5cf13557512185023d768d7c6cb5945226e9cabc..e387975e208863643a436366d01fb66384f20b7d 100644 (file)
@@ -19,6 +19,7 @@ from heat.common import template_format
 from heat.engine.resources import autoscaling as asc
 from heat.engine.resources import instance
 from heat.engine import resource
+from heat.engine import resources
 from heat.engine import scheduler
 from heat.tests.common import HeatTestCase
 from heat.tests.utils import setup_dummy_db
@@ -59,15 +60,21 @@ class InstanceGroupTest(HeatTestCase):
         super(InstanceGroupTest, self).setUp()
         setup_dummy_db()
 
-    def _stub_create(self, num):
+    def _stub_create(self, num, instance_class=instance.Instance):
+        """
+        Expect creation of C{num} number of Instances.
 
-        self.m.StubOutWithMock(instance.Instance, 'handle_create')
-        self.m.StubOutWithMock(instance.Instance, 'check_create_complete')
+        :param instance_class: The resource class to expect to be created
+                               instead of instance.Instance.
+        """
+
+        self.m.StubOutWithMock(instance_class, 'handle_create')
+        self.m.StubOutWithMock(instance_class, 'check_create_complete')
         cookie = object()
         for x in range(num):
-            instance.Instance.handle_create().AndReturn(cookie)
-        instance.Instance.check_create_complete(cookie).AndReturn(False)
-        instance.Instance.check_create_complete(
+            instance_class.handle_create().AndReturn(cookie)
+        instance_class.check_create_complete(cookie).AndReturn(False)
+        instance_class.check_create_complete(
             cookie).MultipleTimes().AndReturn(True)
 
     def create_instance_group(self, t, stack, resource_name):
@@ -99,6 +106,33 @@ class InstanceGroupTest(HeatTestCase):
         rsrc.delete()
         self.m.VerifyAll()
 
+    def test_instance_group_custom_resource(self):
+        """
+        If AWS::EC2::Instance is overridden, InstanceGroup will automatically
+        use that overridden resource type.
+        """
+        # resources may need to be initialised if this is the first test run.
+        resources.initialise()
+
+        class MyInstance(instance.Instance):
+            """A customized Instance resource."""
+
+        original_instance = resource.get_class("AWS::EC2::Instance")
+        resource._register_class("AWS::EC2::Instance", MyInstance)
+        self.addCleanup(resource._register_class, "AWS::EC2::Instance",
+                        original_instance)
+
+        t = template_format.parse(ig_template)
+        stack = parse_stack(t)
+        self._stub_create(1, instance_class=MyInstance)
+
+        self.m.ReplayAll()
+
+        rsrc = self.create_instance_group(t, stack, 'JobServerGroup')
+        self.assertEqual('JobServerGroup', rsrc.FnGetRefId())
+        rsrc.delete()
+        self.m.VerifyAll()
+
     def test_missing_image(self):
 
         t = template_format.parse(ig_template)