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.
362 lines
16 KiB
Python
362 lines
16 KiB
Python
# -*- coding: utf-8 -*-
|
|
'''
|
|
:codeauthor: :email:`Mike Place <mp@saltstack.com>`
|
|
'''
|
|
# Import Python libs
|
|
from __future__ import absolute_import
|
|
import errno
|
|
import logging
|
|
import os
|
|
import shutil
|
|
|
|
# Import Salt Testing libs
|
|
from tests.integration import AdaptedConfigurationTestCaseMixin
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
|
from tests.support.paths import TMP
|
|
from tests.support.unit import TestCase, skipIf
|
|
from tests.support.mock import MagicMock, patch, NO_MOCK, NO_MOCK_REASON
|
|
|
|
# Import salt libs
|
|
import salt.utils.files
|
|
from salt import fileclient
|
|
from salt.ext import six
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
SALTENVS = ('base', 'dev')
|
|
FS_ROOT = os.path.join(TMP, 'fileclient_fs_root')
|
|
CACHE_ROOT = os.path.join(TMP, 'fileclient_cache_root')
|
|
SUBDIR = 'subdir'
|
|
SUBDIR_FILES = ('foo.txt', 'bar.txt', 'baz.txt')
|
|
|
|
|
|
def _get_file_roots():
|
|
return dict(
|
|
[(x, [os.path.join(FS_ROOT, x)]) for x in SALTENVS]
|
|
)
|
|
|
|
|
|
MOCKED_OPTS = {
|
|
'file_roots': _get_file_roots(),
|
|
'fileserver_backend': ['roots'],
|
|
'cachedir': CACHE_ROOT,
|
|
'file_client': 'local',
|
|
}
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
class FileClientTest(TestCase, AdaptedConfigurationTestCaseMixin, LoaderModuleMockMixin):
|
|
|
|
def setup_loader_modules(self):
|
|
return {fileclient: {'__opts__': MOCKED_OPTS}}
|
|
|
|
def setUp(self):
|
|
self.file_client = fileclient.Client(self.master_opts)
|
|
|
|
def tearDown(self):
|
|
del self.file_client
|
|
|
|
def test_file_list_emptydirs(self):
|
|
'''
|
|
Ensure that the fileclient class won't allow a direct call to file_list_emptydirs()
|
|
'''
|
|
with self.assertRaises(NotImplementedError):
|
|
self.file_client.file_list_emptydirs()
|
|
|
|
def test_get_file(self):
|
|
'''
|
|
Ensure that the fileclient class won't allow a direct call to get_file()
|
|
'''
|
|
with self.assertRaises(NotImplementedError):
|
|
self.file_client.get_file(None)
|
|
|
|
def test_get_file_client(self):
|
|
minion_opts = self.get_temp_config('minion')
|
|
minion_opts['file_client'] = 'remote'
|
|
with patch('salt.fileclient.RemoteClient', MagicMock(return_value='remote_client')):
|
|
ret = fileclient.get_file_client(minion_opts)
|
|
self.assertEqual('remote_client', ret)
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
class FileclientCacheTest(TestCase, AdaptedConfigurationTestCaseMixin, LoaderModuleMockMixin):
|
|
'''
|
|
Tests for the fileclient caching. The LocalClient is the only thing we can
|
|
test as it is the only way we can mock the fileclient (the tests run from
|
|
the minion process, so the master cannot be mocked from test code).
|
|
'''
|
|
|
|
def setup_loader_modules(self):
|
|
return {fileclient: {'__opts__': MOCKED_OPTS}}
|
|
|
|
def setUp(self):
|
|
'''
|
|
No need to add a dummy foo.txt to muddy up the github repo, just make
|
|
our own fileserver root on-the-fly.
|
|
'''
|
|
def _new_dir(path):
|
|
'''
|
|
Add a new dir at ``path`` using os.makedirs. If the directory
|
|
already exists, remove it recursively and then try to create it
|
|
again.
|
|
'''
|
|
try:
|
|
os.makedirs(path)
|
|
except OSError as exc:
|
|
if exc.errno == errno.EEXIST:
|
|
# Just in case a previous test was interrupted, remove the
|
|
# directory and try adding it again.
|
|
shutil.rmtree(path)
|
|
os.makedirs(path)
|
|
else:
|
|
raise
|
|
|
|
# Crete the FS_ROOT
|
|
for saltenv in SALTENVS:
|
|
saltenv_root = os.path.join(FS_ROOT, saltenv)
|
|
# Make sure we have a fresh root dir for this saltenv
|
|
_new_dir(saltenv_root)
|
|
|
|
path = os.path.join(saltenv_root, 'foo.txt')
|
|
with salt.utils.files.fopen(path, 'w') as fp_:
|
|
fp_.write(
|
|
'This is a test file in the \'{0}\' saltenv.\n'
|
|
.format(saltenv)
|
|
)
|
|
|
|
subdir_abspath = os.path.join(saltenv_root, SUBDIR)
|
|
os.makedirs(subdir_abspath)
|
|
for subdir_file in SUBDIR_FILES:
|
|
path = os.path.join(subdir_abspath, subdir_file)
|
|
with salt.utils.files.fopen(path, 'w') as fp_:
|
|
fp_.write(
|
|
'This is file \'{0}\' in subdir \'{1} from saltenv '
|
|
'\'{2}\''.format(subdir_file, SUBDIR, saltenv)
|
|
)
|
|
|
|
# Create the CACHE_ROOT
|
|
_new_dir(CACHE_ROOT)
|
|
|
|
def tearDown(self):
|
|
'''
|
|
Remove the directories created for these tests
|
|
'''
|
|
shutil.rmtree(FS_ROOT)
|
|
shutil.rmtree(CACHE_ROOT)
|
|
|
|
def test_cache_dir(self):
|
|
'''
|
|
Ensure entire directory is cached to correct location
|
|
'''
|
|
patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
|
|
patched_opts.update(MOCKED_OPTS)
|
|
|
|
with patch.dict(fileclient.__opts__, patched_opts):
|
|
client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
|
|
for saltenv in SALTENVS:
|
|
self.assertTrue(
|
|
client.cache_dir(
|
|
'salt://{0}'.format(SUBDIR),
|
|
saltenv,
|
|
cachedir=None
|
|
)
|
|
)
|
|
for subdir_file in SUBDIR_FILES:
|
|
cache_loc = os.path.join(fileclient.__opts__['cachedir'],
|
|
'files',
|
|
saltenv,
|
|
SUBDIR,
|
|
subdir_file)
|
|
# Double check that the content of the cached file
|
|
# identifies it as being from the correct saltenv. The
|
|
# setUp function creates the file with the name of the
|
|
# saltenv mentioned in the file, so a simple 'in' check is
|
|
# sufficient here. If opening the file raises an exception,
|
|
# this is a problem, so we are not catching the exception
|
|
# and letting it be raised so that the test fails.
|
|
with salt.utils.files.fopen(cache_loc) as fp_:
|
|
content = fp_.read()
|
|
log.debug('cache_loc = %s', cache_loc)
|
|
log.debug('content = %s', content)
|
|
self.assertTrue(subdir_file in content)
|
|
self.assertTrue(SUBDIR in content)
|
|
self.assertTrue(saltenv in content)
|
|
|
|
def test_cache_dir_with_alternate_cachedir_and_absolute_path(self):
|
|
'''
|
|
Ensure entire directory is cached to correct location when an alternate
|
|
cachedir is specified and that cachedir is an absolute path
|
|
'''
|
|
patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
|
|
patched_opts.update(MOCKED_OPTS)
|
|
alt_cachedir = os.path.join(TMP, 'abs_cachedir')
|
|
|
|
with patch.dict(fileclient.__opts__, patched_opts):
|
|
client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
|
|
for saltenv in SALTENVS:
|
|
self.assertTrue(
|
|
client.cache_dir(
|
|
'salt://{0}'.format(SUBDIR),
|
|
saltenv,
|
|
cachedir=alt_cachedir
|
|
)
|
|
)
|
|
for subdir_file in SUBDIR_FILES:
|
|
cache_loc = os.path.join(alt_cachedir,
|
|
'files',
|
|
saltenv,
|
|
SUBDIR,
|
|
subdir_file)
|
|
# Double check that the content of the cached file
|
|
# identifies it as being from the correct saltenv. The
|
|
# setUp function creates the file with the name of the
|
|
# saltenv mentioned in the file, so a simple 'in' check is
|
|
# sufficient here. If opening the file raises an exception,
|
|
# this is a problem, so we are not catching the exception
|
|
# and letting it be raised so that the test fails.
|
|
with salt.utils.files.fopen(cache_loc) as fp_:
|
|
content = fp_.read()
|
|
log.debug('cache_loc = %s', cache_loc)
|
|
log.debug('content = %s', content)
|
|
self.assertTrue(subdir_file in content)
|
|
self.assertTrue(SUBDIR in content)
|
|
self.assertTrue(saltenv in content)
|
|
|
|
def test_cache_dir_with_alternate_cachedir_and_relative_path(self):
|
|
'''
|
|
Ensure entire directory is cached to correct location when an alternate
|
|
cachedir is specified and that cachedir is a relative path
|
|
'''
|
|
patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
|
|
patched_opts.update(MOCKED_OPTS)
|
|
alt_cachedir = 'foo'
|
|
|
|
with patch.dict(fileclient.__opts__, patched_opts):
|
|
client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
|
|
for saltenv in SALTENVS:
|
|
self.assertTrue(
|
|
client.cache_dir(
|
|
'salt://{0}'.format(SUBDIR),
|
|
saltenv,
|
|
cachedir=alt_cachedir
|
|
)
|
|
)
|
|
for subdir_file in SUBDIR_FILES:
|
|
cache_loc = os.path.join(fileclient.__opts__['cachedir'],
|
|
alt_cachedir,
|
|
'files',
|
|
saltenv,
|
|
SUBDIR,
|
|
subdir_file)
|
|
# Double check that the content of the cached file
|
|
# identifies it as being from the correct saltenv. The
|
|
# setUp function creates the file with the name of the
|
|
# saltenv mentioned in the file, so a simple 'in' check is
|
|
# sufficient here. If opening the file raises an exception,
|
|
# this is a problem, so we are not catching the exception
|
|
# and letting it be raised so that the test fails.
|
|
with salt.utils.files.fopen(cache_loc) as fp_:
|
|
content = fp_.read()
|
|
log.debug('cache_loc = %s', cache_loc)
|
|
log.debug('content = %s', content)
|
|
self.assertTrue(subdir_file in content)
|
|
self.assertTrue(SUBDIR in content)
|
|
self.assertTrue(saltenv in content)
|
|
|
|
def test_cache_file(self):
|
|
'''
|
|
Ensure file is cached to correct location
|
|
'''
|
|
patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
|
|
patched_opts.update(MOCKED_OPTS)
|
|
|
|
with patch.dict(fileclient.__opts__, patched_opts):
|
|
client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
|
|
for saltenv in SALTENVS:
|
|
self.assertTrue(
|
|
client.cache_file('salt://foo.txt', saltenv, cachedir=None)
|
|
)
|
|
cache_loc = os.path.join(
|
|
fileclient.__opts__['cachedir'], 'files', saltenv, 'foo.txt')
|
|
# Double check that the content of the cached file identifies
|
|
# it as being from the correct saltenv. The setUp function
|
|
# creates the file with the name of the saltenv mentioned in
|
|
# the file, so a simple 'in' check is sufficient here. If
|
|
# opening the file raises an exception, this is a problem, so
|
|
# we are not catching the exception and letting it be raised so
|
|
# that the test fails.
|
|
with salt.utils.files.fopen(cache_loc) as fp_:
|
|
content = fp_.read()
|
|
log.debug('cache_loc = %s', cache_loc)
|
|
log.debug('content = %s', content)
|
|
self.assertTrue(saltenv in content)
|
|
|
|
def test_cache_file_with_alternate_cachedir_and_absolute_path(self):
|
|
'''
|
|
Ensure file is cached to correct location when an alternate cachedir is
|
|
specified and that cachedir is an absolute path
|
|
'''
|
|
patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
|
|
patched_opts.update(MOCKED_OPTS)
|
|
alt_cachedir = os.path.join(TMP, 'abs_cachedir')
|
|
|
|
with patch.dict(fileclient.__opts__, patched_opts):
|
|
client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
|
|
for saltenv in SALTENVS:
|
|
self.assertTrue(
|
|
client.cache_file('salt://foo.txt',
|
|
saltenv,
|
|
cachedir=alt_cachedir)
|
|
)
|
|
cache_loc = os.path.join(alt_cachedir,
|
|
'files',
|
|
saltenv,
|
|
'foo.txt')
|
|
# Double check that the content of the cached file identifies
|
|
# it as being from the correct saltenv. The setUp function
|
|
# creates the file with the name of the saltenv mentioned in
|
|
# the file, so a simple 'in' check is sufficient here. If
|
|
# opening the file raises an exception, this is a problem, so
|
|
# we are not catching the exception and letting it be raised so
|
|
# that the test fails.
|
|
with salt.utils.files.fopen(cache_loc) as fp_:
|
|
content = fp_.read()
|
|
log.debug('cache_loc = %s', cache_loc)
|
|
log.debug('content = %s', content)
|
|
self.assertTrue(saltenv in content)
|
|
|
|
def test_cache_file_with_alternate_cachedir_and_relative_path(self):
|
|
'''
|
|
Ensure file is cached to correct location when an alternate cachedir is
|
|
specified and that cachedir is a relative path
|
|
'''
|
|
patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
|
|
patched_opts.update(MOCKED_OPTS)
|
|
alt_cachedir = 'foo'
|
|
|
|
with patch.dict(fileclient.__opts__, patched_opts):
|
|
client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
|
|
for saltenv in SALTENVS:
|
|
self.assertTrue(
|
|
client.cache_file('salt://foo.txt',
|
|
saltenv,
|
|
cachedir=alt_cachedir)
|
|
)
|
|
cache_loc = os.path.join(fileclient.__opts__['cachedir'],
|
|
alt_cachedir,
|
|
'files',
|
|
saltenv,
|
|
'foo.txt')
|
|
# Double check that the content of the cached file identifies
|
|
# it as being from the correct saltenv. The setUp function
|
|
# creates the file with the name of the saltenv mentioned in
|
|
# the file, so a simple 'in' check is sufficient here. If
|
|
# opening the file raises an exception, this is a problem, so
|
|
# we are not catching the exception and letting it be raised so
|
|
# that the test fails.
|
|
with salt.utils.files.fopen(cache_loc) as fp_:
|
|
content = fp_.read()
|
|
log.debug('cache_loc = %s', cache_loc)
|
|
log.debug('content = %s', content)
|
|
self.assertTrue(saltenv in content)
|