Skip to content

Commit

Permalink
Merge pull request #52574 from zer0def/fix52431
Browse files Browse the repository at this point in the history
Address inconsistent usage of `get_xml` function in `virt` execution module.
  • Loading branch information
dwoz authored Apr 18, 2019
2 parents 21d6365 + b2698d1 commit 83a2b43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion salt/modules/virt.py
Original file line number Diff line number Diff line change
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 @@ -1437,6 +1441,23 @@ def test_get_nics(self):
self.assertEqual('bridge', nic['type'])
self.assertEqual('ac:de:48:b6:8b:59', nic['mac'])

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 83a2b43

Please sign in to comment.