Merge "Catch all yaml exceptions"

This commit is contained in:
Jenkins 2013-10-21 11:43:20 +00:00 committed by Gerrit Code Review
commit 999066cb8e
4 changed files with 56 additions and 6 deletions

View File

@ -29,8 +29,8 @@ def parse(env_str):
''' '''
try: try:
env = yaml.load(env_str, Loader=yaml_loader) env = yaml.load(env_str, Loader=yaml_loader)
except (yaml.scanner.ScannerError, yaml.parser.ParserError) as e: except yaml.YAMLError as yea:
raise ValueError(e) raise ValueError(yea)
else: else:
if env is None: if env is None:
env = {} env = {}

View File

@ -66,8 +66,8 @@ def parse(tmpl_str):
else: else:
try: try:
tpl = yaml.load(tmpl_str, Loader=yaml_loader) tpl = yaml.load(tmpl_str, Loader=yaml_loader)
except (yaml.scanner.ScannerError, yaml.parser.ParserError) as e: except yaml.YAMLError as yea:
raise ValueError(e) raise ValueError(yea)
else: else:
if tpl is None: if tpl is None:
tpl = {} tpl = {}

View File

@ -12,10 +12,17 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock
import testscenarios
import yaml
from heat.common import environment_format from heat.common import environment_format
from heat.tests import common from heat.tests import common
load_tests = testscenarios.load_tests_apply_scenarios
class YamlEnvironmentTest(common.HeatTestCase): class YamlEnvironmentTest(common.HeatTestCase):
def test_minimal_yaml(self): def test_minimal_yaml(self):
@ -41,3 +48,22 @@ resource_regis: {}
parameters: } parameters: }
''' '''
self.assertRaises(ValueError, environment_format.parse, env) 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)

View File

@ -12,8 +12,10 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from testtools import skipIf import mock
import os import os
import testtools
import testscenarios
import yaml import yaml
from heat.engine import clients from heat.engine import clients
@ -23,6 +25,8 @@ from heat.common import template_format
from heat.tests.common import HeatTestCase from heat.tests.common import HeatTestCase
from heat.tests import utils from heat.tests import utils
load_tests = testscenarios.load_tests_apply_scenarios
class JsonToYamlTest(HeatTestCase): class JsonToYamlTest(HeatTestCase):
@ -105,6 +109,25 @@ Outputs: {}
self.assertEqual(msg, str(ex)) 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): class JsonYamlResolvedCompareTest(HeatTestCase):
def setUp(self): def setUp(self):
@ -146,7 +169,8 @@ class JsonYamlResolvedCompareTest(HeatTestCase):
for key in stack1: for key in stack1:
self.assertEqual(stack1[key].t, stack2[key].t) 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): def test_neutron_resolved(self):
self.compare_stacks('Neutron.template', 'Neutron.yaml', {}) self.compare_stacks('Neutron.template', 'Neutron.yaml', {})