xmlbuilder: Make embedded device XML print correctly

If you call get_xml() on a device that's part of a Guest class,
the last element has correct indent but not the first element.

Steal the indent from the last element and prepend it to the returned
XML

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2021-10-12 13:38:15 -04:00
parent 2e7b7745aa
commit 68ca651d85
4 changed files with 14 additions and 7 deletions

View File

@ -11,7 +11,7 @@
</devices>
</domain>
<interface type="user">
<interface type="user">
<mac address="00:11:22:33:44:55"/>
<model type="e1000"/>
</interface>

View File

@ -11,7 +11,7 @@
</devices>
</domain>
<interface type="user">
<interface type="user">
<mac address="00:11:22:33:44:55"/>
<model type="e1000"/>
</interface>

View File

@ -1030,11 +1030,11 @@ def testXMLBuilderCoverage():
xml = conn.lookupByName("test-for-virtxml").XMLDesc(0)
guest = virtinst.Guest(conn, parsexml=xml)
assert guest.features.get_xml().startswith("<features")
assert guest.clock.get_xml().startswith("<clock")
assert guest.features.get_xml().startswith(" <features")
assert guest.clock.get_xml().startswith(" <clock")
assert guest.seclabels[0].get_xml().startswith("<seclabel")
assert guest.cpu.get_xml().startswith("<cpu")
assert guest.os.get_xml().startswith("<os")
assert guest.cpu.get_xml().startswith(" <cpu")
assert guest.os.get_xml().startswith(" <os")
assert guest.cpu.get_xml_id() == "./cpu"
assert guest.cpu.get_xml_idx() == 0
assert guest.get_xml_id() == "."

View File

@ -586,7 +586,14 @@ class XMLBuilder(object):
self._add_parse_bits(xmlapi)
ret = xmlapi.get_xml(self._xmlstate.make_abs_xpath("."))
if ret and not ret.endswith("\n"):
if not ret:
return ret
lastline = ret.rstrip().splitlines()[-1]
if not ret.startswith(" ") and lastline.startswith(" "):
ret = lastline.split("<")[0] + ret
if not ret.endswith("\n"):
ret += "\n"
return ret