[Kimchi-devel] [PATCH V3 3/3] VM Edit CPU/Memory: (Backend) Changes mockmodel and tests
Aline Manera
alinefm at linux.vnet.ibm.com
Mon Apr 14 20:18:03 UTC 2014
Reviewed-by: Aline Manera <alinefm at linux.vnet.ibm.com>
On 04/11/2014 03:38 PM, Rodrigo Trujillo wrote:
> 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 | 24 +++++++++++++++++++-----
> tests/test_rest.py | 32 +++++++++++++++++++++++++++++---
> 4 files changed, 68 insertions(+), 23 deletions(-)
>
> diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
> index bf17975..2f92a7a 100644
> --- a/src/kimchi/mockmodel.py
> +++ b/src/kimchi/mockmodel.py
> @@ -88,17 +88,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):
> @@ -951,6 +952,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]
> @@ -962,11 +965,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 3041196..62960ce 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,29 @@ 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_poweroff('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 +819,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 cd40d55..25f839e 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/poweroff', '{}', '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
More information about the Kimchi-devel
mailing list