2016-11-28 06:29:42 +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
|
2016-11-28 06:29:42 +00:00
|
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
import os
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-03-21 17:57:27 +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 (
|
2017-05-09 22:33:18 +00:00
|
|
|
MagicMock,
|
2016-11-28 06:29:42 +00:00
|
|
|
NO_MOCK,
|
2017-05-09 22:33:18 +00:00
|
|
|
NO_MOCK_REASON,
|
2017-05-15 16:13:38 +00:00
|
|
|
patch,
|
|
|
|
mock_open
|
2016-11-28 06:29:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:57:27 +00:00
|
|
|
from salt.exceptions import CommandExecutionError, SaltInvocationError
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.timezone as timezone
|
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
|
|
|
|
import salt.utils.platform
|
|
|
|
import salt.utils.stringutils
|
2016-11-28 06:29:42 +00:00
|
|
|
|
|
|
|
GET_ZONE_FILE = 'salt.modules.timezone._get_zone_file'
|
|
|
|
GET_ETC_LOCALTIME_PATH = 'salt.modules.timezone._get_etc_localtime_path'
|
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-03-21 17:57:27 +00:00
|
|
|
class TimezoneTestCase(TestCase, LoaderModuleMockMixin):
|
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {timezone: {'__grains__': {'os_family': 'Ubuntu'}}}
|
2016-11-28 06:29:42 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.tempfiles = []
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
for tempfile in self.tempfiles:
|
|
|
|
try:
|
|
|
|
os.remove(tempfile.name)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2017-03-15 19:23:47 +00:00
|
|
|
del self.tempfiles
|
2016-11-28 06:29:42 +00:00
|
|
|
|
|
|
|
def test_zone_compare_equal(self):
|
|
|
|
etc_localtime = self.create_tempfile_with_contents('a')
|
|
|
|
zone_path = self.create_tempfile_with_contents('a')
|
|
|
|
|
|
|
|
with patch(GET_ZONE_FILE, lambda p: zone_path.name):
|
|
|
|
with patch(GET_ETC_LOCALTIME_PATH, lambda: etc_localtime.name):
|
|
|
|
|
|
|
|
self.assertTrue(timezone.zone_compare('foo'))
|
|
|
|
|
|
|
|
def test_zone_compare_nonexistent(self):
|
|
|
|
etc_localtime = self.create_tempfile_with_contents('a')
|
|
|
|
|
|
|
|
with patch(GET_ZONE_FILE, lambda p: '/foopath/nonexistent'):
|
|
|
|
with patch(GET_ETC_LOCALTIME_PATH, lambda: etc_localtime.name):
|
|
|
|
|
|
|
|
self.assertRaises(SaltInvocationError, timezone.zone_compare, 'foo')
|
|
|
|
|
|
|
|
def test_zone_compare_unequal(self):
|
|
|
|
etc_localtime = self.create_tempfile_with_contents('a')
|
|
|
|
zone_path = self.create_tempfile_with_contents('b')
|
|
|
|
|
|
|
|
with patch(GET_ZONE_FILE, lambda p: zone_path.name):
|
|
|
|
with patch(GET_ETC_LOCALTIME_PATH, lambda: etc_localtime.name):
|
|
|
|
|
|
|
|
self.assertFalse(timezone.zone_compare('foo'))
|
|
|
|
|
|
|
|
def test_missing_localtime(self):
|
|
|
|
with patch(GET_ZONE_FILE, lambda p: '/nonexisting'):
|
|
|
|
with patch(GET_ETC_LOCALTIME_PATH, lambda: '/also-missing'):
|
|
|
|
self.assertRaises(CommandExecutionError, timezone.zone_compare, 'foo')
|
|
|
|
|
|
|
|
def create_tempfile_with_contents(self, contents):
|
|
|
|
temp = NamedTemporaryFile(delete=False)
|
2017-01-27 19:15:39 +00:00
|
|
|
if six.PY3:
|
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
|
|
|
temp.write(salt.utils.stringutils.to_bytes(contents))
|
2017-01-27 19:15:39 +00:00
|
|
|
else:
|
|
|
|
temp.write(contents)
|
2016-11-28 06:29:42 +00:00
|
|
|
temp.close()
|
|
|
|
self.tempfiles.append(temp)
|
|
|
|
return temp
|
2017-05-09 22:33:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-05-10 19:49:43 +00:00
|
|
|
class TimezoneModuleTestCase(TestCase, LoaderModuleMockMixin):
|
2017-05-09 22:33:18 +00:00
|
|
|
'''
|
|
|
|
Timezone test case
|
|
|
|
'''
|
|
|
|
TEST_TZ = 'UTC'
|
|
|
|
|
2017-05-10 19:49:43 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {timezone: {'__grains__': {'os': ''},
|
|
|
|
'__salt__': {'file.sed': MagicMock(),
|
|
|
|
'cmd.run': MagicMock(),
|
|
|
|
'cmd.retcode': MagicMock(return_value=0)}}}
|
2017-05-09 22:33:18 +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
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
def test_get_zone_centos(self):
|
|
|
|
'''
|
|
|
|
Test CentOS is recognized
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os': 'centos'}):
|
|
|
|
with patch('salt.modules.timezone._get_zone_etc_localtime', MagicMock(return_value=self.TEST_TZ)):
|
|
|
|
assert timezone.get_zone() == self.TEST_TZ
|
2017-05-09 22:33:18 +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
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
def test_get_zone_os_family_rh_suse(self):
|
|
|
|
'''
|
|
|
|
Test RedHat and Suse are recognized
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
for osfamily in ['RedHat', 'Suse']:
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': [osfamily]}):
|
|
|
|
with patch('salt.modules.timezone._get_zone_sysconfig', MagicMock(return_value=self.TEST_TZ)):
|
|
|
|
assert timezone.get_zone() == self.TEST_TZ
|
2017-05-09 22:33:18 +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
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
def test_get_zone_os_family_debian_gentoo(self):
|
|
|
|
'''
|
|
|
|
Test Debian and Gentoo are recognized
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
for osfamily in ['Debian', 'Gentoo']:
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': [osfamily]}):
|
|
|
|
with patch('salt.modules.timezone._get_zone_etc_timezone', MagicMock(return_value=self.TEST_TZ)):
|
|
|
|
assert timezone.get_zone() == self.TEST_TZ
|
2017-05-09 22:33:18 +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
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
def test_get_zone_os_family_allbsd_nilinuxrt(self):
|
|
|
|
'''
|
|
|
|
Test *BSD and NILinuxRT are recognized
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
for osfamily in ['FreeBSD', 'OpenBSD', 'NetBSD', 'NILinuxRT']:
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': osfamily}):
|
|
|
|
with patch('salt.modules.timezone._get_zone_etc_localtime', MagicMock(return_value=self.TEST_TZ)):
|
|
|
|
assert timezone.get_zone() == self.TEST_TZ
|
2017-05-09 22:33:18 +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
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
def test_get_zone_os_family_slowlaris(self):
|
|
|
|
'''
|
|
|
|
Test Slowlaris is recognized
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Solaris']}):
|
|
|
|
with patch('salt.modules.timezone._get_zone_solaris', MagicMock(return_value=self.TEST_TZ)):
|
|
|
|
assert timezone.get_zone() == self.TEST_TZ
|
2017-05-09 22:33:18 +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
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
def test_get_zone_os_family_aix(self):
|
|
|
|
'''
|
|
|
|
Test IBM AIX is recognized
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['AIX']}):
|
|
|
|
with patch('salt.modules.timezone._get_zone_aix', MagicMock(return_value=self.TEST_TZ)):
|
|
|
|
assert timezone.get_zone() == self.TEST_TZ
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
def test_set_zone_redhat(self):
|
|
|
|
'''
|
|
|
|
Test zone set on RH series
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['RedHat']}):
|
|
|
|
assert timezone.set_zone(self.TEST_TZ)
|
|
|
|
name, args, kwargs = timezone.__salt__['file.sed'].mock_calls[0]
|
|
|
|
assert args == ('/etc/sysconfig/clock', '^ZONE=.*', 'ZONE="UTC"')
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
def test_set_zone_suse(self):
|
|
|
|
'''
|
|
|
|
Test zone set on SUSE series
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Suse']}):
|
|
|
|
assert timezone.set_zone(self.TEST_TZ)
|
|
|
|
name, args, kwargs = timezone.__salt__['file.sed'].mock_calls[0]
|
|
|
|
assert args == ('/etc/sysconfig/clock', '^TIMEZONE=.*', 'TIMEZONE="UTC"')
|
2017-05-09 22:33:18 +00:00
|
|
|
|
2017-08-29 20:24:47 +00:00
|
|
|
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
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
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
def test_set_zone_gentoo(self):
|
|
|
|
'''
|
|
|
|
Test zone set on Gentoo series
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Gentoo']}):
|
2018-06-18 04:55:48 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()) as m_open:
|
2017-05-10 16:32:24 +00:00
|
|
|
assert timezone.set_zone(self.TEST_TZ)
|
2018-06-18 04:55:48 +00:00
|
|
|
fh_ = m_open.filehandles['/etc/timezone'][0]
|
|
|
|
assert fh_.call_args == ('/etc/timezone', 'w'), fh_.call_args
|
|
|
|
assert fh_.write_calls == ['UTC', '\n'], fh_.write_calls
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
def test_set_zone_debian(self):
|
|
|
|
'''
|
|
|
|
Test zone set on Debian series
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Debian']}):
|
2018-06-18 04:55:48 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()) as m_open:
|
2017-05-10 16:32:24 +00:00
|
|
|
assert timezone.set_zone(self.TEST_TZ)
|
2018-06-18 04:55:48 +00:00
|
|
|
fh_ = m_open.filehandles['/etc/timezone'][0]
|
|
|
|
assert fh_.call_args == ('/etc/timezone', 'w'), fh_.call_args
|
|
|
|
assert fh_.write_calls == ['UTC', '\n'], fh_.write_calls
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=True))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
def test_get_hwclock_timedate_utc(self):
|
|
|
|
'''
|
|
|
|
Test get hwclock UTC/localtime
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
with patch('salt.modules.timezone._timedatectl', MagicMock(return_value={'stdout': 'rtc in local tz'})):
|
|
|
|
assert timezone.get_hwclock() == 'UTC'
|
|
|
|
with patch('salt.modules.timezone._timedatectl', MagicMock(return_value={'stdout': 'rtc in local tz:yes'})):
|
|
|
|
assert timezone.get_hwclock() == 'localtime'
|
|
|
|
|
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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
def test_get_hwclock_suse(self):
|
|
|
|
'''
|
|
|
|
Test get hwclock on SUSE
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Suse']}):
|
|
|
|
timezone.get_hwclock()
|
|
|
|
name, args, kwarg = timezone.__salt__['cmd.run'].mock_calls[0]
|
|
|
|
assert args == (['tail', '-n', '1', '/etc/adjtime'],)
|
|
|
|
assert kwarg == {'python_shell': False}
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
def test_get_hwclock_redhat(self):
|
|
|
|
'''
|
|
|
|
Test get hwclock on RedHat
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['RedHat']}):
|
|
|
|
timezone.get_hwclock()
|
|
|
|
name, args, kwarg = timezone.__salt__['cmd.run'].mock_calls[0]
|
|
|
|
assert args == (['tail', '-n', '1', '/etc/adjtime'],)
|
|
|
|
assert kwarg == {'python_shell': False}
|
2017-05-09 22:33:18 +00:00
|
|
|
|
|
|
|
def _test_get_hwclock_debian(self): # TODO: Enable this when testing environment is working properly
|
|
|
|
'''
|
|
|
|
Test get hwclock on Debian
|
|
|
|
:return:
|
|
|
|
'''
|
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('salt.utils.path.which', MagicMock(return_value=False)):
|
2017-05-09 22:33:18 +00:00
|
|
|
with patch('os.path.exists', MagicMock(return_value=True)):
|
|
|
|
with patch('os.unlink', MagicMock()):
|
|
|
|
with patch('os.symlink', MagicMock()):
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Debian']}):
|
|
|
|
timezone.get_hwclock()
|
|
|
|
name, args, kwarg = timezone.__salt__['cmd.run'].mock_calls[0]
|
|
|
|
assert args == (['tail', '-n', '1', '/etc/adjtime'],)
|
|
|
|
assert kwarg == {'python_shell': False}
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
def test_get_hwclock_solaris(self):
|
|
|
|
'''
|
|
|
|
Test get hwclock on Solaris
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
# Incomplete
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Solaris']}):
|
|
|
|
assert timezone.get_hwclock() == 'UTC'
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()):
|
2017-05-10 16:32:24 +00:00
|
|
|
assert timezone.get_hwclock() == 'localtime'
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
def test_get_hwclock_aix(self):
|
|
|
|
'''
|
|
|
|
Test get hwclock on AIX
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
# Incomplete
|
2017-06-06 19:06:08 +00:00
|
|
|
hwclock = 'localtime'
|
|
|
|
if not os.path.isfile('/etc/environment'):
|
|
|
|
hwclock = 'UTC'
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['AIX']}):
|
2017-06-06 19:06:08 +00:00
|
|
|
assert timezone.get_hwclock() == hwclock
|
2017-05-09 22:33:18 +00:00
|
|
|
|
2018-06-15 20:51:45 +00:00
|
|
|
@skipIf(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=True))
|
2018-06-11 22:20:56 +00:00
|
|
|
def test_set_hwclock_timedatectl(self):
|
|
|
|
'''
|
|
|
|
Test set hwclock with timedatectl
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
timezone.set_hwclock('UTC')
|
|
|
|
name, args, kwargs = timezone.__salt__['cmd.retcode'].mock_calls[0]
|
|
|
|
assert args == (['timedatectl', 'set-local-rtc', 'false'],)
|
|
|
|
|
|
|
|
timezone.set_hwclock('localtime')
|
|
|
|
name, args, kwargs = timezone.__salt__['cmd.retcode'].mock_calls[1]
|
|
|
|
assert args == (['timedatectl', 'set-local-rtc', 'true'],)
|
|
|
|
|
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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
2017-05-17 21:44:44 +00:00
|
|
|
def test_set_hwclock_aix_nilinuxrt(self):
|
2017-05-09 22:33:18 +00:00
|
|
|
'''
|
2017-05-17 21:44:44 +00:00
|
|
|
Test set hwclock on AIX and NILinuxRT
|
2017-05-09 22:33:18 +00:00
|
|
|
:return:
|
|
|
|
'''
|
2017-05-17 21:44:44 +00:00
|
|
|
for osfamily in ['AIX', 'NILinuxRT']:
|
|
|
|
with patch.dict(timezone.__grains__, {'os_family': osfamily}):
|
|
|
|
with self.assertRaises(SaltInvocationError):
|
|
|
|
assert timezone.set_hwclock('forty two')
|
|
|
|
assert timezone.set_hwclock('UTC')
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
@patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE'))
|
|
|
|
def test_set_hwclock_solaris(self):
|
|
|
|
'''
|
|
|
|
Test set hwclock on Solaris
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Solaris'],
|
|
|
|
'cpuarch': 'x86'}):
|
|
|
|
with self.assertRaises(SaltInvocationError):
|
|
|
|
assert timezone.set_hwclock('forty two')
|
|
|
|
assert timezone.set_hwclock('UTC')
|
|
|
|
name, args, kwargs = timezone.__salt__['cmd.retcode'].mock_calls[0]
|
|
|
|
assert args == (['rtc', '-z', 'GMT'],)
|
|
|
|
assert kwargs == {'python_shell': False}
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
@patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE'))
|
|
|
|
def test_set_hwclock_arch(self):
|
|
|
|
'''
|
|
|
|
Test set hwclock on arch
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Arch']}):
|
|
|
|
assert timezone.set_hwclock('UTC')
|
|
|
|
name, args, kwargs = timezone.__salt__['cmd.retcode'].mock_calls[0]
|
|
|
|
assert args == (['timezonectl', 'set-local-rtc', 'false'],)
|
|
|
|
assert kwargs == {'python_shell': False}
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
@patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE'))
|
|
|
|
def test_set_hwclock_redhat(self):
|
|
|
|
'''
|
|
|
|
Test set hwclock on RedHat
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['RedHat']}):
|
|
|
|
assert timezone.set_hwclock('UTC')
|
|
|
|
name, args, kwargs = timezone.__salt__['file.sed'].mock_calls[0]
|
|
|
|
assert args == ('/etc/sysconfig/clock', '^ZONE=.*', 'ZONE="TEST_TIMEZONE"')
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
@patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE'))
|
|
|
|
def test_set_hwclock_suse(self):
|
|
|
|
'''
|
|
|
|
Test set hwclock on SUSE
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Suse']}):
|
|
|
|
assert timezone.set_hwclock('UTC')
|
|
|
|
name, args, kwargs = timezone.__salt__['file.sed'].mock_calls[0]
|
|
|
|
assert args == ('/etc/sysconfig/clock', '^TIMEZONE=.*', 'TIMEZONE="TEST_TIMEZONE"')
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
@patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE'))
|
|
|
|
def test_set_hwclock_debian(self):
|
|
|
|
'''
|
|
|
|
Test set hwclock on Debian
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Debian']}):
|
|
|
|
assert timezone.set_hwclock('UTC')
|
|
|
|
name, args, kwargs = timezone.__salt__['file.sed'].mock_calls[0]
|
|
|
|
assert args == ('/etc/default/rcS', '^UTC=.*', 'UTC=yes')
|
2017-05-09 22:33:18 +00:00
|
|
|
|
2017-05-10 16:32:24 +00:00
|
|
|
assert timezone.set_hwclock('localtime')
|
|
|
|
name, args, kwargs = timezone.__salt__['file.sed'].mock_calls[1]
|
|
|
|
assert args == ('/etc/default/rcS', '^UTC=.*', 'UTC=no')
|
2017-05-09 22:33:18 +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(salt.utils.platform.is_windows(), 'os.symlink not available in Windows')
|
|
|
|
@patch('salt.utils.path.which', MagicMock(return_value=False))
|
2017-05-09 22:33:18 +00:00
|
|
|
@patch('os.path.exists', MagicMock(return_value=True))
|
|
|
|
@patch('os.unlink', MagicMock())
|
|
|
|
@patch('os.symlink', MagicMock())
|
|
|
|
@patch('salt.modules.timezone.get_zone', MagicMock(return_value='TEST_TIMEZONE'))
|
|
|
|
def test_set_hwclock_gentoo(self):
|
|
|
|
'''
|
|
|
|
Test set hwclock on Gentoo
|
|
|
|
:return:
|
|
|
|
'''
|
2017-05-10 16:32:24 +00:00
|
|
|
with patch.dict(timezone.__grains__, {'os_family': ['Gentoo']}):
|
|
|
|
with self.assertRaises(SaltInvocationError):
|
|
|
|
timezone.set_hwclock('forty two')
|
2017-05-09 22:33:18 +00:00
|
|
|
|
2017-05-10 16:32:24 +00:00
|
|
|
timezone.set_hwclock('UTC')
|
|
|
|
name, args, kwargs = timezone.__salt__['file.sed'].mock_calls[0]
|
|
|
|
assert args == ('/etc/conf.d/hwclock', '^clock=.*', 'clock="UTC"')
|
2017-05-09 22:33:18 +00:00
|
|
|
|
2017-05-10 16:32:24 +00:00
|
|
|
timezone.set_hwclock('localtime')
|
|
|
|
name, args, kwargs = timezone.__salt__['file.sed'].mock_calls[1]
|
|
|
|
assert args == ('/etc/conf.d/hwclock', '^clock=.*', 'clock="local"')
|