salt/tests/unit/modules/test_djangomod.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

225 lines
7.8 KiB
Python

# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@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 TestCase, skipIf
from tests.support.mock import (
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
# Import Salt Libs
import salt.modules.djangomod as djangomod
@skipIf(NO_MOCK, NO_MOCK_REASON)
class DjangomodTestCase(TestCase, LoaderModuleMockMixin):
'''
Test cases for salt.modules.djangomod
'''
def setup_loader_modules(self):
patcher = patch('salt.utils.path.which', lambda exe: exe)
patcher.start()
self.addCleanup(patcher.stop)
return {djangomod: {'_get_django_admin': MagicMock(return_value=True)}}
# 'command' function tests: 1
def test_command(self):
'''
Test if it runs arbitrary django management command
'''
mock = MagicMock(return_value=True)
with patch.dict(djangomod.__salt__, {'cmd.run': mock}):
self.assertTrue(djangomod.command('DJANGO_SETTINGS_MODULE',
'validate'))
# 'syncdb' function tests: 1
def test_syncdb(self):
'''
Test if it runs the Django-Admin syncdb command
'''
mock = MagicMock(return_value=True)
with patch.dict(djangomod.__salt__, {'cmd.run': mock}):
self.assertTrue(djangomod.syncdb('DJANGO_SETTINGS_MODULE'))
# 'createsuperuser' function tests: 1
def test_createsuperuser(self):
'''
Test if it create a super user for the database.
'''
mock = MagicMock(return_value=True)
with patch.dict(djangomod.__salt__, {'cmd.run': mock}):
self.assertTrue(djangomod.createsuperuser('DJANGO_SETTINGS_MODULE',
'SALT',
'salt@slatstack.com'))
# 'loaddata' function tests: 1
def test_loaddata(self):
'''
Test if it loads fixture data
'''
mock = MagicMock(return_value=True)
with patch.dict(djangomod.__salt__, {'cmd.run': mock}):
self.assertTrue(djangomod.loaddata('DJANGO_SETTINGS_MODULE',
'mydata'))
# 'collectstatic' function tests: 1
def test_collectstatic(self):
'''
Test if it collect static files from each of your applications
into a single location
'''
mock = MagicMock(return_value=True)
with patch.dict(djangomod.__salt__, {'cmd.run': mock}):
self.assertTrue(djangomod.collectstatic('DJANGO_SETTINGS_MODULE'))
@skipIf(NO_MOCK, NO_MOCK_REASON)
class DjangomodCliCommandTestCase(TestCase, LoaderModuleMockMixin):
'''
Test cases for salt.modules.djangomod
'''
def setup_loader_modules(self):
patcher = patch('salt.utils.path.which', lambda exe: exe)
patcher.start()
self.addCleanup(patcher.stop)
return {djangomod: {}}
def test_django_admin_cli_command(self):
mock = MagicMock()
with patch.dict(djangomod.__salt__,
{'cmd.run': mock}):
djangomod.command('settings.py', 'runserver')
mock.assert_called_once_with(
'django-admin.py runserver --settings=settings.py',
python_shell=False,
env=None
)
def test_django_admin_cli_command_with_args(self):
mock = MagicMock()
with patch.dict(djangomod.__salt__,
{'cmd.run': mock}):
djangomod.command(
'settings.py',
'runserver',
None,
None,
None,
'noinput',
'somethingelse'
)
mock.assert_called_once_with(
'django-admin.py runserver --settings=settings.py '
'--noinput --somethingelse',
python_shell=False,
env=None
)
def test_django_admin_cli_command_with_kwargs(self):
mock = MagicMock()
with patch.dict(djangomod.__salt__,
{'cmd.run': mock}):
djangomod.command(
'settings.py',
'runserver',
None,
None,
database='something'
)
mock.assert_called_once_with(
'django-admin.py runserver --settings=settings.py '
'--database=something',
python_shell=False,
env=None
)
def test_django_admin_cli_command_with_kwargs_ignore_dunder(self):
mock = MagicMock()
with patch.dict(djangomod.__salt__,
{'cmd.run': mock}):
djangomod.command(
'settings.py', 'runserver', None, None, __ignore='something'
)
mock.assert_called_once_with(
'django-admin.py runserver --settings=settings.py',
python_shell=False,
env=None
)
def test_django_admin_cli_syncdb(self):
mock = MagicMock()
with patch.dict(djangomod.__salt__,
{'cmd.run': mock}):
djangomod.syncdb('settings.py')
mock.assert_called_once_with(
'django-admin.py syncdb --settings=settings.py --noinput',
python_shell=False,
env=None
)
def test_django_admin_cli_syncdb_migrate(self):
mock = MagicMock()
with patch.dict(djangomod.__salt__,
{'cmd.run': mock}):
djangomod.syncdb('settings.py', migrate=True)
mock.assert_called_once_with(
'django-admin.py syncdb --settings=settings.py --migrate '
'--noinput',
python_shell=False,
env=None
)
def test_django_admin_cli_createsuperuser(self):
mock = MagicMock()
with patch.dict(djangomod.__salt__,
{'cmd.run': mock}):
djangomod.createsuperuser(
'settings.py', 'testuser', 'user@example.com'
)
self.assertEqual(mock.call_count, 1)
args, kwargs = mock.call_args
# cmdline arguments are extracted from a kwargs dict so order isn't guaranteed.
self.assertEqual(len(args), 1)
self.assertTrue(args[0].startswith('django-admin.py createsuperuser --'))
self.assertEqual(set(args[0].split()),
set('django-admin.py createsuperuser --settings=settings.py --noinput '
'--username=testuser --email=user@example.com'.split()))
self.assertDictEqual(kwargs, {'python_shell': False, 'env': None})
def no_test_loaddata(self):
mock = MagicMock()
with patch.dict(djangomod.__salt__,
{'cmd.run': mock}):
djangomod.loaddata('settings.py', 'app1,app2')
mock.assert_called_once_with(
'django-admin.py loaddata --settings=settings.py app1 app2',
)
def test_django_admin_cli_collectstatic(self):
mock = MagicMock()
with patch.dict(djangomod.__salt__,
{'cmd.run': mock}):
djangomod.collectstatic(
'settings.py', None, True, 'something', True, True, True, True
)
mock.assert_called_once_with(
'django-admin.py collectstatic --settings=settings.py '
'--noinput --no-post-process --dry-run --clear --link '
'--no-default-ignore --ignore=something',
python_shell=False,
env=None
)