Make resource creation return 400 with empty manifest

When resource pod/rc/service is created with no
manifest, 500 status is returned to user due to
unhandled exception. This patch make this resource
creation return 400 status to user when no manifest
is passed. Also added test for empty manifest.

Change-Id: I7f6edc5bc7f8d46ea0eb0b71e22d2b88b3482154
Closes-bug:#1429767
Closes-bug:#1430064
This commit is contained in:
Madhuri Kumari 2015-03-10 05:56:36 +00:00
parent 255428cc8e
commit 694499d70e
5 changed files with 21 additions and 2 deletions

View File

@ -72,6 +72,8 @@ class K8sResourceBase(base.APIBase):
"""Labels of this k8s resource"""
def _get_manifest(self):
if not self.manifest and not self.manifest_url:
return None
if self.manifest is not wsme.Unset and self.manifest is not None:
return self.manifest
if (self.manifest_url is not wsme.Unset

View File

@ -114,8 +114,6 @@ class Service(v1_base.K8sResourceBase):
return cls._convert_with_links(sample, 'http://localhost:9511', expand)
def parse_manifest(self):
if not self.manifest and not self.manifest_url:
raise exception.InvalidParameterValue("'manifest' can't be empty")
try:
manifest = k8s_manifest.parse(self._get_manifest())
except ValueError as e:

View File

@ -48,6 +48,9 @@ def parse(manifest_str):
This includes determination of whether the string is using the
JSON or YAML format.
'''
if not manifest_str:
msg = _("'manifest' can't be empty")
raise ValueError(msg)
if manifest_str.startswith('{'):
manifest = json.loads(manifest_str)
else:

View File

@ -355,6 +355,14 @@ class TestPost(api_base.FunctionalTest):
self.assertEqual(400, response.status_int)
self.assertTrue(response.json['error_message'])
def test_create_pod_no_manifest(self):
pdict = apiutils.pod_post_data()
del pdict['manifest']
response = self.post_json('/pods', pdict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(400, response.status_int)
self.assertTrue(response.json['error_message'])
class TestDelete(api_base.FunctionalTest):

View File

@ -331,6 +331,14 @@ class TestPost(api_base.FunctionalTest):
self.assertEqual(400, response.status_int)
self.assertTrue(response.json['error_message'])
def test_create_rc_no_manifest(self):
rc_dict = apiutils.rc_post_data()
del rc_dict['manifest']
response = self.post_json('/rcs', rc_dict, expect_errors=True)
self.assertEqual('application/json', response.content_type)
self.assertEqual(400, response.status_int)
self.assertTrue(response.json['error_message'])
class TestDelete(api_base.FunctionalTest):