def test_modern_bases(self): for distro, version in modern_version_bases[_get_arch()].iteritems(): entry = lookup(distro, version) self.assertEquals(entry['disk_bus'], get_template_default('modern', 'disk_bus')) self.assertEquals(entry['nic_model'], get_template_default('modern', 'nic_model'))
def test_lookup_unknown_distro_version_returns_old_distro(self): distro = 'unknown_distro' version = 'unknown_version' entry = lookup(distro, version) self.assertEquals(entry['disk_bus'], get_template_default('old', 'disk_bus')) self.assertEquals(entry['nic_model'], get_template_default('old', 'nic_model'))
def test_old_distros(self): old_versions = {'debian': '5.0', 'ubuntu': '7.04', 'opensuse': '10.1', 'centos': '5.1', 'rhel': '5.1', 'fedora': '15'} for distro, version in old_versions.iteritems(): entry = lookup(distro, version) self.assertEquals(entry['disk_bus'], get_template_default('old', 'disk_bus')) self.assertEquals(entry['nic_model'], get_template_default('old', 'nic_model'))
def test_modern_distros(self): # versions based on ppc64 modern distros modern_versions = {'ubuntu': '14.04', 'opensuse': '13.1', 'rhel': '6.5', 'fedora': '19', 'sles': '11sp3'} for distro, version in modern_versions.iteritems(): entry = lookup(distro, version) self.assertEquals(entry['disk_bus'], get_template_default('modern', 'disk_bus')) self.assertEquals(entry['nic_model'], get_template_default('modern', 'nic_model'))
def test_minimal_construct(self): disk_bus = get_template_default('old', 'disk_bus') memory = get_template_default('old', 'memory') nic_model = get_template_default('old', 'nic_model') fields = (('name', 'test'), ('os_distro', 'unknown'), ('os_version', 'unknown'), ('cpus', 1), ('memory', memory), ('networks', ['default']), ('disk_bus', disk_bus), ('nic_model', nic_model), ('graphics', {'type': 'vnc', 'listen': '127.0.0.1'}), ('cdrom', self.iso)) args = {'name': 'test', 'cdrom': self.iso} t = VMTemplate(args) for name, val in fields: self.assertEquals(val, t.info.get(name))
def test_modern_distros(self): # versions based on ppc64 modern distros modern_versions = { 'ubuntu': '14.04', 'opensuse': '13.1', 'rhel': '6.5', 'fedora': '19', 'sles': '11sp3' } for distro, version in modern_versions.iteritems(): entry = lookup(distro, version) self.assertEquals(entry['disk_bus'], get_template_default('modern', 'disk_bus')) self.assertEquals(entry['nic_model'], get_template_default('modern', 'nic_model'))
def test_old_distros(self): old_versions = { 'debian': '5.0', 'ubuntu': '7.04', 'opensuse': '10.1', 'centos': '5.1', 'rhel': '5.1', 'fedora': '15' } for distro, version in old_versions.iteritems(): entry = lookup(distro, version) self.assertEquals(entry['disk_bus'], get_template_default('old', 'disk_bus')) self.assertEquals(entry['nic_model'], get_template_default('old', 'nic_model'))
def test_vm_info(self): model.templates_create({'name': u'test', 'cdrom': fake_iso}) model.vms_create({'name': u'test-vm', 'template': '/templates/test'}) vms = model.vms_get_list() self.assertEquals(2, len(vms)) self.assertIn(u'test-vm', vms) keys = set( ('name', 'state', 'stats', 'uuid', 'memory', 'cpus', 'screenshot', 'icon', 'graphics', 'users', 'groups', 'access', 'persistent')) stats_keys = set( ('cpu_utilization', 'net_throughput', 'net_throughput_peak', 'io_throughput', 'io_throughput_peak')) info = model.vm_lookup(u'test-vm') self.assertEquals(keys, set(info.keys())) self.assertEquals('shutoff', info['state']) self.assertEquals('test-vm', info['name']) self.assertEquals(get_template_default('old', 'memory'), info['memory']) self.assertEquals(1, info['cpus']) self.assertEquals('images/icon-vm.png', info['icon']) self.assertEquals(stats_keys, set(info['stats'].keys())) self.assertEquals('vnc', info['graphics']['type']) self.assertEquals('127.0.0.1', info['graphics']['listen'])
def test_iso_scan_shallow(self): # fake environment preparation self._create_pool('pool-3') self.request('/storagepools/pool-3/activate', '{}', 'POST') params = {'name': 'fedora.iso', 'capacity': 1073741824, # 1 GiB 'type': 'file', 'format': 'iso'} task_info = model.storagevolumes_create('pool-3', params) wait_task(self._task_lookup, task_info['id']) storagevolume = json.loads(self.request( '/storagepools/kimchi_isos/storagevolumes/').read())[0] self.assertEquals('fedora.iso', storagevolume['name']) self.assertEquals('iso', storagevolume['format']) self.assertEquals('/var/lib/libvirt/images/fedora.iso', storagevolume['path']) self.assertEquals(1073741824, storagevolume['capacity']) # 1 GiB self.assertEquals(0, storagevolume['allocation']) self.assertEquals('17', storagevolume['os_version']) self.assertEquals('fedora', storagevolume['os_distro']) self.assertEquals(True, storagevolume['bootable']) # Create a template # In real model os distro/version can be omitted # as we will scan the iso req = json.dumps({'name': 'test', 'cdrom': storagevolume['path'], 'os_distro': storagevolume['os_distro'], 'os_version': storagevolume['os_version']}) resp = self.request('/templates', req, 'POST') self.assertEquals(201, resp.status) # Verify the template t = json.loads(self.request('/templates/test').read()) self.assertEquals('test', t['name']) self.assertEquals('fedora', t['os_distro']) self.assertEquals('17', t['os_version']) self.assertEquals(get_template_default('old', 'memory'), t['memory']) # Deactivate or destroy scan pool return 405 resp = self.request('/storagepools/kimchi_isos/storagevolumes' '/deactivate', '{}', 'POST') self.assertEquals(405, resp.status) resp = self.request('/storagepools/kimchi_isos/storagevolumes', '{}', 'DELETE') self.assertEquals(405, resp.status) # Delete the template resp = self.request('/templates/%s' % t['name'], '{}', 'DELETE') self.assertEquals(204, resp.status) resp = self.request('/storagepools/pool-3/deactivate', '{}', 'POST') self.assertEquals(200, resp.status) self._delete_pool('pool-3')
def test_minimal_construct(self): disk_bus = get_template_default("old", "disk_bus") memory = get_template_default("old", "memory") nic_model = get_template_default("old", "nic_model") fields = ( ("name", "test"), ("os_distro", "unknown"), ("os_version", "unknown"), ("cpus", 1), ("memory", memory), ("networks", ["default"]), ("disk_bus", disk_bus), ("nic_model", nic_model), ("graphics", {"type": "vnc", "listen": "127.0.0.1"}), ("cdrom", self.iso), ) args = {"name": "test", "cdrom": self.iso} t = VMTemplate(args) for name, val in fields: self.assertEquals(val, t.info.get(name))
def test_vm_iface(self): with RollbackContext() as rollback: # Create a template as a base for our VMs req = json.dumps({'name': 'test', 'cdrom': fake_iso}) resp = self.request('/templates', req, 'POST') self.assertEquals(201, resp.status) # Delete the template rollback.prependDefer(self.request, '/templates/test', '{}', 'DELETE') # Create a VM with default args req = json.dumps({'name': 'test-vm', 'template': '/templates/test'}) resp = self.request('/vms', req, 'POST') self.assertEquals(202, resp.status) task = json.loads(resp.read()) wait_task(self._task_lookup, task['id']) # Delete the VM rollback.prependDefer(self.request, '/vms/test-vm', '{}', 'DELETE') # Create a network req = json.dumps({'name': 'test-network', 'connection': 'nat', 'net': '127.0.1.0/24'}) resp = self.request('/networks', req, 'POST') self.assertEquals(201, resp.status) # Delete the network rollback.prependDefer(self.request, '/networks/test-network', '{}', 'DELETE') ifaces = json.loads(self.request('/vms/test-vm/ifaces').read()) self.assertEquals(1, len(ifaces)) for iface in ifaces: res = json.loads(self.request('/vms/test-vm/ifaces/%s' % iface['mac']).read()) self.assertEquals('default', res['network']) self.assertEquals(17, len(res['mac'])) self.assertEquals(get_template_default('old', 'nic_model'), res['model']) # try to attach an interface without specifying 'model' req = json.dumps({'type': 'network'}) resp = self.request('/vms/test-vm/ifaces', req, 'POST') self.assertEquals(400, resp.status) # attach network interface to vm req = json.dumps({"type": "network", "network": "test-network", "model": "virtio"}) resp = self.request('/vms/test-vm/ifaces', req, 'POST') self.assertEquals(201, resp.status) iface = json.loads(resp.read()) self.assertEquals('test-network', iface['network']) self.assertEquals(17, len(iface['mac'])) self.assertEquals('virtio', iface['model']) self.assertEquals('network', iface['type']) # update vm interface newMacAddr = '54:50:e3:44:8a:af' req = json.dumps({"network": "default", "model": "virtio", "type": "network", "mac": newMacAddr}) resp = self.request('/vms/test-vm/ifaces/%s' % iface['mac'], req, 'PUT') self.assertEquals(303, resp.status) iface = json.loads(self.request('/vms/test-vm/ifaces/%s' % newMacAddr).read()) self.assertEquals(newMacAddr, iface['mac']) # detach network interface from vm resp = self.request('/vms/test-vm/ifaces/%s' % iface['mac'], '{}', 'DELETE') self.assertEquals(204, resp.status)
def test_vm_disk(self): disk_path = '/tmp/existent2.iso' open(disk_path, 'w').close() modern_disk_bus = get_template_default('modern', 'disk_bus') def _attach_disk(expect_bus=modern_disk_bus): disk_args = {"type": "disk", "pool": pool, "vol": vol} disk = inst.vmstorages_create(vm_name, disk_args) storage_list = inst.vmstorages_get_list(vm_name) self.assertEquals(prev_count + 1, len(storage_list)) # Check the bus type to be 'virtio' disk_info = inst.vmstorage_lookup(vm_name, disk) self.assertEquals(u'disk', disk_info['type']) self.assertEquals(vol_path, disk_info['path']) self.assertEquals(expect_bus, disk_info['bus']) return disk inst = model.Model(objstore_loc=self.tmp_store) with RollbackContext() as rollback: path = os.path.join(os.getcwd(), 'kimchi-images') pool = 'test-pool' vol = 'test-volume.img' vol_path = "%s/%s" % (path, vol) if not os.path.exists(path): os.mkdir(path) rollback.prependDefer(shutil.rmtree, path) args = {'name': pool, 'path': path, 'type': 'dir'} inst.storagepools_create(args) rollback.prependDefer(inst.storagepool_delete, pool) # Activate the pool before adding any volume inst.storagepool_activate(pool) rollback.prependDefer(inst.storagepool_deactivate, pool) params = {'name': vol, 'capacity': 1073741824, # 1 GiB 'allocation': 536870912, # 512 MiB 'format': 'qcow2'} task_id = inst.storagevolumes_create(pool, params)['id'] rollback.prependDefer(inst.storagevolume_delete, pool, vol) inst.task_wait(task_id) vm_name = 'kimchi-cdrom' params = {'name': 'test', 'disks': [], 'cdrom': UBUNTU_ISO} inst.templates_create(params) rollback.prependDefer(inst.template_delete, 'test') params = {'name': vm_name, 'template': '/templates/test'} inst.vms_create(params) rollback.prependDefer(inst.vm_delete, vm_name) prev_count = len(inst.vmstorages_get_list(vm_name)) self.assertEquals(1, prev_count) # Volume format with mismatched type raise error cdrom_args = {"type": "cdrom", "pool": pool, "vol": vol} self.assertRaises(InvalidParameter, inst.vmstorages_create, vm_name, cdrom_args) # Cold plug and unplug a disk disk = _attach_disk() inst.vmstorage_delete(vm_name, disk) # Hot plug a disk inst.vm_start(vm_name) disk = _attach_disk() # VM disk still there after powered off inst.vm_poweroff(vm_name) disk_info = inst.vmstorage_lookup(vm_name, disk) self.assertEquals(u'disk', disk_info['type']) inst.vmstorage_delete(vm_name, disk) # Specifying pool and path at same time will fail disk_args = {"type": "disk", "pool": pool, "vol": vol, "path": disk_path} self.assertRaises( InvalidParameter, inst.vmstorages_create, vm_name, disk_args) old_distro_iso = ISO_PATH + 'rhel4_8.iso' iso_gen.construct_fake_iso(old_distro_iso, True, '4.8', 'rhel') vm_name = 'kimchi-ide-bus-vm' params = {'name': 'old_distro_template', 'disks': [], 'cdrom': old_distro_iso} inst.templates_create(params) rollback.prependDefer(inst.template_delete, 'old_distro_template') params = {'name': vm_name, 'template': '/templates/old_distro_template'} inst.vms_create(params) rollback.prependDefer(inst.vm_delete, vm_name) # Need to check the right disk_bus for old distro disk = _attach_disk(get_template_default('old', 'disk_bus')) inst.vmstorage_delete('kimchi-ide-bus-vm', disk) # Hot plug IDE bus disk does not work inst.vm_start(vm_name) self.assertRaises(InvalidOperation, _attach_disk) inst.vm_poweroff(vm_name)
def test_vm_disk(self): disk_path = '/tmp/existent2.iso' open(disk_path, 'w').close() modern_disk_bus = get_template_default('modern', 'disk_bus') def _attach_disk(expect_bus=modern_disk_bus): disk_args = {"type": "disk", "pool": pool, "vol": vol} disk = inst.vmstorages_create(vm_name, disk_args) storage_list = inst.vmstorages_get_list(vm_name) self.assertEquals(prev_count + 1, len(storage_list)) # Check the bus type to be 'virtio' disk_info = inst.vmstorage_lookup(vm_name, disk) self.assertEquals(u'disk', disk_info['type']) self.assertEquals(vol_path, disk_info['path']) self.assertEquals(expect_bus, disk_info['bus']) return disk inst = model.Model(objstore_loc=self.tmp_store) with RollbackContext() as rollback: path = os.path.join(os.getcwd(), 'kimchi-images') pool = 'test-pool' vol = 'test-volume.img' vol_path = "%s/%s" % (path, vol) if not os.path.exists(path): os.mkdir(path) rollback.prependDefer(shutil.rmtree, path) args = {'name': pool, 'path': path, 'type': 'dir'} inst.storagepools_create(args) rollback.prependDefer(inst.storagepool_delete, pool) # Activate the pool before adding any volume inst.storagepool_activate(pool) rollback.prependDefer(inst.storagepool_deactivate, pool) params = { 'name': vol, 'capacity': 1073741824, # 1 GiB 'allocation': 536870912, # 512 MiB 'format': 'qcow2' } task_id = inst.storagevolumes_create(pool, params)['id'] rollback.prependDefer(inst.storagevolume_delete, pool, vol) inst.task_wait(task_id) vm_name = 'kimchi-cdrom' params = {'name': 'test', 'disks': [], 'cdrom': UBUNTU_ISO} inst.templates_create(params) rollback.prependDefer(inst.template_delete, 'test') params = {'name': vm_name, 'template': '/templates/test'} inst.vms_create(params) rollback.prependDefer(inst.vm_delete, vm_name) prev_count = len(inst.vmstorages_get_list(vm_name)) self.assertEquals(1, prev_count) # Volume format with mismatched type raise error cdrom_args = {"type": "cdrom", "pool": pool, "vol": vol} self.assertRaises(InvalidParameter, inst.vmstorages_create, vm_name, cdrom_args) # Cold plug and unplug a disk disk = _attach_disk() inst.vmstorage_delete(vm_name, disk) # Hot plug a disk inst.vm_start(vm_name) disk = _attach_disk() # VM disk still there after powered off inst.vm_poweroff(vm_name) disk_info = inst.vmstorage_lookup(vm_name, disk) self.assertEquals(u'disk', disk_info['type']) inst.vmstorage_delete(vm_name, disk) # Specifying pool and path at same time will fail disk_args = { "type": "disk", "pool": pool, "vol": vol, "path": disk_path } self.assertRaises(InvalidParameter, inst.vmstorages_create, vm_name, disk_args) old_distro_iso = ISO_PATH + 'rhel4_8.iso' iso_gen.construct_fake_iso(old_distro_iso, True, '4.8', 'rhel') vm_name = 'kimchi-ide-bus-vm' params = { 'name': 'old_distro_template', 'disks': [], 'cdrom': old_distro_iso } inst.templates_create(params) rollback.prependDefer(inst.template_delete, 'old_distro_template') params = { 'name': vm_name, 'template': '/templates/old_distro_template' } inst.vms_create(params) rollback.prependDefer(inst.vm_delete, vm_name) # Need to check the right disk_bus for old distro disk = _attach_disk(get_template_default('old', 'disk_bus')) inst.vmstorage_delete('kimchi-ide-bus-vm', disk) # Hot plug IDE bus disk does not work inst.vm_start(vm_name) self.assertRaises(InvalidOperation, _attach_disk) inst.vm_poweroff(vm_name)