[Kimchi-devel] [PATCH v4 5/6] CDROM Management: changes in tests/test_model.py

Daniel Barboza danielhb at linux.vnet.ibm.com
Thu Feb 13 18:47:47 UTC 2014


From: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>

Adding model tests to verify the proper behavior of
CDROM related model functions - add, remove and update.
Changed mockmodel functions to use the new exception model.

Signed-off-by: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>
---
 src/kimchi/mockmodel.py | 21 ++++++++-----------
 tests/test_model.py     | 56 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 12 deletions(-)

diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
index 934f66a..e4b94a4 100644
--- a/src/kimchi/mockmodel.py
+++ b/src/kimchi/mockmodel.py
@@ -542,15 +542,15 @@ class MockModel(object):
     def storages_create(self, vm_name, params):
         path = params.get('path')
         if path.startswith('/') and not os.path.exists(path):
-            msg = "Path specified for device is not valid"
-            raise InvalidParameter(msg)
+            raise InvalidParameter("KCHCDROM0005E")
 
         dom = self._get_vm(vm_name)
         dev = params.get('dev', None)
         if dev and dev in self.storages_get_list(vm_name):
-            return OperationFailed('Device name already in use.')
+            return OperationFailed("KCHCDROM0004E", {'dev_name': dev,
+                                                     'vm_name': vm_name})
         if not dev:
-            return OperationFailed('Must specify a device name.')
+            return OperationFailed("KCHCDROM0008E")
         vmdev = MockVMStorageDevice(params)
         dom.storagedevices[params['dev']] = vmdev
         return params['dev']
@@ -562,17 +562,15 @@ class MockModel(object):
     def storage_lookup(self, vm_name, dev_name):
         dom = self._get_vm(vm_name)
         if dev_name not in self.storages_get_list(vm_name):
-            msg = 'The storage device "%s" does not exist in the guest "%s"' \
-                  % (dev_name,vm_name)
-            raise NotFoundError(msg)
+            raise NotFoundError("KCHCDROM0007E", {'dev_name': dev_name,
+                                                  'vm_name': vm_name})
         return dom.storagedevices.get('dev_name').info
 
     def storage_delete(self, vm_name, dev_name):
         dom = self._get_vm(vm_name)
         if dev_name not in self.storages_get_list(vm_name):
-            msg = 'The storage device "%s" does not exist in the guest "%s"' \
-                  % (dev_name,vm_name)
-            raise NotFoundError(msg)
+            raise NotFoundError("KCHCDROM0007E", {'dev_name': dev_name,
+                                                  'vm_name': vm_name})
         dom.storagedevices.pop(dev_name)
 
     def storage_update(self, vm_name, dev_name, params):
@@ -580,8 +578,7 @@ class MockModel(object):
             dom = self._get_vm(vm_name)
             dom.storagedevices[dev_name].info.update(params)
         except Exception as e:
-            msg = 'Was not possible to update storage device: %s' % e.message
-            raise OperationFailed(e.message)
+            raise OperationFailed("KCHCDROM0009E", {'error': e.message})
         return dev_name
 
     def vmifaces_create(self, vm, params):
diff --git a/tests/test_model.py b/tests/test_model.py
index b374d2d..58a814b 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -163,6 +163,62 @@ class ModelTests(unittest.TestCase):
             self.assertEquals("virtio", iface["model"])
 
     @unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
+    def test_vm_cdrom(self):
+        inst = model.Model(objstore_loc=self.tmp_store)
+        with RollbackContext() as rollback:
+            vm_name = 'kimchi-cdrom'
+            params = {'name': 'test', 'disks': []}
+            inst.templates_create(params)
+            rollback.prependDefer(inst.template_delete, 'test')
+            params = {'name': vm_name, 'template': '/templates/test'}
+            inst.vms_create(params)
+            rollback.prependDefer(inst.vm_delete, vm_name)
+
+            # dummy .iso files
+            iso_path = '/tmp/existent.iso'
+            iso_path2 = '/tmp/existent2.iso'
+            open(iso_path, 'w').close()
+            open(iso_path2, 'w').close()
+            wrong_iso_path = '/nonexistent.iso'
+            rollback.prependDefer(os.remove, iso_path)
+            rollback.prependDefer(os.remove, iso_path2)
+
+            # storage count before adding new cdroms
+            prev_count = len(inst.storages_get_list(vm_name))
+
+            # Create a cdrom
+            cdrom_args = {"type": "cdrom",
+                          "path": iso_path}
+            cdrom_dev = inst.storages_create(vm_name, cdrom_args)
+            storage_list = inst.storages_get_list(vm_name)
+            self.assertEquals(prev_count + 1, len(storage_list))
+
+            # create a cdrom with existing dev_name
+            cdrom_args['dev'] = cdrom_dev
+            self.assertRaises(OperationFailed, inst.storages_create,
+                              vm_name, cdrom_args)
+
+            # update path of existing cd with
+            # non existent iso
+            self.assertRaises(OperationFailed, inst.storage_update,
+                              vm_name, cdrom_dev, {'path': wrong_iso_path})
+
+            # update path of existing cd with
+            # existent iso
+            inst.storage_update(vm_name, cdrom_dev, {'path': iso_path2})
+            cdrom_info = inst.storage_lookup(vm_name, cdrom_dev)
+            self.assertEquals(iso_path2, cdrom_info['path'])
+
+            # removing non existent cdrom
+            self.assertRaises(NotFoundError, inst.storage_delete, vm_name,
+                              "fakedev")
+
+            # removing valid cdrom
+            inst.storage_delete(vm_name, cdrom_dev)
+            storage_list = inst.storages_get_list(vm_name)
+            self.assertEquals(prev_count, len(storage_list))
+
+    @unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
     def test_vm_storage_provisioning(self):
         inst = model.Model(objstore_loc=self.tmp_store)
 
-- 
1.8.3.1




More information about the Kimchi-devel mailing list