full patch

This commit is contained in:
cmcmarrow 2020-07-09 13:44:03 -06:00 committed by Daniel Wozniak
parent aa18874223
commit 315dab5614
4 changed files with 61 additions and 41 deletions

View file

@ -70,7 +70,7 @@ from salt.exceptions import CommandExecutionError
# Import 3rd-party libs
from salt.ext import six
from salt.utils.args import get_function_argspec as _argspec
from salt.utils.systemd import HAS_SYSTEMD
from salt.utils.systemd import booted
SYSTEMD_ONLY = ("no_block", "unmask", "unmask_runtime")
@ -95,12 +95,15 @@ def __virtual__():
# Double-asterisk deliberately not used here
def _get_systemd_only(func, kwargs):
if not hasattr(_get_systemd_only, "HAS_SYSTEMD"):
setattr(_get_systemd_only, "HAS_SYSTEMD", booted())
ret = {}
warnings = []
valid_args = _argspec(func).args
for systemd_arg in SYSTEMD_ONLY:
if systemd_arg in kwargs and systemd_arg in valid_args:
if HAS_SYSTEMD:
if _get_systemd_only.HAS_SYSTEMD:
ret[systemd_arg] = kwargs[systemd_arg]
else:
warnings.append(

View file

@ -18,17 +18,6 @@ from salt.exceptions import SaltInvocationError
log = logging.getLogger(__name__)
def _has_systemd_support():
try:
output = subprocess.check_output(("systemctl",), shell=False).decode().lower()
return "systemd" in output
except FileNotFoundError:
return False
HAS_SYSTEMD = _has_systemd_support()
def booted(context=None):
"""
Return True if the system was booted with systemd, False otherwise. If the

View file

@ -29,6 +29,56 @@ class ServiceTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
return {service: {}}
def test_get_systemd_only(self):
def test_func(cats, dogs, no_block):
pass
with patch.object(service._get_systemd_only, "HAS_SYSTEMD", True):
ret, warnings = service._get_systemd_only(
test_func, {"cats": 1, "no_block": 2, "unmask": 3}
)
self.assertEqual(len(warnings), 0)
self.assertEqual(ret, {"no_block": 2})
ret, warnings = service._get_systemd_only(
test_func, {"cats": 1, "unmask": 3}
)
self.assertEqual(len(warnings), 0)
self.assertEqual(ret, {})
def test_get_systemd_only_platform(self):
def test_func(cats, dogs, no_block):
pass
with patch.object(service._get_systemd_only, "HAS_SYSTEMD", False):
ret, warnings = service._get_systemd_only(
test_func, {"cats": 1, "no_block": 2, "unmask": 3}
)
self.assertEqual(
warnings, ["The 'no_block' argument is not supported by this platform"]
)
self.assertEqual(ret, {})
ret, warnings = service._get_systemd_only(
test_func, {"cats": 1, "unmask": 3}
)
self.assertEqual(len(warnings), 0)
self.assertEqual(ret, {})
def test_get_systemd_only_no_mock(self):
def test_func(cats, dogs, no_block):
pass
ret, warnings = service._get_systemd_only(
test_func, {"cats": 1, "no_block": 2, "unmask": 3}
)
self.assertIsInstance(ret, dict)
self.assertIsInstance(warnings, list)
def test_running(self):
"""
Test to verify that the service is running
@ -76,9 +126,12 @@ class ServiceTestCase(TestCase, LoaderModuleMockMixin):
"comment": "Started Service salt\nService masking not available on this minion",
"name": "salt",
"result": True,
"warnings": [
"The 'unmask' argument is not supported by this platform/action"
],
},
{
"changes": "saltstack",
"comment": "Started Service salt\nService masking not available on this minion",
"name": "salt",
"result": True,
},
]

View file

@ -30,31 +30,6 @@ class SystemdTestCase(TestCase):
Tests the functions in salt.utils.systemd
"""
def test_has_systemd_support_true(self):
with patch(
"subprocess.check_output",
return_value="Cats\n\t\n\rSyStemD_data".encode("utf-8"),
) as check_output:
self.assertTrue(_systemd._has_systemd_support())
check_output.assert_called_once()
def test_has_systemd_support_false(self):
with patch(
"subprocess.check_output", return_value="Cats\nMeow".encode("utf-8")
) as check_output:
self.assertFalse(_systemd._has_systemd_support())
check_output.assert_called_once()
def test_has_systemd_support_file_not_found_error(self):
with patch(
"subprocess.check_output", side_effect=FileNotFoundError
) as check_output:
self.assertFalse(_systemd._has_systemd_support())
check_output.assert_called_once()
def test_has_systemd_support_no_mock(self):
self.assertIsInstance(_systemd._has_systemd_support(), bool)
def test_booted(self):
"""
Test that salt.utils.systemd.booted() returns True when minion is