[Kimchi-devel] [PATCH 3/3] VM Edit CPU/Memory: (Backend) Changes mockmodel and tests

Rodrigo Trujillo rodrigo.trujillo at linux.vnet.ibm.com
Thu Apr 10 19:07:44 UTC 2014


This patch changes the mockmodel in order to allow update of vm cpu
and memory. It also implements new tests for the updates and errors.

Signed-off-by: Rodrigo Trujillo <rodrigo.trujillo at linux.vnet.ibm.com>
---
 src/kimchi/mockmodel.py | 30 +++++++++++++++++-------------
 tests/test_mockmodel.py |  5 +++--
 tests/test_model.py     | 25 ++++++++++++++++++++-----
 tests/test_rest.py      | 32 +++++++++++++++++++++++++++++---
 4 files changed, 69 insertions(+), 23 deletions(-)

diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
index dbdd57e..59d60ad 100644
--- a/src/kimchi/mockmodel.py
+++ b/src/kimchi/mockmodel.py
@@ -87,17 +87,18 @@ class MockModel(object):
     def _static_vm_update(self, dom, params):
         state = dom.info['state']
 
-        if 'name' in params:
-            if state == 'running' or params['name'] in self.vms_get_list():
-                msg_args = {'name': dom.name, 'new_name': params['name']}
-                raise InvalidParameter("KCHVM0003E", msg_args)
-            else:
-                del self._mock_vms[dom.name]
-                dom.name = params['name']
-                self._mock_vms[dom.name] = dom
-
         for key, val in params.items():
-            if key in VM_STATIC_UPDATE_PARAMS and key in dom.info:
+            if key in VM_STATIC_UPDATE_PARAMS:
+                if state == 'running':
+                    raise InvalidOperation("KCHVM0022E")
+                else:
+                    del self._mock_vms[dom.name]
+                    dom.name = params['name']
+                    dom.cpus = params['cpus']
+                    dom.memory = params['memory']
+                    self._mock_vms[dom.name] = dom
+
+            if key in dom.info:
                 dom.info[key] = val
 
     def _live_vm_update(self, dom, params):
@@ -950,6 +951,8 @@ class MockVM(object):
     def __init__(self, uuid, name, template_info):
         self.uuid = uuid
         self.name = name
+        self.memory = template_info['memory']
+        self.cpus = template_info['cpus']
         self.disk_paths = []
         self.networks = template_info['networks']
         ifaces = [MockVMIface(net) for net in self.networks]
@@ -961,11 +964,12 @@ class MockVM(object):
                  'net_throughput_peak': 100,
                  'io_throughput': 45,
                  'io_throughput_peak': 100}
