2013-09-26 15:21:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import python libs
|
2018-01-19 20:59:53 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2013-09-12 00:01:35 +00:00
|
|
|
import re
|
2017-09-27 22:12:05 +00:00
|
|
|
import os
|
2013-09-26 15:21:37 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
|
2013-09-26 15:21:37 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
2017-12-28 04:31:50 +00:00
|
|
|
import salt.utils.yaml
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.virt as virt
|
|
|
|
import salt.modules.config as config
|
2013-10-26 16:46:17 +00:00
|
|
|
from salt._compat import ElementTree as ET
|
2017-09-27 22:12:05 +00:00
|
|
|
import salt.config
|
2013-09-26 15:21:37 +00:00
|
|
|
|
2013-09-26 23:11:28 +00:00
|
|
|
# Import third party libs
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
from salt.ext import six
|
2013-09-26 23:11:28 +00:00
|
|
|
|
2013-09-26 15:21:37 +00:00
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 15:35:30 +00:00
|
|
|
class VirtTestCase(TestCase, LoaderModuleMockMixin):
|
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
loader_globals = {
|
2017-02-19 15:35:30 +00:00
|
|
|
'__salt__': {
|
|
|
|
'config.get': config.get,
|
|
|
|
'config.option': config.option,
|
|
|
|
}
|
|
|
|
}
|
2017-03-22 12:12:36 +00:00
|
|
|
return {virt: loader_globals, config: loader_globals}
|
2013-09-26 15:21:37 +00:00
|
|
|
|
2013-10-16 18:38:41 +00:00
|
|
|
def test_boot_default_dev(self):
|
|
|
|
diskp = virt._disk_profile('default', 'kvm')
|
|
|
|
nicp = virt._nic_profile('default', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm'
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 18:38:41 +00:00
|
|
|
self.assertEqual(root.find('os/boot').attrib['dev'], 'hd')
|
|
|
|
|
|
|
|
def test_boot_custom_dev(self):
|
|
|
|
diskp = virt._disk_profile('default', 'kvm')
|
|
|
|
nicp = virt._nic_profile('default', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm',
|
|
|
|
boot_dev='cdrom'
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 18:38:41 +00:00
|
|
|
self.assertEqual(root.find('os/boot').attrib['dev'], 'cdrom')
|
|
|
|
|
|
|
|
def test_boot_multiple_devs(self):
|
|
|
|
diskp = virt._disk_profile('default', 'kvm')
|
|
|
|
nicp = virt._nic_profile('default', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm',
|
|
|
|
boot_dev='cdrom network'
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 18:38:41 +00:00
|
|
|
devs = root.findall('.//boot')
|
|
|
|
self.assertTrue(len(devs) == 2)
|
|
|
|
|
2013-10-16 15:04:11 +00:00
|
|
|
def test_gen_xml_for_serial_console(self):
|
2013-10-03 18:18:23 +00:00
|
|
|
diskp = virt._disk_profile('default', 'kvm')
|
|
|
|
nicp = virt._nic_profile('default', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm',
|
2013-10-16 15:04:11 +00:00
|
|
|
serial_type='pty',
|
2013-10-03 18:18:23 +00:00
|
|
|
console=True
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 08:14:50 +00:00
|
|
|
self.assertEqual(root.find('devices/serial').attrib['type'], 'pty')
|
|
|
|
self.assertEqual(root.find('devices/console').attrib['type'], 'pty')
|
2013-10-03 18:18:23 +00:00
|
|
|
|
2013-10-16 15:04:11 +00:00
|
|
|
def test_gen_xml_for_telnet_console(self):
|
|
|
|
diskp = virt._disk_profile('default', 'kvm')
|
|
|
|
nicp = virt._nic_profile('default', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm',
|
|
|
|
serial_type='tcp',
|
|
|
|
console=True,
|
|
|
|
telnet_port=22223
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 15:04:11 +00:00
|
|
|
self.assertEqual(root.find('devices/serial').attrib['type'], 'tcp')
|
|
|
|
self.assertEqual(root.find('devices/console').attrib['type'], 'tcp')
|
|
|
|
self.assertEqual(root.find('devices/console/source').attrib['service'], '22223')
|
|
|
|
|
2013-10-16 15:20:47 +00:00
|
|
|
def test_gen_xml_for_telnet_console_unspecified_port(self):
|
|
|
|
diskp = virt._disk_profile('default', 'kvm')
|
|
|
|
nicp = virt._nic_profile('default', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm',
|
|
|
|
serial_type='tcp',
|
|
|
|
console=True
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 15:20:47 +00:00
|
|
|
self.assertEqual(root.find('devices/serial').attrib['type'], 'tcp')
|
|
|
|
self.assertEqual(root.find('devices/console').attrib['type'], 'tcp')
|
|
|
|
self.assertIsInstance(int(root.find('devices/console/source').attrib['service']), int)
|
|
|
|
|
2013-10-03 18:18:23 +00:00
|
|
|
def test_gen_xml_for_serial_no_console(self):
|
|
|
|
diskp = virt._disk_profile('default', 'kvm')
|
|
|
|
nicp = virt._nic_profile('default', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm',
|
2013-10-16 15:04:11 +00:00
|
|
|
serial_type='pty',
|
2013-10-03 18:18:23 +00:00
|
|
|
console=False
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 08:14:50 +00:00
|
|
|
self.assertEqual(root.find('devices/serial').attrib['type'], 'pty')
|
|
|
|
self.assertEqual(root.find('devices/console'), None)
|
2013-10-16 15:04:11 +00:00
|
|
|
|
|
|
|
def test_gen_xml_for_telnet_no_console(self):
|
|
|
|
diskp = virt._disk_profile('default', 'kvm')
|
|
|
|
nicp = virt._nic_profile('default', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm',
|
|
|
|
serial_type='tcp',
|
|
|
|
console=False,
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 15:04:11 +00:00
|
|
|
self.assertEqual(root.find('devices/serial').attrib['type'], 'tcp')
|
|
|
|
self.assertEqual(root.find('devices/console'), None)
|
2013-10-03 18:18:23 +00:00
|
|
|
|
2013-09-26 15:21:37 +00:00
|
|
|
def test_default_disk_profile_hypervisor_esxi(self):
|
|
|
|
mock = MagicMock(return_value={})
|
|
|
|
with patch.dict(virt.__salt__, {'config.get': mock}):
|
2014-11-05 23:28:57 +00:00
|
|
|
ret = virt._disk_profile('nonexistent', 'esxi')
|
2013-09-26 15:21:37 +00:00
|
|
|
self.assertTrue(len(ret) == 1)
|
|
|
|
self.assertIn('system', ret[0])
|
|
|
|
system = ret[0]['system']
|
|
|
|
self.assertEqual(system['format'], 'vmdk')
|
|
|
|
self.assertEqual(system['model'], 'scsi')
|
2015-06-09 19:06:44 +00:00
|
|
|
self.assertTrue(int(system['size']) >= 1)
|
2013-09-26 15:21:37 +00:00
|
|
|
|
|
|
|
def test_default_disk_profile_hypervisor_kvm(self):
|
|
|
|
mock = MagicMock(return_value={})
|
|
|
|
with patch.dict(virt.__salt__, {'config.get': mock}):
|
2014-11-05 23:28:57 +00:00
|
|
|
ret = virt._disk_profile('nonexistent', 'kvm')
|
2013-09-26 15:21:37 +00:00
|
|
|
self.assertTrue(len(ret) == 1)
|
|
|
|
self.assertIn('system', ret[0])
|
|
|
|
system = ret[0]['system']
|
|
|
|
self.assertEqual(system['format'], 'qcow2')
|
|
|
|
self.assertEqual(system['model'], 'virtio')
|
2015-06-09 19:06:44 +00:00
|
|
|
self.assertTrue(int(system['size']) >= 1)
|
2013-09-26 15:21:37 +00:00
|
|
|
|
|
|
|
def test_default_nic_profile_hypervisor_esxi(self):
|
|
|
|
mock = MagicMock(return_value={})
|
|
|
|
with patch.dict(virt.__salt__, {'config.get': mock}):
|
2014-11-05 23:28:57 +00:00
|
|
|
ret = virt._nic_profile('nonexistent', 'esxi')
|
2013-09-26 15:21:37 +00:00
|
|
|
self.assertTrue(len(ret) == 1)
|
2013-09-12 00:01:35 +00:00
|
|
|
eth0 = ret[0]
|
|
|
|
self.assertEqual(eth0['name'], 'eth0')
|
|
|
|
self.assertEqual(eth0['type'], 'bridge')
|
|
|
|
self.assertEqual(eth0['source'], 'DEFAULT')
|
2013-09-26 15:21:37 +00:00
|
|
|
self.assertEqual(eth0['model'], 'e1000')
|
|
|
|
|
|
|
|
def test_default_nic_profile_hypervisor_kvm(self):
|
|
|
|
mock = MagicMock(return_value={})
|
|
|
|
with patch.dict(virt.__salt__, {'config.get': mock}):
|
2014-11-05 23:28:57 +00:00
|
|
|
ret = virt._nic_profile('nonexistent', 'kvm')
|
2013-09-26 15:21:37 +00:00
|
|
|
self.assertTrue(len(ret) == 1)
|
2013-09-12 00:01:35 +00:00
|
|
|
eth0 = ret[0]
|
|
|
|
self.assertEqual(eth0['name'], 'eth0')
|
|
|
|
self.assertEqual(eth0['type'], 'bridge')
|
|
|
|
self.assertEqual(eth0['source'], 'br0')
|
2013-09-26 15:21:37 +00:00
|
|
|
self.assertEqual(eth0['model'], 'virtio')
|
|
|
|
|
2013-10-16 07:58:37 +00:00
|
|
|
def test_gen_vol_xml_for_kvm(self):
|
|
|
|
xml_data = virt._gen_vol_xml('vmname', 'system', 8192, 'kvm')
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 07:58:37 +00:00
|
|
|
self.assertEqual(root.find('name').text, 'vmname/system.qcow2')
|
|
|
|
self.assertEqual(root.find('key').text, 'vmname/system')
|
|
|
|
self.assertEqual(root.find('capacity').attrib['unit'], 'KiB')
|
2018-01-19 20:59:53 +00:00
|
|
|
self.assertEqual(root.find('capacity').text, six.text_type(8192 * 1024))
|
2013-10-16 07:58:37 +00:00
|
|
|
|
|
|
|
def test_gen_vol_xml_for_esxi(self):
|
|
|
|
xml_data = virt._gen_vol_xml('vmname', 'system', 8192, 'esxi')
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 07:58:37 +00:00
|
|
|
self.assertEqual(root.find('name').text, 'vmname/system.vmdk')
|
|
|
|
self.assertEqual(root.find('key').text, 'vmname/system')
|
|
|
|
self.assertEqual(root.find('capacity').attrib['unit'], 'KiB')
|
2018-01-19 20:59:53 +00:00
|
|
|
self.assertEqual(root.find('capacity').text, six.text_type(8192 * 1024))
|
2013-10-16 07:58:37 +00:00
|
|
|
|
2013-09-26 23:39:07 +00:00
|
|
|
def test_gen_xml_for_kvm_default_profile(self):
|
|
|
|
diskp = virt._disk_profile('default', 'kvm')
|
|
|
|
nicp = virt._nic_profile('default', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm',
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 08:18:03 +00:00
|
|
|
self.assertEqual(root.attrib['type'], 'kvm')
|
|
|
|
self.assertEqual(root.find('vcpu').text, '1')
|
2018-01-19 20:59:53 +00:00
|
|
|
self.assertEqual(root.find('memory').text, six.text_type(512 * 1024))
|
2013-10-16 08:18:03 +00:00
|
|
|
self.assertEqual(root.find('memory').attrib['unit'], 'KiB')
|
2013-10-16 13:44:01 +00:00
|
|
|
|
|
|
|
disks = root.findall('.//disk')
|
|
|
|
self.assertEqual(len(disks), 1)
|
|
|
|
disk = disks[0]
|
2017-09-27 22:12:05 +00:00
|
|
|
root_dir = salt.config.DEFAULT_MINION_OPTS.get('root_dir')
|
|
|
|
self.assertTrue(disk.find('source').attrib['file'].startswith(root_dir))
|
|
|
|
self.assertTrue(os.path.join('hello', 'system') in disk.find('source').attrib['file'])
|
2013-10-16 13:44:01 +00:00
|
|
|
self.assertEqual(disk.find('target').attrib['dev'], 'vda')
|
|
|
|
self.assertEqual(disk.find('target').attrib['bus'], 'virtio')
|
|
|
|
self.assertEqual(disk.find('driver').attrib['name'], 'qemu')
|
|
|
|
self.assertEqual(disk.find('driver').attrib['type'], 'qcow2')
|
2013-09-12 00:01:35 +00:00
|
|
|
|
|
|
|
interfaces = root.findall('.//interface')
|
2013-10-16 08:14:50 +00:00
|
|
|
self.assertEqual(len(interfaces), 1)
|
2013-09-12 00:01:35 +00:00
|
|
|
iface = interfaces[0]
|
2013-10-16 08:14:50 +00:00
|
|
|
self.assertEqual(iface.attrib['type'], 'bridge')
|
|
|
|
self.assertEqual(iface.find('source').attrib['bridge'], 'br0')
|
|
|
|
self.assertEqual(iface.find('model').attrib['type'], 'virtio')
|
2013-09-12 00:01:35 +00:00
|
|
|
|
|
|
|
mac = iface.find('mac').attrib['address']
|
|
|
|
self.assertTrue(
|
|
|
|
re.match('^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$', mac, re.I))
|
|
|
|
|
2013-09-26 23:39:07 +00:00
|
|
|
def test_gen_xml_for_esxi_default_profile(self):
|
|
|
|
diskp = virt._disk_profile('default', 'esxi')
|
|
|
|
nicp = virt._nic_profile('default', 'esxi')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'esxi',
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 08:18:03 +00:00
|
|
|
self.assertEqual(root.attrib['type'], 'vmware')
|
|
|
|
self.assertEqual(root.find('vcpu').text, '1')
|
2018-01-19 20:59:53 +00:00
|
|
|
self.assertEqual(root.find('memory').text, six.text_type(512 * 1024))
|
2013-10-16 08:18:03 +00:00
|
|
|
self.assertEqual(root.find('memory').attrib['unit'], 'KiB')
|
2013-10-16 13:44:01 +00:00
|
|
|
|
|
|
|
disks = root.findall('.//disk')
|
|
|
|
self.assertEqual(len(disks), 1)
|
|
|
|
disk = disks[0]
|
|
|
|
self.assertTrue('[0]' in disk.find('source').attrib['file'])
|
2017-09-27 22:12:05 +00:00
|
|
|
self.assertTrue(os.path.join('hello', 'system') in disk.find('source').attrib['file'])
|
2013-10-16 13:44:01 +00:00
|
|
|
self.assertEqual(disk.find('target').attrib['dev'], 'sda')
|
|
|
|
self.assertEqual(disk.find('target').attrib['bus'], 'scsi')
|
|
|
|
self.assertEqual(disk.find('address').attrib['unit'], '0')
|
2013-09-12 00:01:35 +00:00
|
|
|
|
|
|
|
interfaces = root.findall('.//interface')
|
2013-10-16 08:14:50 +00:00
|
|
|
self.assertEqual(len(interfaces), 1)
|
2013-09-12 00:01:35 +00:00
|
|
|
iface = interfaces[0]
|
2013-10-16 08:14:50 +00:00
|
|
|
self.assertEqual(iface.attrib['type'], 'bridge')
|
|
|
|
self.assertEqual(iface.find('source').attrib['bridge'], 'DEFAULT')
|
|
|
|
self.assertEqual(iface.find('model').attrib['type'], 'e1000')
|
2013-09-12 00:01:35 +00:00
|
|
|
|
|
|
|
mac = iface.find('mac').attrib['address']
|
|
|
|
self.assertTrue(
|
|
|
|
re.match('^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$', mac, re.I))
|
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
def test_gen_xml_for_esxi_custom_profile(self):
|
2013-09-26 23:11:28 +00:00
|
|
|
diskp_yaml = '''
|
|
|
|
- first:
|
|
|
|
size: 8192
|
|
|
|
format: vmdk
|
|
|
|
model: scsi
|
2013-09-12 00:01:35 +00:00
|
|
|
pool: datastore1
|
2013-09-26 23:11:28 +00:00
|
|
|
- second:
|
|
|
|
size: 4096
|
2013-09-26 23:39:07 +00:00
|
|
|
format: vmdk # FIX remove line, currently test fails
|
|
|
|
model: scsi # FIX remove line, currently test fails
|
2013-09-12 00:01:35 +00:00
|
|
|
pool: datastore2
|
2013-09-26 23:11:28 +00:00
|
|
|
'''
|
|
|
|
nicp_yaml = '''
|
2013-09-12 00:01:35 +00:00
|
|
|
- type: bridge
|
|
|
|
name: eth1
|
|
|
|
source: ONENET
|
|
|
|
model: e1000
|
|
|
|
mac: '00:00:00:00:00:00'
|
|
|
|
- name: eth2
|
|
|
|
type: bridge
|
|
|
|
source: TWONET
|
2013-09-26 23:11:28 +00:00
|
|
|
model: e1000
|
2013-09-12 00:01:35 +00:00
|
|
|
mac: '00:00:00:00:00:00'
|
2013-09-26 23:11:28 +00:00
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.virt._nic_profile') as nic_profile, \
|
|
|
|
patch('salt.modules.virt._disk_profile') as disk_profile:
|
2017-12-28 04:31:50 +00:00
|
|
|
disk_profile.return_value = salt.utils.yaml.safe_load(diskp_yaml)
|
|
|
|
nic_profile.return_value = salt.utils.yaml.safe_load(nicp_yaml)
|
2017-04-10 13:00:57 +00:00
|
|
|
diskp = virt._disk_profile('noeffect', 'esxi')
|
|
|
|
nicp = virt._nic_profile('noeffect', 'esxi')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'esxi',
|
|
|
|
)
|
|
|
|
root = ET.fromstring(xml_data)
|
|
|
|
self.assertEqual(root.attrib['type'], 'vmware')
|
|
|
|
self.assertEqual(root.find('vcpu').text, '1')
|
2018-01-19 20:59:53 +00:00
|
|
|
self.assertEqual(root.find('memory').text, six.text_type(512 * 1024))
|
2017-04-10 13:00:57 +00:00
|
|
|
self.assertEqual(root.find('memory').attrib['unit'], 'KiB')
|
|
|
|
self.assertTrue(len(root.findall('.//disk')) == 2)
|
|
|
|
self.assertTrue(len(root.findall('.//interface')) == 2)
|
2013-09-26 15:21:37 +00:00
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
def test_gen_xml_for_kvm_custom_profile(self):
|
2013-09-26 23:39:07 +00:00
|
|
|
diskp_yaml = '''
|
|
|
|
- first:
|
|
|
|
size: 8192
|
|
|
|
format: qcow2
|
|
|
|
model: virtio
|
2013-09-12 00:01:35 +00:00
|
|
|
pool: /var/lib/images
|
2013-09-26 23:39:07 +00:00
|
|
|
- second:
|
|
|
|
size: 4096
|
|
|
|
format: qcow2 # FIX remove line, currently test fails
|
|
|
|
model: virtio # FIX remove line, currently test fails
|
2013-09-12 00:01:35 +00:00
|
|
|
pool: /var/lib/images
|
2013-09-26 23:39:07 +00:00
|
|
|
'''
|
|
|
|
nicp_yaml = '''
|
2013-09-12 00:01:35 +00:00
|
|
|
- type: bridge
|
|
|
|
name: eth1
|
2013-10-16 08:06:36 +00:00
|
|
|
source: b2
|
|
|
|
model: virtio
|
2013-09-12 00:01:35 +00:00
|
|
|
mac: '00:00:00:00:00:00'
|
|
|
|
- name: eth2
|
|
|
|
type: bridge
|
2013-10-16 08:06:36 +00:00
|
|
|
source: b2
|
2013-09-26 23:39:07 +00:00
|
|
|
model: virtio
|
2013-09-12 00:01:35 +00:00
|
|
|
mac: '00:00:00:00:00:00'
|
2013-09-26 23:39:07 +00:00
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.virt._nic_profile') as nic_profile, \
|
|
|
|
patch('salt.modules.virt._disk_profile') as disk_profile:
|
2017-12-28 04:31:50 +00:00
|
|
|
disk_profile.return_value = salt.utils.yaml.safe_load(diskp_yaml)
|
|
|
|
nic_profile.return_value = salt.utils.yaml.safe_load(nicp_yaml)
|
2017-04-10 13:00:57 +00:00
|
|
|
diskp = virt._disk_profile('noeffect', 'kvm')
|
|
|
|
nicp = virt._nic_profile('noeffect', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm',
|
|
|
|
)
|
|
|
|
root = ET.fromstring(xml_data)
|
|
|
|
self.assertEqual(root.attrib['type'], 'kvm')
|
|
|
|
self.assertEqual(root.find('vcpu').text, '1')
|
2018-01-19 20:59:53 +00:00
|
|
|
self.assertEqual(root.find('memory').text, six.text_type(512 * 1024))
|
2017-04-10 13:00:57 +00:00
|
|
|
self.assertEqual(root.find('memory').attrib['unit'], 'KiB')
|
|
|
|
self.assertTrue(len(root.findall('.//disk')) == 2)
|
|
|
|
self.assertTrue(len(root.findall('.//interface')) == 2)
|
2013-09-12 00:01:35 +00:00
|
|
|
|
2013-10-16 19:06:26 +00:00
|
|
|
def test_controller_for_esxi(self):
|
|
|
|
diskp = virt._disk_profile('default', 'esxi')
|
|
|
|
nicp = virt._nic_profile('default', 'esxi')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'esxi'
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 19:06:26 +00:00
|
|
|
controllers = root.findall('.//devices/controller')
|
|
|
|
self.assertTrue(len(controllers) == 1)
|
|
|
|
controller = controllers[0]
|
|
|
|
self.assertEqual(controller.attrib['model'], 'lsilogic')
|
|
|
|
|
2013-10-16 19:10:59 +00:00
|
|
|
def test_controller_for_kvm(self):
|
|
|
|
diskp = virt._disk_profile('default', 'kvm')
|
|
|
|
nicp = virt._nic_profile('default', 'kvm')
|
|
|
|
xml_data = virt._gen_xml(
|
|
|
|
'hello',
|
|
|
|
1,
|
|
|
|
512,
|
|
|
|
diskp,
|
|
|
|
nicp,
|
|
|
|
'kvm'
|
|
|
|
)
|
2013-10-26 16:46:17 +00:00
|
|
|
root = ET.fromstring(xml_data)
|
2013-10-16 19:10:59 +00:00
|
|
|
controllers = root.findall('.//devices/controller')
|
|
|
|
# There should be no controller
|
|
|
|
self.assertTrue(len(controllers) == 0)
|
2017-10-12 08:32:45 +00:00
|
|
|
# kvm mac address shoud start with 52:54:00
|
|
|
|
self.assertTrue("mac address='52:54:00" in xml_data)
|
2013-10-16 19:10:59 +00:00
|
|
|
|
2013-09-12 00:01:35 +00:00
|
|
|
def test_mixed_dict_and_list_as_profile_objects(self):
|
|
|
|
|
|
|
|
yaml_config = '''
|
|
|
|
virt.nic:
|
|
|
|
new-listonly-profile:
|
|
|
|
- bridge: br0
|
|
|
|
name: eth0
|
|
|
|
- model: virtio
|
|
|
|
name: eth1
|
|
|
|
source: test_network
|
|
|
|
type: network
|
|
|
|
new-list-with-legacy-names:
|
|
|
|
- eth0:
|
|
|
|
bridge: br0
|
|
|
|
- eth1:
|
|
|
|
bridge: br1
|
|
|
|
model: virtio
|
|
|
|
non-default-legacy-profile:
|
|
|
|
eth0:
|
|
|
|
bridge: br0
|
|
|
|
eth1:
|
|
|
|
bridge: br1
|
|
|
|
model: virtio
|
|
|
|
'''
|
2017-12-28 04:31:50 +00:00
|
|
|
mock_config = salt.utils.yaml.safe_load(yaml_config)
|
2017-02-19 15:35:30 +00:00
|
|
|
with patch.dict(salt.modules.config.__opts__, mock_config):
|
|
|
|
|
|
|
|
for name in six.iterkeys(mock_config['virt.nic']):
|
|
|
|
profile = salt.modules.virt._nic_profile(name, 'kvm')
|
|
|
|
self.assertEqual(len(profile), 2)
|
|
|
|
|
|
|
|
interface_attrs = profile[0]
|
|
|
|
self.assertIn('source', interface_attrs)
|
|
|
|
self.assertIn('type', interface_attrs)
|
|
|
|
self.assertIn('name', interface_attrs)
|
|
|
|
self.assertIn('model', interface_attrs)
|
|
|
|
self.assertEqual(interface_attrs['model'], 'virtio')
|
|
|
|
self.assertIn('mac', interface_attrs)
|
|
|
|
self.assertTrue(
|
|
|
|
re.match('^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$',
|
|
|
|
interface_attrs['mac'], re.I))
|
2013-09-26 23:39:07 +00:00
|
|
|
|
2015-03-10 01:51:44 +00:00
|
|
|
def test_get_graphics(self):
|
2017-03-31 11:51:30 +00:00
|
|
|
get_xml_mock = MagicMock(return_value='''<domain type='kvm' id='7'>
|
2015-03-10 01:51:44 +00:00
|
|
|
<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>
|
|
|
|
''')
|
2017-03-31 11:51:30 +00:00
|
|
|
with patch.object(virt, 'get_xml', get_xml_mock):
|
|
|
|
graphics = virt.get_graphics('test-vm')
|
|
|
|
self.assertEqual('vnc', graphics['type'])
|
|
|
|
self.assertEqual('5900', graphics['port'])
|
|
|
|
self.assertEqual('0.0.0.0', graphics['listen'])
|
2015-03-10 01:51:44 +00:00
|
|
|
|
|
|
|
def test_get_nics(self):
|
2017-03-31 11:51:30 +00:00
|
|
|
get_xml_mock = MagicMock(return_value='''<domain type='kvm' id='7'>
|
2015-03-10 01:51:44 +00:00
|
|
|
<name>test-vm</name>
|
|
|
|
<devices>
|
|
|
|
<interface type='bridge'>
|
|
|
|
<mac address='ac:de:48:b6:8b:59'/>
|
|
|
|
<source bridge='br0'/>
|
|
|
|
<model type='virtio'/>
|
|
|
|
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
|
|
|
|
</interface>
|
|
|
|
</devices>
|
|
|
|
</domain>
|
|
|
|
''')
|
2017-03-31 11:51:30 +00:00
|
|
|
with patch.object(virt, 'get_xml', get_xml_mock):
|
|
|
|
nics = virt.get_nics('test-vm')
|
|
|
|
nic = nics[list(nics)[0]]
|
|
|
|
self.assertEqual('bridge', nic['type'])
|
|
|
|
self.assertEqual('ac:de:48:b6:8b:59', nic['mac'])
|