mirror of
https://github.com/valitydev/salt.git
synced 2024-11-09 01:36:48 +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.
154 lines
5.4 KiB
Python
154 lines
5.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
'''
|
|
:codeauthor: :email:`jmoney <justin@saltstack.com>`
|
|
'''
|
|
|
|
# Import Python libs
|
|
from __future__ import absolute_import
|
|
|
|
# Import Salt Testing Libs
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
|
from tests.support.unit import skipIf, TestCase
|
|
from tests.support.mock import (
|
|
Mock,
|
|
MagicMock,
|
|
mock_open,
|
|
patch,
|
|
NO_MOCK,
|
|
NO_MOCK_REASON
|
|
)
|
|
|
|
# Import Salt Libs
|
|
import salt.utils.files
|
|
import salt.utils.templates as templates
|
|
import salt.transport
|
|
import salt.modules.cp as cp
|
|
from salt.exceptions import CommandExecutionError
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
class CpTestCase(TestCase, LoaderModuleMockMixin):
|
|
'''
|
|
TestCase for salt.modules.cp module
|
|
'''
|
|
|
|
def setup_loader_modules(self):
|
|
return {cp: {}}
|
|
|
|
def test__render_filenames_undefined_template(self):
|
|
'''
|
|
Test if _render_filenames fails upon getting a template not in
|
|
TEMPLATE_REGISTRY.
|
|
'''
|
|
path = '/srv/salt/saltines'
|
|
dest = '/srv/salt/cheese'
|
|
saltenv = 'base'
|
|
template = 'biscuits'
|
|
ret = (path, dest)
|
|
self.assertRaises(CommandExecutionError,
|
|
cp._render_filenames,
|
|
path, dest, saltenv, template)
|
|
|
|
def test__render_filenames_render_failed(self):
|
|
'''
|
|
Test if _render_filenames fails when template rendering fails.
|
|
'''
|
|
path = 'salt://saltines'
|
|
dest = '/srv/salt/cheese'
|
|
saltenv = 'base'
|
|
template = 'jinja'
|
|
file_data = 'Remember to keep your files well salted.'
|
|
mock_jinja = lambda *args, **kwargs: {'result': False,
|
|
'data': file_data}
|
|
with patch.dict(templates.TEMPLATE_REGISTRY,
|
|
{'jinja': mock_jinja}):
|
|
with patch('salt.utils.files.fopen', mock_open(read_data=file_data)):
|
|
self.assertRaises(CommandExecutionError,
|
|
cp._render_filenames,
|
|
path, dest, saltenv, template)
|
|
|
|
def test__render_filenames_success(self):
|
|
'''
|
|
Test if _render_filenames succeeds.
|
|
'''
|
|
path = 'salt://saltines'
|
|
dest = '/srv/salt/cheese'
|
|
saltenv = 'base'
|
|
template = 'jinja'
|
|
file_data = '/srv/salt/biscuits'
|
|
mock_jinja = lambda *args, **kwargs: {'result': True,
|
|
'data': file_data}
|
|
ret = (file_data, file_data) # salt.utils.files.fopen can only be mocked once
|
|
with patch.dict(templates.TEMPLATE_REGISTRY,
|
|
{'jinja': mock_jinja}):
|
|
with patch('salt.utils.files.fopen', mock_open(read_data=file_data)):
|
|
self.assertEqual(cp._render_filenames(
|
|
path, dest, saltenv, template), ret)
|
|
|
|
def test_get_file_not_found(self):
|
|
'''
|
|
Test if get_file can't find the file.
|
|
'''
|
|
with patch('salt.modules.cp.hash_file', MagicMock(return_value=False)):
|
|
path = 'salt://saltines'
|
|
dest = '/srv/salt/cheese'
|
|
ret = ''
|
|
self.assertEqual(cp.get_file(path, dest), ret)
|
|
|
|
def test_get_file_str_success(self):
|
|
'''
|
|
Test if get_file_str succeeds.
|
|
'''
|
|
path = 'salt://saltines'
|
|
dest = '/srv/salt/cheese/saltines'
|
|
file_data = 'Remember to keep your files well salted.'
|
|
saltenv = 'base'
|
|
ret = file_data
|
|
with patch('salt.utils.files.fopen', mock_open(read_data=file_data)):
|
|
with patch('salt.modules.cp.cache_file',
|
|
MagicMock(return_value=dest)):
|
|
self.assertEqual(cp.get_file_str(path, dest), ret)
|
|
|
|
def test_push_non_absolute_path(self):
|
|
'''
|
|
Test if push fails on a non absolute path.
|
|
'''
|
|
path = '../saltines'
|
|
ret = False
|
|
|
|
self.assertEqual(cp.push(path), ret)
|
|
|
|
def test_push_dir_non_absolute_path(self):
|
|
'''
|
|
Test if push_dir fails on a non absolute path.
|
|
'''
|
|
path = '../saltines'
|
|
ret = False
|
|
|
|
self.assertEqual(cp.push_dir(path), ret)
|
|
|
|
def test_push(self):
|
|
'''
|
|
Test if push works with good posix path.
|
|
'''
|
|
with patch('salt.modules.cp.os.path',
|
|
MagicMock(isfile=Mock(return_value=True), wraps=cp.os.path)), \
|
|
patch.multiple('salt.modules.cp',
|
|
_auth=MagicMock(**{'return_value.gen_token.return_value': 'token'}),
|
|
__opts__={'id': 'abc', 'file_buffer_size': 10}), \
|
|
patch('salt.utils.files.fopen', mock_open(read_data='content')), \
|
|
patch('salt.transport.Channel.factory', MagicMock()):
|
|
response = cp.push('/saltines/test.file')
|
|
self.assertEqual(response, True)
|
|
self.assertEqual(salt.utils.files.fopen().read.call_count, 2) # pylint: disable=resource-leakage
|
|
salt.transport.Channel.factory({}).send.assert_called_once_with(
|
|
dict(
|
|
loc=salt.utils.files.fopen().tell(), # pylint: disable=resource-leakage
|
|
cmd='_file_recv',
|
|
tok='token',
|
|
path=['saltines', 'test.file'],
|
|
data='', # data is empty here because load['data'] is overwritten
|
|
id='abc'
|
|
)
|
|
)
|