2016-03-03 17:36:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
mac_utils tests
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import MagicMock, patch, NO_MOCK, NO_MOCK_REASON
|
2016-03-03 17:36:27 +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
|
|
|
# Import Salt libs
|
|
|
|
import salt.utils.mac_utils as mac_utils
|
2016-03-03 17:36:27 +00:00
|
|
|
from salt.exceptions import SaltInvocationError, CommandExecutionError
|
|
|
|
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
# Import 3rd-party libs
|
|
|
|
from salt.ext.six.moves import range
|
|
|
|
|
2016-03-03 17:36:27 +00:00
|
|
|
|
2016-03-03 18:21:00 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2016-03-03 17:36:27 +00:00
|
|
|
class MacUtilsTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
test mac_utils salt utility
|
|
|
|
'''
|
2016-03-03 18:21:00 +00:00
|
|
|
def test_execute_return_success_not_supported(self):
|
2016-03-03 17:36:27 +00:00
|
|
|
'''
|
2016-03-03 18:21:00 +00:00
|
|
|
test execute_return_success function
|
|
|
|
command not supported
|
2016-03-03 17:36:27 +00:00
|
|
|
'''
|
2016-03-03 18:21:00 +00:00
|
|
|
mock_cmd = MagicMock(return_value={'retcode': 0,
|
2016-03-30 14:45:48 +00:00
|
|
|
'stdout': 'not supported',
|
|
|
|
'stderr': 'error'})
|
2016-03-04 23:36:02 +00:00
|
|
|
with patch.object(mac_utils, '_run_all', mock_cmd):
|
2016-03-30 14:45:48 +00:00
|
|
|
self.assertRaises(CommandExecutionError,
|
|
|
|
mac_utils.execute_return_success,
|
|
|
|
'dir c:\\')
|
2016-03-03 17:36:27 +00:00
|
|
|
|
2016-03-03 18:21:00 +00:00
|
|
|
def test_execute_return_success_command_failed(self):
|
|
|
|
'''
|
|
|
|
test execute_return_success function
|
|
|
|
command failed
|
|
|
|
'''
|
|
|
|
mock_cmd = MagicMock(return_value={'retcode': 1,
|
2016-03-22 19:46:19 +00:00
|
|
|
'stdout': 'spongebob',
|
|
|
|
'stderr': 'error'})
|
2016-03-04 23:36:02 +00:00
|
|
|
with patch.object(mac_utils, '_run_all', mock_cmd):
|
2016-03-03 18:21:00 +00:00
|
|
|
self.assertRaises(CommandExecutionError,
|
|
|
|
mac_utils.execute_return_success,
|
|
|
|
'dir c:\\')
|
2016-03-03 17:36:27 +00:00
|
|
|
|
2016-03-03 18:21:00 +00:00
|
|
|
def test_execute_return_success_command_succeeded(self):
|
|
|
|
'''
|
|
|
|
test execute_return_success function
|
|
|
|
command succeeded
|
|
|
|
'''
|
|
|
|
mock_cmd = MagicMock(return_value={'retcode': 0,
|
|
|
|
'stdout': 'spongebob'})
|
2016-03-04 23:36:02 +00:00
|
|
|
with patch.object(mac_utils, '_run_all', mock_cmd):
|
2016-03-03 17:36:27 +00:00
|
|
|
ret = mac_utils.execute_return_success('dir c:\\')
|
|
|
|
self.assertEqual(ret, True)
|
|
|
|
|
2016-03-03 18:21:00 +00:00
|
|
|
def test_execute_return_result_command_failed(self):
|
|
|
|
'''
|
|
|
|
test execute_return_result function
|
|
|
|
command failed
|
|
|
|
'''
|
|
|
|
mock_cmd = MagicMock(return_value={'retcode': 1,
|
|
|
|
'stdout': 'spongebob',
|
|
|
|
'stderr': 'squarepants'})
|
2016-03-04 23:36:02 +00:00
|
|
|
with patch.object(mac_utils, '_run_all', mock_cmd):
|
2016-03-03 18:21:00 +00:00
|
|
|
self.assertRaises(CommandExecutionError,
|
|
|
|
mac_utils.execute_return_result,
|
|
|
|
'dir c:\\')
|
|
|
|
|
|
|
|
def test_execute_return_result_command_succeeded(self):
|
|
|
|
'''
|
|
|
|
test execute_return_result function
|
|
|
|
command succeeded
|
|
|
|
'''
|
|
|
|
mock_cmd = MagicMock(return_value={'retcode': 0,
|
|
|
|
'stdout': 'spongebob'})
|
2016-03-04 23:36:02 +00:00
|
|
|
with patch.object(mac_utils, '_run_all', mock_cmd):
|
2016-03-03 18:21:00 +00:00
|
|
|
ret = mac_utils.execute_return_result('dir c:\\')
|
|
|
|
self.assertEqual(ret, 'spongebob')
|
|
|
|
|
|
|
|
def test_parse_return_space(self):
|
|
|
|
'''
|
|
|
|
test parse_return function
|
|
|
|
space after colon
|
|
|
|
'''
|
|
|
|
self.assertEqual(mac_utils.parse_return('spongebob: squarepants'),
|
|
|
|
'squarepants')
|
|
|
|
|
|
|
|
def test_parse_return_new_line(self):
|
|
|
|
'''
|
|
|
|
test parse_return function
|
|
|
|
new line after colon
|
|
|
|
'''
|
|
|
|
self.assertEqual(mac_utils.parse_return('spongebob:\nsquarepants'),
|
|
|
|
'squarepants')
|
|
|
|
|
|
|
|
def test_parse_return_no_delimiter(self):
|
|
|
|
'''
|
|
|
|
test parse_return function
|
|
|
|
no delimiter
|
|
|
|
'''
|
|
|
|
self.assertEqual(mac_utils.parse_return('squarepants'),
|
|
|
|
'squarepants')
|
|
|
|
|
|
|
|
def test_validate_enabled_on(self):
|
|
|
|
'''
|
|
|
|
test validate_enabled function
|
|
|
|
test on
|
|
|
|
'''
|
|
|
|
self.assertEqual(mac_utils.validate_enabled('On'),
|
|
|
|
'on')
|
|
|
|
|
|
|
|
def test_validate_enabled_off(self):
|
|
|
|
'''
|
|
|
|
test validate_enabled function
|
|
|
|
test off
|
|
|
|
'''
|
|
|
|
self.assertEqual(mac_utils.validate_enabled('Off'),
|
|
|
|
'off')
|
|
|
|
|
|
|
|
def test_validate_enabled_bad_string(self):
|
|
|
|
'''
|
|
|
|
test validate_enabled function
|
|
|
|
test bad string
|
|
|
|
'''
|
|
|
|
self.assertRaises(SaltInvocationError,
|
|
|
|
mac_utils.validate_enabled,
|
|
|
|
'bad string')
|
|
|
|
|
|
|
|
def test_validate_enabled_non_zero(self):
|
|
|
|
'''
|
|
|
|
test validate_enabled function
|
|
|
|
test non zero
|
|
|
|
'''
|
|
|
|
for x in range(1, 179, 3):
|
|
|
|
self.assertEqual(mac_utils.validate_enabled(x),
|
|
|
|
'on')
|
|
|
|
|
|
|
|
def test_validate_enabled_0(self):
|
|
|
|
'''
|
|
|
|
test validate_enabled function
|
|
|
|
test 0
|
|
|
|
'''
|
|
|
|
self.assertEqual(mac_utils.validate_enabled(0),
|
|
|
|
'off')
|
|
|
|
|
|
|
|
def test_validate_enabled_true(self):
|
|
|
|
'''
|
|
|
|
test validate_enabled function
|
|
|
|
test True
|
|
|
|
'''
|
|
|
|
self.assertEqual(mac_utils.validate_enabled(True),
|
|
|
|
'on')
|
|
|
|
|
|
|
|
def test_validate_enabled_false(self):
|
|
|
|
'''
|
|
|
|
test validate_enabled function
|
|
|
|
test False
|
|
|
|
'''
|
|
|
|
self.assertEqual(mac_utils.validate_enabled(False),
|
|
|
|
'off')
|