When creating a storage volume using the REST API
(i.e. "POST /storagepools/<pool>/storagevolumes {'capacity': 5}",
the parameters 'capacity' and 'allocation' must be specified in megabytes
(actually, mebibytes). This approach is not consistent with the REST
command "GET /storagepools/<pool>/storagevolumes/<vol>", which
returns
the same values in bytes, and it also doesn't allow creating a volume
with a size not multiple of 1 MiB.
Update the REST command "POST /storagepools/<pool>/storagevolumes" so it
receives size parameters in bytes.
Signed-off-by: Crístian Viana <vianac(a)linux.vnet.ibm.com>
---
docs/API.md | 4 ++--
src/kimchi/model/storagevolumes.py | 5 ++---
tests/test_model.py | 19 +++++++++----------
tests/test_rest.py | 12 ++++++------
4 files changed, 19 insertions(+), 21 deletions(-)
diff --git a/docs/API.md b/docs/API.md
index 210a036..1d20f0b 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -475,7 +475,7 @@ A interface represents available network interface on VM.
* name: The name of the Storage Volume
* type: The type of the defined Storage Volume
* capacity: The total space which can be used to store volumes
- The unit is MBytes
+ The unit is bytes
* format: The format of the defined Storage Volume
* file: File to be uploaded, passed through form data
* url: URL to be downloaded
@@ -510,7 +510,7 @@ A interface represents available network interface on VM.
* resize: Resize a Storage Volume
* size: resize the total space which can be used to store data
- The unit is MBytes
+ The unit is bytes
* wipe: Wipe a Storage Volume
* clone: Clone a Storage Volume.
* pool: The name of the destination pool (optional).
diff --git a/src/kimchi/model/storagevolumes.py b/src/kimchi/model/storagevolumes.py
index 56c54fa..4bf029a 100644
--- a/src/kimchi/model/storagevolumes.py
+++ b/src/kimchi/model/storagevolumes.py
@@ -154,8 +154,8 @@ class StorageVolumesModel(object):
vol_xml = """
<volume>
<name>%(name)s</name>
- <allocation unit="MiB">%(allocation)s</allocation>
- <capacity unit="MiB">%(capacity)s</capacity>
+ <allocation unit='bytes'>%(allocation)s</allocation>
+ <capacity unit='bytes'>%(capacity)s</capacity>
<source>
</source>
<target>
@@ -319,7 +319,6 @@ class StorageVolumeModel(object):
{'name': name, 'err':
e.get_error_message()})
def resize(self, pool, name, size):
- size = size << 20
volume = StorageVolumeModel.get_storagevolume(pool, name, self.conn)
try:
volume.resize(size, 0)
diff --git a/tests/test_model.py b/tests/test_model.py
index 5984906..2c44893 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -186,8 +186,8 @@ class ModelTests(unittest.TestCase):
with RollbackContext() as rollback:
vol = 'base-vol.img'
params = {'name': vol,
- 'capacity': 1024,
- 'allocation': 1,
+ 'capacity': 1073741824, # 1 GiB
+ 'allocation': 1048576, # 1 MiB
'format': 'qcow2'}
task_id = inst.storagevolumes_create('default',
params)['id']
rollback.prependDefer(inst.storagevolume_delete, 'default', vol)
@@ -357,8 +357,8 @@ class ModelTests(unittest.TestCase):
rollback.prependDefer(inst.storagepool_deactivate, pool)
params = {'name': vol,
- 'capacity': 1024,
- 'allocation': 512,
+ 'capacity': 1073741824, # 1 GiB
+ 'allocation': 536870912, # 512 MiB
'format': 'qcow2'}
task_id = inst.storagevolumes_create(pool, params)['id']
rollback.prependDefer(inst.storagevolume_delete, pool, vol)
@@ -635,8 +635,8 @@ class ModelTests(unittest.TestCase):
vols = inst.storagevolumes_get_list(pool)
num = len(vols) + 2
- params = {'capacity': 1024,
- 'allocation': 512,
+ params = {'capacity': 1073741824, # 1 GiB
+ 'allocation': 536870912, # 512 MiB
'format': 'raw'}
# 'name' is required for this type of volume
self.assertRaises(InvalidParameter, inst.storagevolumes_create,
@@ -660,13 +660,12 @@ class ModelTests(unittest.TestCase):
self.assertEquals(0, volinfo['ref_cnt'])
volinfo = inst.storagevolume_lookup(pool, vol)
- # Define the size = capacity + 16M
- capacity = volinfo['capacity'] >> 20
- size = capacity + 16
+ # Define the size = capacity + 16 MiB
+ size = volinfo['capacity'] + 16777216
inst.storagevolume_resize(pool, vol, size)
volinfo = inst.storagevolume_lookup(pool, vol)
- self.assertEquals((1024 + 16) << 20, volinfo['capacity'])
+ self.assertEquals(size, volinfo['capacity'])
poolinfo = inst.storagepool_lookup(pool)
self.assertEquals(len(vols), poolinfo['nr_volumes'])
diff --git a/tests/test_rest.py b/tests/test_rest.py
index fa15ef6..e61cfcf 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -1189,8 +1189,8 @@ class RestTests(unittest.TestCase):
# Create a storage volume can only be successful for active pool
req = json.dumps({'name': 'test-volume',
- 'capacity': 1024,
- 'allocation': 512,
+ 'capacity': 1073741824, # 1 GiB
+ 'allocation': 536870912, # 512 MiB
'type': 'disk',
'format': 'raw'})
resp = self.request('/storagepools/pool-2/storagevolumes/',
@@ -1212,12 +1212,12 @@ class RestTests(unittest.TestCase):
self.assertEquals('raw', storagevolume['format'])
# Resize the storage volume
- req = json.dumps({'size': 768})
+ req = json.dumps({'size': 805306368}) # 768 MiB
uri = '/storagepools/pool-2/storagevolumes/test-volume/resize'
resp = self.request(uri, req, 'POST')
uri = '/storagepools/pool-2/storagevolumes/test-volume'
storagevolume = json.loads(self.request(uri).read())
- self.assertEquals(768 << 20, storagevolume['capacity'])
+ self.assertEquals(805306368, storagevolume['capacity']) # 768 MiB
# Wipe the storage volume
uri = '/storagepools/pool-2/storagevolumes/test-volume/wipe'
@@ -1489,7 +1489,7 @@ class RestTests(unittest.TestCase):
self._create_pool('pool-3')
self.request('/storagepools/pool-3/activate', '{}',
'POST')
params = {'name': 'fedora.iso',
- 'capacity': 1024,
+ 'capacity': 1073741824, # 1 GiB
'type': 'file',
'format': 'iso'}
model.storagevolumes_create('pool-3', params)
@@ -1500,7 +1500,7 @@ class RestTests(unittest.TestCase):
self.assertEquals('iso', storagevolume['format'])
self.assertEquals('/var/lib/libvirt/images/fedora.iso',
storagevolume['path'])
- self.assertEquals(1024 << 20, storagevolume['capacity'])
+ self.assertEquals(1073741824, storagevolume['capacity']) # 1 GiB
self.assertEquals(0, storagevolume['allocation'])
self.assertEquals('unknown', storagevolume['os_version'])
self.assertEquals('unknown', storagevolume['os_distro'])
--
2.1.0