Merge "Show better error when json fail to parse template"

This commit is contained in:
Jenkins 2013-12-20 05:07:21 +00:00 committed by Gerrit Code Review
commit e9897ff9b9
2 changed files with 26 additions and 3 deletions

View File

@ -11,7 +11,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import httplib2
import os
import re
import six
@ -19,6 +18,7 @@ import sys
import yaml
import fixtures
import httplib2
import tempfile
import testscenarios
import testtools
@ -904,6 +904,21 @@ class ShellTestStandaloneToken(ShellTestUserPass):
# The StanaloneMode shouldn't need any keystoneclient stubbing
pass
def test_bad_template_file(self):
failed_msg = 'Cannot parse template file:'
with tempfile.NamedTemporaryFile() as bad_json_file:
bad_json_file.write("{foo:}")
bad_json_file.flush()
self.shell_error("stack-create ts -f %s" % bad_json_file.name,
failed_msg)
with tempfile.NamedTemporaryFile() as bad_json_file:
bad_json_file.write('{"foo": None}')
bad_json_file.flush()
self.shell_error("stack-create ts -f %s" % bad_json_file.name,
failed_msg)
class ShellEnvironmentTest(TestCase):

View File

@ -28,7 +28,11 @@ def _set_template_fields(hc, args, fields):
if args.template_file:
tpl = open(args.template_file).read()
if tpl.startswith('{'):
fields['template'] = jsonutils.loads(tpl)
try:
fields['template'] = jsonutils.loads(tpl)
except ValueError as e:
raise exc.CommandError(
"Cannot parse template file: %s" % e)
else:
fields['template'] = tpl
elif args.template_url:
@ -36,7 +40,11 @@ def _set_template_fields(hc, args, fields):
elif args.template_object:
template_body = hc.http_client.raw_request('GET', args.template_object)
if template_body:
fields['template'] = jsonutils.loads(template_body)
try:
fields['template'] = jsonutils.loads(template_body)
except ValueError as e:
raise exc.CommandError(
"Cannot parse template file: %s" % e)
else:
raise exc.CommandError('Could not fetch template from %s'
% args.template_object)