Always cleanup the changed environ

This commit is contained in:
Pedro Algarvio 2019-04-30 18:21:32 +01:00
parent 087c74d907
commit da2e147e54
No known key found for this signature in database
GPG Key ID: BB36BF6584A298FF
4 changed files with 67 additions and 69 deletions

View File

@ -32,9 +32,9 @@ class PipModuleTest(ModuleCase):
super(PipModuleTest, self).setUp()
# Restore the environ
def cleanup_environ(envcopy):
def cleanup_environ(environ):
os.environ.clear()
os.environ.update(envcopy)
os.environ.update(environ)
self.addCleanup(cleanup_environ, os.environ.copy())

View File

@ -123,37 +123,34 @@ class PipStateTest(ModuleCase, SaltReturnAssertsMixin):
venv_dir = os.path.join(
RUNTIME_VARS.TMP, 'pip-installed-errors'
)
orig_shell = os.environ.get('SHELL')
try:
# Since we don't have the virtualenv created, pip.installed will
# throw an error.
# Example error strings:
# * "Error installing 'pep8': /tmp/pip-installed-errors: not found"
# * "Error installing 'pep8': /bin/sh: 1: /tmp/pip-installed-errors: not found"
# * "Error installing 'pep8': /bin/bash: /tmp/pip-installed-errors: No such file or directory"
os.environ['SHELL'] = '/bin/sh'
ret = self.run_function('state.sls', mods='pip-installed-errors')
self.assertSaltFalseReturn(ret)
self.assertSaltCommentRegexpMatches(
ret,
'Error installing \'pep8\':'
)
# We now create the missing virtualenv
ret = self._create_virtualenv(venv_dir)
self.assertEqual(ret['retcode'], 0)
def cleanup_environ(environ):
os.environ.clear()
os.environ.update(environ)
# The state should not have any issues running now
ret = self.run_function('state.sls', mods='pip-installed-errors')
self.assertSaltTrueReturn(ret)
finally:
if orig_shell is None:
# Didn't exist before, don't leave it there. This should never
# happen, but if it does, we don't want this test to affect
# others elsewhere in the suite.
os.environ.pop('SHELL')
else:
os.environ['SHELL'] = orig_shell
self.addCleanup(cleanup_environ, os.environ.copy())
# Since we don't have the virtualenv created, pip.installed will
# throw an error.
# Example error strings:
# * "Error installing 'pep8': /tmp/pip-installed-errors: not found"
# * "Error installing 'pep8': /bin/sh: 1: /tmp/pip-installed-errors: not found"
# * "Error installing 'pep8': /bin/bash: /tmp/pip-installed-errors: No such file or directory"
os.environ['SHELL'] = '/bin/sh'
ret = self.run_function('state.sls', mods='pip-installed-errors')
self.assertSaltFalseReturn(ret)
self.assertSaltCommentRegexpMatches(
ret,
'Error installing \'pep8\':'
)
# We now create the missing virtualenv
ret = self._create_virtualenv(venv_dir)
self.assertEqual(ret['retcode'], 0)
# The state should not have any issues running now
ret = self.run_function('state.sls', mods='pip-installed-errors')
self.assertSaltTrueReturn(ret)
@skipIf(six.PY3, 'Issue is specific to carbon module, which is PY2-only')
@skipIf(salt.utils.platform.is_windows(), "Carbon does not install in Windows")

View File

@ -70,7 +70,6 @@ _OPTS = {
'git_pillar_includes': True,
}
PROC_TIMEOUT = 10
NOTSET = object()
class ProcessManager(object):
@ -634,14 +633,15 @@ class GitPillarSSHTestBase(GitPillarTestBase, SSHDMixin):
passphraselsess key is used to auth without needing to modify the root
user's ssh config file.
'''
orig_git_ssh = os.environ.pop('GIT_SSH', NOTSET)
def cleanup_environ(environ):
os.environ.clear()
os.environ.update(environ)
self.addCleanup(cleanup_environ, os.environ.copy())
os.environ['GIT_SSH'] = self.git_ssh
try:
return super(GitPillarSSHTestBase, self).get_pillar(ext_pillar_conf)
finally:
os.environ.pop('GIT_SSH', None)
if orig_git_ssh is not NOTSET:
os.environ['GIT_SSH'] = orig_git_ssh
return super(GitPillarSSHTestBase, self).get_pillar(ext_pillar_conf)
class GitPillarHTTPTestBase(GitPillarTestBase, WebserverMixin):

View File

@ -405,39 +405,40 @@ class GitFSTestBase(object):
username_key = str('USERNAME')
orig_username = os.environ.get(username_key)
try:
if username_key not in os.environ:
try:
if salt.utils.platform.is_windows():
os.environ[username_key] = \
salt.utils.win_functions.get_current_user()
else:
os.environ[username_key] = \
pwd.getpwuid(os.geteuid()).pw_name
except AttributeError:
log.error(
'Unable to get effective username, falling back to '
'\'root\'.'
)
os.environ[username_key] = str('root')
if username_key not in os.environ:
repo.index.add([x for x in os.listdir(TMP_REPO_DIR)
if x != '.git'])
repo.index.commit('Test')
def cleanup_environ(environ):
os.environ.clear()
os.environ.update(environ)
# Add another branch with unicode characters in the name
repo.create_head(UNICODE_ENVNAME, 'HEAD')
self.addCleanup(cleanup_environ, os.environ.copy())
# Add a tag
repo.create_tag(TAG_NAME, 'HEAD')
# Older GitPython versions do not have a close method.
if hasattr(repo, 'close'):
repo.close()
finally:
if orig_username is not None:
os.environ[username_key] = orig_username
else:
os.environ.pop(username_key, None)
try:
if salt.utils.platform.is_windows():
os.environ[username_key] = \
salt.utils.win_functions.get_current_user()
else:
os.environ[username_key] = \
pwd.getpwuid(os.geteuid()).pw_name
except AttributeError:
log.error(
'Unable to get effective username, falling back to '
'\'root\'.'
)
os.environ[username_key] = str('root')
repo.index.add([x for x in os.listdir(TMP_REPO_DIR)
if x != '.git'])
repo.index.commit('Test')
# Add another branch with unicode characters in the name
repo.create_head(UNICODE_ENVNAME, 'HEAD')
# Add a tag
repo.create_tag(TAG_NAME, 'HEAD')
# Older GitPython versions do not have a close method.
if hasattr(repo, 'close'):
repo.close()
@classmethod
def tearDownClass(cls):