diff options
author | Jenkins <jenkins@review.openstack.org> | 2016-06-01 21:52:21 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2016-06-01 21:52:22 +0000 |
commit | ccd82a556b37396a9b3592d0b051e0199cf9ddf8 (patch) | |
tree | 07791c4ccd5b69b21d762383ad06e4ba8e9e537d | |
parent | 4adee208f990b7a2388e2b0d244730844b6eea3d (diff) | |
parent | 7b6f7ecfa6c7190d9136ed9287cfd86bdd5023d7 (diff) | |
download | nova-ccd82a556b37396a9b3592d0b051e0199cf9ddf8.tar.gz |
Merge "libvirt: New configuration classes to parse device address element"
-rw-r--r-- | nova/tests/unit/virt/libvirt/test_config.py | 55 | ||||
-rw-r--r-- | nova/virt/libvirt/config.py | 67 |
2 files changed, 122 insertions, 0 deletions
diff --git a/nova/tests/unit/virt/libvirt/test_config.py b/nova/tests/unit/virt/libvirt/test_config.py index f330ca0ca6..09c4512a2b 100644 --- a/nova/tests/unit/virt/libvirt/test_config.py +++ b/nova/tests/unit/virt/libvirt/test_config.py @@ -927,6 +927,44 @@ class LibvirtConfigGuestDiskTest(LibvirtConfigBaseTest): <blockio logical_block_size="4096" physical_block_size="4096"/> </disk>""", xml) + def test_config_disk_device_address(self): + xml = """ + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/var/lib/libvirt/images/centos.qcow2'/> + <target dev='vda' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x09' + function='0x0'/> + </disk>""" + + obj = config.LibvirtConfigGuestDisk() + obj.parse_str(xml) + + self.assertIsInstance(obj.device_addr, + config.LibvirtConfigGuestDeviceAddressPCI) + self.assertEqual('0000:00:09.0', obj.device_addr.format_address()) + + def test_config_disk_device_address_no_format(self): + xml = """ + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/var/lib/libvirt/images/generic.qcow2'/> + <target dev='sda' bus='scsi'/> + <address type='drive' controller='0' bus='0' + target='0' unit='1'/> + </disk>""" + + obj = config.LibvirtConfigGuestDisk() + obj.parse_str(xml) + + self.assertIsInstance(obj.device_addr, + config.LibvirtConfigGuestDeviceAddressDrive) + self.assertEqual(('0', '0', '0', '1'), (obj.device_addr.controller, + obj.device_addr.bus, + obj.device_addr.target, + obj.device_addr.unit)) + self.assertIsNone(obj.device_addr.format_address()) + class LibvirtConfigGuestSnapshotDiskTest(LibvirtConfigBaseTest): @@ -1541,6 +1579,23 @@ class LibvirtConfigGuestInterfaceTest(LibvirtConfigBaseTest): obj2.parse_str(xml) self.assertXmlEqual(xml, obj2.to_xml()) + def test_config_interface_address(self): + xml = """ + <interface type='network'> + <mac address='52:54:00:f6:35:8f'/> + <source network='default'/> + <model type='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' + slot='0x03' function='0x0'/> + </interface>""" + + obj = config.LibvirtConfigGuestInterface() + obj.parse_str(xml) + + self.assertIsInstance(obj.device_addr, + config.LibvirtConfigGuestDeviceAddressPCI) + self.assertEqual('0000:00:03.0', obj.device_addr.format_address()) + class LibvirtConfigGuestFeatureTest(LibvirtConfigBaseTest): diff --git a/nova/virt/libvirt/config.py b/nova/virt/libvirt/config.py index d0bfe70fd6..e3d5209b36 100644 --- a/nova/virt/libvirt/config.py +++ b/nova/virt/libvirt/config.py @@ -738,6 +738,7 @@ class LibvirtConfigGuestDisk(LibvirtConfigGuestDevice): self.shareable = False self.snapshot = None self.backing_store = None + self.device_addr = None def format_dom(self): dev = super(LibvirtConfigGuestDisk, self).format_dom() @@ -889,6 +890,10 @@ class LibvirtConfigGuestDisk(LibvirtConfigGuestDevice): self.readonly = True elif c.tag == 'shareable': self.shareable = True + elif c.tag == 'address': + obj = LibvirtConfigGuestDeviceAddress.factory(c) + obj.parse_dom(c) + self.device_addr = obj class LibvirtConfigGuestDiskBackingStore(LibvirtConfigObject): @@ -1131,6 +1136,63 @@ class LibvirtConfigGuestGIDMap(LibvirtConfigGuestIDMap): **kwargs) +class LibvirtConfigGuestDeviceAddress(LibvirtConfigObject): + def __init__(self, type=None, **kwargs): + super(LibvirtConfigGuestDeviceAddress, self).__init__( + root_name='address', **kwargs) + self.type = type + + @staticmethod + def factory(xmldoc): + addr_type = xmldoc.get('type') + if addr_type == 'pci': + return LibvirtConfigGuestDeviceAddressPCI() + elif addr_type == 'drive': + return LibvirtConfigGuestDeviceAddressDrive() + + +class LibvirtConfigGuestDeviceAddressDrive(LibvirtConfigGuestDeviceAddress): + def __init__(self, **kwargs): + super(LibvirtConfigGuestDeviceAddressDrive, self).\ + __init__(type='drive', **kwargs) + self.controller = None + self.bus = None + self.target = None + self.unit = None + + def parse_dom(self, xmldoc): + self.controller = xmldoc.get('controller') + self.bus = xmldoc.get('bus') + self.target = xmldoc.get('target') + self.unit = xmldoc.get('unit') + + def format_address(self): + return None + + +class LibvirtConfigGuestDeviceAddressPCI(LibvirtConfigGuestDeviceAddress): + def __init__(self, **kwargs): + super(LibvirtConfigGuestDeviceAddressPCI, self).\ + __init__(type='pci', **kwargs) + self.domain = None + self.bus = None + self.slot = None + self.function = None + + def parse_dom(self, xmldoc): + self.domain = xmldoc.get('domain') + self.bus = xmldoc.get('bus') + self.slot = xmldoc.get('slot') + self.function = xmldoc.get('function') + + def format_address(self): + if self.domain is not None: + return pci_utils.get_pci_address(self.domain[2:], + self.bus[2:], + self.slot[2:], + self.function[2:]) + + class LibvirtConfigGuestInterface(LibvirtConfigGuestDevice): def __init__(self, **kwargs): @@ -1161,6 +1223,7 @@ class LibvirtConfigGuestInterface(LibvirtConfigGuestDevice): self.vif_outbound_burst = None self.vif_outbound_average = None self.vlan = None + self.device_addr = None def format_dom(self): dev = super(LibvirtConfigGuestInterface, self).format_dom() @@ -1331,6 +1394,10 @@ class LibvirtConfigGuestInterface(LibvirtConfigGuestDevice): self.vif_outbound_burst = int(sub.get('burst')) if sub.get('peak'): self.vif_outbound_peak = int(sub.get('peak')) + elif c.tag == 'address': + obj = LibvirtConfigGuestDeviceAddress.factory(c) + obj.parse_dom(c) + self.device_addr = obj def add_filter_param(self, key, value): self.filterparams.append({'key': key, 'value': value}) |