Skip to content

Commit

Permalink
Bug fix: Use encoding option of ET.tostring() to get XML as string
Browse files Browse the repository at this point in the history
Signed-off-by: Aline Manera <[email protected]>
  • Loading branch information
alinefm committed Aug 27, 2019
1 parent 5bcc5d8 commit b3104fb
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 75 deletions.
15 changes: 6 additions & 9 deletions mockmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def domainXMLDesc(dom, flags=0):
break
if add:
root.devices.append(dev)
return ET.tostring(root, encoding='utf-8').decode('utf-8')
return ET.tostring(root, encoding='unicode')

@staticmethod
def undefineDomain(dom):
Expand All @@ -226,7 +226,7 @@ def attachDeviceFlags(dom, xml, flags=0):
myxml.target.size._setText(
str(int(convert_data_size(myxml.target.size.text, unit, 'KiB')))
)
xml = ET.tostring(myxml).decode('utf-8')
xml = ET.tostring(myxml, encoding='unicode')
dom.setMaxMemory(int(dom.maxMemory() + myxml.target.size))
MockModel._mock_vms[dom.name()].append(xml)

Expand Down Expand Up @@ -254,17 +254,14 @@ def _get_device_node(dom, xml):
@staticmethod
def detachDeviceFlags(dom, xml, flags=0):
node = ET.fromstring(xml)
xml = ET.tostring(node, encoding='utf-8',
pretty_print=True).decode('utf-8')
xml = ET.tostring(node, encoding='unicode', pretty_print=True)
if xml in MockModel._mock_vms[dom.name()]:
MockModel._mock_vms[dom.name()].remove(xml)

@staticmethod
def updateDeviceFlags(dom, xml, flags=0):
_, old_dev = MockModel._get_device_node(dom, xml)
old_xml = ET.tostring(old_dev, encoding='utf-8', pretty_print=True).decode(
'utf-8'
)
old_xml = ET.tostring(old_dev, encoding='unicode', pretty_print=True)
if old_xml in MockModel._mock_vms[dom.name()]:
MockModel._mock_vms[dom.name()].remove(old_xml)
MockModel._mock_vms[dom.name()].append(xml)
Expand Down Expand Up @@ -329,7 +326,7 @@ def _update_lvm_disks(self, pool_name, disks):
dev = E.device(path=d)
source.append(dev)

conn.storagePoolDefineXML(ET.tostring(root).decode('utf-8'), 0)
conn.storagePoolDefineXML(ET.tostring(root, encoding='unicode'), 0)

def _mock_storagevolumes_create(self, pool, params):
vol_source = ['url', 'capacity']
Expand Down Expand Up @@ -562,7 +559,7 @@ def _get_pci_device_xml(self, dev_info, slot, is_multifunction):
source, driver, mode='subsystem', type='pci', managed='yes'
)

return ET.tostring(host_dev).decode('utf-8')
return ET.tostring(host_dev, encoding='unicode')


class MockStorageVolumes(object):
Expand Down
2 changes: 1 addition & 1 deletion model/featuretests.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def libvirt_supports_iso_stream(conn, protocol):
def libvirt_support_nfs_probe(conn):
def _get_xml():
obj = E.source(E.host(name='127.0.0.1'), E.format(type='nfs'))
xml = ET.tostring(obj).decode('utf-8')
xml = ET.tostring(obj, encoding='unicode')
return xml

try:
Expand Down
4 changes: 2 additions & 2 deletions model/libvirtstoragepool.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def xml(self):
pool = E.pool(type='dir')
pool.append(E.name(self.poolArgs['name']))
pool.append(E.target(E.path(self.poolArgs['path'])))
return ET.tostring(pool, encoding='utf-8', pretty_print=True).decode('utf-8')
return ET.tostring(pool, encoding='unicode', pretty_print=True)


class NetfsPoolDef(StoragePoolDef):
Expand Down Expand Up @@ -217,7 +217,7 @@ def _prepare_auth(self, conn):
usage = E.usage(type='iscsi')
usage.append(E.target(self.poolArgs['name']))
secret.append(usage)
virSecret = conn.secretDefineXML(ET.tostring(secret))
virSecret = conn.secretDefineXML(ET.tostring(secret, encoding='unicode'))

virSecret.setValue(auth['password'])

