[Kimchi-devel] [PATCH 5/9 - v6] Test: Fix and add test related to disks in templates

Rodrigo Trujillo rodrigo.trujillo at linux.vnet.ibm.com
Wed Dec 2 15:59:22 UTC 2015


This patch fixes problems in test cases after change the way Kimchi
updates the disks in the Templates. Now disks have the storagepool
assigned in its data.

Signed-off-by: Rodrigo Trujillo <rodrigo.trujillo at linux.vnet.ibm.com>
---
 src/wok/plugins/kimchi/mockmodel.py           |  8 +++-
 src/wok/plugins/kimchi/tests/test_model.py    | 18 ++++++--
 src/wok/plugins/kimchi/tests/test_rest.py     | 15 ++++---
 src/wok/plugins/kimchi/tests/test_template.py | 59 ++++++++++++++++++++-------
 4 files changed, 75 insertions(+), 25 deletions(-)

diff --git a/src/wok/plugins/kimchi/mockmodel.py b/src/wok/plugins/kimchi/mockmodel.py
index c679378..2e58724 100644
--- a/src/wok/plugins/kimchi/mockmodel.py
+++ b/src/wok/plugins/kimchi/mockmodel.py
@@ -48,18 +48,20 @@ from wok.plugins.kimchi.model import storagevolumes
 from wok.plugins.kimchi.model.templates import LibvirtVMTemplate
 from wok.plugins.kimchi.model.users import PAMUsersModel
 from wok.plugins.kimchi.model.groups import PAMGroupsModel
+from wok.plugins.kimchi.utils import pool_name_from_uri
 from wok.plugins.kimchi.vmtemplate import VMTemplate
 
 
 fake_user = {'root': 'letmein!'}
 mockmodel_defaults = {
-    'storagepool': '/plugins/kimchi/storagepools/default-pool',
     'domain': 'test', 'arch': 'i686'
 }
 storagevolumes.VALID_RAW_CONTENT = ['dos/mbr boot sector',
                                     'x86 boot sector',
                                     'data', 'empty']
 
+DEFAULT_POOL = '/plugins/kimchi/storagepools/default-pool'
+
 
 class MockModel(Model):
     _mock_vms = defaultdict(list)
@@ -73,6 +75,7 @@ class MockModel(Model):
         # test:///default driver
         defaults = dict(osinfo.defaults)
         defaults.update(mockmodel_defaults)
+        defaults['disks'][0]['pool'] = DEFAULT_POOL
         osinfo.defaults = dict(defaults)
 
         self._mock_vgs = MockVolumeGroups()
@@ -238,7 +241,8 @@ class MockModel(Model):
     def _probe_image(self, path):
         return ('unknown', 'unknown')
 
-    def _get_volume_path(self, pool, vol):
+    def _get_volume_path(self, pool_uri, vol):
+        pool = pool_name_from_uri(pool_uri)
         pool_info = self.storagepool_lookup(pool)
         if pool_info['type'] == 'scsi':
             return self._mock_storagevolumes.scsi_volumes[vol]['path']
diff --git a/src/wok/plugins/kimchi/tests/test_model.py b/src/wok/plugins/kimchi/tests/test_model.py
index 65d97f2..c5407d6 100644
--- a/src/wok/plugins/kimchi/tests/test_model.py
+++ b/src/wok/plugins/kimchi/tests/test_model.py
@@ -76,6 +76,16 @@ def tearDownModule():
     shutil.rmtree(TMP_DIR)
 
 
+def _setDiskPoolDefault():
+    osinfo.defaults['disks'][0]['pool'] = \
+        '/plugins/kimchi/storagepools/default'
+
+
+def _setDiskPoolDefaultTest():
+    osinfo.defaults['disks'][0]['pool'] = \
+        '/plugins/kimchi/storagepools/default-pool'
+
+
 class ModelTests(unittest.TestCase):
     def setUp(self):
         self.tmp_store = '/tmp/kimchi-store-test'
@@ -266,8 +276,8 @@ class ModelTests(unittest.TestCase):
                          "networks": ["default"], "memory": 1024, "folder": [],
                          "icon": "images/icon-vm.png",
                          "os_distro": "unknown", "os_version": "unknown",
-                         "disks": [{"base": vol_path, "size": 10}],
-                         "storagepool": "/plugins/kimchi/storagepools/default"}
+                         "disks": [{"base": vol_path, "size": 10, "pool": {
+                             "name": "/plugins/kimchi/storagepools/default"}}]}
 
             with inst.objstore as session:
                 session.store('template', tmpl_name, tmpl_info,
@@ -1046,11 +1056,13 @@ class ModelTests(unittest.TestCase):
                 'name': 'test',
                 'disks': [],
                 'cdrom': UBUNTU_ISO,
-                'storagepool': '/plugins/kimchi/storagepools/default-pool',
                 'domain': 'test',
                 'arch': 'i686'
             }
 
+            _setDiskPoolDefaultTest()
+            rollback.prependDefer(_setDiskPoolDefault)
+
             inst.templates_create(params)
             rollback.prependDefer(inst.template_delete, 'test')
 
