summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Prince <dprince@redhat.com>2012-11-27 10:28:01 -0500
committerDan Prince <dprince@redhat.com>2012-11-27 11:12:41 -0500
commit47ddb8ca76fba11a6e77b12e9eeb61b5a9c78c48 (patch)
treee0c083e1acd656fc4627e55b081aaa66171f7be5
parente4f1a70c37352730a4fa1b7466e526e1f3b168ed (diff)
Validate rxtx_factor as a float.
Updates the rxtx_factor validations in the instance_types modules so that rxtx_factor is validated as a float. Given that rxtx_factor is stored as a float in the database this makes sense... and also adheres to some of the extension documentation as well (although some of the extension docs were incorrect as well). Previously rxtx_factor was being cast into an int which caused it to be stored and displayed incorrectly via the API. This patchset adds a test which fails with the old code. Additionally some of the API docs are corrected so that rxtx_factor is listed as a float in all examples. Fixes LP Bug #1081287. Change-Id: Iae98522a1f2ed63cbd2497b0b0af5ac2d9bb935c
-rwxr-xr-xbin/nova-manage2
-rw-r--r--doc/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.json4
-rw-r--r--doc/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.xml4
-rw-r--r--doc/api_samples/all_extensions/flavor-get-resp.json2
-rw-r--r--nova/compute/instance_types.py16
-rw-r--r--nova/tests/integrated/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.json.tpl4
-rw-r--r--nova/tests/integrated/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.xml.tpl4
-rw-r--r--nova/tests/test_instance_types.py19
8 files changed, 38 insertions, 17 deletions
diff --git a/bin/nova-manage b/bin/nova-manage
index 23a76c5d3..6e90e2e00 100755
--- a/bin/nova-manage
+++ b/bin/nova-manage
@@ -808,7 +808,7 @@ class InstanceTypeCommands(object):
@args('--is_public', dest="is_public", metavar='<is_public>',
help='Make flavor accessible to the public')
def create(self, name, memory, vcpus, root_gb, ephemeral_gb=0,
- flavorid=None, swap=0, rxtx_factor=1, is_public=True):
+ flavorid=None, swap=0, rxtx_factor=1.0, is_public=True):
"""Creates instance types / flavors"""
try:
instance_types.create(name, memory, vcpus, root_gb,
diff --git a/doc/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.json b/doc/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.json
index b0e481a62..0a88eb248 100644
--- a/doc/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.json
+++ b/doc/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.json
@@ -5,8 +5,8 @@
"id": "666",
"name": "flavortest",
"ram": 1024,
- "rxtx_factor": 2,
+ "rxtx_factor": 2.0,
"swap": 5,
"vcpus": 2
}
-} \ No newline at end of file
+}
diff --git a/doc/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.xml b/doc/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.xml
index ec1ec2e2b..cc2d05eed 100644
--- a/doc/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.xml
+++ b/doc/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.xml
@@ -7,5 +7,5 @@
disk="10"
id="666"
swap="5"
- rxtx_factor="2"
- OS-FLV-EXT-DATA:ephemeral="30" /> \ No newline at end of file
+ rxtx_factor="2.0"
+ OS-FLV-EXT-DATA:ephemeral="30" />
diff --git a/doc/api_samples/all_extensions/flavor-get-resp.json b/doc/api_samples/all_extensions/flavor-get-resp.json
index bbd681e66..d37117d84 100644
--- a/doc/api_samples/all_extensions/flavor-get-resp.json
+++ b/doc/api_samples/all_extensions/flavor-get-resp.json
@@ -21,4 +21,4 @@
"swap": "",
"vcpus": 1
}
-} \ No newline at end of file
+}
diff --git a/nova/compute/instance_types.py b/nova/compute/instance_types.py
index 906a5b4f9..6c4e9293c 100644
--- a/nova/compute/instance_types.py
+++ b/nova/compute/instance_types.py
@@ -46,7 +46,7 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb=None, flavorid=None,
if swap is None:
swap = 0
if rxtx_factor is None:
- rxtx_factor = 1
+ rxtx_factor = 1.0
if ephemeral_gb is None:
ephemeral_gb = 0
@@ -66,20 +66,28 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb=None, flavorid=None,
raise exception.InvalidInput(reason=msg)
# ensure some attributes are integers and greater than or equal to 0
- for option in kwargs:
+ for option in ['memory_mb', 'vcpus', 'root_gb', 'ephemeral_gb', 'swap']:
try:
kwargs[option] = int(kwargs[option])
assert kwargs[option] >= 0
except (ValueError, AssertionError):
- msg = _("create arguments must be positive integers")
+ msg = _("'%s' argument must be a positive integer") % option
raise exception.InvalidInput(reason=msg)
+ # rxtx_factor should be a positive float
+ try:
+ kwargs['rxtx_factor'] = float(kwargs['rxtx_factor'])
+ assert kwargs['rxtx_factor'] > 0
+ except (ValueError, AssertionError):
+ msg = _("'rxtx_factor' argument must be a positive float")
+ raise exception.InvalidInput(reason=msg)
+
# some value are required to be nonzero, not just positive
for option in ['memory_mb', 'vcpus']:
try:
assert kwargs[option] > 0
except AssertionError:
- msg = _("create arguments must be positive integers")
+ msg = _("'%s' argument must be greater than 0") % option
raise exception.InvalidInput(reason=msg)
kwargs['name'] = name
diff --git a/nova/tests/integrated/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.json.tpl b/nova/tests/integrated/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.json.tpl
index b0e481a62..0a88eb248 100644
--- a/nova/tests/integrated/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.json.tpl
+++ b/nova/tests/integrated/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.json.tpl
@@ -5,8 +5,8 @@
"id": "666",
"name": "flavortest",
"ram": 1024,
- "rxtx_factor": 2,
+ "rxtx_factor": 2.0,
"swap": 5,
"vcpus": 2
}
-} \ No newline at end of file
+}
diff --git a/nova/tests/integrated/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.xml.tpl b/nova/tests/integrated/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.xml.tpl
index ec1ec2e2b..cc2d05eed 100644
--- a/nova/tests/integrated/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.xml.tpl
+++ b/nova/tests/integrated/api_samples/OS-FLV-EXT-DATA/flavors-extra-data-post-req.xml.tpl
@@ -7,5 +7,5 @@
disk="10"
id="666"
swap="5"
- rxtx_factor="2"
- OS-FLV-EXT-DATA:ephemeral="30" /> \ No newline at end of file
+ rxtx_factor="2.0"
+ OS-FLV-EXT-DATA:ephemeral="30" />
diff --git a/nova/tests/test_instance_types.py b/nova/tests/test_instance_types.py
index db44778f1..51576a8ea 100644
--- a/nova/tests/test_instance_types.py
+++ b/nova/tests/test_instance_types.py
@@ -71,7 +71,7 @@ class InstanceTypeTestCase(test.TestCase):
self.assertEqual(inst_type['root_gb'], 120)
self.assertEqual(inst_type['ephemeral_gb'], 0)
self.assertEqual(inst_type['swap'], 0)
- self.assertEqual(inst_type['rxtx_factor'], 1)
+ self.assertEqual(inst_type['rxtx_factor'], 1.0)
# make sure new type shows up in list
new_list = instance_types.get_all_types()
@@ -95,7 +95,7 @@ class InstanceTypeTestCase(test.TestCase):
self.assertEqual(inst_type['root_gb'], 120)
self.assertEqual(inst_type['ephemeral_gb'], 100)
self.assertEqual(inst_type['swap'], 0)
- self.assertEqual(inst_type['rxtx_factor'], 1)
+ self.assertEqual(inst_type['rxtx_factor'], 1.0)
# make sure new type shows up in list
new_list = instance_types.get_all_types()
@@ -120,7 +120,20 @@ class InstanceTypeTestCase(test.TestCase):
self.assertEqual(inst_type['root_gb'], 120)
self.assertEqual(inst_type['ephemeral_gb'], 100)
self.assertEqual(inst_type['swap'], 0)
- self.assertEqual(inst_type['rxtx_factor'], 1)
+ self.assertEqual(inst_type['rxtx_factor'], 1.0)
+
+ def test_instance_type_create_with_custom_rxtx_factor(self):
+ name = 'Custom RXTX Factor'
+ inst_type = instance_types.create(name, 256, 1, 120, 100,
+ rxtx_factor=9.9)
+ self.assertNotEqual(inst_type['flavorid'], None)
+ self.assertEqual(inst_type['name'], name)
+ self.assertEqual(inst_type['memory_mb'], 256)
+ self.assertEqual(inst_type['vcpus'], 1)
+ self.assertEqual(inst_type['root_gb'], 120)
+ self.assertEqual(inst_type['ephemeral_gb'], 100)
+ self.assertEqual(inst_type['swap'], 0)
+ self.assertEqual(inst_type['rxtx_factor'], 9.9)
def test_instance_type_create_with_special_characters(self):
"""Ensure instance types raises InvalidInput for invalid characters"""