"version" section should be required in template

Adding the version checking to keep place with heat.

Change-Id: Ib2f0b55c3c9e8b0a8f26e972b37a979d8d703f7c
Closes-Bug: #1273980
This commit is contained in:
huangtianhua
2014-01-29 16:03:46 +08:00
parent 55ad4b7154
commit c49fd710e4
4 changed files with 26 additions and 25 deletions

View File

@@ -59,22 +59,9 @@ def parse(tmpl_str):
else:
if tpl is None:
tpl = {}
if u'heat_template_version' not in tpl:
default_for_missing(tpl, u'HeatTemplateFormatVersion',
HEAT_VERSIONS)
# Looking for supported version keys in the loaded template
if not ('HeatTemplateFormatVersion' in tpl
or 'heat_template_version' in tpl
or 'AWSTemplateFormatVersion' in tpl):
raise ValueError("Template format version not found.")
return tpl
def default_for_missing(tpl, version_param, versions):
'''Checks a parsed template for missing version and sections.
This is currently only applied to YAML templates.
'''
# if version is missing, implicitly use the lastest one
if version_param not in tpl:
tpl[version_param] = versions[-1]
# create empty placeholders for any of the main dict sections
for param in (u'Parameters', u'Mappings', u'Resources', u'Outputs'):
if param not in tpl:
tpl[param] = {}

View File

@@ -718,7 +718,7 @@ class ShellTestUserPass(ShellBase):
None)
self.m.StubOutWithMock(urlutils, 'urlopen')
urlutils.urlopen('http://no.where/minimal.template').AndReturn(
six.StringIO('{}'))
six.StringIO('{"AWSTemplateFormatVersion" : "2010-09-09"}'))
http.HTTPClient.json_request(
'POST', '/stacks', data=mox.IgnoreArg(),

View File

@@ -40,3 +40,13 @@ class YamlParseExceptions(testtools.TestCase):
self.assertRaises(ValueError,
template_format.parse, text)
def test_parse_no_version_format(self):
yaml = ''
self.assertRaises(ValueError, template_format.parse, yaml)
yaml2 = '''Parameters: {}
Mappings: {}
Resources: {}
Outputs: {}
'''
self.assertRaises(ValueError, template_format.parse, yaml2)

View File

@@ -280,13 +280,15 @@ class TestGetTemplateContents(testtools.TestCase):
def test_get_template_contents_file(self):
with tempfile.NamedTemporaryFile() as tmpl_file:
tmpl = b'{"foo": "bar"}'
tmpl = b'{"AWSTemplateFormatVersion" : "2010-09-09",' \
b' "foo": "bar"}'
tmpl_file.write(tmpl)
tmpl_file.flush()
files, tmpl_parsed = template_utils.get_template_contents(
tmpl_file.name)
self.assertEqual({"foo": "bar"}, tmpl_parsed)
self.assertEqual({"AWSTemplateFormatVersion": "2010-09-09",
"foo": "bar"}, tmpl_parsed)
self.assertEqual({}, files)
def test_get_template_contents_file_empty(self):
@@ -326,7 +328,7 @@ class TestGetTemplateContents(testtools.TestCase):
'Error parsing template file://%s ' % tmpl_file.name))
def test_get_template_contents_url(self):
tmpl = '{"foo": "bar"}'
tmpl = '{"AWSTemplateFormatVersion" : "2010-09-09", "foo": "bar"}'
url = 'http://no.where/path/to/a.yaml'
self.m.StubOutWithMock(urlutils, 'urlopen')
urlutils.urlopen(url).AndReturn(six.StringIO(tmpl))
@@ -334,11 +336,12 @@ class TestGetTemplateContents(testtools.TestCase):
files, tmpl_parsed = template_utils.get_template_contents(
template_url=url)
self.assertEqual({"foo": "bar"}, tmpl_parsed)
self.assertEqual({"AWSTemplateFormatVersion": "2010-09-09",
"foo": "bar"}, tmpl_parsed)
self.assertEqual({}, files)
def test_get_template_contents_object(self):
tmpl = '{"foo": "bar"}'
tmpl = '{"AWSTemplateFormatVersion" : "2010-09-09", "foo": "bar"}'
url = 'http://no.where/path/to/a.yaml'
self.m.ReplayAll()
@@ -354,7 +357,8 @@ class TestGetTemplateContents(testtools.TestCase):
template_object=url,
object_request=object_request)
self.assertEqual({"foo": "bar"}, tmpl_parsed)
self.assertEqual({"AWSTemplateFormatVersion": "2010-09-09",
"foo": "bar"}, tmpl_parsed)
self.assertEqual({}, files)
self.assertTrue(self.object_requested)