Skip to content

Commit 0562d36

Browse files
committed
add test for automatic daemon-reload in systemd_service module
1 parent b1cbd17 commit 0562d36

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

tests/unit/modules/test_systemd_service.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,51 @@ def test_execs(self):
327327
with patch.object(systemd, "show", mock):
328328
self.assertDictEqual(systemd.execs(), {"a": "c", "b": "c"})
329329

330+
def _check_automatic_daemon_reload(self, reload_needed):
331+
"""
332+
Test if a needed 'systemctl daemon-reload' is detected and executed when starting a service
333+
"""
334+
status = _SYSTEMCTL_STATUS["sshd.service"]
335+
if reload_needed:
336+
status[
337+
"stdout"
338+
] = f"""\
339+
Warning: The unit file, source configuration file or drop-ins of sshd.service changed on disk. Run 'systemctl daemon-reload' to reload units.
340+
{status["stdout"]}"""
341+
342+
mock_systemctl_reload = MagicMock(return_value=True)
343+
mock_run = MagicMock(
344+
side_effect=lambda x, **_: (
345+
status
346+
if "status" in x
347+
else {"stdout": "", "stderr": "", "retcode": 0, "pid": 54321}
348+
)
349+
)
350+
351+
with patch("salt.utils.path.which", lambda x: "/bin/" + x):
352+
with patch.object(
353+
systemd, "_untracked_custom_unit_found", MagicMock(return_value=False)
354+
):
355+
with patch.object(systemd, "systemctl_reload", mock_systemctl_reload):
356+
with patch.object(
357+
systemd, "_check_unmask", MagicMock(return_value=None)
358+
):
359+
with patch.dict(
360+
systemd.__salt__,
361+
{
362+
"config.get": MagicMock(return_value=False),
363+
"cmd.run_all": mock_run,
364+
},
365+
):
366+
self.assertTrue(systemd.start("sshd.service"))
367+
self.assertTrue(
368+
(mock_systemctl_reload.call_count == 1) == reload_needed
369+
)
370+
371+
def test_automatic_daemon_reload(self):
372+
self._check_automatic_daemon_reload(False)
373+
self._check_automatic_daemon_reload(True)
374+
330375

331376
class SystemdScopeTestCase(TestCase, LoaderModuleMockMixin):
332377
"""
@@ -395,7 +440,7 @@ def _change_state(self, action, no_block=False):
395440
self.assertTrue(ret)
396441
self.mock_run_all_success.assert_called_with(
397442
scope_prefix + systemctl_command,
398-
**assert_kwargs
443+
**assert_kwargs,
399444
)
400445

401446
# Scope enabled, failed
@@ -418,7 +463,7 @@ def _change_state(self, action, no_block=False):
418463
)
419464
self.mock_run_all_failure.assert_called_with(
420465
scope_prefix + systemctl_command,
421-
**assert_kwargs
466+
**assert_kwargs,
422467
)
423468

424469
# Scope disabled, successful

0 commit comments

Comments
 (0)