Improve error for custom resource

Improve error for case when custom template resource specified via http
or https url. Same behaviour is presented for python-heatclent.

Change-Id: I05c6bad3e6a52ca041fd8a51849ff4e6af23baed
Closes-Bug: #1376656
This commit is contained in:
Sergey Kraynev 2014-10-02 05:29:47 -04:00
parent bc940a214e
commit da0194b01a
3 changed files with 37 additions and 4 deletions

View File

@ -54,7 +54,8 @@ def simple_parse(tmpl_str):
try: try:
tpl = yaml.load(tmpl_str, Loader=yaml_loader) tpl = yaml.load(tmpl_str, Loader=yaml_loader)
except yaml.YAMLError as yea: except yaml.YAMLError as yea:
raise ValueError(yea) msg = _('Error parsing template: %s') % yea
raise ValueError(msg)
else: else:
if tpl is None: if tpl is None:
tpl = {} tpl = {}

View File

@ -661,6 +661,36 @@ class ProviderTemplateTest(HeatTestCase):
self.assertRaises(exception.StackValidationFailed, temp_res.validate) self.assertRaises(exception.StackValidationFailed, temp_res.validate)
self.m.VerifyAll() self.m.VerifyAll()
def test_incorrect_template_provided_with_url(self):
wrong_template = '''
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#
'''
env = environment.Environment()
test_templ_name = 'http://heatr/bad_tmpl.yaml'
env.load({'resource_registry':
{'Test::Tmpl': test_templ_name}})
stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template), env=env,
stack_id=str(uuid.uuid4()))
self.m.StubOutWithMock(urlfetch, "get")
urlfetch.get(test_templ_name,
allowed_schemes=('http', 'https'))\
.AndReturn(wrong_template)
self.m.ReplayAll()
definition = rsrc_defn.ResourceDefinition('test_t_res',
'Test::Tmpl')
temp_res = template_resource.TemplateResource('test_t_res',
definition,
stack)
err = self.assertRaises(exception.StackValidationFailed,
temp_res.validate)
self.assertIn('Error parsing template: ', six.text_type(err))
self.m.VerifyAll()
class NestedProvider(HeatTestCase): class NestedProvider(HeatTestCase):
"""Prove that we can use the registry in a nested provider.""" """Prove that we can use the registry in a nested provider."""

View File

@ -139,7 +139,7 @@ class YamlParseExceptions(HeatTestCase):
('scanner', dict(raised_exception=yaml.scanner.ScannerError())), ('scanner', dict(raised_exception=yaml.scanner.ScannerError())),
('parser', dict(raised_exception=yaml.parser.ParserError())), ('parser', dict(raised_exception=yaml.parser.ParserError())),
('reader', ('reader',
dict(raised_exception=yaml.reader.ReaderError('', '', '', '', ''))), dict(raised_exception=yaml.reader.ReaderError('', 42, 'x', '', ''))),
] ]
def test_parse_to_value_exception(self): def test_parse_to_value_exception(self):
@ -148,9 +148,11 @@ class YamlParseExceptions(HeatTestCase):
with mock.patch.object(yaml, 'load') as yaml_loader: with mock.patch.object(yaml, 'load') as yaml_loader:
yaml_loader.side_effect = self.raised_exception yaml_loader.side_effect = self.raised_exception
self.assertRaises(ValueError, err = self.assertRaises(ValueError,
template_format.parse, text) template_format.parse, text)
self.assertIn('Error parsing template: ', six.text_type(err))
class JsonYamlResolvedCompareTest(HeatTestCase): class JsonYamlResolvedCompareTest(HeatTestCase):