Expand Down
2 changes: 1 addition & 1 deletion model/storagepools.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _check_default_pools(self):
# Try to create the pool
pool = E.pool(E.name(pool_name), type='dir')
pool.append(E.target(E.path(pool_path)))
xml = ET.tostring(pool)
xml = ET.tostring(pool, encoding='unicode')
try:
pool = conn.storagePoolDefineXML(xml, 0)
except libvirt.libvirtError as e:
Expand Down
2 changes: 1 addition & 1 deletion model/storagetargets.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _get_storage_server_spec(self, **kwargs):
host_attr['port'] = server_port

obj = E.source(E.host(host_attr), *extra_args)
xml = ET.tostring(obj)
xml = ET.tostring(obj, encoding='unicode')
return xml

def _parse_target_source_result(self, target_type, xml_str):
Expand Down
4 changes: 2 additions & 2 deletions model/storagevolumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ def _clone_task(self, cb, params):
target_elem.append(E.format(type=orig_vol['format']))
root_elem.append(target_elem)
new_vol_xml = ET.tostring(
root_elem, encoding='utf-8', pretty_print=True
).decode('utf-8')
root_elem, encoding='unicode', pretty_print=True
)

cb('cloning volume')
new_vir_pool.createXMLFrom(new_vol_xml, orig_vir_vol, 0)
Expand Down
6 changes: 3 additions & 3 deletions model/vmhostdevs.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def _update_win_memory_size(self, dom, counter, wnd_size):
line.remove(arg.getprevious())
line.remove(arg)

return etree.tostring(root, encoding='utf-8', pretty_print=True)
return etree.tostring(root, encoding='unicode', pretty_print=True)

return None

Expand All @@ -484,7 +484,7 @@ def _add_win_memory_size(self, dom, wnd_size):
args = {}
args['-global'] = val
root.append(etree.fromstring(get_qemucmdline_xml(args)))
return etree.tostring(root, encoding='utf-8', pretty_print=True)
return etree.tostring(root, encoding='unicode', pretty_print=True)

# <qemu:commandline> exists but there is no <qemu:arg value global>
# so, we add those missing arguments inside the exising cmdline
Expand All @@ -493,7 +493,7 @@ def _add_win_memory_size(self, dom, wnd_size):
cmdline.append(EM.arg(value='-global'))
cmdline.append(EM.arg(value=val))

return etree.tostring(root, encoding='utf-8', pretty_print=True)
return etree.tostring(root, encoding='unicode', pretty_print=True)

def _get_scsi_device_xml(self, dev_info):
adapter = E.adapter(name=('scsi_host%s' % dev_info['host']))
Expand Down
44 changes: 23 additions & 21 deletions model/vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ def update(self, name, params):

# You can only change <maxMemory> offline, updating guest XML
if (
('memory' in params) and
('maxmemory' in params['memory']) and
(DOM_STATE_MAP[dom.info()[0]] != 'shutoff')
('memory' in params)
and ('maxmemory' in params['memory'])
and (DOM_STATE_MAP[dom.info()[0]] != 'shutoff')
):
raise InvalidParameter('KCHVM0080E')

Expand Down Expand Up @@ -722,8 +722,8 @@ def _get_access_info(self, dom):
access_info = dictize(access_xml)
auth = config.get('authentication', 'method')
if 'auth' in access_info['access'] and (
'type' in access_info['access']['auth'] or
len(access_info['access']['auth']) > 1
'type' in access_info['access']['auth']
or len(access_info['access']['auth']) > 1
):
users = xpath_get_text(
access_xml, f"/access/auth[@type='{auth}']/user")
Expand Down Expand Up @@ -772,7 +772,7 @@ def _update_graphics(self, dom, params):

conn = self.conn.get()
if not dom.isActive():
return conn.defineXML(ET.tostring(root, encoding='utf-8').decode('utf-8'))
return conn.defineXML(ET.tostring(root, encoding='unicode'))

