From d916c2b49cba8a6ce3cf74652010121b2cb7972d Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Tue, 4 Oct 2016 09:55:56 -0500 Subject: [PATCH] Handle cases where retcode/output feature is backported This will handle the case of RHEL 7.3, which maintains an older version of systemd with the retcode/output changes from "systemctl status" in systemd 231 backported. --- salt/modules/systemd.py | 6 ++++++ tests/unit/modules/systemd_test.py | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/salt/modules/systemd.py b/salt/modules/systemd.py index 66576d6b22..92f75282f4 100644 --- a/salt/modules/systemd.py +++ b/salt/modules/systemd.py @@ -85,7 +85,13 @@ def _check_available(name): # See: https://github.com/systemd/systemd/pull/3385 # Also: https://github.com/systemd/systemd/commit/3dced37 return 0 <= _status['retcode'] < 4 + out = _status['stdout'].lower() + if 'could not be found' in out: + # Catch cases where the systemd version is < 231 but the return code + # and output changes have been backported (e.g. RHEL 7.3). + return False + for line in salt.utils.itertools.split(out, '\n'): match = re.match(r'\s+loaded:\s+(\S+)', line) if match: diff --git a/tests/unit/modules/systemd_test.py b/tests/unit/modules/systemd_test.py index 3eb64f4867..091c47be9d 100644 --- a/tests/unit/modules/systemd_test.py +++ b/tests/unit/modules/systemd_test.py @@ -189,33 +189,53 @@ class SystemdTestCase(TestCase): Test to check that the given service is available ''' mock = MagicMock(side_effect=lambda x: _SYSTEMCTL_STATUS[x]) + + # systemd < 231 with patch.dict(systemd.__context__, {'salt.utils.systemd.version': 230}): with patch.object(systemd, '_systemctl_status', mock): self.assertTrue(systemd.available('sshd.service')) self.assertFalse(systemd.available('foo.service')) + # systemd >= 231 with patch.dict(systemd.__context__, {'salt.utils.systemd.version': 231}): with patch.dict(_SYSTEMCTL_STATUS, _SYSTEMCTL_STATUS_GTE_231): with patch.object(systemd, '_systemctl_status', mock): self.assertTrue(systemd.available('sshd.service')) self.assertFalse(systemd.available('bar.service')) + # systemd < 231 with retcode/output changes backported (e.g. RHEL 7.3) + with patch.dict(systemd.__context__, {'salt.utils.systemd.version': 219}): + with patch.dict(_SYSTEMCTL_STATUS, _SYSTEMCTL_STATUS_GTE_231): + with patch.object(systemd, '_systemctl_status', mock): + self.assertTrue(systemd.available('sshd.service')) + self.assertFalse(systemd.available('bar.service')) + def test_missing(self): ''' Test to the inverse of service.available. ''' mock = MagicMock(side_effect=lambda x: _SYSTEMCTL_STATUS[x]) + + # systemd < 231 with patch.dict(systemd.__context__, {'salt.utils.systemd.version': 230}): with patch.object(systemd, '_systemctl_status', mock): self.assertFalse(systemd.missing('sshd.service')) self.assertTrue(systemd.missing('foo.service')) + # systemd >= 231 with patch.dict(systemd.__context__, {'salt.utils.systemd.version': 231}): with patch.dict(_SYSTEMCTL_STATUS, _SYSTEMCTL_STATUS_GTE_231): with patch.object(systemd, '_systemctl_status', mock): self.assertFalse(systemd.missing('sshd.service')) self.assertTrue(systemd.missing('bar.service')) + # systemd < 231 with retcode/output changes backported (e.g. RHEL 7.3) + with patch.dict(systemd.__context__, {'salt.utils.systemd.version': 219}): + with patch.dict(_SYSTEMCTL_STATUS, _SYSTEMCTL_STATUS_GTE_231): + with patch.object(systemd, '_systemctl_status', mock): + self.assertFalse(systemd.missing('sshd.service')) + self.assertTrue(systemd.missing('bar.service')) + def test_show(self): ''' Test to show properties of one or more units/jobs or the manager