From c0f281c1423f72c1afe4a5e5ae48cfd7dce74980 Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Wed, 19 Jul 2017 17:35:39 -0400 Subject: [PATCH] Unit tests: make tests more realistic In some tests the template we provide for the Stack does not match the actual resources we are testing. This never happens in reality, and it makes the tests brittle when other legitimate changes occur in the engine. This makes the integration testing more realistic. Change-Id: I777309f310e18f8a16b5ae675419d7eeeffa09ee --- heat/tests/aws/test_user.py | 5 +- .../openstack/heat/test_instance_group.py | 6 +- heat/tests/openstack/heat/test_restarter.py | 84 ++++++++----------- heat/tests/test_stack.py | 11 ++- 4 files changed, 49 insertions(+), 57 deletions(-) diff --git a/heat/tests/aws/test_user.py b/heat/tests/aws/test_user.py index aeda8be8b..4cb15b76a 100644 --- a/heat/tests/aws/test_user.py +++ b/heat/tests/aws/test_user.py @@ -319,10 +319,7 @@ class AccessKeyTest(common.HeatTestCase): return rsrc def create_access_key(self, t, stack, resource_name): - resource_defns = stack.t.resource_definitions(stack) - rsrc = user.AccessKey(resource_name, - resource_defns[resource_name], - stack) + rsrc = stack[resource_name] self.assertIsNone(rsrc.validate()) scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) diff --git a/heat/tests/openstack/heat/test_instance_group.py b/heat/tests/openstack/heat/test_instance_group.py index 3897e5e91..72998ab39 100644 --- a/heat/tests/openstack/heat/test_instance_group.py +++ b/heat/tests/openstack/heat/test_instance_group.py @@ -342,12 +342,12 @@ class InstanceGroupWithNestedStack(common.HeatTestCase): super(InstanceGroupWithNestedStack, self).setUp() t = template_format.parse(inline_templates.as_template) self.stack = utils.parse_stack(t, params=inline_templates.as_params) - lc = self.create_launch_config(t, self.stack) - lcid = lc.FnGetRefId() + self.create_launch_config(t, self.stack) + wsg_props = self.stack['WebServerGroup'].t._properties self.defn = rsrc_defn.ResourceDefinition( 'asg', 'OS::Heat::InstanceGroup', {'Size': 2, 'AvailabilityZones': ['zoneb'], - 'LaunchConfigurationName': lcid}) + 'LaunchConfigurationName': wsg_props['LaunchConfigurationName']}) self.group = instgrp.InstanceGroup('asg', self.defn, self.stack) self.group._lb_reload = mock.Mock() diff --git a/heat/tests/openstack/heat/test_restarter.py b/heat/tests/openstack/heat/test_restarter.py index 0ebf72430..94857b736 100644 --- a/heat/tests/openstack/heat/test_restarter.py +++ b/heat/tests/openstack/heat/test_restarter.py @@ -15,9 +15,6 @@ import mock from heat.common import template_format from heat.engine.clients.os import nova -from heat.engine.resources.aws.ec2 import instance -from heat.engine.resources.openstack.heat import ha_restarter -from heat.engine import scheduler from heat.tests import common from heat.tests import utils @@ -28,10 +25,29 @@ restarter_template = ''' "Description" : "Template to test HARestarter", "Parameters" : {}, "Resources" : { + "instance": { + "Type": "OS::Heat::None" + }, "restarter": { "Type": "OS::Heat::HARestarter", "Properties": { - "InstanceId": "1234" + "InstanceId": {"Ref": "instance"} + } + } + } +} +''' + +bogus_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "Template to test HARestarter", + "Parameters" : {}, + "Resources" : { + "restarter": { + "Type": "OS::Heat::HARestarter", + "Properties": { + "InstanceId": "instance" } } } @@ -40,72 +56,46 @@ restarter_template = ''' class RestarterTest(common.HeatTestCase): - def create_restarter(self): - snippet = template_format.parse(restarter_template) + def create_restarter(self, template=restarter_template): + snippet = template_format.parse(template) self.stack = utils.parse_stack(snippet) - resource_defns = self.stack.t.resource_definitions(self.stack) - restarter = ha_restarter.Restarter( - 'restarter', resource_defns['restarter'], self.stack) + restarter = self.stack['restarter'] self.patchobject(nova.NovaClientPlugin, 'get_server', return_value=mock.MagicMock()) restarter.handle_create = mock.Mock(return_value=None) + self.stack.create() return restarter - def create_mock_instance(self, stack): - inst = mock.Mock(spec=instance.Instance) - inst.resource_id = '1234' - inst.name = 'instance' - inst.action = inst.CREATE - inst.status = inst.COMPLETE - inst.state = (inst.action, inst.status) - inst.FnGetRefId = lambda: inst.resource_id - stack.resources['instance'] = inst - def test_create(self): rsrc = self.create_restarter() - scheduler.TaskRunner(rsrc.create)() self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state) rsrc.handle_create.assert_called_once_with() def test_handle_signal(self): rsrc = self.create_restarter() - scheduler.TaskRunner(rsrc.create)() - self.create_mock_instance(rsrc.stack) - - rsrc.stack.restart_resource = mock.Mock(return_value=None) - - self.assertIsNone(rsrc.handle_signal()) - rsrc.stack.restart_resource.assert_called_once_with('instance') + with mock.patch.object(rsrc.stack, 'restart_resource') as rr: + self.assertIsNone(rsrc.handle_signal()) + rr.assert_called_once_with('instance') def test_handle_signal_alarm(self): rsrc = self.create_restarter() - scheduler.TaskRunner(rsrc.create)() - self.create_mock_instance(rsrc.stack) - - rsrc.stack.restart_resource = mock.Mock(return_value=None) - - self.assertIsNone(rsrc.handle_signal({'state': 'Alarm'})) - rsrc.stack.restart_resource.assert_called_once_with('instance') + with mock.patch.object(rsrc.stack, 'restart_resource') as rr: + self.assertIsNone(rsrc.handle_signal({'state': 'Alarm'})) + rr.assert_called_once_with('instance') def test_handle_signal_not_alarm(self): rsrc = self.create_restarter() - scheduler.TaskRunner(rsrc.create)() - self.create_mock_instance(rsrc.stack) - - rsrc.stack.restart_resource = mock.Mock(return_value=None) - - self.assertIsNone(rsrc.handle_signal({'state': 'spam'})) - self.assertEqual([], rsrc.stack.restart_resource.mock_calls) + with mock.patch.object(rsrc.stack, 'restart_resource') as rr: + self.assertIsNone(rsrc.handle_signal({'state': 'spam'})) + self.assertEqual([], rr.mock_calls) def test_handle_signal_no_instance(self): - rsrc = self.create_restarter() - scheduler.TaskRunner(rsrc.create)() + rsrc = self.create_restarter(bogus_template) - rsrc.stack.restart_resource = mock.Mock(return_value=None) - - self.assertIsNone(rsrc.handle_signal()) - self.assertEqual([], rsrc.stack.restart_resource.mock_calls) + with mock.patch.object(rsrc.stack, 'restart_resource') as rr: + self.assertIsNone(rsrc.handle_signal()) + self.assertEqual([], rr.mock_calls) diff --git a/heat/tests/test_stack.py b/heat/tests/test_stack.py index cd560ebd6..fe6dc4008 100644 --- a/heat/tests/test_stack.py +++ b/heat/tests/test_stack.py @@ -35,7 +35,6 @@ from heat.engine.clients.os import nova from heat.engine import environment from heat.engine import function from heat.engine import node_data -from heat.engine import output from heat.engine import resource from heat.engine import scheduler from heat.engine import service @@ -2757,8 +2756,14 @@ class StackTest(common.HeatTestCase): self.assertEqual(expected_msg, six.text_type(expected_exception)) mock_dependency.validate.assert_called_once_with() - stc = stack.Stack(self.ctx, utils.random_name(), self.tmpl) - stc._outputs = {'foo': output.OutputDefinition('foo', 'bar')} + tmpl = template_format.parse(""" + HeatTemplateFormatVersion: '2012-12-12' + Outputs: + foo: + Value: bar + """) + stc = stack.Stack(self.ctx, utils.random_name(), + template.Template(tmpl)) func_val.side_effect = AssertionError(expected_msg) expected_exception = self.assertRaises(AssertionError, stc.validate) self.assertEqual(expected_msg, six.text_type(expected_exception))