
Reviewed-by: zhoumeina <zhoumein@linux.vnet.ibm.com> On 01/10/2014 08:44 PM, apporc wrote:
Because all of params validation are moved to control, i removed the test code about graphics data validation in model.py. Test code for ipv6 is added too.
Signed-off-by: apporc <appleorchard2000@gmail.com> --- tests/test_mockmodel.py | 2 + tests/test_model.py | 27 ++++++++++- tests/test_rest.py | 111 ++++++++++++++++++++++++++++++++++++++++++++-- tests/test_vmtemplate.py | 40 +++++++++++++++-- 4 files changed, 171 insertions(+), 9 deletions(-)
diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py index f167dc6..29413a9 100644 --- a/tests/test_mockmodel.py +++ b/tests/test_mockmodel.py @@ -150,3 +150,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 c689bcc..d502bbf 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -67,7 +67,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') @@ -96,6 +95,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 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 a960868..8ab1bc3 100644 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -255,9 +255,86 @@ 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 listen as ipv6 address + graphics = {'type': 'spice', 'listen': 'fe00::0'} + 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('fe00::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) + + # 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', @@ -638,9 +715,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) @@ -655,9 +733,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) @@ -678,6 +758,17 @@ 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) + + # Verify the template + res = json.loads(self.request('/templates/test').read()) + verify_template(t, res) + + # Update the template with ipv6 address as listen + t['graphics'] = {'type': 'vnc', 'listen': 'fe00::0'} req = json.dumps(t) resp = self.request('/templates/%s' % t['name'], req, 'PUT') self.assertEquals(200, resp.status) @@ -736,6 +827,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 dc9c0ef..ee20dbf 100644 --- a/tests/test_vmtemplate.py +++ b/tests/test_vmtemplate.py @@ -33,7 +33,8 @@ class VMTemplateTests(unittest.TestCase): fields = (('name', 'test'), ('os_distro', 'unknown'), ('os_version', 'unknown'), ('cpus', 1), ('memory', 1024), ('cdrom', ''), ('networks', ['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) @@ -41,27 +42,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, 'networks': ['foo'], - 'cdrom': '/cd.iso'} + '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('networks')) self.assertEquals('/cd.iso', t.info.get('cdrom')) + self.assertEquals(graphics, t.info.get('graphics'))