diff --git a/src/wok/plugins/kimchi/tests/test_rest.py b/src/wok/plugins/kimchi/tests/test_rest.py
index 9fa8c8d..2947df2 100644
--- a/src/wok/plugins/kimchi/tests/test_rest.py
+++ b/src/wok/plugins/kimchi/tests/test_rest.py
@@ -226,6 +226,8 @@ class RestTests(unittest.TestCase):
         vm = json.loads(
             self.request('/plugins/kimchi/vms/∨м-црdαtеd', req).read()
         )
+        # Memory was hot plugged
+        params['memory'] += 1024
         for key in params.keys():
             self.assertEquals(params[key], vm[key])
 
@@ -895,7 +897,7 @@ class RestTests(unittest.TestCase):
 
         # Test template not changed after vm customise its pool
         t = json.loads(self.request('/plugins/kimchi/templates/test').read())
-        self.assertEquals(t['storagepool'],
+        self.assertEquals(t['disks'][0]['pool']['name'],
                           '/plugins/kimchi/storagepools/default-pool')
 
         # Verify the volume was created
@@ -928,9 +930,9 @@ class RestTests(unittest.TestCase):
 
         # Create template fails because SCSI volume is missing
         tmpl_params = {
-            'name': 'test_fc_pool', 'cdrom': fake_iso,
-            'storagepool': '/plugins/kimchi/storagepools/scsi_fc_pool'
-        }
+            'name': 'test_fc_pool', 'cdrom': fake_iso, 'disks': [{'pool': {
+                'name': '/plugins/kimchi/storagepools/scsi_fc_pool'}}]}
+
         req = json.dumps(tmpl_params)
         resp = self.request('/plugins/kimchi/templates', req, 'POST')
         self.assertEquals(400, resp.status)
@@ -940,8 +942,9 @@ class RestTests(unittest.TestCase):
             '/plugins/kimchi/storagepools/scsi_fc_pool/storagevolumes'
         )
         lun_name = json.loads(resp.read())[0]['name']
-
-        tmpl_params['disks'] = [{'index': 0, 'volume': lun_name}]
+        pool_name = tmpl_params['disks'][0]['pool']['name']
+        tmpl_params['disks'] = [{'index': 0, 'volume': lun_name,
+                                 'pool': {'name': pool_name}}]
         req = json.dumps(tmpl_params)
         resp = self.request('/plugins/kimchi/templates', req, 'POST')
         self.assertEquals(201, resp.status)
diff --git a/src/wok/plugins/kimchi/tests/test_template.py b/src/wok/plugins/kimchi/tests/test_template.py
index 3f026f3..7bd856e 100644
--- a/src/wok/plugins/kimchi/tests/test_template.py
+++ b/src/wok/plugins/kimchi/tests/test_template.py
@@ -25,7 +25,6 @@ from functools import partial
 
 from tests.utils import get_free_port, patch_auth, request, run_server
 
-from wok.plugins.kimchi import osinfo
 from wok.plugins.kimchi.config import READONLY_POOL_TYPE
 from wok.plugins.kimchi.mockmodel import MockModel
 
@@ -37,6 +36,8 @@ port = None
 ssl_port = None
 cherrypy_port = None
 
+DEFAULT_POOL = u'/plugins/kimchi/storagepools/default-pool'
+
 
 def setUpModule():
     global test_server, model, host, port, ssl_port, cherrypy_port
@@ -68,8 +69,7 @@ class TemplateTests(unittest.TestCase):
 
         # Create a template without cdrom and disk specified fails with 400
         t = {'name': 'test', 'os_distro': 'ImagineOS',
-             'os_version': '1.0', 'memory': 1024, 'cpus': 1,
-             'storagepool': '/plugins/kimchi/storagepools/alt'}
+             'os_version': '1.0', 'memory': 1024, 'cpus': 1}
         req = json.dumps(t)
         resp = self.request('/plugins/kimchi/templates', req, 'POST')
         self.assertEquals(400, resp.status)
@@ -82,16 +82,18 @@ class TemplateTests(unittest.TestCase):
 
         # Verify the template
         keys = ['name', 'icon', 'invalid', 'os_distro', 'os_version', 'cpus',
-                'memory', 'cdrom', 'disks', 'storagepool', 'networks',
+                'memory', 'cdrom', 'disks', 'networks',
                 'folder', 'graphics', 'cpu_info']
         tmpl = json.loads(
             self.request('/plugins/kimchi/templates/test').read()
         )
         self.assertEquals(sorted(tmpl.keys()), sorted(keys))
 
