mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Implement change_cwd
context manager and use it.
This commit is contained in:
parent
b6c78ecba6
commit
dd951758e8
2 changed files with 54 additions and 33 deletions
|
@ -22,7 +22,7 @@ import salt.utils.yaml
|
|||
from salt.ext import six
|
||||
from tests.integration.utils import testprogram
|
||||
from tests.support.case import ShellCase
|
||||
from tests.support.helpers import flaky, with_tempfile
|
||||
from tests.support.helpers import change_cwd, flaky, with_tempfile
|
||||
from tests.support.mixins import ShellCaseCommonTestsMixin
|
||||
from tests.support.runtests import RUNTIME_VARS
|
||||
from tests.support.unit import skipIf
|
||||
|
@ -164,42 +164,45 @@ class CallTest(ShellCase, testprogram.TestProgramCase, ShellCaseCommonTestsMixin
|
|||
"""
|
||||
test when log_file is set to a syslog file that does not exist
|
||||
"""
|
||||
old_cwd = os.getcwd()
|
||||
config_dir = os.path.join(RUNTIME_VARS.TMP, "log_file_incorrect")
|
||||
if not os.path.isdir(config_dir):
|
||||
os.makedirs(config_dir)
|
||||
|
||||
os.chdir(config_dir)
|
||||
|
||||
with salt.utils.files.fopen(self.get_config_file_path("minion"), "r") as fh_:
|
||||
minion_config = salt.utils.yaml.load(fh_.read())
|
||||
minion_config["log_file"] = "file:///dev/doesnotexist"
|
||||
with salt.utils.files.fopen(os.path.join(config_dir, "minion"), "w") as fh_:
|
||||
fh_.write(salt.utils.yaml.dump(minion_config, default_flow_style=False))
|
||||
ret = self.run_script(
|
||||
"salt-call",
|
||||
'--config-dir {0} cmd.run "echo foo"'.format(config_dir),
|
||||
timeout=120,
|
||||
catch_stderr=True,
|
||||
with_retcode=True,
|
||||
)
|
||||
try:
|
||||
if sys.version_info >= (3, 5, 4):
|
||||
self.assertIn("local:", ret[0])
|
||||
self.assertIn(
|
||||
"[WARNING ] The log_file does not exist. Logging not setup correctly or syslog service not started.",
|
||||
ret[1],
|
||||
)
|
||||
self.assertEqual(ret[2], 0)
|
||||
else:
|
||||
self.assertIn(
|
||||
"Failed to setup the Syslog logging handler", "\n".join(ret[1])
|
||||
)
|
||||
self.assertEqual(ret[2], 2)
|
||||
finally:
|
||||
self.chdir(old_cwd)
|
||||
if os.path.isdir(config_dir):
|
||||
shutil.rmtree(config_dir)
|
||||
with change_cwd(config_dir):
|
||||
with salt.utils.files.fopen(
|
||||
self.get_config_file_path("minion"), "r"
|
||||
) as fh_:
|
||||
minion_config = salt.utils.yaml.load(fh_.read())
|
||||
minion_config["log_file"] = "file:///dev/doesnotexist"
|
||||
with salt.utils.files.fopen(
|
||||
os.path.join(config_dir, "minion"), "w"
|
||||
) as fh_:
|
||||
fh_.write(
|
||||
salt.utils.yaml.dump(minion_config, default_flow_style=False)
|
||||
)
|
||||
ret = self.run_script(
|
||||
"salt-call",
|
||||
'--config-dir {0} cmd.run "echo foo"'.format(config_dir),
|
||||
timeout=120,
|
||||
catch_stderr=True,
|
||||
with_retcode=True,
|
||||
)
|
||||
try:
|
||||
if sys.version_info >= (3, 5, 4):
|
||||
self.assertIn("local:", ret[0])
|
||||
self.assertIn(
|
||||
"[WARNING ] The log_file does not exist. Logging not setup correctly or syslog service not started.",
|
||||
ret[1],
|
||||
)
|
||||
self.assertEqual(ret[2], 0)
|
||||
else:
|
||||
self.assertIn(
|
||||
"Failed to setup the Syslog logging handler", "\n".join(ret[1])
|
||||
)
|
||||
self.assertEqual(ret[2], 2)
|
||||
finally:
|
||||
if os.path.isdir(config_dir):
|
||||
shutil.rmtree(config_dir)
|
||||
|
||||
@skipIf(True, "This test is unreliable. Need to investigate why more deeply.")
|
||||
@flaky
|
||||
|
|
|
@ -31,6 +31,7 @@ import textwrap
|
|||
import threading
|
||||
import time
|
||||
import types
|
||||
from contextlib import contextmanager
|
||||
|
||||
import pytest
|
||||
import salt.ext.tornado.ioloop
|
||||
|
@ -1742,3 +1743,20 @@ class VirtualEnv(object):
|
|||
sminion.functions.virtualenv.create(
|
||||
self.venv_dir, python=self._get_real_python()
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def change_cwd(path):
|
||||
"""
|
||||
Context manager helper to change CWD for a with code block and restore
|
||||
it at the end
|
||||
"""
|
||||
old_cwd = os.getcwd()
|
||||
|
||||
os.chdir(path)
|
||||
|
||||
# Do stuff
|
||||
yield
|
||||
|
||||
# Restore Old CWD
|
||||
os.chdir(old_cwd)
|
||||
|
|
Loading…
Add table
Reference in a new issue