2015-01-06 05:49:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
|
2015-01-06 05:49:36 +00:00
|
|
|
'''
|
|
|
|
|
2015-01-08 19:44:03 +00:00
|
|
|
# Import Python libs
|
2018-01-14 06:40:03 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2015-01-08 19:44:03 +00:00
|
|
|
|
2015-01-06 05:49:36 +00:00
|
|
|
# Import Salt Testing Libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2015-01-06 05:49:36 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
)
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.augeas_cfg as augeas_cfg
|
2015-01-06 05:49:36 +00:00
|
|
|
from salt.exceptions import SaltInvocationError
|
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
|
2015-01-07 08:49:35 +00:00
|
|
|
# Make sure augeas python interface is installed
|
2017-02-20 13:52:12 +00:00
|
|
|
if augeas_cfg.HAS_AUGEAS:
|
2015-01-07 08:49:35 +00:00
|
|
|
from augeas import Augeas as _Augeas
|
2015-01-06 05:49:36 +00:00
|
|
|
|
|
|
|
|
2017-02-20 13:52:12 +00:00
|
|
|
@skipIf(augeas_cfg.HAS_AUGEAS is False, "python-augeas is required for this test case")
|
2015-01-06 05:49:36 +00:00
|
|
|
class AugeasCfgTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
Test cases for salt.modules.augeas_cfg
|
|
|
|
'''
|
|
|
|
# 'execute' function tests: 3
|
2017-06-07 12:45:20 +00:00
|
|
|
@skipIf(six.PY3, 'Disabled pending https://github.com/hercules-team/python-augeas/issues/30')
|
2015-01-06 05:49:36 +00:00
|
|
|
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})
|