2016-02-22 22:35:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Erik Johnson <erik@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python libs
|
2018-01-10 17:37:59 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2017-11-25 20:32:43 +00:00
|
|
|
import logging
|
2016-02-22 22:35:53 +00:00
|
|
|
import os
|
2017-12-30 13:49:21 +00:00
|
|
|
import socket
|
2016-02-22 22:35:53 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2018-02-20 14:25:19 +00:00
|
|
|
try:
|
|
|
|
import pytest
|
|
|
|
except ImportError as import_error:
|
|
|
|
pytest = None
|
|
|
|
|
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 TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2016-02-22 22:35:53 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
2016-06-20 10:56:06 +00:00
|
|
|
mock_open,
|
2016-02-22 22:35:53 +00:00
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2018-03-27 15:57:29 +00:00
|
|
|
import salt.utils.dns
|
2018-02-27 22:20:45 +00:00
|
|
|
import salt.utils.files
|
2017-12-21 17:33:02 +00:00
|
|
|
import salt.utils.network
|
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
|
|
|
import salt.utils.platform
|
2017-02-19 15:35:30 +00:00
|
|
|
import salt.grains.core as core
|
2016-02-22 22:35:53 +00:00
|
|
|
|
2017-02-19 15:35:30 +00:00
|
|
|
# Import 3rd-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
|
2017-12-19 17:11:28 +00:00
|
|
|
if six.PY3:
|
|
|
|
import ipaddress
|
|
|
|
else:
|
|
|
|
import salt.ext.ipaddress as ipaddress
|
2016-02-22 22:35:53 +00:00
|
|
|
|
2017-12-12 20:24:30 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2016-02-22 22:35:53 +00:00
|
|
|
|
|
|
|
# Globals
|
2017-12-19 17:11:28 +00:00
|
|
|
IPv4Address = ipaddress.IPv4Address
|
|
|
|
IPv6Address = ipaddress.IPv6Address
|
2017-05-16 15:48:04 +00:00
|
|
|
IP4_LOCAL = '127.0.0.1'
|
|
|
|
IP4_ADD1 = '10.0.0.1'
|
|
|
|
IP4_ADD2 = '10.0.0.2'
|
|
|
|
IP6_LOCAL = '::1'
|
|
|
|
IP6_ADD1 = '2001:4860:4860::8844'
|
|
|
|
IP6_ADD2 = '2001:4860:4860::8888'
|
2018-02-19 12:14:45 +00:00
|
|
|
IP6_ADD_SCOPE = 'fe80::6238:e0ff:fe06:3f6b%enp2s0'
|
2018-02-22 14:00:28 +00:00
|
|
|
OS_RELEASE_DIR = os.path.join(os.path.dirname(__file__), "os-releases")
|
2016-02-22 22:35:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2018-02-20 14:25:19 +00:00
|
|
|
@skipIf(not pytest, False)
|
2017-02-19 15:35:30 +00:00
|
|
|
class CoreGrainsTestCase(TestCase, LoaderModuleMockMixin):
|
2016-02-22 22:35:53 +00:00
|
|
|
'''
|
|
|
|
Test cases for core grains
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {core: {}}
|
2017-02-19 15:35:30 +00:00
|
|
|
|
2018-02-26 12:38:23 +00:00
|
|
|
@patch("os.path.isfile")
|
|
|
|
def test_parse_etc_os_release(self, path_isfile_mock):
|
|
|
|
path_isfile_mock.side_effect = lambda x: x == "/usr/lib/os-release"
|
2018-02-27 22:20:45 +00:00
|
|
|
with salt.utils.files.fopen(os.path.join(OS_RELEASE_DIR, "ubuntu-17.10")) as os_release_file:
|
2018-02-26 12:38:23 +00:00
|
|
|
os_release_content = os_release_file.readlines()
|
2018-02-27 22:20:45 +00:00
|
|
|
with patch("salt.utils.files.fopen", mock_open()) as os_release_file:
|
2018-02-26 12:38:23 +00:00
|
|
|
os_release_file.return_value.__iter__.return_value = os_release_content
|
|
|
|
os_release = core._parse_os_release(["/etc/os-release", "/usr/lib/os-release"])
|
|
|
|
self.assertEqual(os_release, {
|
|
|
|
"NAME": "Ubuntu",
|
|
|
|
"VERSION": "17.10 (Artful Aardvark)",
|
|
|
|
"ID": "ubuntu",
|
|
|
|
"ID_LIKE": "debian",
|
|
|
|
"PRETTY_NAME": "Ubuntu 17.10",
|
|
|
|
"VERSION_ID": "17.10",
|
|
|
|
"HOME_URL": "https://www.ubuntu.com/",
|
|
|
|
"SUPPORT_URL": "https://help.ubuntu.com/",
|
|
|
|
"BUG_REPORT_URL": "https://bugs.launchpad.net/ubuntu/",
|
|
|
|
"PRIVACY_POLICY_URL": "https://www.ubuntu.com/legal/terms-and-policies/privacy-policy",
|
|
|
|
"VERSION_CODENAME": "artful",
|
|
|
|
"UBUNTU_CODENAME": "artful",
|
|
|
|
})
|
|
|
|
|
|
|
|
@patch("os.path.isfile")
|
|
|
|
def test_missing_os_release(self, path_isfile_mock):
|
|
|
|
path_isfile_mock.return_value = False
|
|
|
|
os_release = core._parse_os_release(["/etc/os-release", "/usr/lib/os-release"])
|
|
|
|
self.assertEqual(os_release, {})
|
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2016-02-22 22:35:53 +00:00
|
|
|
def test_gnu_slash_linux_in_os_name(self):
|
|
|
|
'''
|
|
|
|
Test to return a list of all enabled services
|
|
|
|
'''
|
|
|
|
_path_exists_map = {
|
|
|
|
'/proc/1/cmdline': False
|
|
|
|
}
|
|
|
|
_path_isfile_map = {}
|
|
|
|
_cmd_run_map = {
|
|
|
|
'dpkg --print-architecture': 'amd64'
|
|
|
|
}
|
|
|
|
|
|
|
|
path_exists_mock = MagicMock(side_effect=lambda x: _path_exists_map[x])
|
|
|
|
path_isfile_mock = MagicMock(
|
|
|
|
side_effect=lambda x: _path_isfile_map.get(x, False)
|
|
|
|
)
|
|
|
|
cmd_run_mock = MagicMock(
|
|
|
|
side_effect=lambda x: _cmd_run_map[x]
|
|
|
|
)
|
|
|
|
empty_mock = MagicMock(return_value={})
|
|
|
|
|
|
|
|
orig_import = __import__
|
2016-07-07 22:41:21 +00:00
|
|
|
if six.PY2:
|
|
|
|
built_in = '__builtin__'
|
|
|
|
else:
|
|
|
|
built_in = 'builtins'
|
2016-02-22 22:35:53 +00:00
|
|
|
|
|
|
|
def _import_mock(name, *args):
|
|
|
|
if name == 'lsb_release':
|
|
|
|
raise ImportError('No module named lsb_release')
|
|
|
|
return orig_import(name, *args)
|
|
|
|
|
|
|
|
# Skip the first if statement
|
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
|
|
|
with patch.object(salt.utils.platform, 'is_proxy',
|
2016-02-22 22:35:53 +00:00
|
|
|
MagicMock(return_value=False)):
|
|
|
|
# Skip the selinux/systemd stuff (not pertinent)
|
|
|
|
with patch.object(core, '_linux_bin_exists',
|
|
|
|
MagicMock(return_value=False)):
|
|
|
|
# Skip the init grain compilation (not pertinent)
|
|
|
|
with patch.object(os.path, 'exists', path_exists_mock):
|
|
|
|
# Ensure that lsb_release fails to import
|
2016-07-07 22:41:21 +00:00
|
|
|
with patch('{0}.__import__'.format(built_in),
|
2016-02-22 22:35:53 +00:00
|
|
|
side_effect=_import_mock):
|
|
|
|
# Skip all the /etc/*-release stuff (not pertinent)
|
|
|
|
with patch.object(os.path, 'isfile', path_isfile_mock):
|
2017-03-14 14:35:32 +00:00
|
|
|
# Mock linux_distribution to give us the OS name
|
|
|
|
# that we want.
|
2016-02-22 22:35:53 +00:00
|
|
|
distro_mock = MagicMock(
|
|
|
|
return_value=('Debian GNU/Linux', '8.3', '')
|
|
|
|
)
|
|
|
|
with patch.object(
|
2017-03-14 14:35:32 +00:00
|
|
|
core,
|
2016-02-22 22:35:53 +00:00
|
|
|
'linux_distribution',
|
|
|
|
distro_mock):
|
|
|
|
# Make a bunch of functions return empty dicts,
|
|
|
|
# we don't care about these grains for the
|
|
|
|
# purposes of this test.
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_linux_cpudata',
|
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_linux_gpu_data',
|
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_memdata',
|
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_hw_data',
|
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_virtual',
|
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_ps',
|
|
|
|
empty_mock):
|
|
|
|
# Mock the osarch
|
|
|
|
with patch.dict(
|
|
|
|
core.__salt__,
|
|
|
|
{'cmd.run': cmd_run_mock}):
|
|
|
|
os_grains = core.os_data()
|
|
|
|
|
|
|
|
self.assertEqual(os_grains.get('os_family'), 'Debian')
|
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2016-06-09 14:09:00 +00:00
|
|
|
def test_suse_os_from_cpe_data(self):
|
|
|
|
'''
|
|
|
|
Test if 'os' grain is parsed from CPE_NAME of /etc/os-release
|
|
|
|
'''
|
|
|
|
_path_exists_map = {
|
|
|
|
'/proc/1/cmdline': False
|
|
|
|
}
|
2016-06-13 09:19:17 +00:00
|
|
|
_os_release_map = {
|
|
|
|
'NAME': 'SLES',
|
|
|
|
'VERSION': '12-SP1',
|
|
|
|
'VERSION_ID': '12.1',
|
|
|
|
'PRETTY_NAME': 'SUSE Linux Enterprise Server 12 SP1',
|
|
|
|
'ID': 'sles',
|
|
|
|
'ANSI_COLOR': '0;32',
|
|
|
|
'CPE_NAME': 'cpe:/o:suse:sles:12:sp1'
|
|
|
|
}
|
2016-06-09 14:09:00 +00:00
|
|
|
|
|
|
|
path_exists_mock = MagicMock(side_effect=lambda x: _path_exists_map[x])
|
|
|
|
empty_mock = MagicMock(return_value={})
|
2016-06-10 08:48:37 +00:00
|
|
|
osarch_mock = MagicMock(return_value="amd64")
|
2016-06-13 09:19:17 +00:00
|
|
|
os_release_mock = MagicMock(return_value=_os_release_map)
|
2016-06-09 14:09:00 +00:00
|
|
|
|
|
|
|
orig_import = __import__
|
2016-07-07 22:41:21 +00:00
|
|
|
if six.PY2:
|
|
|
|
built_in = '__builtin__'
|
|
|
|
else:
|
|
|
|
built_in = 'builtins'
|
2016-06-09 14:09:00 +00:00
|
|
|
|
|
|
|
def _import_mock(name, *args):
|
|
|
|
if name == 'lsb_release':
|
|
|
|
raise ImportError('No module named lsb_release')
|
|
|
|
return orig_import(name, *args)
|
|
|
|
|
|
|
|
# Skip the first if statement
|
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
|
|
|
with patch.object(salt.utils.platform, 'is_proxy',
|
2016-06-09 14:09:00 +00:00
|
|
|
MagicMock(return_value=False)):
|
|
|
|
# Skip the selinux/systemd stuff (not pertinent)
|
|
|
|
with patch.object(core, '_linux_bin_exists',
|
|
|
|
MagicMock(return_value=False)):
|
|
|
|
# Skip the init grain compilation (not pertinent)
|
|
|
|
with patch.object(os.path, 'exists', path_exists_mock):
|
|
|
|
# Ensure that lsb_release fails to import
|
2016-07-07 22:41:21 +00:00
|
|
|
with patch('{0}.__import__'.format(built_in),
|
2016-06-09 14:09:00 +00:00
|
|
|
side_effect=_import_mock):
|
|
|
|
# Skip all the /etc/*-release stuff (not pertinent)
|
2018-02-21 18:32:52 +00:00
|
|
|
with patch.object(os.path, 'isfile', MagicMock(return_value=False)):
|
2016-06-13 09:19:17 +00:00
|
|
|
with patch.object(core, '_parse_os_release', os_release_mock):
|
2017-03-14 14:35:32 +00:00
|
|
|
# Mock linux_distribution to give us the OS
|
|
|
|
# name that we want.
|
2016-06-13 09:19:17 +00:00
|
|
|
distro_mock = MagicMock(
|
|
|
|
return_value=('SUSE Linux Enterprise Server ', '12', 'x86_64')
|
|
|
|
)
|
2017-03-14 14:35:32 +00:00
|
|
|
with patch.object(core, 'linux_distribution', distro_mock):
|
2016-06-13 09:19:17 +00:00
|
|
|
with patch.object(core, '_linux_gpu_data', empty_mock):
|
2017-05-03 13:10:29 +00:00
|
|
|
with patch.object(core, '_hw_data', empty_mock):
|
|
|
|
with patch.object(core, '_linux_cpudata', empty_mock):
|
|
|
|
with patch.object(core, '_virtual', empty_mock):
|
|
|
|
# Mock the osarch
|
|
|
|
with patch.dict(core.__salt__, {'cmd.run': osarch_mock}):
|
|
|
|
os_grains = core.os_data()
|
2016-06-09 14:09:00 +00:00
|
|
|
|
2016-08-12 12:33:27 +00:00
|
|
|
self.assertEqual(os_grains.get('os_family'), 'Suse')
|
2016-06-09 14:09:00 +00:00
|
|
|
self.assertEqual(os_grains.get('os'), 'SUSE')
|
|
|
|
|
2018-02-21 17:56:48 +00:00
|
|
|
def _run_os_grains_tests(self, os_release_filename, os_release_map, expectation):
|
2018-02-21 18:32:52 +00:00
|
|
|
path_isfile_mock = MagicMock(side_effect=lambda x: x in os_release_map.get('files', []))
|
2016-06-20 10:22:01 +00:00
|
|
|
empty_mock = MagicMock(return_value={})
|
|
|
|
osarch_mock = MagicMock(return_value="amd64")
|
2018-02-22 14:00:28 +00:00
|
|
|
if os_release_filename:
|
|
|
|
os_release_data = core._parse_os_release(
|
|
|
|
[os.path.join(OS_RELEASE_DIR, os_release_filename)])
|
|
|
|
else:
|
|
|
|
os_release_data = os_release_map.get('os_release_file', {})
|
|
|
|
os_release_mock = MagicMock(return_value=os_release_data)
|
2016-06-20 10:22:01 +00:00
|
|
|
|
|
|
|
orig_import = __import__
|
2016-07-07 22:41:21 +00:00
|
|
|
if six.PY2:
|
|
|
|
built_in = '__builtin__'
|
|
|
|
else:
|
|
|
|
built_in = 'builtins'
|
2016-06-20 10:22:01 +00:00
|
|
|
|
|
|
|
def _import_mock(name, *args):
|
|
|
|
if name == 'lsb_release':
|
|
|
|
raise ImportError('No module named lsb_release')
|
|
|
|
return orig_import(name, *args)
|
|
|
|
|
|
|
|
# Skip the first if statement
|
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
|
|
|
with patch.object(salt.utils.platform, 'is_proxy',
|
2016-06-20 10:22:01 +00:00
|
|
|
MagicMock(return_value=False)):
|
|
|
|
# Skip the selinux/systemd stuff (not pertinent)
|
|
|
|
with patch.object(core, '_linux_bin_exists',
|
|
|
|
MagicMock(return_value=False)):
|
|
|
|
# Skip the init grain compilation (not pertinent)
|
|
|
|
with patch.object(os.path, 'exists', path_isfile_mock):
|
|
|
|
# Ensure that lsb_release fails to import
|
2016-07-07 22:41:21 +00:00
|
|
|
with patch('{0}.__import__'.format(built_in),
|
2016-06-20 10:22:01 +00:00
|
|
|
side_effect=_import_mock):
|
|
|
|
# Skip all the /etc/*-release stuff (not pertinent)
|
|
|
|
with patch.object(os.path, 'isfile', path_isfile_mock):
|
|
|
|
with patch.object(core, '_parse_os_release', os_release_mock):
|
2017-03-14 14:35:32 +00:00
|
|
|
# Mock linux_distribution to give us the OS
|
|
|
|
# name that we want.
|
2016-06-20 10:22:01 +00:00
|
|
|
distro_mock = MagicMock(
|
2018-02-21 14:32:24 +00:00
|
|
|
return_value=os_release_map['linux_distribution']
|
2016-06-20 10:22:01 +00:00
|
|
|
)
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()) as suse_release_file:
|
2018-02-21 14:32:24 +00:00
|
|
|
suse_release_file.return_value.__iter__.return_value = \
|
|
|
|
os_release_map.get('suse_release_file', '').splitlines()
|
2017-03-14 14:35:32 +00:00
|
|
|
with patch.object(core, 'linux_distribution', distro_mock):
|
2016-06-20 10:22:01 +00:00
|
|
|
with patch.object(core, '_linux_gpu_data', empty_mock):
|
|
|
|
with patch.object(core, '_linux_cpudata', empty_mock):
|
|
|
|
with patch.object(core, '_virtual', empty_mock):
|
|
|
|
# Mock the osarch
|
|
|
|
with patch.dict(core.__salt__, {'cmd.run': osarch_mock}):
|
|
|
|
os_grains = core.os_data()
|
|
|
|
|
2018-02-21 17:56:48 +00:00
|
|
|
grains = {k: v for k, v in os_grains.items()
|
|
|
|
if k in set(["os", "os_family", "osfullname", "oscodename", "osfinger",
|
|
|
|
"osrelease", "osrelease_info", "osmajorrelease"])}
|
|
|
|
self.assertEqual(grains, expectation)
|
2016-06-20 10:22:01 +00:00
|
|
|
|
2018-02-21 17:56:48 +00:00
|
|
|
def _run_suse_os_grains_tests(self, os_release_map, expectation):
|
2018-02-21 14:32:24 +00:00
|
|
|
os_release_map['linux_distribution'] = ('SUSE test', 'version', 'arch')
|
2018-02-21 17:56:48 +00:00
|
|
|
expectation['os'] = 'SUSE'
|
|
|
|
expectation['os_family'] = 'Suse'
|
|
|
|
self._run_os_grains_tests(None, os_release_map, expectation)
|
2016-06-20 10:22:01 +00:00
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2016-06-20 10:22:01 +00:00
|
|
|
def test_suse_os_grains_sles11sp3(self):
|
|
|
|
'''
|
|
|
|
Test if OS grains are parsed correctly in SLES 11 SP3
|
|
|
|
'''
|
|
|
|
_os_release_map = {
|
2016-06-20 10:56:06 +00:00
|
|
|
'suse_release_file': '''SUSE Linux Enterprise Server 11 (x86_64)
|
|
|
|
VERSION = 11
|
|
|
|
PATCHLEVEL = 3
|
|
|
|
''',
|
2018-02-21 17:56:48 +00:00
|
|
|
'files': ["/etc/SuSE-release"],
|
|
|
|
}
|
|
|
|
expectation = {
|
2016-06-20 10:56:06 +00:00
|
|
|
'oscodename': 'SUSE Linux Enterprise Server 11 SP3',
|
2016-06-20 10:22:01 +00:00
|
|
|
'osfullname': "SLES",
|
2016-06-20 10:56:06 +00:00
|
|
|
'osrelease': '11.3',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (11, 3),
|
2017-04-11 17:36:36 +00:00
|
|
|
'osmajorrelease': 11,
|
2018-02-21 17:56:48 +00:00
|
|
|
'osfinger': 'SLES-11',
|
2016-06-20 10:22:01 +00:00
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_suse_os_grains_tests(_os_release_map, expectation)
|
2016-06-20 10:22:01 +00:00
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2016-06-20 10:22:01 +00:00
|
|
|
def test_suse_os_grains_sles11sp4(self):
|
|
|
|
'''
|
|
|
|
Test if OS grains are parsed correctly in SLES 11 SP4
|
|
|
|
'''
|
|
|
|
_os_release_map = {
|
|
|
|
'os_release_file': {
|
|
|
|
'NAME': 'SLES',
|
|
|
|
'VERSION': '11.4',
|
|
|
|
'VERSION_ID': '11.4',
|
|
|
|
'PRETTY_NAME': 'SUSE Linux Enterprise Server 11 SP4',
|
|
|
|
'ID': 'sles',
|
|
|
|
'ANSI_COLOR': '0;32',
|
|
|
|
'CPE_NAME': 'cpe:/o:suse:sles:11:4'
|
|
|
|
},
|
2018-02-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
expectation = {
|
2016-06-20 10:22:01 +00:00
|
|
|
'oscodename': 'SUSE Linux Enterprise Server 11 SP4',
|
|
|
|
'osfullname': "SLES",
|
|
|
|
'osrelease': '11.4',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (11, 4),
|
2017-04-11 17:36:36 +00:00
|
|
|
'osmajorrelease': 11,
|
2018-02-21 17:56:48 +00:00
|
|
|
'osfinger': 'SLES-11',
|
2016-06-20 10:22:01 +00:00
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_suse_os_grains_tests(_os_release_map, expectation)
|
2016-06-20 10:22:01 +00:00
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2016-06-20 10:22:01 +00:00
|
|
|
def test_suse_os_grains_sles12(self):
|
|
|
|
'''
|
|
|
|
Test if OS grains are parsed correctly in SLES 12
|
|
|
|
'''
|
|
|
|
_os_release_map = {
|
|
|
|
'os_release_file': {
|
|
|
|
'NAME': 'SLES',
|
|
|
|
'VERSION': '12',
|
|
|
|
'VERSION_ID': '12',
|
|
|
|
'PRETTY_NAME': 'SUSE Linux Enterprise Server 12',
|
|
|
|
'ID': 'sles',
|
|
|
|
'ANSI_COLOR': '0;32',
|
|
|
|
'CPE_NAME': 'cpe:/o:suse:sles:12'
|
|
|
|
},
|
2018-02-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
expectation = {
|
2016-06-20 10:22:01 +00:00
|
|
|
'oscodename': 'SUSE Linux Enterprise Server 12',
|
|
|
|
'osfullname': "SLES",
|
|
|
|
'osrelease': '12',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (12,),
|
2017-04-11 17:36:36 +00:00
|
|
|
'osmajorrelease': 12,
|
2018-02-21 17:56:48 +00:00
|
|
|
'osfinger': 'SLES-12',
|
2016-06-20 10:22:01 +00:00
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_suse_os_grains_tests(_os_release_map, expectation)
|
2016-06-20 10:22:01 +00:00
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2016-06-20 10:22:01 +00:00
|
|
|
def test_suse_os_grains_sles12sp1(self):
|
|
|
|
'''
|
|
|
|
Test if OS grains are parsed correctly in SLES 12 SP1
|
|
|
|
'''
|
|
|
|
_os_release_map = {
|
|
|
|
'os_release_file': {
|
|
|
|
'NAME': 'SLES',
|
|
|
|
'VERSION': '12-SP1',
|
|
|
|
'VERSION_ID': '12.1',
|
|
|
|
'PRETTY_NAME': 'SUSE Linux Enterprise Server 12 SP1',
|
|
|
|
'ID': 'sles',
|
|
|
|
'ANSI_COLOR': '0;32',
|
|
|
|
'CPE_NAME': 'cpe:/o:suse:sles:12:sp1'
|
|
|
|
},
|
2018-02-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
expectation = {
|
2016-06-20 10:22:01 +00:00
|
|
|
'oscodename': 'SUSE Linux Enterprise Server 12 SP1',
|
|
|
|
'osfullname': "SLES",
|
|
|
|
'osrelease': '12.1',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (12, 1),
|
2017-04-11 17:36:36 +00:00
|
|
|
'osmajorrelease': 12,
|
2018-02-21 17:56:48 +00:00
|
|
|
'osfinger': 'SLES-12',
|
2016-06-20 10:22:01 +00:00
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_suse_os_grains_tests(_os_release_map, expectation)
|
2016-06-20 10:22:01 +00:00
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2016-06-20 10:22:01 +00:00
|
|
|
def test_suse_os_grains_opensuse_leap_42_1(self):
|
|
|
|
'''
|
|
|
|
Test if OS grains are parsed correctly in openSUSE Leap 42.1
|
|
|
|
'''
|
|
|
|
_os_release_map = {
|
|
|
|
'os_release_file': {
|
|
|
|
'NAME': 'openSUSE Leap',
|
|
|
|
'VERSION': '42.1',
|
|
|
|
'VERSION_ID': '42.1',
|
|
|
|
'PRETTY_NAME': 'openSUSE Leap 42.1 (x86_64)',
|
|
|
|
'ID': 'opensuse',
|
|
|
|
'ANSI_COLOR': '0;32',
|
|
|
|
'CPE_NAME': 'cpe:/o:opensuse:opensuse:42.1'
|
|
|
|
},
|
2018-02-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
expectation = {
|
2016-06-20 10:22:01 +00:00
|
|
|
'oscodename': 'openSUSE Leap 42.1 (x86_64)',
|
|
|
|
'osfullname': "Leap",
|
|
|
|
'osrelease': '42.1',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (42, 1),
|
2017-04-11 17:36:36 +00:00
|
|
|
'osmajorrelease': 42,
|
2018-02-21 17:56:48 +00:00
|
|
|
'osfinger': 'Leap-42',
|
2016-06-20 10:22:01 +00:00
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_suse_os_grains_tests(_os_release_map, expectation)
|
2016-06-20 10:22:01 +00:00
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2016-06-20 10:22:01 +00:00
|
|
|
def test_suse_os_grains_tumbleweed(self):
|
|
|
|
'''
|
|
|
|
Test if OS grains are parsed correctly in openSUSE Tumbleweed
|
|
|
|
'''
|
|
|
|
_os_release_map = {
|
|
|
|
'os_release_file': {
|
|
|
|
'NAME': 'openSUSE',
|
|
|
|
'VERSION': 'Tumbleweed',
|
|
|
|
'VERSION_ID': '20160504',
|
|
|
|
'PRETTY_NAME': 'openSUSE Tumbleweed (20160504) (x86_64)',
|
|
|
|
'ID': 'opensuse',
|
|
|
|
'ANSI_COLOR': '0;32',
|
|
|
|
'CPE_NAME': 'cpe:/o:opensuse:opensuse:20160504'
|
|
|
|
},
|
2018-02-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
expectation = {
|
2016-06-20 11:04:10 +00:00
|
|
|
'oscodename': 'openSUSE Tumbleweed (20160504) (x86_64)',
|
2016-06-20 10:22:01 +00:00
|
|
|
'osfullname': "Tumbleweed",
|
|
|
|
'osrelease': '20160504',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (20160504,),
|
2017-04-11 17:36:36 +00:00
|
|
|
'osmajorrelease': 20160504,
|
2018-02-21 17:56:48 +00:00
|
|
|
'osfinger': 'Tumbleweed-20160504',
|
2016-06-20 10:22:01 +00:00
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_suse_os_grains_tests(_os_release_map, expectation)
|
2016-06-20 10:22:01 +00:00
|
|
|
|
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
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2018-02-21 15:04:12 +00:00
|
|
|
def test_debian_7_os_grains(self):
|
2016-08-05 09:55:02 +00:00
|
|
|
'''
|
2018-02-21 15:04:12 +00:00
|
|
|
Test if OS grains are parsed correctly in Debian 7 "wheezy"
|
2016-08-05 09:55:02 +00:00
|
|
|
'''
|
|
|
|
_os_release_map = {
|
2018-02-21 15:04:12 +00:00
|
|
|
'linux_distribution': ('debian', '7.11', ''),
|
2018-02-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
expectation = {
|
2018-02-21 15:04:12 +00:00
|
|
|
'os': 'Debian',
|
|
|
|
'os_family': 'Debian',
|
|
|
|
'oscodename': 'wheezy',
|
|
|
|
'osfullname': 'Debian GNU/Linux',
|
|
|
|
'osrelease': '7',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (7,),
|
2018-02-21 15:04:12 +00:00
|
|
|
'osmajorrelease': 7,
|
|
|
|
'osfinger': 'Debian-7',
|
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_os_grains_tests("debian-7", _os_release_map, expectation)
|
2018-02-21 15:04:12 +00:00
|
|
|
|
2018-02-27 22:20:45 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2018-02-21 15:04:12 +00:00
|
|
|
def test_debian_8_os_grains(self):
|
|
|
|
'''
|
|
|
|
Test if OS grains are parsed correctly in Debian 8 "jessie"
|
|
|
|
'''
|
|
|
|
_os_release_map = {
|
|
|
|
'linux_distribution': ('debian', '8.10', ''),
|
2018-02-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
expectation = {
|
2018-02-21 15:04:12 +00:00
|
|
|
'os': 'Debian',
|
|
|
|
'os_family': 'Debian',
|
|
|
|
'oscodename': 'jessie',
|
|
|
|
'osfullname': 'Debian GNU/Linux',
|
|
|
|
'osrelease': '8',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (8,),
|
2018-02-21 15:04:12 +00:00
|
|
|
'osmajorrelease': 8,
|
|
|
|
'osfinger': 'Debian-8',
|
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_os_grains_tests("debian-8", _os_release_map, expectation)
|
2018-02-21 15:04:12 +00:00
|
|
|
|
2018-02-27 22:20:45 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2018-02-21 15:04:12 +00:00
|
|
|
def test_debian_9_os_grains(self):
|
|
|
|
'''
|
|
|
|
Test if OS grains are parsed correctly in Debian 9 "stretch"
|
|
|
|
'''
|
|
|
|
_os_release_map = {
|
|
|
|
'linux_distribution': ('debian', '9.3', ''),
|
2018-02-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
expectation = {
|
2018-02-21 15:04:12 +00:00
|
|
|
'os': 'Debian',
|
|
|
|
'os_family': 'Debian',
|
|
|
|
'oscodename': 'stretch',
|
|
|
|
'osfullname': 'Debian GNU/Linux',
|
|
|
|
'osrelease': '9',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (9,),
|
2018-02-21 15:04:12 +00:00
|
|
|
'osmajorrelease': 9,
|
|
|
|
'osfinger': 'Debian-9',
|
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_os_grains_tests("debian-9", _os_release_map, expectation)
|
2018-02-21 15:04:12 +00:00
|
|
|
|
2018-02-27 22:20:45 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2018-02-21 14:15:44 +00:00
|
|
|
def test_ubuntu_xenial_os_grains(self):
|
2016-08-05 09:55:02 +00:00
|
|
|
'''
|
2018-02-21 14:15:44 +00:00
|
|
|
Test if OS grains are parsed correctly in Ubuntu 16.04 "Xenial Xerus"
|
2016-08-05 09:55:02 +00:00
|
|
|
'''
|
|
|
|
_os_release_map = {
|
2018-02-22 13:20:50 +00:00
|
|
|
'linux_distribution': ('Ubuntu', '16.04', 'xenial'),
|
2018-02-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
expectation = {
|
2018-02-22 13:20:50 +00:00
|
|
|
'os': 'Ubuntu',
|
|
|
|
'os_family': 'Debian',
|
2016-08-05 09:55:02 +00:00
|
|
|
'oscodename': 'xenial',
|
|
|
|
'osfullname': 'Ubuntu',
|
|
|
|
'osrelease': '16.04',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (16, 4),
|
2017-04-11 17:36:36 +00:00
|
|
|
'osmajorrelease': 16,
|
2016-08-05 09:55:02 +00:00
|
|
|
'osfinger': 'Ubuntu-16.04',
|
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_os_grains_tests("ubuntu-16.04", _os_release_map, expectation)
|
2016-08-05 09:55:02 +00:00
|
|
|
|
2018-02-27 22:13:28 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2018-02-21 14:15:44 +00:00
|
|
|
def test_ubuntu_artful_os_grains(self):
|
|
|
|
'''
|
|
|
|
Test if OS grains are parsed correctly in Ubuntu 17.10 "Artful Aardvark"
|
|
|
|
'''
|
|
|
|
_os_release_map = {
|
|
|
|
'linux_distribution': ('Ubuntu', '17.10', 'artful'),
|
2018-02-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
expectation = {
|
2018-02-21 14:15:44 +00:00
|
|
|
'os': 'Ubuntu',
|
|
|
|
'os_family': 'Debian',
|
|
|
|
'oscodename': 'artful',
|
|
|
|
'osfullname': 'Ubuntu',
|
|
|
|
'osrelease': '17.10',
|
2018-02-21 17:56:48 +00:00
|
|
|
'osrelease_info': (17, 10),
|
2018-02-21 14:15:44 +00:00
|
|
|
'osmajorrelease': 17,
|
|
|
|
'osfinger': 'Ubuntu-17.10',
|
|
|
|
}
|
2018-02-21 17:56:48 +00:00
|
|
|
self._run_os_grains_tests("ubuntu-17.10", _os_release_map, expectation)
|
2017-09-28 23:53:19 +00:00
|
|
|
|
2017-10-18 19:01:12 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
|
|
|
def test_linux_memdata(self):
|
|
|
|
'''
|
|
|
|
Test memdata on Linux systems
|
|
|
|
'''
|
|
|
|
_path_exists_map = {
|
|
|
|
'/proc/1/cmdline': False,
|
|
|
|
'/proc/meminfo': True
|
|
|
|
}
|
|
|
|
_path_isfile_map = {
|
|
|
|
'/proc/meminfo': True
|
|
|
|
}
|
|
|
|
_cmd_run_map = {
|
2017-11-28 03:37:46 +00:00
|
|
|
'dpkg --print-architecture': 'amd64',
|
|
|
|
'rpm --eval %{_host_cpu}': 'x86_64'
|
2017-10-18 19:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
path_exists_mock = MagicMock(side_effect=lambda x: _path_exists_map[x])
|
|
|
|
path_isfile_mock = MagicMock(
|
|
|
|
side_effect=lambda x: _path_isfile_map.get(x, False)
|
|
|
|
)
|
|
|
|
cmd_run_mock = MagicMock(
|
|
|
|
side_effect=lambda x: _cmd_run_map[x]
|
|
|
|
)
|
|
|
|
empty_mock = MagicMock(return_value={})
|
|
|
|
|
|
|
|
_proc_meminfo_file = '''MemTotal: 16277028 kB
|
|
|
|
SwapTotal: 4789244 kB'''
|
|
|
|
|
|
|
|
orig_import = __import__
|
|
|
|
if six.PY2:
|
|
|
|
built_in = '__builtin__'
|
|
|
|
else:
|
|
|
|
built_in = 'builtins'
|
|
|
|
|
|
|
|
def _import_mock(name, *args):
|
|
|
|
if name == 'lsb_release':
|
|
|
|
raise ImportError('No module named lsb_release')
|
|
|
|
return orig_import(name, *args)
|
|
|
|
|
|
|
|
# Skip the first if statement
|
|
|
|
with patch.object(salt.utils.platform, 'is_proxy',
|
|
|
|
MagicMock(return_value=False)):
|
|
|
|
# Skip the selinux/systemd stuff (not pertinent)
|
|
|
|
with patch.object(core, '_linux_bin_exists',
|
|
|
|
MagicMock(return_value=False)):
|
|
|
|
# Skip the init grain compilation (not pertinent)
|
|
|
|
with patch.object(os.path, 'exists', path_exists_mock):
|
|
|
|
# Ensure that lsb_release fails to import
|
|
|
|
with patch('{0}.__import__'.format(built_in),
|
|
|
|
side_effect=_import_mock):
|
|
|
|
# Skip all the /etc/*-release stuff (not pertinent)
|
|
|
|
with patch.object(os.path, 'isfile', path_isfile_mock):
|
|
|
|
# Make a bunch of functions return empty dicts,
|
|
|
|
# we don't care about these grains for the
|
|
|
|
# purposes of this test.
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_linux_cpudata',
|
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_linux_gpu_data',
|
|
|
|
empty_mock):
|
|
|
|
with patch('salt.utils.files.fopen', mock_open()) as _proc_meminfo:
|
|
|
|
_proc_meminfo.return_value.__iter__.return_value = _proc_meminfo_file.splitlines()
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_hw_data',
|
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_virtual',
|
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_ps',
|
|
|
|
empty_mock):
|
|
|
|
# Mock the osarch
|
|
|
|
with patch.dict(
|
|
|
|
core.__salt__,
|
|
|
|
{'cmd.run': cmd_run_mock}):
|
|
|
|
os_grains = core.os_data()
|
|
|
|
|
|
|
|
self.assertEqual(os_grains.get('mem_total'), 15895)
|
|
|
|
self.assertEqual(os_grains.get('swap_total'), 4676)
|
|
|
|
|
2018-01-08 22:38:26 +00:00
|
|
|
@skipIf(salt.utils.platform.is_windows(), 'System is Windows')
|
2017-10-18 19:01:12 +00:00
|
|
|
def test_bsd_memdata(self):
|
|
|
|
'''
|
|
|
|
Test to memdata on *BSD systems
|
|
|
|
'''
|
|
|
|
_path_exists_map = {}
|
|
|
|
_path_isfile_map = {}
|
|
|
|
_cmd_run_map = {
|
|
|
|
'freebsd-version -u': '10.3-RELEASE',
|
|
|
|
'/sbin/sysctl -n hw.physmem': '2121781248',
|
|
|
|
'/sbin/sysctl -n vm.swap_total': '419430400'
|
|
|
|
}
|
|
|
|
|
|
|
|
path_exists_mock = MagicMock(side_effect=lambda x: _path_exists_map[x])
|
|
|
|
path_isfile_mock = MagicMock(
|
|
|
|
side_effect=lambda x: _path_isfile_map.get(x, False)
|
|
|
|
)
|
|
|
|
cmd_run_mock = MagicMock(
|
|
|
|
side_effect=lambda x: _cmd_run_map[x]
|
|
|
|
)
|
|
|
|
empty_mock = MagicMock(return_value={})
|
|
|
|
|
|
|
|
mock_freebsd_uname = ('FreeBSD',
|
|
|
|
'freebsd10.3-hostname-8148',
|
|
|
|
'10.3-RELEASE',
|
|
|
|
'FreeBSD 10.3-RELEASE #0 r297264: Fri Mar 25 02:10:02 UTC 2016 root@releng1.nyi.freebsd.org:/usr/obj/usr/src/sys/GENERIC',
|
|
|
|
'amd64',
|
|
|
|
'amd64')
|
|
|
|
|
|
|
|
with patch('platform.uname',
|
|
|
|
MagicMock(return_value=mock_freebsd_uname)):
|
|
|
|
with patch.object(salt.utils.platform, 'is_linux',
|
|
|
|
MagicMock(return_value=False)):
|
|
|
|
with patch.object(salt.utils.platform, 'is_freebsd',
|
|
|
|
MagicMock(return_value=True)):
|
|
|
|
# Skip the first if statement
|
|
|
|
with patch.object(salt.utils.platform, 'is_proxy',
|
|
|
|
MagicMock(return_value=False)):
|
|
|
|
# Skip the init grain compilation (not pertinent)
|
|
|
|
with patch.object(os.path, 'exists', path_exists_mock):
|
|
|
|
with patch('salt.utils.path.which') as mock:
|
|
|
|
mock.return_value = '/sbin/sysctl'
|
|
|
|
# Make a bunch of functions return empty dicts,
|
|
|
|
# we don't care about these grains for the
|
|
|
|
# purposes of this test.
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_bsd_cpudata',
|
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
|
|
|
'_hw_data',
|
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
2017-11-03 20:46:01 +00:00
|
|
|
'_virtual',
|
2017-10-18 19:01:12 +00:00
|
|
|
empty_mock):
|
|
|
|
with patch.object(
|
|
|
|
core,
|
2017-11-03 20:46:01 +00:00
|
|
|
'_ps',
|
2017-10-18 19:01:12 +00:00
|
|
|
empty_mock):
|
2017-11-03 20:46:01 +00:00
|
|
|
# Mock the osarch
|
|
|
|
with patch.dict(
|
|
|
|
core.__salt__,
|
|
|
|
{'cmd.run': cmd_run_mock}):
|
|
|
|
os_grains = core.os_data()
|
2017-10-18 19:01:12 +00:00
|
|
|
|
|
|
|
self.assertEqual(os_grains.get('mem_total'), 2023)
|
2017-11-27 21:15:44 +00:00
|
|
|
self.assertEqual(os_grains.get('swap_total'), 400)
|
2017-12-11 23:24:41 +00:00
|
|
|
|
2018-01-08 22:38:26 +00:00
|
|
|
@skipIf(salt.utils.platform.is_windows(), 'System is Windows')
|
2017-11-25 20:32:43 +00:00
|
|
|
def test_docker_virtual(self):
|
|
|
|
'''
|
|
|
|
Test if OS grains are parsed correctly in Ubuntu Xenial Xerus
|
|
|
|
'''
|
2017-12-07 15:25:26 +00:00
|
|
|
with patch.object(os.path, 'isdir', MagicMock(return_value=False)):
|
|
|
|
with patch.object(os.path,
|
|
|
|
'isfile',
|
|
|
|
MagicMock(side_effect=lambda x: True if x == '/proc/1/cgroup' else False)):
|
|
|
|
for cgroup_substr in (':/system.slice/docker', ':/docker/',
|
|
|
|
':/docker-ce/'):
|
|
|
|
cgroup_data = \
|
|
|
|
'10:memory{0}a_long_sha256sum'.format(cgroup_substr)
|
|
|
|
log.debug(
|
|
|
|
'Testing Docker cgroup substring \'%s\'', cgroup_substr)
|
2017-12-24 01:47:37 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open(read_data=cgroup_data)):
|
2018-01-18 17:48:07 +00:00
|
|
|
with patch.dict(core.__salt__, {'cmd.run_all': MagicMock()}):
|
|
|
|
self.assertEqual(
|
|
|
|
core._virtual({'kernel': 'Linux'}).get('virtual_subtype'),
|
|
|
|
'Docker'
|
|
|
|
)
|
2017-11-25 20:32:43 +00:00
|
|
|
|
2017-05-17 18:53:39 +00:00
|
|
|
def _check_ipaddress(self, value, ip_v):
|
|
|
|
'''
|
|
|
|
check if ip address in a list is valid
|
|
|
|
'''
|
|
|
|
for val in value:
|
|
|
|
assert isinstance(val, six.string_types)
|
|
|
|
ip_method = 'is_ipv{0}'.format(ip_v)
|
|
|
|
self.assertTrue(getattr(salt.utils.network, ip_method)(val))
|
|
|
|
|
|
|
|
def _check_empty(self, key, value, empty):
|
|
|
|
'''
|
|
|
|
if empty is False and value does not exist assert error
|
|
|
|
if empty is True and value exists assert error
|
|
|
|
'''
|
|
|
|
if not empty and not value:
|
|
|
|
raise Exception("{0} is empty, expecting a value".format(key))
|
|
|
|
elif empty and value:
|
|
|
|
raise Exception("{0} is suppose to be empty. value: {1} \
|
|
|
|
exists".format(key, value))
|
|
|
|
|
2017-12-11 23:43:33 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2017-05-16 15:48:04 +00:00
|
|
|
def test_fqdn_return(self):
|
|
|
|
'''
|
|
|
|
test ip4 and ip6 return values
|
|
|
|
'''
|
2017-05-17 18:53:39 +00:00
|
|
|
net_ip4_mock = [IP4_LOCAL, IP4_ADD1, IP4_ADD2]
|
|
|
|
net_ip6_mock = [IP6_LOCAL, IP6_ADD1, IP6_ADD2]
|
|
|
|
|
|
|
|
self._run_fqdn_tests(net_ip4_mock, net_ip6_mock,
|
|
|
|
ip4_empty=False, ip6_empty=False)
|
2017-05-16 15:48:04 +00:00
|
|
|
|
2017-12-11 23:43:33 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2017-05-17 18:53:39 +00:00
|
|
|
def test_fqdn6_empty(self):
|
|
|
|
'''
|
|
|
|
test when ip6 is empty
|
|
|
|
'''
|
2017-05-16 15:48:04 +00:00
|
|
|
net_ip4_mock = [IP4_LOCAL, IP4_ADD1, IP4_ADD2]
|
2017-05-17 18:53:39 +00:00
|
|
|
net_ip6_mock = []
|
|
|
|
|
|
|
|
self._run_fqdn_tests(net_ip4_mock, net_ip6_mock,
|
|
|
|
ip4_empty=False)
|
|
|
|
|
2017-12-11 23:43:33 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2017-05-17 18:53:39 +00:00
|
|
|
def test_fqdn4_empty(self):
|
|
|
|
'''
|
|
|
|
test when ip4 is empty
|
|
|
|
'''
|
|
|
|
net_ip4_mock = []
|
2017-05-16 15:48:04 +00:00
|
|
|
net_ip6_mock = [IP6_LOCAL, IP6_ADD1, IP6_ADD2]
|
|
|
|
|
2017-05-17 18:53:39 +00:00
|
|
|
self._run_fqdn_tests(net_ip4_mock, net_ip6_mock,
|
|
|
|
ip6_empty=False)
|
|
|
|
|
2017-12-11 23:43:33 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2017-05-17 18:53:39 +00:00
|
|
|
def test_fqdn_all_empty(self):
|
|
|
|
'''
|
|
|
|
test when both ip4 and ip6 are empty
|
|
|
|
'''
|
|
|
|
net_ip4_mock = []
|
|
|
|
net_ip6_mock = []
|
|
|
|
|
|
|
|
self._run_fqdn_tests(net_ip4_mock, net_ip6_mock)
|
|
|
|
|
|
|
|
def _run_fqdn_tests(self, net_ip4_mock, net_ip6_mock,
|
|
|
|
ip6_empty=True, ip4_empty=True):
|
2017-05-16 15:48:04 +00:00
|
|
|
|
2017-05-17 18:53:39 +00:00
|
|
|
def _check_type(key, value, ip4_empty, ip6_empty):
|
|
|
|
'''
|
|
|
|
check type and other checks
|
|
|
|
'''
|
|
|
|
assert isinstance(value, list)
|
2017-05-16 15:48:04 +00:00
|
|
|
|
2017-05-17 18:53:39 +00:00
|
|
|
if '4' in key:
|
|
|
|
self._check_empty(key, value, ip4_empty)
|
|
|
|
self._check_ipaddress(value, ip_v='4')
|
|
|
|
elif '6' in key:
|
|
|
|
self._check_empty(key, value, ip6_empty)
|
|
|
|
self._check_ipaddress(value, ip_v='6')
|
2017-05-16 15:48:04 +00:00
|
|
|
|
2017-11-14 19:39:15 +00:00
|
|
|
ip4_mock = [(2, 1, 6, '', (IP4_ADD1, 0)),
|
|
|
|
(2, 3, 0, '', (IP4_ADD2, 0))]
|
|
|
|
ip6_mock = [(10, 1, 6, '', (IP6_ADD1, 0, 0, 0)),
|
|
|
|
(10, 3, 0, '', (IP6_ADD2, 0, 0, 0))]
|
|
|
|
|
2017-05-16 16:00:16 +00:00
|
|
|
with patch.dict(core.__opts__, {'ipv6': False}):
|
|
|
|
with patch.object(salt.utils.network, 'ip_addrs',
|
|
|
|
MagicMock(return_value=net_ip4_mock)):
|
|
|
|
with patch.object(salt.utils.network, 'ip_addrs6',
|
|
|
|
MagicMock(return_value=net_ip6_mock)):
|
2017-11-14 19:39:15 +00:00
|
|
|
with patch.object(core.socket, 'getaddrinfo', side_effect=[ip4_mock, ip6_mock]):
|
|
|
|
get_fqdn = core.ip_fqdn()
|
|
|
|
ret_keys = ['fqdn_ip4', 'fqdn_ip6', 'ipv4', 'ipv6']
|
|
|
|
for key in ret_keys:
|
|
|
|
value = get_fqdn[key]
|
|
|
|
_check_type(key, value, ip4_empty, ip6_empty)
|
2017-05-16 15:48:04 +00:00
|
|
|
|
2017-12-11 23:43:33 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2018-02-20 17:36:27 +00:00
|
|
|
@patch.object(salt.utils.platform, 'is_windows', MagicMock(return_value=False))
|
2018-02-20 14:25:54 +00:00
|
|
|
@patch('salt.grains.core.__opts__', {'ipv6': False})
|
2017-05-16 15:48:04 +00:00
|
|
|
def test_dns_return(self):
|
|
|
|
'''
|
|
|
|
test the return for a dns grain. test for issue:
|
|
|
|
https://github.com/saltstack/salt/issues/41230
|
|
|
|
'''
|
|
|
|
resolv_mock = {'domain': '', 'sortlist': [], 'nameservers':
|
2018-01-11 14:40:27 +00:00
|
|
|
[ipaddress.IPv4Address(IP4_ADD1),
|
2018-02-19 12:14:45 +00:00
|
|
|
ipaddress.IPv6Address(IP6_ADD1),
|
|
|
|
IP6_ADD_SCOPE], 'ip4_nameservers':
|
2018-01-11 14:40:27 +00:00
|
|
|
[ipaddress.IPv4Address(IP4_ADD1)],
|
2017-05-16 15:48:04 +00:00
|
|
|
'search': ['test.saltstack.com'], 'ip6_nameservers':
|
2018-02-19 12:14:45 +00:00
|
|
|
[ipaddress.IPv6Address(IP6_ADD1),
|
|
|
|
IP6_ADD_SCOPE], 'options': []}
|
2017-05-16 15:48:04 +00:00
|
|
|
ret = {'dns': {'domain': '', 'sortlist': [], 'nameservers':
|
2018-02-19 12:14:45 +00:00
|
|
|
[IP4_ADD1, IP6_ADD1,
|
|
|
|
IP6_ADD_SCOPE], 'ip4_nameservers':
|
2017-05-16 15:48:04 +00:00
|
|
|
[IP4_ADD1], 'search': ['test.saltstack.com'],
|
2018-02-19 12:14:45 +00:00
|
|
|
'ip6_nameservers': [IP6_ADD1, IP6_ADD_SCOPE],
|
|
|
|
'options': []}}
|
2018-02-20 14:25:54 +00:00
|
|
|
with patch.object(salt.utils.dns, 'parse_resolv', MagicMock(return_value=resolv_mock)):
|
|
|
|
assert core.dns() == ret
|
2017-05-16 15:48:04 +00:00
|
|
|
|
|
|
|
def _run_dns_test(self, resolv_mock, ret):
|
|
|
|
with patch.object(salt.utils, 'is_windows',
|
|
|
|
MagicMock(return_value=False)):
|
|
|
|
with patch.dict(core.__opts__, {'ipv6': False}):
|
|
|
|
with patch.object(salt.utils.dns, 'parse_resolv',
|
|
|
|
MagicMock(return_value=resolv_mock)):
|
|
|
|
get_dns = core.dns()
|
|
|
|
self.assertEqual(get_dns, ret)
|
2017-12-21 17:33:02 +00:00
|
|
|
|
|
|
|
@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
|
2018-04-03 10:08:00 +00:00
|
|
|
@patch.object(salt.utils, 'is_windows', MagicMock(return_value=False))
|
|
|
|
@patch('salt.utils.network.ip_addrs', MagicMock(return_value=['1.2.3.4', '5.6.7.8']))
|
|
|
|
@patch('salt.utils.network.ip_addrs6',
|
|
|
|
MagicMock(return_value=['fe80::a8b2:93ff:fe00:0', 'fe80::a8b2:93ff:dead:beef']))
|
|
|
|
@patch('salt.utils.network.socket.getfqdn', MagicMock(side_effect=lambda v: v)) # Just pass-through
|
2017-12-21 17:33:02 +00:00
|
|
|
def test_fqdns_return(self):
|
|
|
|
'''
|
|
|
|
test the return for a dns grain. test for issue:
|
|
|
|
https://github.com/saltstack/salt/issues/41230
|
|
|
|
'''
|
2017-12-22 09:47:09 +00:00
|
|
|
reverse_resolv_mock = [('foo.bar.baz', [], ['1.2.3.4']),
|
2018-04-03 10:08:00 +00:00
|
|
|
('rinzler.evil-corp.com', [], ['5.6.7.8']),
|
|
|
|
('foo.bar.baz', [], ['fe80::a8b2:93ff:fe00:0']),
|
|
|
|
('bluesniff.foo.bar', [], ['fe80::a8b2:93ff:dead:beef'])]
|
2018-03-14 16:20:23 +00:00
|
|
|
ret = {'fqdns': ['bluesniff.foo.bar', 'foo.bar.baz', 'rinzler.evil-corp.com']}
|
2018-04-03 10:08:00 +00:00
|
|
|
with patch.object(socket, 'gethostbyaddr', side_effect=reverse_resolv_mock):
|
|
|
|
fqdns = core.fqdns()
|
2018-04-11 08:46:00 +00:00
|
|
|
self.assertIn('fqdns', fqdns)
|
|
|
|
self.assertEqual(len(fqdns['fqdns']), len(ret['fqdns']))
|
|
|
|
self.assertEqual(set(fqdns['fqdns']), set(ret['fqdns']))
|