Skip to content

Commit

Permalink
Align virt full info fixes with upstream (saltstack#192)
Browse files Browse the repository at this point in the history
* Porting PR saltstack#52574 to 2019.2.1

* Partly revert 4ce0bc5 to match upstream's fix
  • Loading branch information
cbosdo authored and meaksh committed Dec 9, 2019
1 parent 43cdd24 commit d569054
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
12 changes: 7 additions & 5 deletions salt/modules/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _get_uuid(dom):
salt '*' virt.get_uuid <domain>
'''
return ElementTree.fromstring(dom.XMLDesc(0)).find('uuid').text
return ElementTree.fromstring(get_xml(dom)).find('uuid').text


def _get_on_poweroff(dom):
Expand All @@ -344,7 +344,7 @@ def _get_on_poweroff(dom):
salt '*' virt.get_on_restart <domain>
'''
node = ElementTree.fromstring(dom.XMLDesc(0)).find('on_poweroff')
node = ElementTree.fromstring(get_xml(dom)).find('on_poweroff')
return node.text if node is not None else ''


Expand All @@ -358,7 +358,7 @@ def _get_on_reboot(dom):
salt '*' virt.get_on_reboot <domain>
'''
node = ElementTree.fromstring(dom.XMLDesc(0)).find('on_reboot')
node = ElementTree.fromstring(get_xml(dom)).find('on_reboot')
return node.text if node is not None else ''


Expand All @@ -372,7 +372,7 @@ def _get_on_crash(dom):
salt '*' virt.get_on_crash <domain>
'''
node = ElementTree.fromstring(dom.XMLDesc(0)).find('on_crash')
node = ElementTree.fromstring(get_xml(dom)).find('on_crash')
return node.text if node is not None else ''


Expand Down Expand Up @@ -2435,7 +2435,9 @@ def get_xml(vm_, **kwargs):
salt '*' virt.get_xml <domain>
'''
conn = __get_conn(**kwargs)
xml_desc = _get_domain(conn, vm_).XMLDesc(0)
xml_desc = vm_.XMLDesc(0) if isinstance(
vm_, libvirt.virDomain
) else _get_domain(conn, vm_).XMLDesc(0)
conn.close()
return xml_desc

Expand Down
23 changes: 22 additions & 1 deletion tests/unit/modules/test_virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class LibvirtMock(MagicMock): # pylint: disable=too-many-ancestors
'''
Libvirt library mock
'''
class virDomain(MagicMock):
'''
virDomain mock
'''

class libvirtError(Exception):
'''
Expand Down Expand Up @@ -76,7 +80,7 @@ def set_mock_vm(self, name, xml):
Define VM to use in tests
'''
self.mock_conn.listDefinedDomains.return_value = [name] # pylint: disable=no-member
mock_domain = MagicMock()
mock_domain = self.mock_libvirt.virDomain()
self.mock_conn.lookupByName.return_value = mock_domain # pylint: disable=no-member
mock_domain.XMLDesc.return_value = xml # pylint: disable=no-member

Expand Down Expand Up @@ -1396,6 +1400,23 @@ def test_mixed_dict_and_list_as_profile_objects(self):
re.match('^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$',
interface_attrs['mac'], re.I))

def test_get_xml(self):
'''
Test virt.get_xml()
'''
xml = '''<domain type='kvm' id='7'>
<name>test-vm</name>
<devices>
<graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
</devices>
</domain>
'''
domain = self.set_mock_vm("test-vm", xml)
self.assertEqual(xml, virt.get_xml('test-vm'))
self.assertEqual(xml, virt.get_xml(domain))

def test_parse_qemu_img_info(self):
'''
Make sure that qemu-img info output is properly parsed
Expand Down

0 comments on commit d569054

Please sign in to comment.