Merge pull request #50037 from cbosdo/fluorine-backport

Fluorine backport of virt module fixes
This commit is contained in:
Nicole Thomas 2018-10-15 09:31:30 -04:00 committed by GitHub
commit 23c7142bfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 155 additions and 30 deletions

View File

@ -1263,7 +1263,7 @@ def init(name,
Disk dictionaries can contain the following properties:
disk_name
name
Name of the disk. This is mostly used in the name of the disk image and as a key to merge
with the profile data.
@ -4270,7 +4270,7 @@ def list_networks(**kwargs):
conn.close()
def network_info(name, **kwargs):
def network_info(name=None, **kwargs):
'''
Return informations on a virtual network provided its name.
@ -4279,6 +4279,8 @@ def network_info(name, **kwargs):
:param username: username to connect with, overriding defaults
:param password: password to connect with, overriding defaults
If no name is provided, return the infos for all defined virtual networks.
.. versionadded:: Fluorine
CLI Example:
@ -4289,8 +4291,11 @@ def network_info(name, **kwargs):
'''
result = {}
conn = __get_conn(**kwargs)
try:
net = conn.networkLookupByName(name)
def _net_get_leases(net):
'''
Get all DHCP leases for a network
'''
leases = net.DHCPLeases()
for lease in leases:
if lease['type'] == libvirt.VIR_IP_ADDR_TYPE_IPV4:
@ -4299,15 +4304,17 @@ def network_info(name, **kwargs):
lease['type'] = 'ipv6'
else:
lease['type'] = 'unknown'
return leases
result = {
'uuid': net.UUIDString(),
'bridge': net.bridgeName(),
'autostart': net.autostart(),
'active': net.isActive(),
'persistent': net.isPersistent(),
'leases': leases
}
try:
nets = [net for net in conn.listAllNetworks() if name is None or net.name() == name]
result = {net.name(): {
'uuid': net.UUIDString(),
'bridge': net.bridgeName(),
'autostart': net.autostart(),
'active': net.isActive(),
'persistent': net.isPersistent(),
'leases': _net_get_leases(net)} for net in nets}
except libvirt.libvirtError as err:
log.debug('Silenced libvirt error: %s', str(err))
finally:
@ -4604,7 +4611,7 @@ def list_pools(**kwargs):
conn.close()
def pool_info(name, **kwargs):
def pool_info(name=None, **kwargs):
'''
Return informations on a storage pool provided its name.
@ -4613,6 +4620,8 @@ def pool_info(name, **kwargs):
:param username: username to connect with, overriding defaults
:param password: password to connect with, overriding defaults
If no name is provided, return the infos for all defined storage pools.
.. versionadded:: Fluorine
CLI Example:
@ -4623,14 +4632,19 @@ def pool_info(name, **kwargs):
'''
result = {}
conn = __get_conn(**kwargs)
try:
pool = conn.storagePoolLookupByName(name)
infos = pool.info()
def _pool_extract_infos(pool):
'''
Format the pool info dictionary
:param pool: the libvirt pool object
'''
states = ['inactive', 'building', 'running', 'degraded', 'inaccessible']
infos = pool.info()
state = states[infos[0]] if infos[0] < len(states) else 'unknown'
desc = ElementTree.fromstring(pool.XMLDesc())
path_node = desc.find('target/path')
result = {
return {
'uuid': pool.UUIDString(),
'state': state,
'capacity': infos[1],
@ -4641,6 +4655,10 @@ def pool_info(name, **kwargs):
'target_path': path_node.text if path_node is not None else None,
'type': desc.get('type')
}
try:
pools = [pool for pool in conn.listAllStoragePools() if name is None or pool.name() == name]
result = {pool.name(): _pool_extract_infos(pool) for pool in pools}
except libvirt.libvirtError as err:
log.debug('Silenced libvirt error: %s', str(err))
finally:

View File

@ -25,6 +25,8 @@ from salt.exceptions import CommandExecutionError
# Import third party libs
from salt.ext import six
# pylint: disable=import-error
from salt.ext.six.moves import range # pylint: disable=redefined-builtin
# pylint: disable=invalid-name,protected-access,attribute-defined-outside-init,too-many-public-methods,unused-argument
@ -1820,6 +1822,7 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin):
net_mock = MagicMock()
# pylint: disable=no-member
net_mock.name.return_value = 'foo'
net_mock.UUIDString.return_value = 'some-uuid'
net_mock.bridgeName.return_value = 'br0'
net_mock.autostart.return_value = True
@ -1838,11 +1841,11 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin):
'iaid': None
}
]
self.mock_conn.networkLookupByName.return_value = net_mock
self.mock_conn.listAllNetworks.return_value = [net_mock]
# pylint: enable=no-member
net = virt.network_info('foo')
self.assertEqual({
self.assertEqual({'foo': {
'uuid': 'some-uuid',
'bridge': 'br0',
'autostart': True,
@ -1860,15 +1863,58 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin):
'clientid': '01:52:54:00:01:71:bd',
'iaid': None
}
]}, net)
]}}, net)
def test_network_info_all(self):
'''
Test virt.network_info()
'''
self.mock_libvirt.VIR_IP_ADDR_TYPE_IPV4 = 0
self.mock_libvirt.VIR_IP_ADDR_TYPE_IPV6 = 1
net_mocks = []
for i in range(2):
net_mock = MagicMock()
# pylint: disable=no-member
net_mock.name.return_value = 'net{0}'.format(i)
net_mock.UUIDString.return_value = 'some-uuid'
net_mock.bridgeName.return_value = 'br{0}'.format(i)
net_mock.autostart.return_value = True
net_mock.isActive.return_value = False
net_mock.isPersistent.return_value = True
net_mock.DHCPLeases.return_value = []
net_mocks.append(net_mock)
self.mock_conn.listAllNetworks.return_value = net_mocks
# pylint: enable=no-member
net = virt.network_info()
self.assertEqual({
'net0':
{
'uuid': 'some-uuid',
'bridge': 'br0',
'autostart': True,
'active': False,
'persistent': True,
'leases': []
}, 'net1':
{
'uuid': 'some-uuid',
'bridge': 'br1',
'autostart': True,
'active': False,
'persistent': True,
'leases': []
}
}, net)
def test_network_info_notfound(self):
'''
Test virt.network_info() when the network can't be found
'''
# pylint: disable=no-member
self.mock_conn.networkLookupByName.side_effect = \
self.mock_libvirt.libvirtError("Network not found")
self.mock_conn.listAllNetworks.return_value = []
# pylint: enable=no-member
net = virt.network_info('foo')
self.assertEqual({}, net)
@ -2020,6 +2066,7 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin):
'''
# pylint: disable=no-member
pool_mock = MagicMock()
pool_mock.name.return_value = 'foo'
pool_mock.UUIDString.return_value = 'some-uuid'
pool_mock.info.return_value = [0, 1234, 5678, 123]
pool_mock.autostart.return_value = True
@ -2041,11 +2088,11 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin):
</permissions>
</target>
</pool>'''
self.mock_conn.storagePoolLookupByName.return_value = pool_mock
self.mock_conn.listAllStoragePools.return_value = [pool_mock]
# pylint: enable=no-member
pool = virt.pool_info('foo')
self.assertEqual({
self.assertEqual({'foo': {
'uuid': 'some-uuid',
'state': 'inactive',
'capacity': 1234,
@ -2054,7 +2101,7 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin):
'autostart': True,
'persistent': True,
'type': 'dir',
'target_path': '/srv/vms'}, pool)
'target_path': '/srv/vms'}}, pool)
def test_pool_info_notarget(self):
'''
@ -2062,6 +2109,7 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin):
'''
# pylint: disable=no-member
pool_mock = MagicMock()
pool_mock.name.return_value = 'ceph'
pool_mock.UUIDString.return_value = 'some-uuid'
pool_mock.info.return_value = [0, 0, 0, 0]
pool_mock.autostart.return_value = True
@ -2081,11 +2129,11 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin):
</auth>
</source>
</pool>'''
self.mock_conn.storagePoolLookupByName.return_value = pool_mock
self.mock_conn.listAllStoragePools.return_value = [pool_mock]
# pylint: enable=no-member
pool = virt.pool_info('ceph')
self.assertEqual({
self.assertEqual({'ceph': {
'uuid': 'some-uuid',
'state': 'inactive',
'capacity': 0,
@ -2094,19 +2142,78 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin):
'autostart': True,
'persistent': True,
'type': 'rbd',
'target_path': None}, pool)
'target_path': None}}, pool)
def test_pool_info_notfound(self):
'''
Test virt.pool_info() when the pool can't be found
'''
# pylint: disable=no-member
self.mock_conn.storagePoolLookupByName.side_effect = \
self.mock_libvirt.libvirtError("Pool not found")
self.mock_conn.listAllStoragePools.return_value = []
# pylint: enable=no-member
pool = virt.pool_info('foo')
self.assertEqual({}, pool)
def test_pool_info_all(self):
'''
Test virt.pool_info()
'''
# pylint: disable=no-member
pool_mocks = []
for i in range(2):
pool_mock = MagicMock()
pool_mock.name.return_value = 'pool{0}'.format(i)
pool_mock.UUIDString.return_value = 'some-uuid-{0}'.format(i)
pool_mock.info.return_value = [0, 1234, 5678, 123]
pool_mock.autostart.return_value = True
pool_mock.isPersistent.return_value = True
pool_mock.XMLDesc.return_value = '''<pool type='dir'>
<name>default</name>
<uuid>d92682d0-33cf-4e10-9837-a216c463e158</uuid>
<capacity unit='bytes'>854374301696</capacity>
<allocation unit='bytes'>596275986432</allocation>
<available unit='bytes'>258098315264</available>
<source>
</source>
<target>
<path>/srv/vms</path>
<permissions>
<mode>0755</mode>
<owner>0</owner>
<group>0</group>
</permissions>
</target>
</pool>'''
pool_mocks.append(pool_mock)
self.mock_conn.listAllStoragePools.return_value = pool_mocks
# pylint: enable=no-member
pool = virt.pool_info()
self.assertEqual({
'pool0':
{
'uuid': 'some-uuid-0',
'state': 'inactive',
'capacity': 1234,
'allocation': 5678,
'free': 123,
'autostart': True,
'persistent': True,
'type': 'dir',
'target_path': '/srv/vms'
}, 'pool1': {
'uuid': 'some-uuid-1',
'state': 'inactive',
'capacity': 1234,
'allocation': 5678,
'free': 123,
'autostart': True,
'persistent': True,
'type': 'dir',
'target_path': '/srv/vms'
}
}, pool)
def test_pool_list_volumes(self):
'''
Test virt.pool_list_volumes