2012-09-30 11:20:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-12-11 10:23:37 +00:00
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Pedro Algarvio (pedro@algarvio.me)
|
2013-09-16 16:24:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
tests.integration.states.virtualenv
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-12-11 10:23:37 +00:00
|
|
|
'''
|
2012-09-30 11:20:38 +00:00
|
|
|
|
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 Python libs
|
2018-01-19 20:59:53 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2012-09-30 11:20:38 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf
|
2017-04-04 17:57:27 +00:00
|
|
|
from tests.support.helpers import destructiveTest, skip_if_not_root
|
2017-04-02 16:09:47 +00:00
|
|
|
from tests.support.mixins import SaltReturnAssertsMixin
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.runtests import RUNTIME_VARS
|
2013-06-27 12:58:08 +00:00
|
|
|
|
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 libs
|
2017-07-18 16:31:01 +00:00
|
|
|
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.path
|
|
|
|
import salt.utils.platform
|
2014-01-11 19:19:29 +00:00
|
|
|
from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
|
2012-09-30 11:20:38 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
@skipIf(salt.utils.path.which_bin(KNOWN_BINARY_NAMES) is None, 'virtualenv not installed')
|
2017-04-03 16:04:09 +00:00
|
|
|
class VirtualenvTest(ModuleCase, SaltReturnAssertsMixin):
|
2018-09-05 21:49:07 +00:00
|
|
|
@skipIf(salt.utils.platform.is_darwin(), 'Test is flaky on macosx')
|
2012-09-30 11:20:38 +00:00
|
|
|
@destructiveTest
|
2017-04-04 17:57:27 +00:00
|
|
|
@skip_if_not_root
|
2012-09-30 11:20:38 +00:00
|
|
|
def test_issue_1959_virtualenv_runas(self):
|
|
|
|
user = 'issue-1959'
|
2017-03-15 22:18:25 +00:00
|
|
|
self.assertSaltTrueReturn(self.run_state('user.present', name=user))
|
2012-09-30 11:20:38 +00:00
|
|
|
|
|
|
|
uinfo = self.run_function('user.info', [user])
|
|
|
|
|
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
|
|
|
if salt.utils.platform.is_darwin():
|
2017-06-06 19:28:02 +00:00
|
|
|
# MacOS does not support createhome with user.present
|
|
|
|
self.assertSaltTrueReturn(self.run_state('file.directory', name=uinfo['home'], user=user, group=uinfo['groups'][0], dir_mode=755))
|
|
|
|
|
2012-11-06 12:44:53 +00:00
|
|
|
venv_dir = os.path.join(
|
2017-04-04 23:34:39 +00:00
|
|
|
RUNTIME_VARS.SYS_TMP_DIR, 'issue-1959-virtualenv-runas'
|
2012-11-06 12:44:53 +00:00
|
|
|
)
|
2012-09-30 11:20:38 +00:00
|
|
|
try:
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.sls', mods='issue-1959-virtualenv-runas'
|
|
|
|
)
|
2012-11-21 12:19:18 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-09-30 11:20:38 +00:00
|
|
|
|
|
|
|
# Lets check proper ownership
|
|
|
|
statinfo = self.run_function('file.stats', [venv_dir])
|
|
|
|
self.assertEqual(statinfo['user'], uinfo['name'])
|
|
|
|
self.assertEqual(statinfo['uid'], uinfo['uid'])
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
2017-04-04 23:51:35 +00:00
|
|
|
self.assertSaltTrueReturn(self.run_state('user.absent', name=user, purge=True))
|
2012-12-12 18:29:25 +00:00
|
|
|
|
|
|
|
def test_issue_2594_non_invalidated_cache(self):
|
|
|
|
# Testing virtualenv directory
|
2017-04-03 16:04:09 +00:00
|
|
|
venv_path = os.path.join(RUNTIME_VARS.TMP, 'issue-2594-ve')
|
2012-12-12 18:29:25 +00:00
|
|
|
if os.path.exists(venv_path):
|
|
|
|
shutil.rmtree(venv_path)
|
|
|
|
# Our virtualenv requirements file
|
|
|
|
requirements_file_path = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP_STATE_TREE, 'issue-2594-requirements.txt'
|
2012-12-12 18:29:25 +00:00
|
|
|
)
|
|
|
|
if os.path.exists(requirements_file_path):
|
|
|
|
os.unlink(requirements_file_path)
|
|
|
|
|
2013-04-25 15:36:19 +00:00
|
|
|
# Our state template
|
2012-12-12 18:29:25 +00:00
|
|
|
template = [
|
|
|
|
'{0}:'.format(venv_path),
|
|
|
|
' virtualenv.managed:',
|
2013-07-12 15:07:55 +00:00
|
|
|
' - system_site_packages: False',
|
2012-12-12 18:29:25 +00:00
|
|
|
' - clear: false',
|
|
|
|
' - requirements: salt://issue-2594-requirements.txt',
|
|
|
|
]
|
|
|
|
|
|
|
|
# Let's populate the requirements file, just pep-8 for now
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(requirements_file_path, 'a') as fhw:
|
2014-11-26 18:02:24 +00:00
|
|
|
fhw.write('pep8==1.3.3\n')
|
2012-12-12 18:29:25 +00:00
|
|
|
|
|
|
|
# Let's run our state!!!
|
|
|
|
try:
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.template_str', ['\n'.join(template)]
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2013-08-18 04:46:33 +00:00
|
|
|
self.assertInSaltComment('Created new virtualenv', ret)
|
2012-12-12 18:29:25 +00:00
|
|
|
self.assertSaltStateChangesEqual(
|
|
|
|
ret, ['pep8==1.3.3'], keys=('packages', 'new')
|
|
|
|
)
|
|
|
|
except AssertionError:
|
|
|
|
# Always clean up the tests temp files
|
|
|
|
if os.path.exists(venv_path):
|
|
|
|
shutil.rmtree(venv_path)
|
|
|
|
if os.path.exists(requirements_file_path):
|
|
|
|
os.unlink(requirements_file_path)
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Let's make sure, it really got installed
|
|
|
|
ret = self.run_function('pip.freeze', bin_env=venv_path)
|
|
|
|
self.assertIn('pep8==1.3.3', ret)
|
|
|
|
self.assertNotIn('zope.interface==4.0.1', ret)
|
|
|
|
|
|
|
|
# Now let's update the requirements file, which is now cached.
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(requirements_file_path, 'w') as fhw:
|
2014-11-26 18:02:24 +00:00
|
|
|
fhw.write('zope.interface==4.0.1\n')
|
2012-12-12 18:29:25 +00:00
|
|
|
|
|
|
|
# Let's run our state!!!
|
|
|
|
try:
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.template_str', ['\n'.join(template)]
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2013-08-18 04:46:33 +00:00
|
|
|
self.assertInSaltComment('virtualenv exists', ret)
|
2012-12-12 18:29:25 +00:00
|
|
|
self.assertSaltStateChangesEqual(
|
|
|
|
ret, ['zope.interface==4.0.1'], keys=('packages', 'new')
|
|
|
|
)
|
|
|
|
except AssertionError:
|
|
|
|
# Always clean up the tests temp files
|
|
|
|
if os.path.exists(venv_path):
|
|
|
|
shutil.rmtree(venv_path)
|
|
|
|
if os.path.exists(requirements_file_path):
|
|
|
|
os.unlink(requirements_file_path)
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Let's make sure, it really got installed
|
|
|
|
ret = self.run_function('pip.freeze', bin_env=venv_path)
|
|
|
|
self.assertIn('pep8==1.3.3', ret)
|
|
|
|
self.assertIn('zope.interface==4.0.1', ret)
|
|
|
|
|
|
|
|
# If we reached this point no assertion failed, so, cleanup!
|
|
|
|
if os.path.exists(venv_path):
|
|
|
|
shutil.rmtree(venv_path)
|
|
|
|
if os.path.exists(requirements_file_path):
|
|
|
|
os.unlink(requirements_file_path)
|