From: Christopher Armstrong Date: Tue, 16 Jul 2013 19:57:55 +0000 (+0000) Subject: Add a test for customizing AWS::EC2::Instance X-Git-Tag: 2014.1~358^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=1f1b66cbaeb1bb3d871cb8cb1ebbc3aa0487f2c8;p=openstack-build%2Fheat-build.git Add a test for customizing AWS::EC2::Instance 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 --- diff --git a/heat/engine/resources/autoscaling.py b/heat/engine/resources/autoscaling.py index da937f11..94edf816 100644 --- a/heat/engine/resources/autoscaling.py +++ b/heat/engine/resources/autoscaling.py @@ -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 diff --git a/heat/tests/test_instance_group.py b/heat/tests/test_instance_group.py index 5cf13557..e387975e 100644 --- a/heat/tests/test_instance_group.py +++ b/heat/tests/test_instance_group.py @@ -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)