[Kimchi-devel] [PATCH V4 3/3] VM Edit CPU/Memory: (Backend) Changes mockmodel and tests
Aline Manera
alinefm at linux.vnet.ibm.com
Tue Apr 22 13:36:36 UTC 2014
Reviewed-by: Aline Manera <alinefm at linux.vnet.ibm.com>
On 04/16/2014 03:30 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 | 27 +++++++++++++++------------
> tests/test_mockmodel.py | 6 ++++--
> tests/test_model.py | 14 ++++++++++----
> tests/test_rest.py | 32 +++++++++++++++++++++++++++++---
> 4 files changed, 58 insertions(+), 21 deletions(-)
>
> diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
> index 8c7d7bb..e602a54 100644
> --- a/src/kimchi/mockmodel.py
> +++ b/src/kimchi/mockmodel.py
> @@ -87,16 +87,16 @@ 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 == 'name':
> + 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
> +
> if key in dom.info:
> dom.info[key] = val
>
> @@ -954,6 +954,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]
> @@ -965,11 +967,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 9e258ce..8dd6a03 100644
> --- a/tests/test_mockmodel.py
> +++ b/tests/test_mockmodel.py
> @@ -134,8 +134,9 @@ 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', 'users', 'groups'))
> + keys = set(('name', 'state', 'stats', 'uuid', 'memory', 'cpus',
> + 'screenshot', 'icon', 'graphics', 'users', 'groups'))
> +
> stats_keys = set(('cpu_utilization',
> 'net_throughput', 'net_throughput_peak',
> 'io_throughput', 'io_throughput_peak'))
> @@ -143,6 +144,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 357d969..eee20f0 100644
> --- a/tests/test_model.py
> +++ b/tests/test_model.py
> @@ -62,14 +62,16 @@ class ModelTests(unittest.TestCase):
> self.assertEquals(1, len(vms))
> self.assertEquals('test', vms[0])
>
> - keys = set(('state', 'stats', 'uuid', 'memory', 'cpus', 'screenshot',
> - 'icon', 'graphics', 'users', 'groups'))
> + keys = set(('name', 'state', 'stats', 'uuid', 'memory', 'cpus',
> + 'screenshot', 'icon', 'graphics', 'users', 'groups'))
> +
> 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'])
> @@ -602,13 +604,17 @@ class ModelTests(unittest.TestCase):
> '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])
>
> # change only VM users - groups are not changed (default is empty)
> users = ['root']
> diff --git a/tests/test_rest.py b/tests/test_rest.py
> index a40ba93..4cba5a4 100644
> --- a/tests/test_rest.py
> +++ b/tests/test_rest.py
> @@ -203,6 +203,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(200, resp.status)
> +
> + req = json.dumps({'memory': 2048})
> + resp = self.request('/vms/vm-1', req, 'PUT')
> + self.assertEquals(200, resp.status)
> +
> resp = self.request('/vms/vm-1/poweroff', '{}', 'POST')
> self.assertEquals(200, resp.status)
>
> @@ -214,15 +222,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])
>
> # change only VM users - groups are not changed (default is empty)
> req = json.dumps({'users': ['root']})
More information about the Kimchi-devel
mailing list