Catch all yaml exceptions

Instead of catching 2 specific exceptions, rather catch the generic
YAMLError as the documentation suggests:
http://pyyaml.org/wiki/PyYAMLDocumentation#YAMLError

Closes-bug: #1242155
Change-Id: I840d5eccafcc576d7a0022d81e5bc50af2d77803
This commit is contained in:
Angus Salkeld 2013-10-19 10:43:40 -10:00
parent 9bf6aec99a
commit 424ca9b632
4 changed files with 56 additions and 6 deletions

View File

@ -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 = {}

View File

@ -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 = {}

View File

@ -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)

View File

@ -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', {})