From 752c0112adc7bddbb594ba947f6bf0c886993b82 Mon Sep 17 00:00:00 2001 From: Sergey Kraynev Date: Thu, 9 Oct 2014 13:46:59 -0400 Subject: [PATCH] Handle error in templates with incorrect resources This patch adds additional validation resources section for hot templates, it allows determine of the real problem in template and give the user an adequate message. Change-Id: Iff5e1b541b00a82dfce524f00ff1d20f50ae92da Closes-Bug: #1365949 --- heat/engine/hot/template.py | 5 ++++- heat/tests/test_hot.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/heat/engine/hot/template.py b/heat/engine/hot/template.py index 6ff0d3a5d..099a7559a 100644 --- a/heat/engine/hot/template.py +++ b/heat/engine/hot/template.py @@ -13,6 +13,7 @@ import collections import six +from heat.common import exception from heat.common.i18n import _ from heat.engine.cfn import functions as cfn_funcs from heat.engine.cfn import template as cfn_template @@ -128,10 +129,12 @@ class HOTemplate20130523(template.Template): 'update_policy': 'UpdatePolicy'} cfn_resources = {} - for resource_name, attrs in six.iteritems(resources): cfn_resource = {} + if isinstance(attrs, six.string_types): + message = _('"resources" must contain a map of resource maps.') + raise exception.StackValidationFailed(message=message) for attr, attr_value in six.iteritems(attrs): cfn_attr = self._translate(attr, HOT_TO_CFN_ATTRS, _('"%s" is not a valid keyword ' diff --git a/heat/tests/test_hot.py b/heat/tests/test_hot.py index c2c341b07..80124d1e9 100644 --- a/heat/tests/test_hot.py +++ b/heat/tests/test_hot.py @@ -204,6 +204,24 @@ class HOTemplateTest(HeatTestCase): 'inside a resource definition\'', six.text_type(err)) + def test_translate_resources_resources_without_name(self): + hot_tpl = template_format.parse(''' + heat_template_version: 2013-05-23 + resources: + type: AWS::EC2::Instance + properties: + property1: value1 + metadata: + foo: bar + depends_on: dummy + deletion_policy: dummy + ''') + tmpl = parser.Template(hot_tpl) + error = self.assertRaises(exception.StackValidationFailed, + tmpl.__getitem__, tmpl.RESOURCES) + self.assertEqual('"resources" must contain a map of resource maps.', + six.text_type(error)) + def test_translate_resources_bad_metadata(self): """Test translation of resources including invalid keyword."""