mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 17:33:54 +00:00
3184168365
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.
152 lines
4.8 KiB
Python
152 lines
4.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
'''
|
|
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
|
|
'''
|
|
|
|
# Import Python libs
|
|
from __future__ import absolute_import
|
|
|
|
# Import Salt Testing Libs
|
|
from tests.support.unit import TestCase, skipIf
|
|
from tests.support.mock import (
|
|
MagicMock,
|
|
patch,
|
|
)
|
|
# Import Salt Libs
|
|
import salt.modules.augeas_cfg as augeas_cfg
|
|
from salt.exceptions import SaltInvocationError
|
|
from salt.ext import six
|
|
# Make sure augeas python interface is installed
|
|
if augeas_cfg.HAS_AUGEAS:
|
|
from augeas import Augeas as _Augeas
|
|
|
|
|
|
@skipIf(augeas_cfg.HAS_AUGEAS is False, "python-augeas is required for this test case")
|
|
class AugeasCfgTestCase(TestCase):
|
|
'''
|
|
Test cases for salt.modules.augeas_cfg
|
|
'''
|
|
# 'execute' function tests: 3
|
|
@skipIf(six.PY3, 'Disabled pending https://github.com/hercules-team/python-augeas/issues/30')
|
|
def test_execute(self):
|
|
'''
|
|
Test if it execute Augeas commands
|
|
'''
|
|
self.assertEqual(augeas_cfg.execute(), {'retval': True})
|
|
|
|
def test_execute_io_error(self):
|
|
'''
|
|
Test if it execute Augeas commands
|
|
'''
|
|
ret = {'error': 'Command is not supported (yet)', 'retval': False}
|
|
self.assertEqual(augeas_cfg.execute(None, None, [" "]), ret)
|
|
|
|
def test_execute_value_error(self):
|
|
'''
|
|
Test if it execute Augeas commands
|
|
'''
|
|
ret = {'retval': False,
|
|
'error':
|
|
'Invalid formatted command, see debug log for details: '}
|
|
self.assertEqual(augeas_cfg.execute(None, None, ["set "]), ret)
|
|
|
|
# 'get' function tests: 1
|
|
|
|
def test_get(self):
|
|
'''
|
|
Test if it get a value for a specific augeas path
|
|
'''
|
|
mock = MagicMock(side_effect=RuntimeError('error'))
|
|
with patch.object(_Augeas, 'match', mock):
|
|
self.assertEqual(augeas_cfg.get('/etc/hosts'),
|
|
{'error': 'error'})
|
|
|
|
mock = MagicMock(return_value=True)
|
|
with patch.object(_Augeas, 'match', mock):
|
|
self.assertEqual(augeas_cfg.get('/etc/hosts'),
|
|
{'/etc/hosts': None})
|
|
|
|
# 'setvalue' function tests: 4
|
|
|
|
def test_setvalue(self):
|
|
'''
|
|
Test if it set a value for a specific augeas path
|
|
'''
|
|
self.assertEqual(augeas_cfg.setvalue('prefix=/etc/hosts'),
|
|
{'retval': True})
|
|
|
|
def test_setvalue_io_error(self):
|
|
'''
|
|
Test if it set a value for a specific augeas path
|
|
'''
|
|
mock = MagicMock(side_effect=IOError(''))
|
|
with patch.object(_Augeas, 'save', mock):
|
|
self.assertEqual(augeas_cfg.setvalue('prefix=/files/etc/'),
|
|
{'retval': False, 'error': ''})
|
|
|
|
def test_setvalue_uneven_path(self):
|
|
'''
|
|
Test if it set a value for a specific augeas path
|
|
'''
|
|
mock = MagicMock(side_effect=RuntimeError('error'))
|
|
with patch.object(_Augeas, 'match', mock):
|
|
self.assertRaises(SaltInvocationError, augeas_cfg.setvalue,
|
|
['/files/etc/hosts/1/canonical', 'localhost'])
|
|
|
|
def test_setvalue_one_prefix(self):
|
|
'''
|
|
Test if it set a value for a specific augeas path
|
|
'''
|
|
self.assertRaises(SaltInvocationError, augeas_cfg.setvalue,
|
|
'prefix=/files', '10.18.1.1', 'prefix=/etc', 'test')
|
|
|
|
# 'match' function tests: 2
|
|
|
|
def test_match(self):
|
|
'''
|
|
Test if it matches for path expression
|
|
'''
|
|
self.assertEqual(augeas_cfg.match('/etc/service', 'ssh'), {})
|
|
|
|
def test_match_runtime_error(self):
|
|
'''
|
|
Test if it matches for path expression
|
|
'''
|
|
mock = MagicMock(side_effect=RuntimeError('error'))
|
|
with patch.object(_Augeas, 'match', mock):
|
|
self.assertEqual(augeas_cfg.match('/etc/service-name', 'ssh'), {})
|
|
|
|
# 'remove' function tests: 2
|
|
|
|
def test_remove(self):
|
|
'''
|
|
Test if it removes for path expression
|
|
'''
|
|
self.assertEqual(augeas_cfg.remove('/etc/service'),
|
|
{'count': 0, 'retval': True})
|
|
|
|
def test_remove_io_runtime_error(self):
|
|
'''
|
|
Test if it removes for path expression
|
|
'''
|
|
mock = MagicMock(side_effect=RuntimeError('error'))
|
|
with patch.object(_Augeas, 'save', mock):
|
|
self.assertEqual(augeas_cfg.remove('/etc/service-name'),
|
|
{'count': 0, 'error': 'error', 'retval': False})
|
|
|
|
# 'ls' function tests: 1
|
|
|
|
def test_ls(self):
|
|
'''
|
|
Test if it list the direct children of a node
|
|
'''
|
|
self.assertEqual(augeas_cfg.ls('/etc/passwd'), {})
|
|
|
|
# 'tree' function tests: 1
|
|
|
|
def test_tree(self):
|
|
'''
|
|
Test if it returns recursively the complete tree of a node
|
|
'''
|
|
self.assertEqual(augeas_cfg.tree('/etc/'), {'/etc': None})
|