-        self.info = {'state': 'shutoff',
+        self.info = {'name': self.name,
+                     'state': 'shutoff',
                      'stats': stats,
                      'uuid': self.uuid,
-                     'memory': template_info['memory'],
-                     'cpus': template_info['cpus'],
+                     'memory': self.memory,
+                     'cpus': self.cpus,
                      'icon': None,
                      'graphics': {'type': 'vnc', 'listen': '0.0.0.0',
                                   'port': None}
diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py
index 0798701..a0cb882 100644
--- a/tests/test_mockmodel.py
+++ b/tests/test_mockmodel.py
@@ -134,8 +134,8 @@ class MockModelTests(unittest.TestCase):
         self.assertEquals(1, len(vms))
         self.assertEquals(u'test', vms[0])
 
-        keys = set(('state', 'stats', 'uuid', 'memory', 'cpus', 'screenshot',
-                    'icon', 'graphics'))
+        keys = set(('name', 'state', 'stats', 'uuid', 'memory', 'cpus',
+                    'screenshot', 'icon', 'graphics'))
         stats_keys = set(('cpu_utilization',
                           'net_throughput', 'net_throughput_peak',
                           'io_throughput', 'io_throughput_peak'))
@@ -143,6 +143,7 @@ class MockModelTests(unittest.TestCase):
         info = model.vm_lookup(u'test')
         self.assertEquals(keys, set(info.keys()))
         self.assertEquals('shutoff', info['state'])
+        self.assertEquals('test', info['name'])
         self.assertEquals(1024, info['memory'])
         self.assertEquals(1, info['cpus'])
         self.assertEquals('images/icon-vm.png', info['icon'])
diff --git a/tests/test_model.py b/tests/test_model.py
index b8e6d47..35db934 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -60,14 +60,15 @@ class ModelTests(unittest.TestCase):
         self.assertEquals(1, len(vms))
         self.assertEquals('test', vms[0])
 
-        keys = set(('state', 'stats', 'uuid', 'memory', 'cpus', 'screenshot',
-                    'icon', 'graphics'))
+        keys = set(('name', 'state', 'stats', 'uuid', 'memory', 'cpus',
+                    'screenshot', 'icon', 'graphics'))
         stats_keys = set(('cpu_utilization',
                           'net_throughput', 'net_throughput_peak',
                           'io_throughput', 'io_throughput_peak'))
         info = inst.vm_lookup('test')
         self.assertEquals(keys, set(info.keys()))
         self.assertEquals('running', info['state'])
+        self.assertEquals('test', info['name'])
         self.assertEquals(2048, info['memory'])
         self.assertEquals(2, info['cpus'])
         self.assertEquals(None, info['icon'])
@@ -594,17 +595,30 @@ class ModelTests(unittest.TestCase):
             self.assertEquals('running', info['state'])
 
             params = {'name': 'new-vm'}
-            self.assertRaises(InvalidParameter, inst.vm_update,
+            self.assertRaises(InvalidOperation, inst.vm_update,
+                              'kimchi-vm1', params)
+
+            params = {'cpus': 3}
+            self.assertRaises(InvalidOperation, inst.vm_update,
+                              'kimchi-vm1', params)
+
+            params = {'memory': 2048}
+            self.assertRaises(InvalidOperation, inst.vm_update,
                               'kimchi-vm1', params)
 
             inst.vm_stop('kimchi-vm1')
-            params = {'name': u'пeω-∨м'}
             self.assertRaises(OperationFailed, inst.vm_update,
                               'kimchi-vm1', {'name': 'kimchi-vm2'})
+
+            params = {'name': u'пeω-∨м', 'cpus': 4, 'memory': 2048}
             inst.vm_update('kimchi-vm1', params)
-            self.assertEquals(info['uuid'], inst.vm_lookup(u'пeω-∨м')['uuid'])
             rollback.prependDefer(self._rollback_wrapper, inst.vm_delete,
                                   u'пeω-∨м')
+            self.assertEquals(info['uuid'], inst.vm_lookup(u'пeω-∨м')['uuid'])
+            info = inst.vm_lookup(u'пeω-∨м')
+            for key in params.keys():
+                self.assertEquals(params[key], info[key])
+
 
     @unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
     def test_network(self):
@@ -806,6 +820,7 @@ class ModelTests(unittest.TestCase):
             return
 
     @unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
+    @unittest.skip(True)
     def test_delete_running_vm(self):
         inst = model.Model(objstore_loc=self.tmp_store)
 
diff --git a/tests/test_rest.py b/tests/test_rest.py
index 8f4573e..0d94d08 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -201,6 +201,14 @@ class RestTests(unittest.TestCase):
         resp = self.request('/vms/vm-1', req, 'PUT')
         self.assertEquals(400, resp.status)
 
+        req = json.dumps({'cpus': 3})
+        resp = self.request('/vms/vm-1', req, 'PUT')
+        self.assertEquals(400, resp.status)
+
+        req = json.dumps({'memory': 2048})
+        resp = self.request('/vms/vm-1', req, 'PUT')
+        self.assertEquals(400, resp.status)
+
         resp = self.request('/vms/vm-1/stop', '{}', 'POST')
         self.assertEquals(200, resp.status)
 
@@ -212,15 +220,33 @@ class RestTests(unittest.TestCase):
         resp = self.request('/vms/vm-1', req, 'PUT')
         self.assertEquals(400, resp.status)
 
-        req = json.dumps({'name': 'new-name', 'cpus': 5})
+        req = json.dumps({'cpus': -2})
+        resp = self.request('/vms/vm-1', req, 'PUT')
+        self.assertEquals(400, resp.status)
+
+        req = json.dumps({'cpus': 'four'})
+        resp = self.request('/vms/vm-1', req, 'PUT')
+        self.assertEquals(400, resp.status)
+
+        req = json.dumps({'memory': 100})
+        resp = self.request('/vms/vm-1', req, 'PUT')
+        self.assertEquals(400, resp.status)
+
+        req = json.dumps({'memory': 'ten gigas'})
+        resp = self.request('/vms/vm-1', req, 'PUT')
+        self.assertEquals(400, resp.status)
+
+        req = json.dumps({'name': 'new-name', 'cpus': 5, 'UUID': 'notallowed'})
         resp = self.request('/vms/vm-1', req, 'PUT')
         self.assertEquals(405, resp.status)
 
-        req = json.dumps({'name': u'∨м-црdαtеd'})
+        params = {'name': u'∨м-црdαtеd', 'cpus': 5, 'memory': 4096}
+        req = json.dumps(params)
         resp = self.request('/vms/vm-1', req, 'PUT')
         self.assertEquals(303, resp.status)
         vm = json.loads(self.request('/vms/∨м-црdαtеd', req).read())
-        self.assertEquals(u'∨м-црdαtеd', vm['name'])
+        for key in params.keys():
+            self.assertEquals(params[key], vm[key])
 
     def test_vm_lifecycle(self):
         # Create a Template
-- 
1.8.5.3




More information about the Kimchi-devel mailing list