From: Royce Lv <lvroyce(a)linux.vnet.ibm.com>
Add testcases to cover vmstorage create fails when
pool and path specified at the same time,
and vol not specified when pool is passed.
Signed-off-by: Royce Lv <lvroyce(a)linux.vnet.ibm.com>
---
tests/test_model.py | 11 +++++++++
tests/test_rest.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 76 insertions(+), 4 deletions(-)
diff --git a/tests/test_model.py b/tests/test_model.py
index ea38f59..89cadaa 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -184,6 +184,9 @@ class ModelTests(unittest.TestCase):
@unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
def test_vm_disk(self):
+ disk_path = '/tmp/existent2.iso'
+ open(disk_path, 'w').close()
+
def _attach_disk(bus_type=None):
disk_args = {"type": "disk",
"pool": pool,
@@ -253,6 +256,14 @@ class ModelTests(unittest.TestCase):
self.assertEquals(u'disk', disk_info['type'])
inst.vmstorage_delete(vm_name, disk)
+ # Specify pool and path at sametime will fail
+ disk_args = {"type": "disk",
+ "bus": "virtio",
+ "pool": pool,
+ "vol": vol,
+ "path": disk_path}
+ self.assertRaises(
+ InvalidParameter, inst.vmstorages_create, vm_name, disk_args)
# Hot plug 'ide' bus disk does not work
inst.vm_start(vm_name)
self.assertRaises(InvalidOperation, _attach_disk, 'ide')
diff --git a/tests/test_rest.py b/tests/test_rest.py
index 18ba66e..2c57ecd 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -457,15 +457,74 @@ class RestTests(unittest.TestCase):
resp = self.request('/vms/test-vm/storages', req, 'POST')
self.assertEquals(400, resp.status)
+ # Create temp storage pool
+ req = json.dumps({'name': 'tmp',
+ 'capacity': 1024,
+ 'allocated': 512,
+ 'path': '/tmp',
+ 'type': 'dir'})
+ resp = self.request('/storagepools', req, 'POST')
+ self.assertEquals(201, resp.status)
+ resp = self.request('/storagepools/tmp/activate', req,
'POST')
+ self.assertEquals(200, resp.status)
+
+ req = json.dumps({'name': "attach-volume",
+ 'capacity': 1024,
+ 'allocation': 512,
+ 'type': 'disk',
+ 'format': 'raw'})
+ resp = self.request('/storagepools/tmp/storagevolumes',
+ req, 'POST')
+ self.assertEquals(201, resp.status)
+
+ # Attach cdrom with both path and volume specified
+ open('/tmp/mock.iso', 'w').close()
+ req = json.dumps({'dev': 'hdx',
+ 'type': 'cdrom',
+ 'pool': 'tmp',
+ 'vol': 'attach-volume',
+ 'path': '/tmp/mock.iso'})
+ resp = self.request('/vms/test-vm/storages', req, 'POST')
+ self.assertEquals(400, resp.status)
+
+ # Attach disk with both path and volume specified
+ req = json.dumps({'dev': 'hdx',
+ 'type': 'disk',
+ 'pool': 'tmp',
+ 'vol': 'attach-volume',
+ 'path': '/tmp/mock.iso'})
+ resp = self.request('/vms/test-vm/storages', req, 'POST')
+ self.assertEquals(400, resp.status)
+
+ # Attach disk with only pool specified
+ req = json.dumps({'dev': 'hdx',
+ 'type': 'cdrom',
+ 'pool': 'tmp'})
+ resp = self.request('/vms/test-vm/storages', req, 'POST')
+ self.assertEquals(400, resp.status)
+
+ # Attach disk with pool and vol specified
+ req = json.dumps({'dev': 'hdx',
+ 'type': 'disk',
+ 'pool': 'tmp',
+ 'vol': 'attach-volume'})
+ resp = self.request('/vms/test-vm/storages', req, 'POST')
+ self.assertEquals(201, resp.status)
+ cd_info = json.loads(resp.read())
+ self.assertEquals('hdx', cd_info['dev'])
+ self.assertEquals('disk', cd_info['type'])
+ self.assertEquals('tmp', cd_info['pool'])
+ self.assertEquals('attach-volume', cd_info['vol'])
+
# Attach a cdrom with existent dev name
open('/tmp/existent.iso', 'w').close()
- req = json.dumps({'dev': 'hdx',
+ req = json.dumps({'dev': 'hdk',
'type': 'cdrom',
'path': '/tmp/existent.iso'})
resp = self.request('/vms/test-vm/storages', req, 'POST')
self.assertEquals(201, resp.status)
cd_info = json.loads(resp.read())
- self.assertEquals('hdx', cd_info['dev'])
+ self.assertEquals('hdk', cd_info['dev'])
self.assertEquals('cdrom', cd_info['type'])
self.assertEquals('/tmp/existent.iso', cd_info['path'])
# Delete the file and cdrom
@@ -482,7 +541,7 @@ class RestTests(unittest.TestCase):
# Test GET
devs = json.loads(self.request('/vms/test-vm/storages').read())
- self.assertEquals(3, len(devs))
+ self.assertEquals(4, len(devs))
# Detach storage cdrom
resp = self.request('/vms/test-vm/storages/hdx', '{}',
'DELETE')
@@ -490,7 +549,9 @@ class RestTests(unittest.TestCase):
# Test GET
devs = json.loads(self.request('/vms/test-vm/storages').read())
- self.assertEquals(2, len(devs))
+ self.assertEquals(3, len(devs))
+ resp = self.request('/storagepools/tmp', {}, 'DELETE')
+ self.assertEquals(204, resp.status)
def test_vm_iface(self):
--
1.8.3.2