diff --git a/heat/common/environment_format.py b/heat/common/environment_format.py index a8446fd798..694aa0f8e6 100644 --- a/heat/common/environment_format.py +++ b/heat/common/environment_format.py @@ -29,8 +29,8 @@ def parse(env_str): ''' try: env = yaml.load(env_str, Loader=yaml_loader) - except (yaml.scanner.ScannerError, yaml.parser.ParserError) as e: - raise ValueError(e) + except yaml.YAMLError as yea: + raise ValueError(yea) else: if env is None: env = {} diff --git a/heat/common/template_format.py b/heat/common/template_format.py index 1e74b4a98f..4cacd59340 100644 --- a/heat/common/template_format.py +++ b/heat/common/template_format.py @@ -66,8 +66,8 @@ def parse(tmpl_str): else: try: tpl = yaml.load(tmpl_str, Loader=yaml_loader) - except (yaml.scanner.ScannerError, yaml.parser.ParserError) as e: - raise ValueError(e) + except yaml.YAMLError as yea: + raise ValueError(yea) else: if tpl is None: tpl = {} diff --git a/heat/tests/test_environment_format.py b/heat/tests/test_environment_format.py index 8a6e95738e..6f75faf955 100644 --- a/heat/tests/test_environment_format.py +++ b/heat/tests/test_environment_format.py @@ -12,10 +12,17 @@ # License for the specific language governing permissions and limitations # under the License. +import mock +import testscenarios +import yaml + from heat.common import environment_format from heat.tests import common +load_tests = testscenarios.load_tests_apply_scenarios + + class YamlEnvironmentTest(common.HeatTestCase): def test_minimal_yaml(self): @@ -41,3 +48,22 @@ resource_regis: {} parameters: } ''' self.assertRaises(ValueError, environment_format.parse, env) + + +class YamlParseExceptions(common.HeatTestCase): + + scenarios = [ + ('scanner', dict(raised_exception=yaml.scanner.ScannerError())), + ('parser', dict(raised_exception=yaml.parser.ParserError())), + ('reader', + dict(raised_exception=yaml.reader.ReaderError('', '', '', '', ''))), + ] + + def test_parse_to_value_exception(self): + text = 'not important' + + with mock.patch.object(yaml, 'load') as yaml_loader: + yaml_loader.side_effect = self.raised_exception + + self.assertRaises(ValueError, + environment_format.parse, text) diff --git a/heat/tests/test_template_format.py b/heat/tests/test_template_format.py index 7477aefe0d..750246b646 100644 --- a/heat/tests/test_template_format.py +++ b/heat/tests/test_template_format.py @@ -12,8 +12,10 @@ # License for the specific language governing permissions and limitations # under the License. -from testtools import skipIf +import mock import os +import testtools +import testscenarios import yaml from heat.engine import clients @@ -23,6 +25,8 @@ from heat.common import template_format from heat.tests.common import HeatTestCase from heat.tests import utils +load_tests = testscenarios.load_tests_apply_scenarios + class JsonToYamlTest(HeatTestCase): @@ -105,6 +109,25 @@ Outputs: {} self.assertEqual(msg, str(ex)) +class YamlParseExceptions(HeatTestCase): + + scenarios = [ + ('scanner', dict(raised_exception=yaml.scanner.ScannerError())), + ('parser', dict(raised_exception=yaml.parser.ParserError())), + ('reader', + dict(raised_exception=yaml.reader.ReaderError('', '', '', '', ''))), + ] + + def test_parse_to_value_exception(self): + text = 'not important' + + with mock.patch.object(yaml, 'load') as yaml_loader: + yaml_loader.side_effect = self.raised_exception + + self.assertRaises(ValueError, + template_format.parse, text) + + class JsonYamlResolvedCompareTest(HeatTestCase): def setUp(self): @@ -146,7 +169,8 @@ class JsonYamlResolvedCompareTest(HeatTestCase): for key in stack1: self.assertEqual(stack1[key].t, stack2[key].t) - @skipIf(clients.neutronclient is None, 'neutronclient unavailable') + @testtools.skipIf(clients.neutronclient is None, + 'neutronclient unavailable') def test_neutron_resolved(self): self.compare_stacks('Neutron.template', 'Neutron.yaml', {})