
Because all of params validation are moved to controller.py, i removed the test code about graphics data validation in model.py. Signed-off-by: apporc <appleorchard2000@gmail.com> --- tests/test_mockmodel.py | 2 ++ tests/test_model.py | 27 ++++++++++++++- tests/test_rest.py | 88 +++++++++++++++++++++++++++++++++++++++++++++--- tests/test_vmtemplate.py | 42 ++++++++++++++++++++--- 4 files changed, 149 insertions(+), 10 deletions(-) diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py index b819172..a806d50 100644 --- a/tests/test_mockmodel.py +++ b/tests/test_mockmodel.py @@ -143,3 +143,5 @@ class MockModelTests(unittest.TestCase): self.assertEquals(1, info['cpus']) self.assertEquals('images/icon-vm.png', info['icon']) self.assertEquals(stats_keys, set(eval(info['stats']).keys())) + self.assertEquals('vnc', info['graphics']['type']) + self.assertEquals('0.0.0.0', info['graphics']['listen']) diff --git a/tests/test_model.py b/tests/test_model.py index fb7d6dd..4086550 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -62,7 +62,6 @@ class ModelTests(unittest.TestCase): self.assertEquals(2, info['cpus']) self.assertEquals(None, info['icon']) self.assertEquals(stats_keys, set(eval(info['stats']).keys())) - self.assertRaises(NotFoundError, inst.vm_lookup, 'nosuchvm') @unittest.skipUnless(utils.running_as_root(), 'Must be run as root') @@ -91,6 +90,32 @@ class ModelTests(unittest.TestCase): self.assertFalse('kimchi-vm' in vms) @unittest.skipUnless(utils.running_as_root(), 'Must be run as root') + def test_vm_graphics(self): + inst = kimchi.model.Model(objstore_loc=self.tmp_store) + params = {'name': 'test', 'disks': []} + inst.templates_create(params) + with utils.RollbackContext() as rollback: + params = {'name': 'kimchi-vnc', 'template': '/templates/test'} + inst.vms_create(params) + rollback.prependDefer(inst.vm_delete, 'kimchi-vnc') + + info = inst.vm_lookup('kimchi-vnc') + self.assertEquals('vnc', info['graphics']['type']) + self.assertEquals('0.0.0.0', info['graphics']['listen']) + + graphics = {'type': 'spice', 'listen': '127.0.0.1'} + params = {'name': 'kimchi-spice', 'template': '/templates/test', + 'graphics': graphics} + inst.vms_create(params) + rollback.prependDefer(inst.vm_delete, 'kimchi-spice') + + info = inst.vm_lookup('kimchi-spice') + self.assertEquals('spice', info['graphics']['type']) + self.assertEquals('127.0.0.1', info['graphics']['listen']) + + inst.template_delete('test') + + @unittest.skipUnless(utils.running_as_root(), 'Must be run as root') def test_vm_storage_provisioning(self): inst = kimchi.model.Model(objstore_loc=self.tmp_store) diff --git a/tests/test_rest.py b/tests/test_rest.py index f597796..2e62ce6 100644 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -249,9 +249,73 @@ class RestTests(unittest.TestCase): resp = self.request('/vms/test-vm', '{}', 'DELETE') self.assertEquals(204, resp.status) + # Delete the Template + resp = self.request('/templates/test', '{}', 'DELETE') + self.assertEquals(204, resp.status) + # Verify the volume was deleted self.assertHTTPStatus(404, vol_uri) + def test_vm_graphics(self): + # Create a Template + req = json.dumps({'name': 'test', 'cdrom': '/nonexistent.iso'}) + resp = self.request('/templates', req, 'POST') + self.assertEquals(201, resp.status) + + # Create a VM with default args + req = json.dumps({'name': 'test-vm', 'template': '/templates/test'}) + resp = self.request('/vms', req, 'POST') + self.assertEquals(201, resp.status) + # Verify the VM + vm = json.loads(self.request('/vms/test-vm').read()) + self.assertEquals('0.0.0.0', vm['graphics']['listen']) + self.assertEquals('vnc', vm['graphics']['type']) + # Delete the VM + resp = self.request('/vms/test-vm', '{}', 'DELETE') + self.assertEquals(204, resp.status) + + # Create a VM with specified graphics type and listen + graphics = {'type': 'vnc', 'listen': '127.0.0.1'} + req = json.dumps({'name': 'test-vm', 'template': '/templates/test', 'graphics': graphics}) + resp = self.request('/vms', req, 'POST') + self.assertEquals(201, resp.status) + # Verify the VM + vm = json.loads(self.request('/vms/test-vm').read()) + self.assertEquals('127.0.0.1', vm['graphics']['listen']) + self.assertEquals('vnc', vm['graphics']['type']) + # Delete the VM + resp = self.request('/vms/test-vm', '{}', 'DELETE') + self.assertEquals(204, resp.status) + + # Create a VM with specified graphics type and default listen + graphics = {'type': 'spice'} + req = json.dumps({'name': 'test-vm', 'template': '/templates/test', 'graphics': graphics}) + resp = self.request('/vms', req, 'POST') + self.assertEquals(201, resp.status) + # Verify the VM + vm = json.loads(self.request('/vms/test-vm').read()) + self.assertEquals('0.0.0.0', vm['graphics']['listen']) + self.assertEquals('spice', vm['graphics']['type']) + # Delete the VM + resp = self.request('/vms/test-vm', '{}', 'DELETE') + self.assertEquals(204, resp.status) + + # Try to create a VM with invalid graphics type + graphics = {'type': 'invalid'} + req = json.dumps({'name': 'test-vm', 'template': '/templates/test', 'graphics': graphics}) + resp = self.request('/vms', req, 'POST') + self.assertEquals(400, resp.status) + + # Try to create a VM with invalid graphics listen + graphics = {'type': 'spice', 'listen': 'invalid'} + req = json.dumps({'name': 'test-vm', 'template': '/templates/test', 'graphics': graphics}) + resp = self.request('/vms', req, 'POST') + self.assertEquals(400, resp.status) + + # Delete the Template + resp = self.request('/templates/test', '{}', 'DELETE') + self.assertEquals(204, resp.status) + def test_vm_customise_storage(self): # Create a Template req = json.dumps({'name': 'test', 'cdrom': '/nonexistent.iso', @@ -565,9 +629,10 @@ class RestTests(unittest.TestCase): def test_templates(self): def verify_template(t, res): - for field in ('name', 'os_distro', - 'os_version', 'memory', 'cpus', 'storagepool'): - self.assertEquals(t[field], res[field]) + for field in ('name', 'os_distro', 'os_version', 'memory', + 'cpus', 'storagepool', 'graphics'): + if field in t: + self.assertEquals(t[field], res[field]) resp = self.request('/templates') self.assertEquals(200, resp.status) @@ -582,9 +647,11 @@ class RestTests(unittest.TestCase): self.assertEquals(400, resp.status) # Create a template + graphics = {'type': 'spice', 'listen': '127.0.0.1'} t = {'name': 'test', 'os_distro': 'ImagineOS', 'os_version': '1.0', 'memory': 1024, 'cpus': 1, - 'storagepool': '/storagepools/alt', 'cdrom': '/nonexistent.iso'} + 'storagepool': '/storagepools/alt', 'cdrom': '/nonexistent.iso', + 'graphics': graphics} req = json.dumps(t) resp = self.request('/templates', req, 'POST') self.assertEquals(201, resp.status) @@ -605,6 +672,7 @@ class RestTests(unittest.TestCase): # Update the template t['os_distro'] = 'Linux.ISO' t['os_version'] = '1.1' + t['graphics'] = {'type': 'vnc', 'listen': '0.0.0.0'} req = json.dumps(t) resp = self.request('/templates/%s' % t['name'], req, 'PUT') self.assertEquals(200, resp.status) @@ -663,6 +731,18 @@ class RestTests(unittest.TestCase): resp = self.request('/templates/%s' % tmpl_name, req, 'PUT') self.assertEquals(400, resp.status) + # Try to change template graphics type to invalid value + t['graphics'] = {'type': 'invalid'} + req = json.dumps(t) + resp = self.request('/templates/%s' % tmpl_name, req, 'PUT') + self.assertEquals(400, resp.status) + + # Try to change template graphics type to invalid listen + t['graphics'] = {'type': 'vnc', 'listen': 'invalid'} + req = json.dumps(t) + resp = self.request('/templates/%s' % tmpl_name, req, 'PUT') + self.assertEquals(400, resp.status) + # Try to clean up template cpus value t['cpus'] = ' ' req = json.dumps(t) diff --git a/tests/test_vmtemplate.py b/tests/test_vmtemplate.py index 81382c7..1386a97 100644 --- a/tests/test_vmtemplate.py +++ b/tests/test_vmtemplate.py @@ -31,7 +31,8 @@ class VMTemplateTests(unittest.TestCase): fields = (('name', 'test'), ('os_distro', 'unknown'), ('os_version', 'unknown'), ('cpus', 1), ('memory', 1024), ('cdrom', ''), ('network', 'default'), - ('disk_bus', 'ide'), ('nic_model', 'e1000')) + ('disk_bus', 'ide'), ('nic_model', 'e1000'), + ('graphics', {'type': 'vnc', 'listen': '0.0.0.0'})) args = {'name': 'test'} t = VMTemplate(args) @@ -39,27 +40,58 @@ class VMTemplateTests(unittest.TestCase): self.assertEquals(val, t.info.get(name)) def test_construct_overrides(self): - args = {'name': 'test', 'disks': [{'size': 10}, {'size': 20}]} + graphics = {'type': 'spice', 'listen': '127.0.0.1'} + args = {'name': 'test', 'disks': [{'size': 10}, {'size': 20}], + 'graphics': graphics} t = VMTemplate(args) self.assertEquals(2, len(t.info['disks'])) + self.assertEquals(graphics, t.info['graphics']) + + def test_specified_graphics(self): + # Test specified listen + graphics = {'type': 'vnc', 'listen': '127.0.0.1'} + args = {'name': 'test', 'disks': [{'size': 10}, {'size': 20}], + 'graphics': graphics} + t = VMTemplate(args) + self.assertEquals(graphics, t.info['graphics']) + + # Test specified type + graphics = {'type': 'spice', 'listen': '0.0.0.0'} + args['graphics'] = graphics + t = VMTemplate(args) + self.assertEquals(graphics, t.info['graphics']) + + # If no listen specified, test the default listen + graphics = {'type': 'vnc'} + args['graphics'] = graphics + t = VMTemplate(args) + self.assertEquals(graphics['type'], t.info['graphics']['type']) + self.assertEquals('0.0.0.0', t.info['graphics']['listen']) def test_to_xml(self): + graphics = {'type': 'spice', 'listen': '0.0.0.0'} vm_uuid = str(uuid.uuid4()).replace('-', '') t = VMTemplate({'name': 'test-template'}) - xml = t.to_vm_xml('test-vm', vm_uuid) + xml = t.to_vm_xml('test-vm', vm_uuid, graphics=graphics) self.assertEquals(vm_uuid, xpath_get_text(xml, "/domain/uuid")[0]) self.assertEquals('test-vm', xpath_get_text(xml, "/domain/name")[0]) + expr = "/domain/devices/graphics/@type" + self.assertEquals(graphics['type'], xpath_get_text(xml, expr)[0]) + expr = "/domain/devices/graphics/@listen" + self.assertEquals(graphics['listen'], xpath_get_text(xml, expr)[0]) def test_arg_merging(self): """ Make sure that default parameters from osinfo do not override user- provided parameters. """ + graphics = {'type': 'vnc', 'listen': '127.0.0.1'} args = {'name': 'test', 'os_distro': 'opensuse', 'os_version': '12.3', - 'cpus': 2, 'memory': 2048, 'network': 'foo', - 'cdrom': '/cd.iso'} + 'cpus': 2, 'memory': 2048, 'network': 'foo', 'cdrom': '/cd.iso', + 'graphics': graphics} t = VMTemplate(args) self.assertEquals(2, t.info.get('cpus')) self.assertEquals(2048, t.info.get('memory')) self.assertEquals('foo', t.info.get('network')) self.assertEquals('/cd.iso', t.info.get('cdrom')) + self.assertEquals(graphics, t.info.get('graphics')) -- 1.8.1.2