Check that 'heat_template_version' is a HOT version

Previously, any value could be specified as the 'heat_template_version'.
Check that 'heat_template_version' is a valid HOT version and raise a
ValueError if it is not.

Change-Id: I7f9a7acd52cf6273a4730091c08b8cd1a5e07961
Closes-Bug: #1277215
This commit is contained in:
Jason Dunsmore
2014-02-12 13:14:09 -06:00
parent 9ac02f51ea
commit dbf239e2c8
3 changed files with 45 additions and 7 deletions

View File

@@ -47,6 +47,8 @@ class HOTemplate(template.Template):
SECTIONS_NO_DIRECT_ACCESS = set([PARAMETERS, VERSION])
VERSIONS = ('2013-05-23',)
_CFN_TO_HOT_SECTIONS = {template.Template.VERSION: VERSION,
template.Template.DESCRIPTION: DESCRIPTION,
template.Template.PARAMETERS: PARAMETERS,

View File

@@ -19,6 +19,7 @@ import functools
from heat.db import api as db_api
from heat.engine import parameters
from heat.engine.cfn import functions
from heat.openstack.common.gettextutils import _
class Template(collections.Mapping):
@@ -36,10 +37,21 @@ class Template(collections.Mapping):
if cls == Template:
if 'heat_template_version' in template:
# deferred import of HOT module to avoid circular dependency
# defer import of HOT module to avoid circular dependency
# at load time
from heat.engine import hot
return hot.HOTemplate(template, *args, **kwargs)
version = template['heat_template_version']
valid_versions = hot.HOTemplate.VERSIONS
if version in valid_versions:
return hot.HOTemplate(template, *args, **kwargs)
else:
msg = _('"%(version)s" is not a valid '
'heat_template_version. Should be one of: '
'%(valid)s')
raise ValueError(msg % {'version': version,
'valid': str(valid_versions)})
return super(Template, cls).__new__(cls)

View File

@@ -69,11 +69,6 @@ class HOTemplateTest(HeatTestCase):
self.assertEqual({}, tmpl[tmpl.RESOURCES])
self.assertEqual({}, tmpl[tmpl.OUTPUTS])
def test_version(self):
tmpl = parser.Template(hot_tpl_empty)
self.assertEqual(('heat_template_version', '2013-05-23'),
tmpl.version())
def test_translate_resources(self):
"""Test translation of resources into internal engine format."""
@@ -291,6 +286,35 @@ class HOTemplateTest(HeatTestCase):
self.assertEqual(expected_description, tmpl['description'])
self.assertNotIn('parameters', tmpl.keys())
def test_invalid_hot_version(self):
"""
Test HOT version check.
Pass an invalid HOT version to template.Template.__new__() and
validate that we get a ValueError.
"""
tmpl_str = "heat_template_version: this-ain't-valid"
hot_tmpl = template_format.parse(tmpl_str)
exc = self.assertRaises(ValueError, template.Template, hot_tmpl)
self.assertIn('"this-ain\'t-valid" is not a valid '
'heat_template_version', str(exc))
def test_valid_hot_version(self):
"""
Test HOT version check.
Pass a valid HOT version to template.Template.__new__() and
validate that we get back a parsed template.
"""
tmpl_str = "heat_template_version: 2013-05-23"
hot_tmpl = template_format.parse(tmpl_str)
parsed_tmpl = template.Template(hot_tmpl)
expected = '2013-05-23'
observed = parsed_tmpl.version()[1]
self.assertEqual(expected, observed)
class StackTest(test_parser.StackTest):
"""Test stack function when stack was created from HOT template."""