From 3b1675344d8072462a81c03ccc7a5fef8c3eeb6e Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Tue, 21 May 2013 17:46:21 +0000 Subject: libvirt: Moving tests under tests/virt/libvirt Change-Id: I8adfbb48fa0b05f999618b9f17cbca2b3ea04c93 --- nova/tests/fake_imagebackend.py | 60 - nova/tests/fake_libvirt_utils.py | 205 - nova/tests/fakelibvirt.py | 920 ---- nova/tests/test_fakelibvirt.py | 406 -- nova/tests/test_imagebackend.py | 478 -- nova/tests/test_imagecache.py | 972 ---- nova/tests/test_libvirt.py | 5353 -------------------- nova/tests/test_libvirt_blockinfo.py | 446 -- nova/tests/test_libvirt_config.py | 902 ---- nova/tests/test_libvirt_utils.py | 42 - nova/tests/test_libvirt_vif.py | 628 --- nova/tests/test_libvirt_volume.py | 620 --- nova/tests/test_virt_drivers.py | 8 +- nova/tests/test_xenapi.py | 5 +- nova/tests/virt/libvirt/__init__.py | 15 + nova/tests/virt/libvirt/fake_imagebackend.py | 60 + nova/tests/virt/libvirt/fake_libvirt_utils.py | 205 + nova/tests/virt/libvirt/fakelibvirt.py | 920 ++++ nova/tests/virt/libvirt/test_fakelibvirt.py | 406 ++ nova/tests/virt/libvirt/test_imagebackend.py | 478 ++ nova/tests/virt/libvirt/test_imagecache.py | 972 ++++ nova/tests/virt/libvirt/test_libvirt.py | 5354 +++++++++++++++++++++ nova/tests/virt/libvirt/test_libvirt_blockinfo.py | 446 ++ nova/tests/virt/libvirt/test_libvirt_config.py | 902 ++++ nova/tests/virt/libvirt/test_libvirt_utils.py | 42 + nova/tests/virt/libvirt/test_libvirt_vif.py | 628 +++ nova/tests/virt/libvirt/test_libvirt_volume.py | 620 +++ 27 files changed, 11055 insertions(+), 11038 deletions(-) delete mode 100644 nova/tests/fake_imagebackend.py delete mode 100644 nova/tests/fake_libvirt_utils.py delete mode 100644 nova/tests/fakelibvirt.py delete mode 100644 nova/tests/test_fakelibvirt.py delete mode 100644 nova/tests/test_imagebackend.py delete mode 100644 nova/tests/test_imagecache.py delete mode 100644 nova/tests/test_libvirt.py delete mode 100644 nova/tests/test_libvirt_blockinfo.py delete mode 100644 nova/tests/test_libvirt_config.py delete mode 100644 nova/tests/test_libvirt_utils.py delete mode 100644 nova/tests/test_libvirt_vif.py delete mode 100644 nova/tests/test_libvirt_volume.py create mode 100644 nova/tests/virt/libvirt/__init__.py create mode 100644 nova/tests/virt/libvirt/fake_imagebackend.py create mode 100644 nova/tests/virt/libvirt/fake_libvirt_utils.py create mode 100644 nova/tests/virt/libvirt/fakelibvirt.py create mode 100644 nova/tests/virt/libvirt/test_fakelibvirt.py create mode 100644 nova/tests/virt/libvirt/test_imagebackend.py create mode 100644 nova/tests/virt/libvirt/test_imagecache.py create mode 100644 nova/tests/virt/libvirt/test_libvirt.py create mode 100644 nova/tests/virt/libvirt/test_libvirt_blockinfo.py create mode 100644 nova/tests/virt/libvirt/test_libvirt_config.py create mode 100644 nova/tests/virt/libvirt/test_libvirt_utils.py create mode 100644 nova/tests/virt/libvirt/test_libvirt_vif.py create mode 100644 nova/tests/virt/libvirt/test_libvirt_volume.py diff --git a/nova/tests/fake_imagebackend.py b/nova/tests/fake_imagebackend.py deleted file mode 100644 index 48426505e..000000000 --- a/nova/tests/fake_imagebackend.py +++ /dev/null @@ -1,60 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright 2012 Grid Dynamics -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os - -from nova.virt.libvirt import config -from nova.virt.libvirt import imagebackend - - -class Backend(object): - def __init__(self, use_cow): - pass - - def image(self, instance, name, image_type=''): - class FakeImage(imagebackend.Image): - def __init__(self, instance, name): - self.path = os.path.join(instance['name'], name) - - def create_image(self, prepare_template, base, - size, *args, **kwargs): - pass - - def cache(self, fetch_func, filename, size=None, *args, **kwargs): - pass - - def snapshot(self, name): - pass - - def libvirt_info(self, disk_bus, disk_dev, device_type, - cache_mode, extra_specs): - info = config.LibvirtConfigGuestDisk() - info.source_type = 'file' - info.source_device = device_type - info.target_bus = disk_bus - info.target_dev = disk_dev - info.driver_cache = cache_mode - info.driver_format = 'raw' - info.source_path = self.path - return info - - return FakeImage(instance, name) - - def snapshot(self, path, name, image_type=''): - #NOTE(bfilippov): this is done in favor for - # snapshot tests in test_libvirt.LibvirtConnTestCase - return imagebackend.Backend(True).snapshot(path, name, image_type) diff --git a/nova/tests/fake_libvirt_utils.py b/nova/tests/fake_libvirt_utils.py deleted file mode 100644 index ba00a7091..000000000 --- a/nova/tests/fake_libvirt_utils.py +++ /dev/null @@ -1,205 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright (c) 2011 OpenStack Foundation -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os -import StringIO - -from oslo.config import cfg - -from nova.virt.libvirt import utils as libvirt_utils - - -CONF = cfg.CONF -CONF.import_opt('instances_path', 'nova.compute.manager') - - -files = {'console.log': True} -disk_sizes = {} -disk_backing_files = {} -disk_type = "qcow2" - - -def get_iscsi_initiator(): - return "fake.initiator.iqn" - - -def get_fc_hbas(): - return [{'ClassDevice': 'host1', - 'ClassDevicePath': '/sys/devices/pci0000:00/0000:00:03.0' - '/0000:05:00.2/host1/fc_host/host1', - 'dev_loss_tmo': '30', - 'fabric_name': '0x1000000533f55566', - 'issue_lip': '', - 'max_npiv_vports': '255', - 'maxframe_size': '2048 bytes', - 'node_name': '0x200010604b019419', - 'npiv_vports_inuse': '0', - 'port_id': '0x680409', - 'port_name': '0x100010604b019419', - 'port_state': 'Online', - 'port_type': 'NPort (fabric via point-to-point)', - 'speed': '10 Gbit', - 'supported_classes': 'Class 3', - 'supported_speeds': '10 Gbit', - 'symbolic_name': 'Emulex 554M FV4.0.493.0 DV8.3.27', - 'tgtid_bind_type': 'wwpn (World Wide Port Name)', - 'uevent': None, - 'vport_create': '', - 'vport_delete': ''}] - - -def get_fc_hbas_info(): - hbas = get_fc_hbas() - info = [{'port_name': hbas[0]['port_name'].replace('0x', ''), - 'node_name': hbas[0]['node_name'].replace('0x', ''), - 'host_device': hbas[0]['ClassDevice'], - 'device_path': hbas[0]['ClassDevicePath']}] - return info - - -def get_fc_wwpns(): - hbas = get_fc_hbas() - wwpns = [] - for hba in hbas: - wwpn = hba['port_name'].replace('0x', '') - wwpns.append(wwpn) - - return wwpns - - -def get_fc_wwnns(): - hbas = get_fc_hbas() - wwnns = [] - for hba in hbas: - wwnn = hba['node_name'].replace('0x', '') - wwnns.append(wwnn) - - return wwnns - - -def create_image(disk_format, path, size): - pass - - -def create_cow_image(backing_file, path): - pass - - -def get_disk_backing_file(path): - return disk_backing_files.get(path, None) - - -def get_disk_type(path): - return disk_type - - -def copy_image(src, dest): - pass - - -def resize2fs(path): - pass - - -def create_lvm_image(vg, lv, size, sparse=False): - pass - - -def volume_group_free_space(vg): - pass - - -def remove_logical_volumes(*paths): - pass - - -def write_to_file(path, contents, umask=None): - pass - - -def chown(path, owner): - pass - - -def create_snapshot(disk_path, snapshot_name): - pass - - -def delete_snapshot(disk_path, snapshot_name): - pass - - -def extract_snapshot(disk_path, source_fmt, snapshot_name, out_path, dest_fmt): - files[out_path] = '' - - -class File(object): - def __init__(self, path, mode=None): - if path in files: - self.fp = StringIO.StringIO(files[path]) - else: - self.fp = StringIO.StringIO(files[os.path.split(path)[-1]]) - - def __enter__(self): - return self.fp - - def __exit__(self, *args): - return - - def close(self, *args, **kwargs): - self.fp.close() - - -def file_open(path, mode=None): - return File(path, mode) - - -def find_disk(virt_dom): - return "filename" - - -def load_file(path): - if os.path.exists(path): - with open(path, 'r') as fp: - return fp.read() - else: - return '' - - -def logical_volume_info(path): - return {} - - -def file_delete(path): - return True - - -def get_fs_info(path): - return {'total': 128 * (1024 ** 3), - 'used': 44 * (1024 ** 3), - 'free': 84 * (1024 ** 3)} - - -def fetch_image(context, target, image_id, user_id, project_id): - pass - - -def get_instance_path(instance, forceold=False): - return libvirt_utils.get_instance_path(instance, forceold=forceold) - - -def pick_disk_driver_name(is_block_dev=False): - return "qemu" diff --git a/nova/tests/fakelibvirt.py b/nova/tests/fakelibvirt.py deleted file mode 100644 index 30c3e4d9c..000000000 --- a/nova/tests/fakelibvirt.py +++ /dev/null @@ -1,920 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 -# -# Copyright 2010 OpenStack Foundation -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from lxml import etree - -import time -import uuid - -# Allow passing None to the various connect methods -# (i.e. allow the client to rely on default URLs) -allow_default_uri_connection = True - -# string indicating the CPU arch -node_arch = 'x86_64' # or 'i686' (or whatever else uname -m might return) - -# memory size in kilobytes -node_kB_mem = 4096 - -# the number of active CPUs -node_cpus = 2 - -# expected CPU frequency -node_mhz = 800 - -# the number of NUMA cell, 1 for unusual NUMA topologies or uniform -# memory access; check capabilities XML for the actual NUMA topology -node_nodes = 1 # NUMA nodes - -# number of CPU sockets per node if nodes > 1, total number of CPU -# sockets otherwise -node_sockets = 1 - -# number of cores per socket -node_cores = 2 - -# number of threads per core -node_threads = 1 - -# CPU model -node_cpu_model = "Penryn" - -# CPU vendor -node_cpu_vendor = "Intel" - - -def _reset(): - global allow_default_uri_connection - allow_default_uri_connection = True - -# virDomainState -VIR_DOMAIN_NOSTATE = 0 -VIR_DOMAIN_RUNNING = 1 -VIR_DOMAIN_BLOCKED = 2 -VIR_DOMAIN_PAUSED = 3 -VIR_DOMAIN_SHUTDOWN = 4 -VIR_DOMAIN_SHUTOFF = 5 -VIR_DOMAIN_CRASHED = 6 - -VIR_DOMAIN_XML_SECURE = 1 - -VIR_DOMAIN_EVENT_ID_LIFECYCLE = 0 - -VIR_DOMAIN_EVENT_DEFINED = 0 -VIR_DOMAIN_EVENT_UNDEFINED = 1 -VIR_DOMAIN_EVENT_STARTED = 2 -VIR_DOMAIN_EVENT_SUSPENDED = 3 -VIR_DOMAIN_EVENT_RESUMED = 4 -VIR_DOMAIN_EVENT_STOPPED = 5 -VIR_DOMAIN_EVENT_SHUTDOWN = 6 -VIR_DOMAIN_EVENT_PMSUSPENDED = 7 - -VIR_DOMAIN_UNDEFINE_MANAGED_SAVE = 1 - -VIR_DOMAIN_AFFECT_CURRENT = 0 -VIR_DOMAIN_AFFECT_LIVE = 1 -VIR_DOMAIN_AFFECT_CONFIG = 2 - -VIR_CPU_COMPARE_ERROR = -1 -VIR_CPU_COMPARE_INCOMPATIBLE = 0 -VIR_CPU_COMPARE_IDENTICAL = 1 -VIR_CPU_COMPARE_SUPERSET = 2 - -VIR_CRED_USERNAME = 1 -VIR_CRED_AUTHNAME = 2 -VIR_CRED_LANGUAGE = 3 -VIR_CRED_CNONCE = 4 -VIR_CRED_PASSPHRASE = 5 -VIR_CRED_ECHOPROMPT = 6 -VIR_CRED_NOECHOPROMPT = 7 -VIR_CRED_REALM = 8 -VIR_CRED_EXTERNAL = 9 - -VIR_MIGRATE_PEER2PEER = 2 -VIR_MIGRATE_UNDEFINE_SOURCE = 16 - -# libvirtError enums -# (Intentionally different from what's in libvirt. We do this to check, -# that consumers of the library are using the symbolic names rather than -# hardcoding the numerical values) -VIR_FROM_QEMU = 100 -VIR_FROM_DOMAIN = 200 -VIR_FROM_NWFILTER = 330 -VIR_FROM_REMOTE = 340 -VIR_FROM_RPC = 345 -VIR_ERR_XML_DETAIL = 350 -VIR_ERR_NO_DOMAIN = 420 -VIR_ERR_NO_NWFILTER = 620 -VIR_ERR_SYSTEM_ERROR = 900 -VIR_ERR_INTERNAL_ERROR = 950 - - -def _parse_disk_info(element): - disk_info = {} - disk_info['type'] = element.get('type', 'file') - disk_info['device'] = element.get('device', 'disk') - - driver = element.find('./driver') - if driver is not None: - disk_info['driver_name'] = driver.get('name') - disk_info['driver_type'] = driver.get('type') - - source = element.find('./source') - if source is not None: - disk_info['source'] = source.get('file') - if not disk_info['source']: - disk_info['source'] = source.get('dev') - - if not disk_info['source']: - disk_info['source'] = source.get('path') - - target = element.find('./target') - if target is not None: - disk_info['target_dev'] = target.get('dev') - disk_info['target_bus'] = target.get('bus') - - return disk_info - - -class libvirtError(Exception): - def __init__(self, msg, - error_code=VIR_ERR_INTERNAL_ERROR, - error_domain=VIR_FROM_QEMU): - self.error_code = error_code - self.error_domain = error_domain - Exception(self, msg) - - def get_error_code(self): - return self.error_code - - def get_error_domain(self): - return self.error_domain - - -class NWFilter(object): - def __init__(self, connection, xml): - self._connection = connection - - self._xml = xml - self._parse_xml(xml) - - def _parse_xml(self, xml): - tree = etree.fromstring(xml) - root = tree.find('.') - self._name = root.get('name') - - def undefine(self): - self._connection._remove_filter(self) - - -class Domain(object): - def __init__(self, connection, xml, running=False, transient=False): - self._connection = connection - if running: - connection._mark_running(self) - - self._state = running and VIR_DOMAIN_RUNNING or VIR_DOMAIN_SHUTOFF - self._transient = transient - self._def = self._parse_definition(xml) - self._has_saved_state = False - self._snapshots = {} - self._id = self._connection._id_counter - - def _parse_definition(self, xml): - try: - tree = etree.fromstring(xml) - except etree.ParseError: - raise libvirtError("Invalid XML.", - VIR_ERR_XML_DETAIL, VIR_FROM_DOMAIN) - - definition = {} - - name = tree.find('./name') - if name is not None: - definition['name'] = name.text - - uuid_elem = tree.find('./uuid') - if uuid_elem is not None: - definition['uuid'] = uuid_elem.text - else: - definition['uuid'] = str(uuid.uuid4()) - - vcpu = tree.find('./vcpu') - if vcpu is not None: - definition['vcpu'] = int(vcpu.text) - - memory = tree.find('./memory') - if memory is not None: - definition['memory'] = int(memory.text) - - os = {} - os_type = tree.find('./os/type') - if os_type is not None: - os['type'] = os_type.text - os['arch'] = os_type.get('arch', node_arch) - - os_kernel = tree.find('./os/kernel') - if os_kernel is not None: - os['kernel'] = os_kernel.text - - os_initrd = tree.find('./os/initrd') - if os_initrd is not None: - os['initrd'] = os_initrd.text - - os_cmdline = tree.find('./os/cmdline') - if os_cmdline is not None: - os['cmdline'] = os_cmdline.text - - os_boot = tree.find('./os/boot') - if os_boot is not None: - os['boot_dev'] = os_boot.get('dev') - - definition['os'] = os - - features = {} - - acpi = tree.find('./features/acpi') - if acpi is not None: - features['acpi'] = True - - definition['features'] = features - - devices = {} - - device_nodes = tree.find('./devices') - if device_nodes is not None: - disks_info = [] - disks = device_nodes.findall('./disk') - for disk in disks: - disks_info += [_parse_disk_info(disk)] - devices['disks'] = disks_info - - nics_info = [] - nics = device_nodes.findall('./interface') - for nic in nics: - nic_info = {} - nic_info['type'] = nic.get('type') - - mac = nic.find('./mac') - if mac is not None: - nic_info['mac'] = mac.get('address') - - source = nic.find('./source') - if source is not None: - if nic_info['type'] == 'network': - nic_info['source'] = source.get('network') - elif nic_info['type'] == 'bridge': - nic_info['source'] = source.get('bridge') - - nics_info += [nic_info] - - devices['nics'] = nics_info - - definition['devices'] = devices - - return definition - - def create(self): - self.createWithFlags(0) - - def createWithFlags(self, flags): - # FIXME: Not handling flags at the moment - self._state = VIR_DOMAIN_RUNNING - self._connection._mark_running(self) - self._has_saved_state = False - - def isActive(self): - return int(self._state == VIR_DOMAIN_RUNNING) - - def undefine(self): - self._connection._undefine(self) - - def undefineFlags(self, flags): - self.undefine() - if flags & VIR_DOMAIN_UNDEFINE_MANAGED_SAVE: - if self.hasManagedSaveImage(0): - self.managedSaveRemove() - - def destroy(self): - self._state = VIR_DOMAIN_SHUTOFF - self._connection._mark_not_running(self) - - def ID(self): - return self._id - - def name(self): - return self._def['name'] - - def UUIDString(self): - return self._def['uuid'] - - def interfaceStats(self, device): - return [10000242400, 1234, 0, 2, 213412343233, 34214234, 23, 3] - - def blockStats(self, device): - return [2, 10000242400, 234, 2343424234, 34] - - def suspend(self): - self._state = VIR_DOMAIN_PAUSED - - def shutdown(self): - self._state = VIR_DOMAIN_SHUTDOWN - self._connection._mark_not_running(self) - - def reset(self, flags): - # FIXME: Not handling flags at the moment - self._state = VIR_DOMAIN_RUNNING - self._connection._mark_running(self) - - def info(self): - return [self._state, - long(self._def['memory']), - long(self._def['memory']), - self._def['vcpu'], - 123456789L] - - def migrateToURI(self, desturi, flags, dname, bandwidth): - raise libvirtError("Migration always fails for fake libvirt!") - - def attachDevice(self, xml): - disk_info = _parse_disk_info(etree.fromstring(xml)) - disk_info['_attached'] = True - self._def['devices']['disks'] += [disk_info] - return True - - def attachDeviceFlags(self, xml, flags): - if (flags & VIR_DOMAIN_AFFECT_LIVE and - self._state != VIR_DOMAIN_RUNNING): - raise libvirtError("AFFECT_LIVE only allowed for running domains!") - self.attachDevice(xml) - - def detachDevice(self, xml): - disk_info = _parse_disk_info(etree.fromstring(xml)) - disk_info['_attached'] = True - return disk_info in self._def['devices']['disks'] - - def detachDeviceFlags(self, xml, _flags): - self.detachDevice(xml) - - def XMLDesc(self, flags): - disks = '' - for disk in self._def['devices']['disks']: - disks += ''' - - - -
- ''' % disk - - nics = '' - for nic in self._def['devices']['nics']: - nics += ''' - - -
- ''' % nic - - return ''' - %(name)s - %(uuid)s - %(memory)s - %(memory)s - %(vcpu)s - - hvm - - - - - - - - - destroy - restart - restart - - /usr/bin/kvm - %(disks)s - -
- - %(nics)s - - - - - - - - - - - - - - - - -