xml = dom.XMLDesc(libvirt.VIR_DOMAIN_XML_SECURE)
dom.updateDeviceFlags(
Expand Down Expand Up @@ -851,7 +851,7 @@ def _update_bootorder(self, xml, params):
os.append(get_bootmenu_node())

# update <os>
return ET.tostring(et)
return ET.tostring(et, encoding='unicode')

def _update_s390x_console(self, xml, params):
if xpath_get_text(xml, XPATH_DOMAIN_CONSOLE_TARGET):
Expand All @@ -865,7 +865,7 @@ def _update_s390x_console(self, xml, params):
et = ET.fromstring(xml)
devices = et.find('devices')
devices.append(console)
return ET.tostring(et)
return ET.tostring(et, encoding='unicode')

def _update_title(self, new_xml, title):
if len(xpath_get_text(new_xml, XPATH_TITLE)) > 0:
Expand All @@ -874,7 +874,7 @@ def _update_title(self, new_xml, title):
else:
et = ET.fromstring(new_xml)
et.append(E.title(title))
new_xml = ET.tostring(et)
new_xml = ET.tostring(et, encoding='unicode')

return new_xml

Expand All @@ -886,7 +886,7 @@ def _update_description(self, new_xml, description):
else:
et = ET.fromstring(new_xml)
et.append(E.description(description))
new_xml = ET.tostring(et)
new_xml = ET.tostring(et, encoding='unicode')

return new_xml

Expand Down Expand Up @@ -960,8 +960,8 @@ def _static_vm_update(self, vm_name, dom, params):
if 'name' in params:
lflags = libvirt.VIR_DOMAIN_SNAPSHOT_LIST_ROOTS
dflags = (
libvirt.VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN |
libvirt.VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY
libvirt.VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN
| libvirt.VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY
)

for virt_snap in dom.listAllSnapshots(lflags):
Expand Down Expand Up @@ -997,7 +997,8 @@ def _static_vm_update(self, vm_name, dom, params):
return nonascii_name if nonascii_name is not None else vm_name, dom

def _get_new_memory(self, root, newMem, oldMem, memDevs):
memDevsAmount = self._get_mem_dev_total_size(ET.tostring(root))
memDevsAmount = self._get_mem_dev_total_size(
ET.tostring(root, encoding='unicode'))

if newMem > (oldMem << 10):
return newMem - memDevsAmount
Expand All @@ -1013,7 +1014,7 @@ def _get_new_memory(self, root, newMem, oldMem, memDevs):
root.find('./devices').remove(dev)
if ((oldMem << 10) - totRemoved) <= newMem:
return newMem - self._get_mem_dev_total_size(
ET.tostring(root)
ET.tostring(root, encoding='unicode')
)

if newMem == (oldMem << 10):
Expand All @@ -1034,7 +1035,7 @@ def _set_max_memory(self, root, newMem, newMaxMem, maxMemTag):
# Just update value in max memory tag
maxMemTag.text = str(newMaxMem)
elif (maxMemTag is not None) and (newMem == newMaxMem):
if self._get_mem_dev_total_size(ET.tostring(root)) == 0:
if self._get_mem_dev_total_size(ET.tostring(root, encoding='unicode')) == 0:
# Remove the tag
root.remove(maxMemTag)
else:
Expand Down Expand Up @@ -1104,7 +1105,8 @@ def _update_memory_config(self, xml, params, dom):

if (maxMemTag is not None) and (not hasMaxMem):
if newMem == newMaxMem and (
self._get_mem_dev_total_size(ET.tostring(root)) == 0
self._get_mem_dev_total_size(
ET.tostring(root, encoding='unicode')) == 0
):
root.remove(maxMemTag)

Expand All @@ -1116,7 +1118,7 @@ def _update_memory_config(self, xml, params, dom):
memtune.remove(hl)
memtune.insert(0, E.hard_limit(
str(newMaxMem + 1048576), unit='Kib'))
return ET.tostring(root, encoding='utf-8')
return ET.tostring(root, encoding='unicode')

def get_vm_cpu_cores(self, vm_xml):
return xpath_get_text(vm_xml, XPATH_TOPOLOGY + '/@cores')[0]
Expand Down Expand Up @@ -1224,8 +1226,8 @@ def _update_memory_live(self, dom, params):
)
# Check number of slots supported
if (
len(xpath_get_text(xml, './devices/memory')) ==
MEM_DEV_SLOTS[os.uname()[4]]
len(xpath_get_text(xml, './devices/memory'))
== MEM_DEV_SLOTS[os.uname()[4]]
):
raise InvalidOperation('KCHVM0045E')

Expand Down Expand Up @@ -1728,8 +1730,8 @@ def serial(self, name):
)

websocket.add_proxy_token(
name
+ '-console', os.path.join(serialconsole.BASE_DIRECTORY, name), True
name +
'-console', os.path.join(serialconsole.BASE_DIRECTORY, name), True
)

try:
Expand Down
2 changes: 1 addition & 1 deletion model/vmsnapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _create_task(self, cb, params):
cb('building snapshot XML')
root_elem = E.domainsnapshot()
root_elem.append(E.name(name))
xml = ET.tostring(root_elem, encoding='utf-8').decode('utf-8')
xml = ET.tostring(root_elem, encoding='unicode')

