From ce02d917f745da52764c85dd02a48c539eb62782 Mon Sep 17 00:00:00 2001 From: huangtianhua Date: Wed, 29 Jan 2014 15:25:14 +0800 Subject: [PATCH] Fixes environment file using correct YAML format An internal unexpected exception is raised while create a stack with an environment file which only has string content. We should handle the case and raise the correct error message such as "environment file is not Yaml format". Change-Id: I4e124db6a8dca9cf811f151ae95058cdaae16a01 Closes-Bug: #1273974 --- heatclient/common/environment_format.py | 5 ++++- heatclient/tests/test_environment_format.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/heatclient/common/environment_format.py b/heatclient/common/environment_format.py index 136c0aef..d3c002e7 100644 --- a/heatclient/common/environment_format.py +++ b/heatclient/common/environment_format.py @@ -26,7 +26,7 @@ def parse(env_str): '''Takes a string and returns a dict containing the parsed structure. This includes determination of whether the string is using the - JSON or YAML format. + YAML format. ''' try: env = yaml.load(env_str, Loader=yaml_loader) @@ -35,6 +35,9 @@ def parse(env_str): else: if env is None: env = {} + elif not isinstance(env, dict): + raise ValueError('The environment is not a valid ' + 'YAML mapping data type.') for param in env: if param not in SECTIONS: diff --git a/heatclient/tests/test_environment_format.py b/heatclient/tests/test_environment_format.py index 0df52d15..bc694d4a 100644 --- a/heatclient/tests/test_environment_format.py +++ b/heatclient/tests/test_environment_format.py @@ -49,6 +49,18 @@ parameters: } ''' self.assertRaises(ValueError, environment_format.parse, env) + def test_parse_string_environment(self): + env = 'just string' + expect = 'The environment is not a valid YAML mapping data type.' + msg = self.assertRaises(ValueError, environment_format.parse, env) + self.assertIn(expect, msg) + + def test_parse_document(self): + env = '["foo" , "bar"]' + expect = 'The environment is not a valid YAML mapping data type.' + msg = self.assertRaises(ValueError, environment_format.parse, env) + self.assertIn(expect, msg) + class YamlParseExceptions(testtools.TestCase):