def test_pretty_format_safety(self): # Check that dom is not modified in format_xml; we check that by # comparing the exported forms of `dom' created before and after # format_xml call. xml = re.sub(' *\n *', '', self._XML) dom = vmxml.parse_xml(xml) exported_1 = etree.tostring(dom) vmxml.format_xml(dom, pretty=True) exported_2 = etree.tostring(dom) self.assertEqual(exported_1, exported_2)
def test_pretty_format_safety(self): # Check that dom is not modified in format_xml; we check that by # comparing the exported forms of `dom' created before and after # format_xml call. xml = re.sub(' *\n *', '', self._XML) dom = vmxml.parse_xml(xml) exported_1 = etree.tostring(dom) vmxml.format_xml(dom, pretty=True) exported_2 = etree.tostring(dom) self.assertEqual(exported_1, exported_2)
def test_interface(self): interfaceXML = """ <interface type="bridge"> <address %s/> <mac address="52:54:00:59:F5:3F"/> <model type="virtio"/> <source bridge="ovirtmgmt"/> <filterref filter="no-mac-spoofing"/> <boot order="1"/> <driver name="vhost" queues="7"/> <tune> <sndbuf>0</sndbuf> </tune> <bandwidth> <inbound average="1000" burst="1024" peak="5000"/> <outbound average="128" burst="256"/> </bandwidth> </interface>""" % self.PCI_ADDR dev = {'nicModel': 'virtio', 'macAddr': '52:54:00:59:F5:3F', 'network': 'ovirtmgmt', 'address': self.PCI_ADDR_DICT, 'device': 'bridge', 'type': 'interface', 'bootOrder': '1', 'filter': 'no-mac-spoofing', 'specParams': {'inbound': {'average': 1000, 'peak': 5000, 'burst': 1024}, 'outbound': {'average': 128, 'burst': 256}}, 'custom': {'queues': '7'}, 'vm_custom': {'vhost': 'ovirtmgmt:true', 'sndbuf': '0'}, } iface = vmdevices.network.Interface(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(iface.getXML()), interfaceXML)
def test_smartcard(self): smartcardXML = '<smartcard mode="passthrough" type="spicevmc"/>' dev = {'device': 'smartcard', 'specParams': {'mode': 'passthrough', 'type': 'spicevmc'}} smartcard = vmdevices.core.Smartcard(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(smartcard.getXML()), smartcardXML)
def test_interface_filter_parameters(self): interfaceXML = """ <interface type="bridge"> <address %s/> <mac address="52:54:00:59:F5:3F"/> <model type="virtio"/> <source bridge="ovirtmgmt"/> <filterref filter="clean-traffic"> <parameter name='IP' value='10.0.0.1'/> <parameter name='IP' value='10.0.0.2'/> </filterref> <boot order="1"/> <driver name="vhost"/> <tune> <sndbuf>0</sndbuf> </tune> </interface>""" % self.PCI_ADDR dev = { 'nicModel': 'virtio', 'macAddr': '52:54:00:59:F5:3F', 'network': 'ovirtmgmt', 'address': self.PCI_ADDR_DICT, 'device': 'bridge', 'type': 'interface', 'bootOrder': '1', 'filter': 'clean-traffic', 'filterParameters': [ {'name': 'IP', 'value': '10.0.0.1'}, {'name': 'IP', 'value': '10.0.0.2'}, ], 'vm_custom': {'vhost': 'ovirtmgmt:true', 'sndbuf': '0'}, } iface = vmdevices.network.Interface(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(iface.getXML()), interfaceXML)
def test_interface_on_ovs_with_vlan(self): interfaceXML = """ <interface type="bridge"> <address %s/> <mac address="52:54:00:59:F5:3F"/> <model type="virtio"/> <source bridge="ovirtmgmt"/> <virtualport type="openvswitch" /> <vlan> <tag id="101" /> </vlan> <filterref filter="no-mac-spoofing"/> <boot order="1"/> <driver name="vhost" queues="7"/> <tune> <sndbuf>0</sndbuf> </tune> </interface>""" % self.PCI_ADDR dev = { 'nicModel': 'virtio', 'macAddr': '52:54:00:59:F5:3F', 'network': 'ovirtmgmt', 'address': self.PCI_ADDR_DICT, 'device': 'bridge', 'type': 'interface', 'bootOrder': '1', 'filter': 'no-mac-spoofing', 'custom': {'queues': '7'}, 'vm_custom': {'vhost': 'ovirtmgmt:true', 'sndbuf': '0'}, } iface = vmdevices.network.Interface(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(iface.getXML()), interfaceXML)
def test_controller(self): devConfs = [ {'device': 'ide', 'index': '0', 'address': self.PCI_ADDR_DICT}, {'device': 'scsi', 'index': '0', 'model': 'virtio-scsi', 'address': self.PCI_ADDR_DICT}, {'device': 'scsi', 'index': '0', 'model': 'virtio-scsi', 'address': self.PCI_ADDR_DICT, 'specParams': {}}, {'device': 'scsi', 'model': 'virtio-scsi', 'index': '0', 'specParams': {'ioThreadId': '0'}, 'address': self.PCI_ADDR_DICT}, {'device': 'scsi', 'model': 'virtio-scsi', 'index': '0', 'specParams': {'ioThreadId': 0}, 'address': self.PCI_ADDR_DICT}, {'device': 'virtio-serial', 'address': self.PCI_ADDR_DICT}, {'device': 'usb', 'model': 'ich9-ehci1', 'index': '0', 'master': {'startport': '0'}, 'address': self.PCI_ADDR_DICT}] expectedXMLs = [ """ <controller index="0" type="ide"> <address %s/> </controller>""", """ <controller index="0" model="virtio-scsi" type="scsi"> <address %s/> </controller>""", """ <controller index="0" model="virtio-scsi" type="scsi"> <address %s/> </controller>""", """ <controller index="0" model="virtio-scsi" type="scsi"> <address %s/> <driver iothread="0"/> </controller>""", """ <controller index="0" model="virtio-scsi" type="scsi"> <address %s/> <driver iothread="0"/> </controller>""", """ <controller index="0" ports="16" type="virtio-serial"> <address %s/> </controller>""", """ <controller index="0" model="ich9-ehci1" type="usb"> <master startport="0"/> <address %s/> </controller>"""] for devConf, xml in zip(devConfs, expectedXMLs): device = vmdevices.core.Controller(self.log, **devConf) self.assertXMLEqual(vmxml.format_xml(device.getXML()), xml % self.PCI_ADDR)
def testCreatePCIHostDeviceWithAddress(self, device_name): dev_spec = {'type': 'hostdev', 'device': device_name, 'address': self._PCI_ADDRESS} device = hostdevice.HostDevice(self.log, **dev_spec) self.assertXMLEqual( vmxml.format_xml(device.getXML()), _DEVICE_XML[device_name] % (self._PCI_ADDRESS_XML))
def testCreateSRIOVVF(self): dev_spec = {'type': hwclass.NIC, 'device': 'hostdev', 'hostdev': _SRIOV_VF, 'macAddr': 'ff:ff:ff:ff:ff:ff', 'specParams': {'vlanid': 3}, 'bootOrder': '9'} device = network.Interface(self.log, **dev_spec) self.assertXMLEqual(vmxml.format_xml(device.getXML()), _DEVICE_XML[_SRIOV_VF] % ('',))
def test_dump_ns(self): expected_xml = u'''<ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:version type="float">4.2</ovirt-vm:version> </ovirt-vm:vm>''' metadata_obj = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') self.assertXMLEqual( vmxml.format_xml(metadata_obj.dump('vm', version=4.2)), expected_xml)
def testNumaTuneXMLMultiNode(self): domxml = libvirtxml.Domain(self.conf, self.log, cpuarch.X86_64) devices = [hostdevice.HostDevice( self.log, **{'type': 'hostdev', 'device': device}) for device in [_SRIOV_PF, _SRIOV_VF, 'pci_0000_00_02_0']] domxml.appendHostdevNumaTune(devices) xml = vmxml.format_xml(domxml.dom) self.assertRaises(AssertionError, lambda: find_xml_element(xml, './numatune'))
def _parse_xml(self, xml_str): root = vmxml.parse_xml(xml_str) md_elem = root.find('./metadata/{%s}%s' % (self._namespace_uri, self._name)) if md_elem is not None: md_uuid = root.find('./uuid') self._log.debug('parsing metadata for %s: %s', md_uuid.text, vmxml.format_xml(md_elem, pretty=True)) self._load(md_elem, self._namespace, self._namespace_uri)
def testCreatePCIHostDeviceWithAddress(self, device_name): dev_spec = { 'type': 'hostdev', 'device': device_name, 'address': self._PCI_ADDRESS } device = hostdevice.HostDevice(self.log, **dev_spec) self.assertXMLEqual(vmxml.format_xml(device.getXML()), _DEVICE_XML[device_name] % (self._PCI_ADDRESS_XML))
def test_console_socket(self): consoleXML = """ <console type="unix"> <source mode="bind" path="%s%s.sock" /> <target port="0" type="virtio"/> </console>""" % (constants.P_OVIRT_VMCONSOLES, self.conf['vmId']) dev = {'device': 'console', 'specParams': {'enableSocket': True}} dev['vmid'] = self.conf['vmId'] console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getXML()), consoleXML)
def test_redir(self): redirXML = """ <redirdev type="spicevmc"> <address %s/> </redirdev>""" % self.PCI_ADDR dev = {'device': 'spicevmc', 'address': self.PCI_ADDR_DICT} redir = vmdevices.core.Redir(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(redir.getXML()), redirXML)
def test_console_pty(self, specParams): consoleXML = """ <console type="pty"> <target port="0" type="virtio"/> </console>""" dev = {'device': 'console'} if specParams is not None: dev['specParams'] = specParams console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getXML()), consoleXML)
def test_console_pty(self, specParams): consoleXML = """ <console type="pty"> <target port="0" type="virtio"/> </console>""" dev = {'device': 'console'} if specParams is not None: dev['specParams'] = specParams console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getXML()), consoleXML)
def test_redir(self): redirXML = """ <redirdev type="spicevmc"> <address %s/> </redirdev>""" % self.PCI_ADDR dev = {'device': 'spicevmc', 'address': self.PCI_ADDR_DICT} redir = vmdevices.core.Redir(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(redir.getXML()), redirXML)
def testCreateSRIOVVFWithAddress(self): dev_spec = {'type': hwclass.NIC, 'device': 'hostdev', 'hostdev': _SRIOV_VF, 'macAddr': 'ff:ff:ff:ff:ff:ff', 'specParams': {'vlanid': 3}, 'bootOrder': '9', 'address': {'slot': '0x02', 'bus': '0x01', 'domain': '0x0000', 'function': '0x0', 'type': 'pci'}} device = network.Interface(self.log, **dev_spec) self.assertXMLEqual( vmxml.format_xml(device.getXML()), _DEVICE_XML[_SRIOV_VF] % (self._PCI_ADDRESS_XML))
def test_file(self): drive = Drive(self.log, **self.conf) expected = """ <disk name='vda' snapshot='external' type='file'> <source file='/image' type='file'/> </disk> """ snap_info = {'path': '/image', 'device': 'disk'} actual = drive.get_snapshot_xml(snap_info) self.assertXMLEqual(vmxml.format_xml(actual), expected)
def test_dump_ns(self): expected_xml = u'''<ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:version type="float">4.2</ovirt-vm:version> </ovirt-vm:vm>''' metadata_obj = metadata.Metadata( 'ovirt-vm', 'http://ovirt.org/vm/1.0' ) self.assertXMLEqual( vmxml.format_xml(metadata_obj.dump('vm', version=4.2)), expected_xml )
def test_block(self): drive = Drive(self.log, diskType=DISK_TYPE.BLOCK, **self.conf) expected = """ <disk name='vda' snapshot='external' type='block'> <source dev='/dev/dm-1' type='block'/> </disk> """ snap_info = {'path': '/dev/dm-1', 'device': 'disk'} actual = drive.get_snapshot_xml(snap_info) self.assertXMLEqual(vmxml.format_xml(actual), expected)
def test_balloon(self): balloonXML = '<memballoon model="virtio"/>' dev = { 'device': 'memballoon', 'type': 'balloon', 'specParams': { 'model': 'virtio' } } balloon = vmdevices.core.Balloon(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(balloon.getXML()), balloonXML)
def test_console_default(self): consoleXML = """ <console type="pty"> <target port="0" type="virtio"/> </console>""" dev = { 'device': 'console', 'vmid': self.conf['vmId'], } console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getXML()), consoleXML)
def test_console_socket(self): consoleXML = """ <console type="unix"> <source mode="bind" path="%s%s.sock" /> <target port="0" type="virtio"/> </console>""" % (constants.P_OVIRT_VMCONSOLES, self.conf['vmId']) dev = {'device': 'console', 'specParams': {'enableSocket': True}} dev['vmid'] = self.conf['vmId'] console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getXML()), consoleXML)
def test_smartcard(self): smartcardXML = '<smartcard mode="passthrough" type="spicevmc"/>' dev = { 'device': 'smartcard', 'specParams': { 'mode': 'passthrough', 'type': 'spicevmc' } } smartcard = vmdevices.core.Smartcard(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(smartcard.getXML()), smartcardXML)
def test_console_default(self): consoleXML = """ <console type="pty"> <target port="0" type="virtio"/> </console>""" dev = { 'device': 'console', 'vmid': self.conf['vmId'], } console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getXML()), consoleXML)
def test_file(self): drive = Drive(self.log, diskType=DISK_TYPE.FILE, **self.conf) expected = """ <disk name='vda' snapshot='external' type='file'> <source file='/image' type='file'/> </disk> """ snap_info = {'path': '/image', 'device': 'disk'} actual = drive.get_snapshot_xml(snap_info) self.assertXMLEqual(vmxml.format_xml(actual), expected)
def test_serial_device(self): serialXML = """ <serial type="pty"> <target port="0"/> </serial>""" dev = { 'device': 'console', 'vmid': self.conf['vmId'], } console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getSerialDeviceXML()), serialXML)
def test_watchdog(self): watchdogXML = '<watchdog action="none" model="i6300esb"/>' dev = { 'device': 'watchdog', 'type': 'watchdog', 'specParams': { 'model': 'i6300esb', 'action': 'none' } } watchdog = vmdevices.core.Watchdog(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(watchdog.getXML()), watchdogXML)
def check_leases(self, conf): drive = Drive(self.log, **conf) leases = list(drive.getLeasesXML()) self.assertEqual(1, len(leases)) xml = """ <lease> <key>vol_id</key> <lockspace>dom_id</lockspace> <target offset="0" path="path" /> </lease> """ self.assertXMLEqual(vmxml.format_xml(leases[0]), xml)
def _check_roundtrip(self, klass, dev_xml, meta=None, expected_xml=None): dev = klass.from_xml_tree(self.log, vmxml.parse_xml(dev_xml), {} if meta is None else meta) dev.setup() try: rebuilt_xml = vmxml.format_xml(dev.getXML(), pretty=True) # make troubleshooting easier print(rebuilt_xml) result_xml = dev_xml if expected_xml is None else expected_xml self.assertXMLEqual(rebuilt_xml, result_xml) finally: dev.teardown()
def test_console_serial(self): consoleXML = """ <console type="pty"> <target port="0" type="serial"/> </console>""" dev = { 'device': 'console', 'specParams': {'consoleType': 'serial'}, 'vmid': self.conf['vmId'], } console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getXML()), consoleXML)
def test_block(self): drive = Drive(self.log, **self.conf) drive._blockDev = True expected = """ <disk name='vda' snapshot='external' type='block'> <source dev='/dev/dm-1' type='block'/> </disk> """ snap_info = {'path': '/dev/dm-1', 'device': 'disk'} actual = drive.get_snapshot_xml(snap_info) self.assertXMLEqual(vmxml.format_xml(actual), expected)
def testNumaTuneXMLMultiNode(self): domxml = libvirtxml.Domain(self.conf, self.log, cpuarch.X86_64) devices = [ hostdevice.HostDevice(self.log, **{ 'type': 'hostdev', 'device': device }) for device in [_SRIOV_PF, _SRIOV_VF, 'pci_0000_00_02_0'] ] domxml.appendHostdevNumaTune(devices) xml = vmxml.format_xml(domxml.dom) self.assertRaises(AssertionError, lambda: find_xml_element(xml, './numatune'))
def check_leases(self, conf): drive = Drive(self.log, diskType=DISK_TYPE.FILE, **conf) leases = list(drive.getLeasesXML()) self.assertEqual(1, len(leases)) xml = """ <lease> <key>vol_id</key> <lockspace>dom_id</lockspace> <target offset="0" path="path" /> </lease> """ self.assertXMLEqual(vmxml.format_xml(leases[0]), xml)
def test_memory_device(self): memoryXML = """<memory model='dimm'> <target> <size unit='KiB'>1048576</size> <node>0</node> </target> </memory> """ params = {'device': 'memory', 'type': 'memory', 'size': 1024, 'node': 0} memory = vmdevices.core.Memory(self.log, **params) self.assertXMLEqual(vmxml.format_xml(memory.getXML()), memoryXML)
def test_serial_device(self): serialXML = """ <serial type="pty"> <target port="0"/> </serial>""" dev = { 'device': 'console', 'vmid': self.conf['vmId'], } console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getSerialDeviceXML()), serialXML)
def test_disk_ignore_volumeinfo_from_metadata_xml(self): xml_snippet = u'''<volumeInfo> <path>/rhev/data-center/omitted/for/brevity</path> <volType>path</volType> </volumeInfo>''' root = vmxml.parse_xml(_DISK_DATA.metadata_xml) dev = vmxml.find_first(root, 'device') vmxml.append_child(dev, etree_child=vmxml.parse_xml(xml_snippet)) data = _TestData(copy.deepcopy(_DISK_DATA.conf), vmxml.format_xml(root)) self._check_drive_from_metadata_xml(data)
def test_rng(self): rngXML = """ <rng model="virtio"> <rate bytes="1234" period="2000"/> <backend model="random">/dev/random</backend> </rng>""" dev = {'type': 'rng', 'model': 'virtio', 'specParams': {'period': '2000', 'bytes': '1234', 'source': 'random'}} rng = vmdevices.core.Rng(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(rng.getXML()), rngXML)
def dev_from_xml(vm, xml): """ Create and return device instance from provided XML. The XML must contain <devices> element with a single device subelement, the one to create the instance for. Depending on the device kind <metadata> element may be required to provide device metadata; the element may and needn't contain unrelated metadata. This function is used in device hot(un)plugs. Example `xml` value (top element tag may be arbitrary): <?xml version='1.0' encoding='UTF-8'?> <hotplug> <devices> <interface type="bridge"> <mac address="66:55:44:33:22:11"/> <model type="virtio" /> <source bridge="ovirtmgmt" /> <filterref filter="vdsm-no-mac-spoofing" /> <link state="up" /> <bandwidth /> </interface> </devices> <metadata xmlns:ns0="http://ovirt.org/vm/tune/1.0" xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:device mac_address='66:55:44:33:22:11'> <ovirt-vm:network>test</ovirt-vm:network> <ovirt-vm:portMirroring> <ovirt-vm:network>network1</ovirt-vm:network> <ovirt-vm:network>network2</ovirt-vm:network> </ovirt-vm:portMirroring> </ovirt-vm:device> </ovirt-vm:vm> </metadata> </hotplug> :param xml: XML specifying the device as described above. :type xml: basestring :returns: Device instance created from the provided XML. """ dom = vmxml.parse_xml(xml) devices = vmxml.find_first(dom, 'devices') dev_elem = next(vmxml.children(devices)) _dev_type, dev_class = identify_from_xml_elem(dev_elem) meta = vmxml.find_first(dom, 'metadata', None) if meta is None: md_desc = metadata.Descriptor() else: md_desc = metadata.Descriptor.from_xml(vmxml.format_xml(meta)) dev_meta = _get_metadata_from_elem_xml(vm.id, md_desc, dev_class, dev_elem) return dev_class.from_xml_tree(vm.log, dev_elem, dev_meta)
def test_interface_filter_parameters(self): interfaceXML = """ <interface type="bridge"> <address %s/> <mac address="52:54:00:59:F5:3F"/> <model type="virtio"/> <source bridge="ovirtmgmt"/> <filterref filter="clean-traffic"> <parameter name='IP' value='10.0.0.1'/> <parameter name='IP' value='10.0.0.2'/> </filterref> <boot order="1"/> <driver name="vhost"/> <tune> <sndbuf>0</sndbuf> </tune> </interface>""" % self.PCI_ADDR dev = { 'nicModel': 'virtio', 'macAddr': '52:54:00:59:F5:3F', 'network': 'ovirtmgmt', 'address': self.PCI_ADDR_DICT, 'device': 'bridge', 'type': 'interface', 'bootOrder': '1', 'filter': 'clean-traffic', 'filterParameters': [ { 'name': 'IP', 'value': '10.0.0.1' }, { 'name': 'IP', 'value': '10.0.0.2' }, ], 'vm_custom': { 'vhost': 'ovirtmgmt:true', 'sndbuf': '0' }, } iface = vmdevices.network.Interface(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(iface.getXML()), interfaceXML)
def test_tpm(self): tpmXML = """ <tpm model="tpm-tis"> <backend type="passthrough"> <device path="/dev/tpm0"/> </backend> </tpm> """ dev = {'device': 'tpm', 'specParams': {'mode': 'passthrough', 'path': '/dev/tpm0', 'model': 'tpm-tis'}} tpm = vmdevices.core.Tpm(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(tpm.getXML()), tpmXML)
def testNumaTuneXMLSingleNode(self, devices, numa_node): numatuneXML = """ <numatune> <memory mode="preferred" nodeset="{}" /> </numatune> """.format(numa_node) domxml = libvirtxml.Domain(self.conf, self.log, cpuarch.X86_64) devices = [hostdevice.HostDevice( self.log, **{'type': 'hostdev', 'device': device}) for device in devices] domxml.appendHostdevNumaTune(devices) xml = vmxml.format_xml(domxml.dom) self.assertXMLEqual(find_xml_element(xml, './numatune'), numatuneXML)
def test_getxml(self): spec = dict(sd_id="sd_id", lease_id="lease_id", path="/path", offset=1048576) lease = vmdevices.lease.Device(self.log, **spec) lease_xml = vmxml.format_xml(lease.getXML()) xml = """ <lease> <key>lease_id</key> <lockspace>sd_id</lockspace> <target offset="1048576" path="/path" /> </lease> """ self.assertXMLEqual(lease_xml, xml)
def _build_xml(self, namespace=None, namespace_uri=None): metadata_obj = Metadata(namespace, namespace_uri) with self._lock: md_elem = metadata_obj.dump(self._name, **self._values) for (attrs, data) in self._devices: if data: dev_elem = _dump_device(metadata_obj, data) dev_elem.attrib.update(attrs) vmxml.append_child(md_elem, etree_child=dev_elem) if self._custom: custom_elem = metadata_obj.dump(_CUSTOM, **self._custom) vmxml.append_child(md_elem, etree_child=custom_elem) return vmxml.format_xml(md_elem, pretty=True)
def testCreateSRIOVVF(self): dev_spec = { 'type': hwclass.NIC, 'device': 'hostdev', 'hostdev': _SRIOV_VF, 'macAddr': 'ff:ff:ff:ff:ff:ff', 'specParams': { 'vlanid': 3 }, 'bootOrder': '9' } device = network.Interface(self.log, **dev_spec) self.assertXMLEqual(vmxml.format_xml(device.getXML()), _DEVICE_XML[_SRIOV_VF] % ('', ))
def test_console_serial(self): consoleXML = """ <console type="pty"> <target port="0" type="serial"/> </console>""" dev = { 'device': 'console', 'specParams': { 'consoleType': 'serial' }, 'vmid': self.conf['vmId'], } console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getXML()), consoleXML)
def test_namespaces(self): expected_xml = ''' <domain xmlns:ovirt-tune="http://ovirt.org/vm/tune/1.0"> <metadata> <ovirt-tune:qos/> </metadata> </domain> ''' domain = vmxml.Element('domain') metadata = vmxml.Element('metadata') domain.appendChild(metadata) qos = vmxml.Element('qos', namespace='ovirt-tune', namespace_uri='http://ovirt.org/vm/tune/1.0') metadata.appendChild(qos) self.assertXMLEqual(vmxml.format_xml(domain), expected_xml)
def _check_roundtrip(self, klass, dev_xml, meta=None, expected_xml=None): dev = klass.from_xml_tree( self.log, vmxml.parse_xml(dev_xml), {} if meta is None else meta ) dev.setup() try: rebuilt_xml = vmxml.format_xml(dev.getXML(), pretty=True) # make troubleshooting easier print(rebuilt_xml) result_xml = dev_xml if expected_xml is None else expected_xml self.assertXMLEqual(rebuilt_xml, result_xml) finally: dev.teardown()
def test_getxml(self): spec = dict(sd_id="sd_id", lease_id="lease_id", path="/path", offset=1048576) lease = vmdevices.lease.Device(self.log, **spec) lease_xml = vmxml.format_xml(lease.getXML()) xml = """ <lease> <key>lease_id</key> <lockspace>sd_id</lockspace> <target offset="1048576" path="/path" /> </lease> """ self.assertXMLEqual(lease_xml, xml)
def test_roundtrip_empty(self, elem_type): elem_spec = ('' if elem_type is None else 'type="{elem_type}"'.format( elem_type=elem_type)) test_xml = u'''<ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:param {elem_spec}/> </ovirt-vm:vm>'''.format(elem_spec=elem_spec) expected_xml = u'''<ovirt-vm:vm xmlns:ovirt-vm="http://ovirt.org/vm/1.0"> <ovirt-vm:param /> </ovirt-vm:vm>''' metadata_src = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') metadata_dst = metadata.Metadata('ovirt-vm', 'http://ovirt.org/vm/1.0') data = metadata_src.load(vmxml.parse_xml(test_xml)) out_xml = vmxml.format_xml(metadata_dst.dump('vm', **data)) self.assertXMLEqual(out_xml, expected_xml)
def _parse_xml(self, xml_str): root = vmxml.parse_xml(xml_str) selector = '{%s}%s' % (self._namespace_uri, self._name) if root.tag == 'metadata': md_elem = root.find('./' + selector) else: md_elem = root.find('./metadata/' + selector) if md_elem is not None: md_uuid = root.find('./uuid') # UUID may not be present in hotplug/hotunplug metadata snippets uuid_text = '?' if md_uuid is None else md_uuid.text self._log.debug( 'parsing metadata for %s: %s', uuid_text, vmxml.format_xml(md_elem, pretty=True)) self._load(md_elem, self._namespace, self._namespace_uri)
def test_createXmlElem(self): dev = {'type': 'graphics', 'device': 'spice'} expected_xml = '''<?xml version=\'1.0\' encoding=\'utf-8\'?> <graphics device="spice" type="test" />''' with fake.VM(self.conf, devices=(dev, ), create_device_objects=True) as testvm: graphics = testvm._devices[hwclass.GRAPHICS][0] element = graphics.createXmlElem('graphics', 'test', attributes=( 'device', 'foo', )) result = vmxml.format_xml(element).decode('utf-8') self.assertXMLEqual(result, expected_xml)
def test_interface(self): interfaceXML = """ <interface type="bridge"> <address %s/> <mac address="52:54:00:59:F5:3F"/> <model type="virtio"/> <source bridge="ovirtmgmt"/> <filterref filter="no-mac-spoofing"/> <boot order="1"/> <driver name="vhost" queues="7"/> <tune> <sndbuf>0</sndbuf> </tune> <bandwidth> <inbound average="1000" burst="1024" peak="5000"/> <outbound average="128" burst="256"/> </bandwidth> </interface>""" % self.PCI_ADDR dev = { 'nicModel': 'virtio', 'macAddr': '52:54:00:59:F5:3F', 'network': 'ovirtmgmt', 'address': self.PCI_ADDR_DICT, 'device': 'bridge', 'type': 'interface', 'bootOrder': '1', 'filter': 'no-mac-spoofing', 'specParams': { 'inbound': { 'average': 1000, 'peak': 5000, 'burst': 1024 }, 'outbound': { 'average': 128, 'burst': 256 } }, 'custom': { 'queues': '7' }, 'vm_custom': { 'vhost': 'ovirtmgmt:true', 'sndbuf': '0' }, } iface = vmdevices.network.Interface(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(iface.getXML()), interfaceXML)
def test_namespaces(self): expected_xml = ''' <domain xmlns:ovirt-tune="http://ovirt.org/vm/tune/1.0"> <metadata> <ovirt-tune:qos/> </metadata> </domain> ''' domain = vmxml.Element('domain') metadata = vmxml.Element('metadata') domain.appendChild(metadata) qos = vmxml.Element('qos', namespace='ovirt-tune', namespace_uri='http://ovirt.org/vm/tune/1.0') metadata.appendChild(qos) self.assertXMLEqual(vmxml.format_xml(domain), expected_xml)
def test_memory_device(self): memoryXML = """<memory model='dimm'> <target> <size unit='KiB'>1048576</size> <node>0</node> </target> </memory> """ params = { 'device': 'memory', 'type': 'memory', 'size': 1024, 'node': 0 } memory = vmdevices.core.Memory(self.log, **params) self.assertXMLEqual(vmxml.format_xml(memory.getXML()), memoryXML)
def test_unix_socket_serial_device(self): path = "/var/run/ovirt-vmconsole-console/%s.sock" % self.conf['vmId'] serialXML = """ <serial type="unix"> <source mode="bind" path="%s" /> <target port="0" /> </serial>""" % path dev = { 'vmid': self.conf['vmId'], 'device': 'console', 'specParams': { 'enableSocket': True } } console = vmdevices.core.Console(self.log, **dev) self.assertXMLEqual(vmxml.format_xml(console.getSerialDeviceXML()), serialXML)
def test_pretty_format_formatting(self): xml = re.sub(' *\n *', '', self._XML) dom = vmxml.parse_xml(xml) pretty = vmxml.format_xml(dom, pretty=True) self.assertEqual(pretty, '''<?xml version='1.0' encoding='UTF-8'?> <topelement> <hello lang="english">hello</hello> <hello cyrillic="yes" lang="русский">здра́вствуйте</hello> <bye>good bye<hello lang="čeština">dobrý den</hello> </bye> <container> <subelement /> </container> <container> <subelement>some content</subelement> </container> <empty /> </topelement> ''')
def _metadata_xml(dom, tag, namespace, namespace_uri): md_xml = "<{tag}/>".format(tag=tag) try: md_xml = dom.metadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT, namespace_uri, 0) except libvirt.libvirtError as e: if e.get_error_code() != libvirt.VIR_ERR_NO_DOMAIN_METADATA: raise md_elem = [vmxml.parse_xml(md_xml)] # we do this because we need to receive back the updated element yield md_elem dom.setMetadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT, vmxml.format_xml(md_elem[0]), namespace, namespace_uri, 0)