[Kimchi-devel] [PATCH] [Kimchi 4/4] Update tests

Lucio Correia luciojhc at linux.vnet.ibm.com
Fri Feb 5 17:42:32 UTC 2016


Signed-off-by: Lucio Correia <luciojhc at linux.vnet.ibm.com>
---
 tests/test_mockmodel.py  |  7 +++---
 tests/test_model.py      | 65 ++++++++++++++++++++++++++++++++++++++++++------
 tests/test_rest.py       | 14 ++++++-----
 tests/test_template.py   | 43 ++++++++++++++++++--------------
 tests/test_vmtemplate.py | 17 +++++++------
 5 files changed, 102 insertions(+), 44 deletions(-)

diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py
index ce6e837..b7f8f23 100644
--- a/tests/test_mockmodel.py
+++ b/tests/test_mockmodel.py
@@ -1,7 +1,7 @@
 #
 # Project Kimchi
 #
-# Copyright IBM, Corp. 2013-2015
+# Copyright IBM, Corp. 2013-2016
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -122,7 +122,7 @@ class MockModelTests(unittest.TestCase):
         self.assertEquals(2, len(vms))
         self.assertIn(u'test-vm', vms)
 
-        keys = set(('name', 'state', 'stats', 'uuid', 'memory', 'cpus',
+        keys = set(('name', 'state', 'stats', 'uuid', 'memory', 'cpu_info',
                     'screenshot', 'icon', 'graphics', 'users', 'groups',
                     'access', 'persistent'))
 
@@ -136,7 +136,8 @@ class MockModelTests(unittest.TestCase):
         self.assertEquals('test-vm', info['name'])
         self.assertEquals(get_template_default('old', 'memory'),
                           info['memory'])
-        self.assertEquals(1, info['cpus'])
+        self.assertEquals(1, info['cpu_info']['vcpus'])
+        self.assertEquals(1, info['cpu_info']['maxvcpus'])
         self.assertEquals('plugins/kimchi/images/icon-vm.png', info['icon'])
         self.assertEquals(stats_keys, set(info['stats'].keys()))
         self.assertEquals('vnc', info['graphics']['type'])
diff --git a/tests/test_model.py b/tests/test_model.py
index 762f0f8..f2ec461 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -2,7 +2,7 @@
 #
 # Project Kimchi
 #
-# Copyright IBM, Corp. 2013-2015
+# Copyright IBM, Corp. 2013-2016
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -102,7 +102,7 @@ class ModelTests(unittest.TestCase):
         self.assertEquals(1, len(vms))
         self.assertEquals('test', vms[0])
 
-        keys = set(('name', 'state', 'stats', 'uuid', 'memory', 'cpus',
+        keys = set(('name', 'state', 'stats', 'uuid', 'memory', 'cpu_info',
                     'screenshot', 'icon', 'graphics', 'users', 'groups',
                     'access', 'persistent'))
 
@@ -114,7 +114,8 @@ class ModelTests(unittest.TestCase):
         self.assertEquals('running', info['state'])
         self.assertEquals('test', info['name'])
         self.assertEquals(2048, info['memory'])
-        self.assertEquals(2, info['cpus'])
+        self.assertEquals(2, info['cpu_info']['vcpus'])
+        self.assertEquals(2, info['cpu_info']['maxvcpus'])
         self.assertEquals(None, info['icon'])
         self.assertEquals(stats_keys, set(info['stats'].keys()))
         self.assertRaises(NotFoundError, inst.vm_lookup, 'nosuchvm')
@@ -267,10 +268,10 @@ class ModelTests(unittest.TestCase):
             # Create template based on IMG file
             tmpl_name = "img-tmpl"
             pool_uri = "/plugins/kimchi/storagepools/default"
-            tmpl_info = {"cpus": 1, "cdrom": "", "name": tmpl_name,
+            tmpl_info = {"cpu_info": {"vcpus": 1}, "name": tmpl_name,
                          "graphics": {"type": "vnc", "listen": "127.0.0.1"},
                          "networks": ["default"], "memory": 1024, "folder": [],
-                         "icon": "images/icon-vm.png",
+                         "icon": "images/icon-vm.png", "cdrom": "",
                          "os_distro": "unknown", "os_version": "unknown",
                          "disks": [{"base": vol_path, "size": 10,
                                     "format": "qcow2",
@@ -768,7 +769,8 @@ class ModelTests(unittest.TestCase):
 
         # template disk format must be qcow2 because vmsnapshot
         # only supports this format
-        orig_params = {'name': 'test', 'memory': 1024, 'cpus': 1,
+        orig_params = {'name': 'test', 'memory': 1024,
+                       'cpu_info': {'vcpus': 1},
                        'cdrom': UBUNTU_ISO,
                        'disks': [{'size': 1, 'format': 'qcow2', 'pool': {
                            'name': '/plugins/kimchi/storagepools/default'}}]}
@@ -827,18 +829,65 @@ class ModelTests(unittest.TestCase):
             vm_info = inst.vm_lookup(u'kimchi-vm1')
             self.assertEquals(['root'], vm_info['users'])
             self.assertEquals(['root'], vm_info['groups'])
+
             # change VM users and groups by removing all elements,
-            # when wm is running.
+            # when vm is running.
             inst.vm_update(u'kimchi-vm1', {'users': [], 'groups': []})
             vm_info = inst.vm_lookup(u'kimchi-vm1')
             self.assertEquals([], vm_info['users'])
             self.assertEquals([], vm_info['groups'])
 
+            # power off vm
             inst.vm_poweroff('kimchi-vm1')
             self.assertRaises(OperationFailed, inst.vm_update,
                               'kimchi-vm1', {'name': 'kimchi-vm2'})
 
-            params = {'name': u'пeω-∨м', 'cpus': 4, 'memory': 2048}
+            # update maxvcpus only
+            inst.vm_update(u'kimchi-vm1', {'cpu_info': {'maxvcpus': 8}})
+            vm_info = inst.vm_lookup(u'kimchi-vm1')
+            self.assertEquals(8, vm_info['cpu_info']['maxvcpus'])
+
+            # update vcpus only
+            inst.vm_update(u'kimchi-vm1', {'cpu_info': {'vcpus': 4}})
+            vm_info = inst.vm_lookup(u'kimchi-vm1')
+            self.assertEquals(4, vm_info['cpu_info']['vcpus'])
+
+            # vcpus > maxvcpus: failure
+            self.assertRaises(InvalidParameter, inst.vm_update, u'kimchi-vm1',
+                              {'cpu_info': {'vcpus': 10}})
+
+            # define CPU topology
+            inst.vm_update(u'kimchi-vm1', {'cpu_info': {'topology': {
+                           'sockets': 4, 'cores': 2, 'threads': 1}}})
+            vm_info = inst.vm_lookup(u'kimchi-vm1')
+            self.assertEquals({'sockets': 4, 'cores': 2, 'threads': 1},
+                              vm_info['cpu_info']['topology'])
+
+            # vcpus not a multiple of (cores * threads)
+            self.assertRaises(InvalidParameter, inst.vm_update, u'kimchi-vm1',
+                              {'cpu_info': {'vcpus': 1}})
+
+            # maxvcpus different of (sockets * cores * threads)
+            self.assertRaises(InvalidParameter, inst.vm_update, u'kimchi-vm1',
+                              {'cpu_info': {'maxvcpus': 4}})
+
+            # topology does not match maxvcpus (8 != 2 * 2 * 1)
+            self.assertRaises(InvalidParameter, inst.vm_update, u'kimchi-vm1',
+                              {'cpu_info': {'topology': {
+                               'sockets': 2, 'cores': 2, 'threads': 1}}})
+
+            # undefine CPU topology
+            inst.vm_update(u'kimchi-vm1', {'cpu_info': {'topology': {}}})
+            vm_info = inst.vm_lookup(u'kimchi-vm1')
+            self.assertEquals({}, vm_info['cpu_info']['topology'])
+
+            # reduce maxvcpus to same as vcpus
+            inst.vm_update(u'kimchi-vm1', {'cpu_info': {'maxvcpus': 4}})
+            vm_info = inst.vm_lookup(u'kimchi-vm1')
+            self.assertEquals(4, vm_info['cpu_info']['maxvcpus'])
+
+            # rename and increase memory when vm is not running
+            params = {'name': u'пeω-∨м', 'memory': 2048}
             inst.vm_update('kimchi-vm1', params)
             rollback.prependDefer(utils.rollback_wrapper, inst.vm_delete,
                                   u'пeω-∨м')
diff --git a/tests/test_rest.py b/tests/test_rest.py
index f4c4f4e..1fc0f71 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -148,7 +148,7 @@ class RestTests(unittest.TestCase):
         vm = json.loads(self.request('/plugins/kimchi/vms/vm-1').read())
         self.assertEquals('vm-1', vm['name'])
 
-        req = json.dumps({'cpus': 3})
+        req = json.dumps({'cpu_info': {'maxvcpus': 5, 'vcpus': 3}})
         resp = self.request('/plugins/kimchi/vms/vm-1', req, 'PUT')
         self.assertEquals(200, resp.status)
 
@@ -164,7 +164,7 @@ class RestTests(unittest.TestCase):
         self.assertEquals(400, resp.status)
 
         # Unable to do CPU hotplug
-        req = json.dumps({'cpus': 5})
+        req = json.dumps({'cpu_info': {'vcpus': 5}})
         resp = self.request('/plugins/kimchi/vms/vm-1', req, 'PUT')
         self.assertEquals(400, resp.status)
 
@@ -203,11 +203,11 @@ class RestTests(unittest.TestCase):
         resp = self.request('/plugins/kimchi/vms/vm-1', req, 'PUT')
         self.assertEquals(400, resp.status)
 
-        req = json.dumps({'cpus': -2})
+        req = json.dumps({'cpu_info': {'vcpus': -2}})
         resp = self.request('/plugins/kimchi/vms/vm-1', req, 'PUT')
         self.assertEquals(400, resp.status)
 
-        req = json.dumps({'cpus': 'four'})
+        req = json.dumps({'cpu_info': {'vcpus': 'four'}})
         resp = self.request('/plugins/kimchi/vms/vm-1', req, 'PUT')
         self.assertEquals(400, resp.status)
 
@@ -219,11 +219,13 @@ class RestTests(unittest.TestCase):
         resp = self.request('/plugins/kimchi/vms/vm-1', req, 'PUT')
         self.assertEquals(400, resp.status)
 
-        req = json.dumps({'name': 'new-name', 'cpus': 5, 'UUID': 'notallowed'})
+        req = json.dumps({'name': 'new-name', 'cpu_info': {'vcpus': 5},
+                          'UUID': 'notallowed'})
         resp = self.request('/plugins/kimchi/vms/vm-1', req, 'PUT')
         self.assertEquals(400, resp.status)
 
-        params = {'name': u'∨м-црdαtеd', 'cpus': 5, 'memory': 3072}
+        params = {'name': u'∨м-црdαtеd', 'cpu_info': {'vcpus': 5},
+                  'memory': 3072}
         req = json.dumps(params)
         resp = self.request('/plugins/kimchi/vms/vm-1', req, 'PUT')
         self.assertEquals(303, resp.status)
diff --git a/tests/test_template.py b/tests/test_template.py
index 0b3dd98..3b9f26a 100644
--- a/tests/test_template.py
+++ b/tests/test_template.py
@@ -2,7 +2,7 @@
 #
 # Project Kimchi
 #
-# Copyright IBM, Corp. 2015
+# Copyright IBM, Corp. 2015-2016
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -70,7 +70,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}
+             'os_version': '1.0', 'memory': 1024, 'cpu_info': {'vcpus': 1}}
         req = json.dumps(t)
         resp = self.request('/plugins/kimchi/templates', req, 'POST')
         self.assertEquals(400, resp.status)
@@ -82,9 +82,8 @@ class TemplateTests(unittest.TestCase):
         self.assertEquals(201, resp.status)
 
         # Verify the template
-        keys = ['name', 'icon', 'invalid', 'os_distro', 'os_version', 'cpus',
-                'memory', 'cdrom', 'disks', 'networks',
-                'folder', 'graphics', 'cpu_info']
+        keys = ['name', 'icon', 'invalid', 'os_distro', 'os_version', 'memory',
+                'cdrom', 'disks', 'networks', 'folder', 'graphics', 'cpu_info']
         tmpl = json.loads(
             self.request('/plugins/kimchi/templates/test').read()
         )
@@ -193,34 +192,40 @@ class TemplateTests(unittest.TestCase):
         self.assertEquals('fedora', update_tmpl['os_distro'])
         self.assertEquals('21', update_tmpl['os_version'])
 
-        # Update cpus
-        req = json.dumps({'cpus': 2})
+        # Update maxvcpus only
+        req = json.dumps({'cpu_info': {'maxvcpus': 2}})
         resp = self.request(new_tmpl_uri, req, 'PUT')
         self.assertEquals(200, resp.status)
         update_tmpl = json.loads(resp.read())
-        self.assertEquals(2, update_tmpl['cpus'])
+        self.assertEquals(2, update_tmpl['cpu_info']['maxvcpus'])
 
-        # Update memory
-        req = json.dumps({'memory': 2048})
+        # Update vcpus only
+        req = json.dumps({'cpu_info': {'vcpus': 2}})
         resp = self.request(new_tmpl_uri, req, 'PUT')
         self.assertEquals(200, resp.status)
         update_tmpl = json.loads(resp.read())
-        self.assertEquals(2048, update_tmpl['memory'])
+        self.assertEquals(2, update_tmpl['cpu_info']['vcpus'])
 
         # Update cpu_info
-        resp = self.request(new_tmpl_uri)
-        cpu_info = json.loads(resp.read())['cpu_info']
-        self.assertEquals(cpu_info, {})
-        self.assertEquals(cpu_info.get('topology'), None)
-
-        cpu_info_data = {'cpu_info': {'topology': {'sockets': 1,
-                                                   'cores': 2,
-                                                   'threads': 1}}}
+        cpu_info_data = {
+            'cpu_info': {
+                'maxvcpus': 2,
+                'vcpus': 2,
+                'topology': {'sockets': 1, 'cores': 2, 'threads': 1}
+            }
+        }
         resp = self.request(new_tmpl_uri, json.dumps(cpu_info_data), 'PUT')
         self.assertEquals(200, resp.status)
         update_tmpl = json.loads(resp.read())
         self.assertEquals(update_tmpl['cpu_info'], cpu_info_data['cpu_info'])
 
+        # Update memory
+        req = json.dumps({'memory': 2048})
+        resp = self.request(new_tmpl_uri, req, 'PUT')
+        self.assertEquals(200, resp.status)
+        update_tmpl = json.loads(resp.read())
+        self.assertEquals(2048, update_tmpl['memory'])
+
         # Update cdrom
         cdrom_data = {'cdrom': '/tmp/mock2.iso'}
         resp = self.request(new_tmpl_uri, json.dumps(cdrom_data), 'PUT')
diff --git a/tests/test_vmtemplate.py b/tests/test_vmtemplate.py
index de2d542..e157198 100644
--- a/tests/test_vmtemplate.py
+++ b/tests/test_vmtemplate.py
@@ -1,7 +1,7 @@
 #
 # Project Kimchi
 #
-# Copyright IBM, Corp. 2013-2015
+# Copyright IBM, Corp. 2013-2016
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -45,12 +45,12 @@ class VMTemplateTests(unittest.TestCase):
         disk_bus = get_template_default('old', 'disk_bus')
         memory = get_template_default('old', 'memory')
         nic_model = get_template_default('old', 'nic_model')
-        fields = (('name', 'test'), ('os_distro', 'unknown'),
-                  ('os_version', 'unknown'), ('cpus', 1),
+        fields = (('name', 'test'), ('cdrom', self.iso),
+                  ('os_distro', 'unknown'), ('os_version', 'unknown'),
+                  ('cpu_info', {'vcpus': 1, 'maxvcpus': 1}),
                   ('memory', memory), ('networks', ['default']),
                   ('disk_bus', disk_bus), ('nic_model', nic_model),
-                  ('graphics', {'type': 'vnc', 'listen': '127.0.0.1'}),
-                  ('cdrom', self.iso))
+                  ('graphics', {'type': 'vnc', 'listen': '127.0.0.1'}))
 
         args = {'name': 'test', 'cdrom': self.iso}
         t = VMTemplate(args)
@@ -121,10 +121,11 @@ class VMTemplateTests(unittest.TestCase):
         """
         graphics = {'type': 'vnc', 'listen': '127.0.0.1'}
         args = {'name': 'test', 'os_distro': 'opensuse', 'os_version': '12.3',
-                'cpus': 2, 'memory': 2048, 'networks': ['foo'],
-                'cdrom': self.iso, 'graphics': graphics}
+                'cpu_info': {'vcpus': 2, 'maxvcpus': 4}, 'memory': 2048,
+                'networks': ['foo'], 'cdrom': self.iso, 'graphics': graphics}
         t = VMTemplate(args)
-        self.assertEquals(2, t.info.get('cpus'))
+        self.assertEquals(2, t.info.get('cpu_info', {}).get('vcpus'))
+        self.assertEquals(4, t.info.get('cpu_info', {}).get('maxvcpus'))
         self.assertEquals(2048, t.info.get('memory'))
         self.assertEquals(['foo'], t.info.get('networks'))
         self.assertEquals(self.iso, t.info.get('cdrom'))
-- 
1.9.1




More information about the Kimchi-devel mailing list