salt/tests/integration/runners/test_fileserver.py
Erik Johnson 3184168365 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-08-08 13:33:43 -05:00

200 lines
7.3 KiB
Python

# -*- coding: utf-8 -*-
'''
Tests for the fileserver runner
'''
# Import Python libs
from __future__ import absolute_import
import contextlib
# Import Salt Testing libs
from tests.support.case import ShellCase
from tests.support.unit import skipIf
# Import Salt libs
import salt.utils.platform
class FileserverTest(ShellCase):
'''
Test the fileserver runner
'''
def test_dir_list(self):
'''
fileserver.dir_list
'''
ret = self.run_run_plus(fun='fileserver.dir_list')
self.assertIsInstance(ret['return'], list)
self.assertTrue('_modules' in ret['return'])
# Backend submitted as a string
ret = self.run_run_plus(fun='fileserver.dir_list', backend='roots')
self.assertIsInstance(ret['return'], list)
self.assertTrue('_modules' in ret['return'])
# Backend submitted as a list
ret = self.run_run_plus(fun='fileserver.dir_list', backend=['roots'])
self.assertIsInstance(ret['return'], list)
self.assertTrue('_modules' in ret['return'])
def test_empty_dir_list(self):
'''
fileserver.empty_dir_list
'''
ret = self.run_run_plus(fun='fileserver.empty_dir_list')
self.assertIsInstance(ret['return'], list)
self.assertEqual(ret['return'], [])
# Backend submitted as a string
ret = self.run_run_plus(
fun='fileserver.empty_dir_list',
backend='roots')
self.assertIsInstance(ret['return'], list)
self.assertEqual(ret['return'], [])
# Backend submitted as a list
ret = self.run_run_plus(
fun='fileserver.empty_dir_list',
backend=['roots'])
self.assertIsInstance(ret['return'], list)
self.assertEqual(ret['return'], [])
def test_envs(self):
'''
fileserver.envs
'''
ret = self.run_run_plus(fun='fileserver.envs')
self.assertIsInstance(ret['return'], list)
# Backend submitted as a string
ret = self.run_run_plus(fun='fileserver.envs', backend='roots')
self.assertIsInstance(ret['return'], list)
# Backend submitted as a list
ret = self.run_run_plus(fun='fileserver.envs', backend=['roots'])
self.assertIsInstance(ret['return'], list)
def test_clear_file_list_cache(self):
'''
fileserver.clear_file_list_cache
If this test fails, then something may have changed in the test suite
and we may have more than just "roots" configured in the fileserver
backends. This assert will need to be updated accordingly.
'''
saltenvs = sorted(self.run_run_plus(fun='fileserver.envs')['return'])
@contextlib.contextmanager
def gen_cache():
'''
Create file_list cache so we have something to clear
'''
for saltenv in saltenvs:
self.run_run_plus(fun='fileserver.file_list', saltenv=saltenv)
yield
# Test with no arguments
with gen_cache():
ret = self.run_run_plus(fun='fileserver.clear_file_list_cache')
ret['return']['roots'].sort()
self.assertEqual(ret['return'], {'roots': saltenvs})
# Test with backend passed as string
with gen_cache():
ret = self.run_run_plus(fun='fileserver.clear_file_list_cache',
backend='roots')
ret['return']['roots'].sort()
self.assertEqual(ret['return'], {'roots': saltenvs})
# Test with backend passed as list
with gen_cache():
ret = self.run_run_plus(fun='fileserver.clear_file_list_cache',
backend=['roots'])
ret['return']['roots'].sort()
self.assertEqual(ret['return'], {'roots': saltenvs})
# Test with backend passed as string, but with a nonsense backend
with gen_cache():
ret = self.run_run_plus(fun='fileserver.clear_file_list_cache',
backend='notarealbackend')
self.assertEqual(ret['return'], {})
# Test with saltenv passed as string
with gen_cache():
ret = self.run_run_plus(fun='fileserver.clear_file_list_cache',
saltenv='base')
self.assertEqual(ret['return'], {'roots': ['base']})
# Test with saltenv passed as list
with gen_cache():
ret = self.run_run_plus(fun='fileserver.clear_file_list_cache',
saltenv=['base'])
self.assertEqual(ret['return'], {'roots': ['base']})
# Test with saltenv passed as string, but with a nonsense saltenv
with gen_cache():
ret = self.run_run_plus(fun='fileserver.clear_file_list_cache',
saltenv='notarealsaltenv')
self.assertEqual(ret['return'], {})
# Test with both backend and saltenv passed
with gen_cache():
ret = self.run_run_plus(fun='fileserver.clear_file_list_cache',
backend='roots',
saltenv='base')
self.assertEqual(ret['return'], {'roots': ['base']})
def test_file_list(self):
'''
fileserver.file_list
'''
ret = self.run_run_plus(fun='fileserver.file_list')
self.assertIsInstance(ret['return'], list)
self.assertTrue('grail/scene33' in ret['return'])
# Backend submitted as a string
ret = self.run_run_plus(fun='fileserver.file_list', backend='roots')
self.assertIsInstance(ret['return'], list)
self.assertTrue('grail/scene33' in ret['return'])
# Backend submitted as a list
ret = self.run_run_plus(fun='fileserver.file_list', backend=['roots'])
self.assertIsInstance(ret['return'], list)
self.assertTrue('grail/scene33' in ret['return'])
# Git doesn't handle symlinks in Windows. See the thread below:
# http://stackoverflow.com/questions/5917249/git-symlinks-in-windows
@skipIf(salt.utils.platform.is_windows(),
'Git for Windows does not preserve symbolic links when cloning')
def test_symlink_list(self):
'''
fileserver.symlink_list
'''
ret = self.run_run_plus(fun='fileserver.symlink_list')
self.assertIsInstance(ret['return'], dict)
self.assertTrue('dest_sym' in ret['return'])
# Backend submitted as a string
ret = self.run_run_plus(fun='fileserver.symlink_list', backend='roots')
self.assertIsInstance(ret['return'], dict)
self.assertTrue('dest_sym' in ret['return'])
# Backend submitted as a list
ret = self.run_run_plus(fun='fileserver.symlink_list', backend=['roots'])
self.assertIsInstance(ret['return'], dict)
self.assertTrue('dest_sym' in ret['return'])
def test_update(self):
'''
fileserver.update
'''
ret = self.run_run_plus(fun='fileserver.update')
self.assertTrue(ret['return'])
# Backend submitted as a string
ret = self.run_run_plus(fun='fileserver.update', backend='roots')
self.assertTrue(ret['return'])
# Backend submitted as a list
ret = self.run_run_plus(fun='fileserver.update', backend=['roots'])
self.assertTrue(ret['return'])