-        # Verify if default disk format was configured
-        default_disk_format = osinfo.defaults['disks'][0]['format']
-        self.assertEquals(tmpl['disks'][0]['format'], default_disk_format)
+        disk_keys = ['index', 'pool', 'size', 'format']
+        disk_pool_keys = ['name', 'type']
+        self.assertEquals(sorted(tmpl['disks'][0].keys()), sorted(disk_keys))
+        self.assertEquals(sorted(tmpl['disks'][0]['pool'].keys()),
+                          sorted(disk_pool_keys))
 
         # Clone a template
         resp = self.request('/plugins/kimchi/templates/test/clone', '{}',
@@ -212,13 +214,20 @@ class TemplateTests(unittest.TestCase):
         self.assertEquals(update_tmpl['cdrom'], cdrom_data['cdrom'])
 
         # Update disks
-        disk_data = {'disks': [{'index': 0, 'size': 10, 'format': 'raw'},
-                               {'index': 1, 'size': 20, 'format': 'raw'}]}
+        disk_data = {'disks': [{'index': 0, 'size': 10, 'format': 'raw',
+                                'pool': DEFAULT_POOL},
+                               {'index': 1, 'size': 20, 'format': 'qcow2',
+                                'pool': DEFAULT_POOL}]}
         resp = self.request(new_tmpl_uri, json.dumps(disk_data), 'PUT')
         self.assertEquals(200, resp.status)
         resp = self.request(new_tmpl_uri)
         self.assertEquals(200, resp.status)
         updated_tmpl = json.loads(resp.read())
+        disk_data['disks'][0]['pool'] = {'name': DEFAULT_POOL,
+                                         'type': 'dir'}
+
+        disk_data['disks'][1]['pool'] = {'name': DEFAULT_POOL,
+                                         'type': 'dir'}
         self.assertEquals(updated_tmpl['disks'], disk_data['disks'])
 
         # For all supported types, edit the template and check if
@@ -227,13 +236,15 @@ class TemplateTests(unittest.TestCase):
                       'qed', 'raw', 'vmdk', 'vpc']
         for disk_type in disk_types:
             disk_data = {'disks': [{'index': 0, 'format': disk_type,
-                                    'size': 10}]}
+                                    'size': 10, 'pool': DEFAULT_POOL}]}
             resp = self.request(new_tmpl_uri, json.dumps(disk_data), 'PUT')
             self.assertEquals(200, resp.status)
 
             resp = self.request(new_tmpl_uri)
             self.assertEquals(200, resp.status)
             updated_tmpl = json.loads(resp.read())
+            disk_data['disks'][0]['pool'] = {u'name': DEFAULT_POOL,
+                                             u'type': u'dir'}
             self.assertEquals(updated_tmpl['disks'], disk_data['disks'])
 
         # Update folder
@@ -338,16 +349,34 @@ class TemplateTests(unittest.TestCase):
                 vols = json.loads(resp.read())
                 if len(vols) > 0:
                     vol = vols[0]['name']
-                    req = json.dumps({'storagepool': pool_uri,
-                                      'disks': [{'volume': vol}]})
+                    req = json.dumps({'disks': [{'volume': vol,
+                                                 'pool': {'name': pool_uri}}]})
             else:
-                req = json.dumps({'storagepool': pool_uri})
+                req = json.dumps({'disks': [{'pool': {'name': pool_uri}}]})
 
             if req is not None:
                 resp = self.request('/plugins/kimchi/templates/test', req,
                                     'PUT')
                 self.assertEquals(200, resp.status)
 
+        # Test disk template update with different pool
+        pool_uri = u'/plugins/kimchi/storagepools/kīмсhīUnitTestDirPool'
+        disk_data = {'disks': [{'size': 5, 'format': 'qcow2',
+                                'pool': pool_uri}]}
+        req = json.dumps(disk_data)
+        resp = self.request('/plugins/kimchi/templates/test', req, 'PUT')
+        self.assertEquals(200, resp.status)
+        del(disk_data['disks'][0]['pool'])
+        disk_data['disks'][0]['index'] = 0
+        disk_data['disks'][0]['pool'] = {u'name': pool_uri,
+                                         u'type': u'dir'}
+        tmpl = json.loads(
+            self.request('/plugins/kimchi/templates/test').read())
+        self.assertEquals(sorted(disk_data['disks'][0].keys()),
+                          sorted(tmpl['disks'][0].keys()))
+        self.assertEquals(sorted(disk_data['disks'][0].values()),
+                          sorted(tmpl['disks'][0].values()))
+
     def test_tmpl_integrity(self):
         # Create a network and a pool for testing template integrity
         net = {'name': u'nat-network', 'connection': 'nat'}
@@ -362,7 +391,9 @@ class TemplateTests(unittest.TestCase):
         # Create a template using the custom network and pool
         t = {'name': 'test', 'cdrom': '/tmp/mock.iso',
              'networks': ['nat-network'],
-             'storagepool': '/plugins/kimchi/storagepools/dir-pool'}
+             'disks': [{'pool': {
+                 'name': '/plugins/kimchi/storagepools/dir-pool'},
+                 'size': 2}]}
         req = json.dumps(t)
         resp = self.request('/plugins/kimchi/templates', req, 'POST')
         self.assertEquals(201, resp.status)
-- 
2.1.0




More information about the Kimchi-devel mailing list