From 47cd5eb2a591e075d403453b73c93c0b5360aada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kami=C5=84ski?= Date: Thu, 10 Apr 2014 10:10:14 +0200 Subject: [PATCH] Dedicated exception for PR malformed requirements Create dedicated manager exception MalformedRequirements returns HTTP 400 Malformed requirements %(rqrms)s when the requirements of reservation are malformed Added small bugfix allow to create reservation with one character value of propety Closes-Bug: #1305048 Change-Id: I5e5fa012dd9556182eac8def0124fa683f9b677c --- climate/manager/exceptions.py | 5 +++++ climate/plugins/oshosts/host_plugin.py | 10 ++++++--- .../plugins/test_physical_host_plugin.py | 21 ++++++++++++++----- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/climate/manager/exceptions.py b/climate/manager/exceptions.py index b9c2e00e..cfae0ae6 100644 --- a/climate/manager/exceptions.py +++ b/climate/manager/exceptions.py @@ -123,3 +123,8 @@ class HypervisorNotFound(exceptions.ClimateException): class NotEnoughHostsAvailable(exceptions.ClimateException): msg_fmt = _("Not enough hosts available") + + +class MalformedRequirements(exceptions.ClimateException): + code = 400 + msg_fmt = _("Malformed requirements %(rqrms)s") diff --git a/climate/plugins/oshosts/host_plugin.py b/climate/plugins/oshosts/host_plugin.py index cb969e4e..c0e23197 100644 --- a/climate/plugins/oshosts/host_plugin.py +++ b/climate/plugins/oshosts/host_plugin.py @@ -382,7 +382,11 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper): # TODO(frossigneux) Support the "or" operator # Convert text to json if isinstance(requirements, six.string_types): - requirements = json.loads(requirements) + try: + requirements = json.loads(requirements) + except ValueError: + raise manager_ex.MalformedRequirements(rqrms=requirements) + # Requirement list looks like ['<', '$ram', '1024'] if self._requirements_with_three_elements(requirements): result = [] @@ -400,7 +404,7 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper): elif isinstance(requirements, list) and not requirements: return requirements else: - raise RuntimeError('Malformed requirements') + raise manager_ex.MalformedRequirements(rqrms=requirements) def _requirements_with_three_elements(self, requirements): """Return true if requirement list looks like ['<', '$ram', '1024'].""" @@ -411,7 +415,7 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper): isinstance(requirements[2], six.string_types) and requirements[0] in ['==', '=', '!=', '>=', '<=', '>', '<'] and len(requirements[1]) > 1 and requirements[1][0] == '$' and - len(requirements[2]) > 1) + len(requirements[2]) > 0) def _requirements_with_and_keyword(self, requirements): return (len(requirements) > 1 and diff --git a/climate/tests/plugins/test_physical_host_plugin.py b/climate/tests/plugins/test_physical_host_plugin.py index e9ad2921..36313cd4 100644 --- a/climate/tests/plugins/test_physical_host_plugin.py +++ b/climate/tests/plugins/test_physical_host_plugin.py @@ -807,17 +807,20 @@ class PhysicalHostPluginTestCase(tests.TestCase): def test_convert_requirements_with_incorrect_syntax_1(self): self.assertRaises( - RuntimeError, self.fake_phys_plugin._convert_requirements, + manager_exceptions.MalformedRequirements, + self.fake_phys_plugin._convert_requirements, '["a", "$memory", "4096"]') def test_convert_requirements_with_incorrect_syntax_2(self): self.assertRaises( - RuntimeError, self.fake_phys_plugin._convert_requirements, + manager_exceptions.MalformedRequirements, + self.fake_phys_plugin._convert_requirements, '["=", "memory", "4096"]') def test_convert_requirements_with_incorrect_syntax_3(self): self.assertRaises( - RuntimeError, self.fake_phys_plugin._convert_requirements, + manager_exceptions.MalformedRequirements, + self.fake_phys_plugin._convert_requirements, '["=", "$memory", 4096]') def test_convert_requirements_complex(self): @@ -827,10 +830,18 @@ class PhysicalHostPluginTestCase(tests.TestCase): def test_convert_requirements_complex_with_incorrect_syntax_1(self): self.assertRaises( - RuntimeError, self.fake_phys_plugin._convert_requirements, + manager_exceptions.MalformedRequirements, + self.fake_phys_plugin._convert_requirements, '["and", [">", "memory", "4096"], [">", "$disk", "40"]]') def test_convert_requirements_complex_with_incorrect_syntax_2(self): self.assertRaises( - RuntimeError, self.fake_phys_plugin._convert_requirements, + manager_exceptions.MalformedRequirements, + self.fake_phys_plugin._convert_requirements, '["fail", [">", "$memory", "4096"], [">", "$disk", "40"]]') + + def test_convert_requirements_complex_with_not_json_value(self): + self.assertRaises( + manager_exceptions.MalformedRequirements, + self.fake_phys_plugin._convert_requirements, + 'something')