mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Cleaned up tests and removal of debug statements
This commit is contained in:
parent
695353ee91
commit
21cf213b10
3 changed files with 71 additions and 104 deletions
|
@ -249,11 +249,8 @@ class GitProvider:
|
|||
def _val_cb(x, y):
|
||||
return str(y)
|
||||
|
||||
# DGM try getting machine_identifier
|
||||
# get machine_identifier
|
||||
## self.mach_id = salt.utils.files.get_machine_identifier()
|
||||
self.mach_id = get_machine_id().get("machine_id", "no_machine_id_available")
|
||||
log.debug("DGM machine_id for lock file, machine_id %s", self.mach_id)
|
||||
|
||||
self.global_saltenv = salt.utils.data.repack_dictlist(
|
||||
self.opts.get(f"{self.role}_saltenv", []),
|
||||
|
|
|
@ -1076,12 +1076,10 @@ class SignalHandlingProcess(Process):
|
|||
msg += ". Exiting"
|
||||
log.debug(msg)
|
||||
|
||||
## mach_id = salt.utils.files.get_machine_identifier()
|
||||
mach_id = get_machine_id().get("machine_id", "no_machine_id_available")
|
||||
dbg_msg = (
|
||||
f"exiting for process id '{os.getpid()}' and machine identifer '{mach_id}'"
|
||||
log.debug(
|
||||
"exiting for process id %s and machine identifer %s", os.getpid(), mach_id
|
||||
)
|
||||
log.debug(dbg_msg)
|
||||
|
||||
cur_pid = os.getpid()
|
||||
if HAS_PSUTIL:
|
||||
|
|
|
@ -7,7 +7,6 @@ import logging
|
|||
import os
|
||||
import pathlib
|
||||
import signal
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
@ -41,44 +40,6 @@ def _get_user():
|
|||
return pwd.getpwuid(os.getuid())[0]
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def _factory_root_dir(salt_factories):
|
||||
return salt_factories.root_dir.resolve()
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def _salt_master_factory_config_parent(salt_master_factory):
|
||||
return pathlib.PurePath(salt_master_factory.config["conf_file"]).parent
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def _salt_master_factory_config_path(salt_master_factory):
|
||||
return pathlib.PurePath(salt_master_factory.config["conf_file"]).parent.joinpath(
|
||||
"master"
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def _salt_minion_factory_config_path(salt_minion_factory):
|
||||
return pathlib.PurePath(salt_minion_factory.config["conf_file"]).parent.joinpath(
|
||||
"minion"
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def _create_old_tempdir(_factory_root_dir):
|
||||
return pathlib.Path(str(_factory_root_dir)).mkdir(exist_ok=True, parents=True)
|
||||
|
||||
|
||||
## @pytest.fixture
|
||||
## def get_tmp_dir(tmp_path):
|
||||
## dirpath = tmp_path / "git_test"
|
||||
## dirpath.mkdir(parents=True)
|
||||
## return dirpath
|
||||
##
|
||||
## ## dirpath.cleanup()
|
||||
|
||||
|
||||
def _clear_instance_map():
|
||||
try:
|
||||
del salt.utils.gitfs.GitFS.instance_map[
|
||||
|
@ -90,30 +51,41 @@ def _clear_instance_map():
|
|||
|
||||
class AdaptedConfigurationTestCaseMixin:
|
||||
|
||||
@staticmethod
|
||||
def get_temp_config(config_for, _factory_root_dir, **config_overrides):
|
||||
def __init__(
|
||||
self,
|
||||
salt_factories,
|
||||
salt_master_factory,
|
||||
salt_minion_factory,
|
||||
tmp_path,
|
||||
):
|
||||
self._tmp_name = str(tmp_path)
|
||||
|
||||
rootdir = config_overrides.get("root_dir", str(_factory_root_dir))
|
||||
self._master_cfg = str(salt_master_factory.config["conf_file"])
|
||||
self._minion_cfg = str(salt_minion_factory.config["conf_file"])
|
||||
self._root_dir = str(salt_factories.root_dir.resolve())
|
||||
self._user = _get_user()
|
||||
|
||||
def get_temp_config(self, config_for, **config_overrides):
|
||||
|
||||
rootdir = config_overrides.get("root_dir", self._root_dir)
|
||||
|
||||
if not pathlib.Path(rootdir).exists():
|
||||
pathlib.Path(str(_factory_root_dir)).mkdir(exist_ok=True, parents=True)
|
||||
pathlib.Path(rootdir).mkdir(exist_ok=True, parents=True)
|
||||
|
||||
rootdir = config_overrides.get("root_dir", str(_factory_root_dir))
|
||||
conf_dir = config_overrides.pop(
|
||||
"conf_dir", str(pathlib.PurePath(rootdir).joinpath("conf"))
|
||||
)
|
||||
|
||||
curr_user = _get_user()
|
||||
for key in ("cachedir", "pki_dir", "sock_dir"):
|
||||
if key not in config_overrides:
|
||||
config_overrides[key] = key
|
||||
if "log_file" not in config_overrides:
|
||||
config_overrides["log_file"] = f"logs/{config_for}.log".format()
|
||||
if "user" not in config_overrides:
|
||||
config_overrides["user"] = curr_user
|
||||
config_overrides["user"] = self._user
|
||||
config_overrides["root_dir"] = rootdir
|
||||
|
||||
cdict = AdaptedConfigurationTestCaseMixin.get_config(
|
||||
cdict = self.get_config(
|
||||
config_for,
|
||||
from_scratch=True,
|
||||
)
|
||||
|
@ -149,7 +121,7 @@ class AdaptedConfigurationTestCaseMixin:
|
|||
rdict["sock_dir"],
|
||||
conf_dir,
|
||||
],
|
||||
curr_user,
|
||||
self._user,
|
||||
root_dir=rdict["root_dir"],
|
||||
)
|
||||
|
||||
|
@ -158,31 +130,25 @@ class AdaptedConfigurationTestCaseMixin:
|
|||
salt.utils.yaml.safe_dump(rdict, wfh, default_flow_style=False)
|
||||
return rdict
|
||||
|
||||
@staticmethod
|
||||
def get_config(
|
||||
self,
|
||||
config_for,
|
||||
from_scratch=False,
|
||||
):
|
||||
if from_scratch:
|
||||
if config_for in ("master"):
|
||||
return salt.config.master_config(str(_salt_master_factory_config_path))
|
||||
return salt.config.master_config(self._master_cfg)
|
||||
elif config_for in ("minion"):
|
||||
return salt.config.minion_config(str(_salt_minion_factory_config_path))
|
||||
return salt.config.minion_config(self._minion_cfg)
|
||||
elif config_for == "client_config":
|
||||
return salt.config_client_config(str(_salt_master_factory_config_path))
|
||||
return salt.config_client_config(self._master_cfg)
|
||||
if config_for not in ("master", "minion", "client_config"):
|
||||
if config_for in ("master"):
|
||||
return freeze(
|
||||
salt.config.master_config(str(_salt_master_factory_config_path))
|
||||
)
|
||||
return freeze(salt.config.master_config(self._master_cfg))
|
||||
elif config_for in ("minion"):
|
||||
return freeze(
|
||||
salt.config.minion_config(str(_salt_minion_factory_config_path))
|
||||
)
|
||||
return freeze(salt.config.minion_config(self._minion_cfg))
|
||||
elif config_for == "client_config":
|
||||
return freeze(
|
||||
salt.config.client_config(str(_salt_master_factory_config_path))
|
||||
)
|
||||
return freeze(salt.config.client_config(self._master_cfg))
|
||||
|
||||
log.error(
|
||||
"Should not reach this section of code for get_config, missing support for input config_for %s",
|
||||
|
@ -190,25 +156,24 @@ class AdaptedConfigurationTestCaseMixin:
|
|||
)
|
||||
|
||||
# at least return master's config
|
||||
return freeze(salt.config.master_config(str(_salt_master_factory_config_path)))
|
||||
return freeze(salt.config.master_config(self._master_cfg))
|
||||
|
||||
@property
|
||||
def config_dir(self):
|
||||
return str(_salt_master_factory_config_parent)
|
||||
return str(pathlib.PurePath(self._master_cfg).parent)
|
||||
|
||||
def get_config_dir(self):
|
||||
log.warning("Use the config_dir attribute instead of calling get_config_dir()")
|
||||
return self.config_dir
|
||||
|
||||
@staticmethod
|
||||
def get_config_file_path(filename):
|
||||
def get_config_file_path(self, filename):
|
||||
if filename == "master":
|
||||
return str(_salt_master_factory_config_path)
|
||||
return str(self._master_cfg)
|
||||
|
||||
if filename == "minion":
|
||||
return str(_salt_minion_factory_config_path)
|
||||
return str(self._minion_cfg)
|
||||
|
||||
return str(_salt_master_factory_config_path)
|
||||
return str(self._master_cfg)
|
||||
|
||||
@property
|
||||
def master_opts(self):
|
||||
|
@ -224,26 +189,28 @@ class AdaptedConfigurationTestCaseMixin:
|
|||
"""
|
||||
return self.get_config("minion")
|
||||
|
||||
@property
|
||||
def sub_minion_opts(self):
|
||||
"""
|
||||
Return the options used for the sub_minion
|
||||
"""
|
||||
return self.get_config("sub_minion")
|
||||
|
||||
|
||||
class TestGitBase(AdaptedConfigurationTestCaseMixin):
|
||||
class MyGitBase(AdaptedConfigurationTestCaseMixin):
|
||||
"""
|
||||
mocked GitFS provider leveraging tmp_path
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
salt_factories,
|
||||
salt_master_factory,
|
||||
salt_minion_factory,
|
||||
tmp_path,
|
||||
):
|
||||
## self._tmp_dir = pathlib.Path(tmp_path / "git_test").mkdir(exist_ok=True, parents=True)
|
||||
## tmp_name = str(self._tmp_dir)
|
||||
self._tmp_dir = tempfile.TemporaryDirectory()
|
||||
tmp_name = self._tmp_dir.name
|
||||
super().__init__(
|
||||
salt_factories,
|
||||
salt_master_factory,
|
||||
salt_minion_factory,
|
||||
tmp_path,
|
||||
)
|
||||
|
||||
tmp_name = self._tmp_name.join("/git_test")
|
||||
pathlib.Path(tmp_name).mkdir(exist_ok=True, parents=True)
|
||||
|
||||
class MockedProvider(
|
||||
salt.utils.gitfs.GitProvider
|
||||
|
@ -293,7 +260,6 @@ class TestGitBase(AdaptedConfigurationTestCaseMixin):
|
|||
|
||||
self.opts = self.get_temp_config(
|
||||
"master",
|
||||
_factory_root_dir,
|
||||
gitfs_remotes=gitfs_remotes,
|
||||
verified_gitfs_provider="mocked",
|
||||
)
|
||||
|
@ -305,21 +271,29 @@ class TestGitBase(AdaptedConfigurationTestCaseMixin):
|
|||
git_providers=git_providers,
|
||||
)
|
||||
|
||||
# DGM TBD do we need this, look at removing
|
||||
def tearDown(self):
|
||||
def cleanup(self):
|
||||
# Providers are preserved with GitFS's instance_map
|
||||
for remote in self.main_class.remotes:
|
||||
remote.fetched = False
|
||||
del self.main_class
|
||||
## self._tmp_dir.cleanup()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def main_class(tmp_path):
|
||||
test_git_base = TestGitBase()
|
||||
yield test_git_base.main_class
|
||||
def main_class(
|
||||
salt_factories,
|
||||
salt_master_factory,
|
||||
salt_minion_factory,
|
||||
tmp_path,
|
||||
):
|
||||
my_git_base = MyGitBase(
|
||||
salt_factories,
|
||||
salt_master_factory,
|
||||
salt_minion_factory,
|
||||
tmp_path,
|
||||
)
|
||||
yield my_git_base.main_class
|
||||
|
||||
test_git_base.tearDown()
|
||||
my_git_base.cleanup()
|
||||
|
||||
|
||||
def test_update_all(main_class):
|
||||
|
@ -403,9 +377,7 @@ def test_git_provider_mp_gen_lock(main_class, caplog):
|
|||
"""
|
||||
Check that gen_lock is obtains lock, and then releases, provider.lock()
|
||||
"""
|
||||
# DGM try getting machine_identifier
|
||||
# get machine_identifier
|
||||
## mach_id = salt.utils.files.get_machine_identifier()
|
||||
mach_id = get_machine_id().get("machine_id", "no_machine_id_available")
|
||||
cur_pid = os.getpid()
|
||||
|
||||
|
@ -430,6 +402,8 @@ def test_git_provider_mp_gen_lock(main_class, caplog):
|
|||
assert test_msg2 in caplog.text
|
||||
assert test_msg3 in caplog.text
|
||||
|
||||
caplog.clear()
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
@pytest.mark.timeout_unless_on_windows(120)
|
||||
|
@ -437,9 +411,7 @@ def test_git_provider_mp_lock_dead_pid(main_class, caplog):
|
|||
"""
|
||||
Check that lock obtains lock, if previous pid in lock file doesn't exist for same machine id
|
||||
"""
|
||||
# DGM try getting machine_identifier
|
||||
# get machine_identifier
|
||||
## mach_id = salt.utils.files.get_machine_identifier()
|
||||
mach_id = get_machine_id().get("machine_id", "no_machine_id_available")
|
||||
cur_pid = os.getpid()
|
||||
|
||||
|
@ -487,6 +459,7 @@ def test_git_provider_mp_lock_dead_pid(main_class, caplog):
|
|||
|
||||
provider._master_lock.release()
|
||||
|
||||
caplog.clear()
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
provider.lock()
|
||||
# check that lock has been released
|
||||
|
@ -501,6 +474,7 @@ def test_git_provider_mp_lock_dead_pid(main_class, caplog):
|
|||
assert test_msg1 in caplog.text
|
||||
assert test_msg2 in caplog.text
|
||||
assert test_msg3 in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
|
@ -509,9 +483,7 @@ def test_git_provider_mp_lock_bad_machine(main_class, caplog):
|
|||
"""
|
||||
Check that lock obtains lock, if previous pid in lock file doesn't exist for same machine id
|
||||
"""
|
||||
# DGM try getting machine_identifier
|
||||
# get machine_identifier
|
||||
## mach_id = salt.utils.files.get_machine_identifier()
|
||||
mach_id = get_machine_id().get("machine_id", "no_machine_id_available")
|
||||
cur_pid = os.getpid()
|
||||
|
||||
|
@ -553,6 +525,7 @@ def test_git_provider_mp_lock_bad_machine(main_class, caplog):
|
|||
|
||||
provider._master_lock.release()
|
||||
|
||||
caplog.clear()
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
provider.lock()
|
||||
# check that lock has been released
|
||||
|
@ -566,6 +539,7 @@ def test_git_provider_mp_lock_bad_machine(main_class, caplog):
|
|||
|
||||
assert test_msg1 in caplog.text
|
||||
assert test_msg2 in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
|
||||
class KillProcessTest(salt.utils.process.SignalHandlingProcess):
|
||||
|
@ -592,12 +566,10 @@ class KillProcessTest(salt.utils.process.SignalHandlingProcess):
|
|||
tsleep = 1
|
||||
time.sleep(tsleep) # give time for kill by sigterm
|
||||
|
||||
log.debug("DGM kill_test_process exit")
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
@pytest.mark.skip_unless_on_linux
|
||||
def test_git_provider_sigterm_cleanup(main_class, caplog):
|
||||
def test_git_provider_sigterm_cleanup(main_class):
|
||||
"""
|
||||
Start process which will obtain lock, and leave it locked
|
||||
then kill the process via SIGTERM and ensure locked resources are cleaned up
|
||||
|
|
Loading…
Add table
Reference in a new issue