Check for empty sections in environment

Passing empty section like parameters in the environment can leave the
user with a strange error message. This fixes the issue by providing a
correct error.

Change-Id: I766cd38e8a70c9f76cf10f540e22a929eaed3768
Closes-Bug: #1606148
This commit is contained in:
Thomas Herve 2016-07-25 10:39:26 +02:00
parent 7538c6d888
commit 9a248ad6c5
3 changed files with 16 additions and 5 deletions

View File

@ -138,10 +138,10 @@ class InstantiationData(object):
env = {}
if self.PARAM_ENVIRONMENT in self.data:
env_data = self.data[self.PARAM_ENVIRONMENT]
if isinstance(env_data, dict):
env = env_data
else:
with self.parse_error_check('Environment'):
with self.parse_error_check('Environment'):
if isinstance(env_data, dict):
env = environment_format.validate(env_data)
else:
env = environment_format.parse(env_data)
environment_format.default_for_missing(env)

View File

@ -1,4 +1,3 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
@ -48,9 +47,15 @@ def parse(env_str):
if not isinstance(env, dict):
raise ValueError(_('The environment is not a valid '
'YAML mapping data type.'))
return validate(env)
def validate(env):
for param in env:
if param not in SECTIONS:
raise ValueError(_('environment has wrong section "%s"') % param)
if env[param] is None:
raise ValueError(_('environment has empty section "%s"') % param)
return env

View File

@ -195,6 +195,12 @@ blarg: wibble
data = stacks.InstantiationData(body)
self.assertEqual(expect, data.environment())
def test_environment_empty_params(self):
env = {'parameters': None}
body = {'environment': env}
data = stacks.InstantiationData(body)
self.assertRaises(webob.exc.HTTPBadRequest, data.environment)
def test_environment_bad_format(self):
env = {'somethingnotsupported': {'blarg': 'wibble'}}
body = {'environment': json.dumps(env)}