try:
cb('fetching snapshot domain')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2120,7 +2120,7 @@ def test_vmhostdev_get_devices_same_addr(self):
<address type="pci" domain="0x0000" bus="0x00" slot="0x05" function="0x1"/>\
</hostdev>"""
same_addr_devices = [
ET.tostring(hostdev_multi_elem).decode('utf-8'),
ET.tostring(hostdev_multi_elem, encoding='unicode'),
hostdev_same_addr_str,
]

Expand All @@ -2135,7 +2135,7 @@ def test_vmhostdev_get_devices_same_addr(self):

self.assertEqual(
inst.vmhostdev_get_devices_same_addr(hostdevs, nomatch_elem),
[ET.tostring(nomatch_elem).decode('utf-8')],
[ET.tostring(nomatch_elem, encoding='unicode')],
)

@mock.patch('wok.plugins.kimchi.model.vmhostdevs.get_vm_config_flag')
Expand Down
12 changes: 6 additions & 6 deletions tests/test_networkxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def normalize_xml(xml_str):
return ET.tostring(ET.fromstring(xml_str, ET.XMLParser(remove_blank_text=True)))
return ET.tostring(ET.fromstring(xml_str, ET.XMLParser(remove_blank_text=True)), encoding='unicode')


class NetworkXmlTests(unittest.TestCase):
Expand All @@ -50,14 +50,14 @@ def test_dhcp_xml(self):
self.assertEqual(None, dhcp)

params['range'] = dhcp_range
xml = ET.tostring(nxml._get_dhcp_elem(**params))
xml = ET.tostring(nxml._get_dhcp_elem(**params), encoding='unicode')
start = xpath_get_text(xml, '/dhcp/range/@start')
end = xpath_get_text(xml, '/dhcp/range/@end')
self.assertEqual(dhcp_range['start'], start[0])
self.assertEqual(dhcp_range['end'], end[0])

params['hosts'] = [host1, host2]
xml = ET.tostring(nxml._get_dhcp_elem(**params))
xml = ET.tostring(nxml._get_dhcp_elem(**params), encoding='unicode')
ip = xpath_get_text(xml, '/dhcp/host/@ip')
self.assertEqual(ip, [host1['ip'], host2['ip']])

Expand All @@ -73,7 +73,7 @@ def test_ip_xml(self):

params['net'] = '192.168.122.0/255.255.255.0'
params['dhcp'] = {'range': dhcp_range}
xml = ET.tostring(nxml._get_ip_elem(**params))
xml = ET.tostring(nxml._get_ip_elem(**params), encoding='unicode')
start = xpath_get_text(xml, '/ip/dhcp/range/@start')[0]
end = xpath_get_text(xml, '/ip/dhcp/range/@end')[0]
self.assertEqual(dhcp_range['start'], start)
Expand All @@ -87,7 +87,7 @@ def test_ip_xml(self):
# test _get_ip_xml can accepts strings: '192.168.122.0/24',
# which is same as "192.168.122.0/255.255.255.0"
params['net'] = '192.168.122.0/24'
xml = ET.tostring(nxml._get_ip_elem(**params))
xml = ET.tostring(nxml._get_ip_elem(**params), encoding='unicode')
netmask = xpath_get_text(xml, '/ip/@netmask')[0]
self.assertEqual(netmask, str(ipaddr.IPNetwork(params['net']).netmask))

Expand All @@ -102,7 +102,7 @@ def test_forward_xml(self):

params['mode'] = 'nat'
params['dev'] = 'eth0'
xml = ET.tostring(nxml._get_forward_elem(**params))
xml = ET.tostring(nxml._get_forward_elem(**params), encoding='unicode')
mode = xpath_get_text(xml, '/forward/@mode')[0]
dev = xpath_get_text(xml, '/forward/@dev')[0]
self.assertEqual(params['mode'], mode)
Expand Down
4 changes: 1 addition & 3 deletions xmlutils/bootorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def get_bootorder_xml(boot_order=None):
"""
boot_xml = ''
for device in get_bootorder_node(boot_order):
boot_xml += ET.tostring(device, encoding='utf-8', pretty_print=True).decode(
'utf-8'
)
boot_xml += ET.tostring(device, encoding='unicode', pretty_print=True)

return boot_xml

Expand Down
Loading

0 comments on commit b3104fb

Please sign in to comment.