fixes saltstack/salt#62942 systemd_service.* functions hard code relative command name

This commit is contained in:
nicholasmhughes 2022-10-24 10:43:20 -04:00
parent 609e5e1837
commit bfca671ac9
No known key found for this signature in database
GPG key ID: CB87254A2EA67E01
3 changed files with 239 additions and 224 deletions

1
changelog/62942.fixed Normal file
View file

@ -0,0 +1 @@
Fix systemd_service.* functions hard code relative command name

View file

@ -305,7 +305,9 @@ def _runlevel():
contextkey = "systemd._runlevel"
if contextkey in __context__:
return __context__[contextkey]
out = __salt__["cmd.run"]("runlevel", python_shell=False, ignore_retcode=True)
out = __salt__["cmd.run"](
salt.utils.path.which("runlevel"), python_shell=False, ignore_retcode=True
)
try:
ret = out.split()[1]
except IndexError:
@ -338,8 +340,8 @@ def _systemctl_cmd(action, name=None, systemd_scope=False, no_block=False, root=
and salt.utils.systemd.has_scope(__context__)
and __salt__["config.get"]("systemd.scope", True)
):
ret.extend(["systemd-run", "--scope"])
ret.append("systemctl")
ret.extend([salt.utils.path.which("systemd-run"), "--scope"])
ret.append(salt.utils.path.which("systemctl"))
if no_block:
ret.append("--no-block")
if root:
@ -1440,7 +1442,7 @@ def firstboot(
salt '*' service.firstboot keymap=jp locale=en_US.UTF-8
"""
cmd = ["systemd-firstboot"]
cmd = [salt.utils.path.which("systemd-firstboot")]
parameters = [
("locale", locale),
("locale-message", locale_message),

View file

@ -361,16 +361,17 @@ class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
func = getattr(systemd, action)
# Remove trailing _ in "reload_"
action = action.rstrip("_").replace("_", "-")
systemctl_command = ["systemctl"]
systemctl_command = ["/bin/systemctl"]
if no_block:
systemctl_command.append("--no-block")
systemctl_command.extend([action, self.unit_name + ".service"])
scope_prefix = ["systemd-run", "--scope"]
scope_prefix = ["/bin/systemd-run", "--scope"]
assert_kwargs = {"python_shell": False}
if action in ("enable", "disable"):
assert_kwargs["ignore_retcode"] = True
with patch("salt.utils.path.which", lambda x: "/bin/" + x):
with patch.object(systemd, "_check_for_unit_changes", self.mock_none):
with patch.object(systemd, "_unit_file_changed", self.mock_none):
with patch.object(systemd, "_check_unmask", self.mock_none):
@ -394,7 +395,8 @@ class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
ret = func(self.unit_name, no_block=no_block)
self.assertTrue(ret)
self.mock_run_all_success.assert_called_with(
scope_prefix + systemctl_command, **assert_kwargs
scope_prefix + systemctl_command,
**assert_kwargs
)
# Scope enabled, failed
@ -416,7 +418,8 @@ class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
no_block=no_block,
)
self.mock_run_all_failure.assert_called_with(
scope_prefix + systemctl_command, **assert_kwargs
scope_prefix + systemctl_command,
**assert_kwargs
)
# Scope disabled, successful
@ -489,7 +492,9 @@ class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
},
):
if action in ("stop", "disable"):
ret = func(self.unit_name, no_block=no_block)
ret = func(
self.unit_name, no_block=no_block
)
self.assertFalse(ret)
else:
self.assertRaises(
@ -512,16 +517,17 @@ class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
func = getattr(systemd, action)
# Remove trailing _ in "unmask_"
action = action.rstrip("_").replace("_", "-")
systemctl_command = ["systemctl", action]
systemctl_command = ["/bin/systemctl", action]
if runtime:
systemctl_command.append("--runtime")
systemctl_command.append(self.unit_name + ".service")
scope_prefix = ["systemd-run", "--scope"]
scope_prefix = ["/bin/systemd-run", "--scope"]
args = [self.unit_name, runtime]
masked_mock = self.mock_true if action == "unmask" else self.mock_false
with patch("salt.utils.path.which", lambda x: "/bin/" + x):
with patch.object(systemd, "_check_for_unit_changes", self.mock_none):
if action == "unmask":
mock_not_run = MagicMock(
@ -586,7 +592,9 @@ class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
ret = func(*args)
self.assertTrue(ret)
self.mock_run_all_success.assert_called_with(
systemctl_command, python_shell=False, redirect_stderr=True
systemctl_command,
python_shell=False,
redirect_stderr=True,
)
# Scope disabled, failed
@ -599,7 +607,9 @@ class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
):
self.assertRaises(CommandExecutionError, func, *args)
self.mock_run_all_failure.assert_called_with(
systemctl_command, python_shell=False, redirect_stderr=True
systemctl_command,
python_shell=False,
redirect_stderr=True,
)
# Does not have scopes available
@ -690,9 +700,10 @@ class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
salt_mock = {
"cmd.run_all": MagicMock(return_value=result),
}
with patch("salt.utils.path.which", lambda x: "/bin/" + x):
with patch.dict(systemd.__salt__, salt_mock):
assert systemd.firstboot()
salt_mock["cmd.run_all"].assert_called_with(["systemd-firstboot"])
salt_mock["cmd.run_all"].assert_called_with(["/bin/systemd-firstboot"])
def test_firstboot_params(self):
"""
@ -702,6 +713,7 @@ class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
salt_mock = {
"cmd.run_all": MagicMock(return_value=result),
}
with patch("salt.utils.path.which", lambda x: "/bin/" + x):
with patch.dict(systemd.__salt__, salt_mock):
assert systemd.firstboot(
locale="en_US.UTF-8",
@ -714,7 +726,7 @@ class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
)
salt_mock["cmd.run_all"].assert_called_with(
[
"systemd-firstboot",
"/bin/systemd-firstboot",
"--locale",
"en_US.UTF-8",
"--locale-message",