salt/tests/unit/modules/test_cp.py

154 lines
5.4 KiB
Python
Raw Normal View History

2014-11-03 23:16:58 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`jmoney <justin@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
2014-11-03 23:16:58 +00:00
# Import Salt Testing Libs
2017-02-19 15:35:30 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
from tests.support.mock import (
2017-02-13 12:01:09 +00:00
Mock,
2014-11-03 23:16:58 +00:00
MagicMock,
mock_open,
patch,
NO_MOCK,
NO_MOCK_REASON
)
# Import Salt Libs
import salt.utils.files
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.utils.templates as templates
2017-04-04 12:11:54 +00:00
import salt.transport
import salt.modules.cp as cp
from salt.exceptions import CommandExecutionError
2014-11-03 23:16:58 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-02-19 15:35:30 +00:00
class CpTestCase(TestCase, LoaderModuleMockMixin):
2014-11-03 23:16:58 +00:00
'''
TestCase for salt.modules.cp module
'''
def setup_loader_modules(self):
return {cp: {}}
2017-02-19 15:35:30 +00:00
2014-11-03 23:16:58 +00:00
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)):
2014-11-03 23:16:58 +00:00
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
2014-11-03 23:16:58 +00:00
with patch.dict(templates.TEMPLATE_REGISTRY,
{'jinja': mock_jinja}):
with patch('salt.utils.files.fopen', mock_open(read_data=file_data)):
2014-11-03 23:16:58 +00:00
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.
'''
2017-04-10 13:00:57 +00:00
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)
2014-11-03 23:16:58 +00:00
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)):
2014-11-03 23:16:58 +00:00
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)
2017-02-13 12:01:09 +00:00
def test_push(self):
'''
Test if push works with good posix path.
'''
2017-04-10 13:00:57 +00:00
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=b'content')), \
2017-04-10 13:00:57 +00:00
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
2017-04-10 13:00:57 +00:00
salt.transport.Channel.factory({}).send.assert_called_once_with(
dict(
loc=salt.utils.files.fopen().tell(), # pylint: disable=resource-leakage
2017-04-10 13:00:57 +00:00
cmd='_file_recv',
tok='token',
path=['saltines', 'test.file'],
data='', # data is empty here because load['data'] is overwritten
id='abc'
)
2017-02-13 12:01:09 +00:00
)