summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--nova/api/openstack/compute/contrib/flavormanage.py2
-rw-r--r--nova/compute/flavors.py14
-rw-r--r--nova/tests/api/openstack/compute/contrib/test_flavor_manage.py15
3 files changed, 24 insertions, 7 deletions
diff --git a/nova/api/openstack/compute/contrib/flavormanage.py b/nova/api/openstack/compute/contrib/flavormanage.py
index 43d5d2110..441858c25 100644
--- a/nova/api/openstack/compute/contrib/flavormanage.py
+++ b/nova/api/openstack/compute/contrib/flavormanage.py
@@ -76,6 +76,8 @@ class FlavorManageController(wsgi.Controller):
except (exception.InstanceTypeExists,
exception.InstanceTypeIdExists) as err:
raise webob.exc.HTTPConflict(explanation=err.format_message())
+ except exception.InvalidInput as exc:
+ raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
return self._view_builder.show(req, flavor)
diff --git a/nova/compute/flavors.py b/nova/compute/flavors.py
index 59d5d5715..a18b375d8 100644
--- a/nova/compute/flavors.py
+++ b/nova/compute/flavors.py
@@ -95,20 +95,20 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb=0, flavorid=None,
# Some attributes are positive ( > 0) integers
for option in ['memory_mb', 'vcpus']:
try:
+ assert int(str(kwargs[option])) > 0
kwargs[option] = int(kwargs[option])
- assert kwargs[option] > 0
- except (ValueError, AssertionError):
- msg = _("'%s' argument must be greater than 0") % option
+ except (ValueError, AssertionError, TypeError):
+ msg = _("'%s' argument must be a positive integer") % option
raise exception.InvalidInput(reason=msg)
# Some attributes are non-negative ( >= 0) integers
for option in ['root_gb', 'ephemeral_gb', 'swap']:
try:
+ assert int(str(kwargs[option])) >= 0
kwargs[option] = int(kwargs[option])
- assert kwargs[option] >= 0
- except (ValueError, AssertionError):
- msg = _("'%s' argument must be greater than or equal"
- " to 0") % option
+ except (ValueError, AssertionError, TypeError):
+ msg = _("'%s' argument must be an integer greater than or"
+ " equal to 0") % option
raise exception.InvalidInput(reason=msg)
# rxtx_factor should be a positive float
diff --git a/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py b/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py
index df2c3d392..459dae932 100644
--- a/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py
+++ b/nova/tests/api/openstack/compute/contrib/test_flavor_manage.py
@@ -218,3 +218,18 @@ class FlavorManageTest(test.TestCase):
req.body = jsonutils.dumps(expected)
res = req.get_response(self.app)
self.assertEqual(res.status_int, 409)
+
+ def test_invalid_memory_mb(self):
+ """Check negative and decimal number can't be accepted."""
+
+ self.stubs.UnsetAll()
+ self.assertRaises(exception.InvalidInput, flavors.create, "abc",
+ -512, 2, 1, 1, 1234, 512, 1, True)
+ self.assertRaises(exception.InvalidInput, flavors.create, "abcd",
+ 512.2, 2, 1, 1, 1234, 512, 1, True)
+ self.assertRaises(exception.InvalidInput, flavors.create, "abcde",
+ None, 2, 1, 1, 1234, 512, 1, True)
+ self.assertRaises(exception.InvalidInput, flavors.create, "abcdef",
+ 512, 2, None, 1, 1234, 512, 1, True)
+ self.assertRaises(exception.InvalidInput, flavors.create, "abcdef",
+ "test_memory_mb", 2, None, 1, 1234, 512, 1, True)