From 15047918a9c7b918fb48cf47f0e130da22257895 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Fri, 18 Dec 2015 15:13:41 -0600 Subject: [PATCH] systemd test fixes This removes a bunch of tests that don't test anything (see https://github.com/saltstack/salt/issues/29852). It also changes some of the tests to reflect recent changes in systemd.py. --- tests/unit/modules/systemd_test.py | 245 +++++++---------------------- 1 file changed, 57 insertions(+), 188 deletions(-) diff --git a/tests/unit/modules/systemd_test.py b/tests/unit/modules/systemd_test.py index 34fcc31957..a67abcd616 100644 --- a/tests/unit/modules/systemd_test.py +++ b/tests/unit/modules/systemd_test.py @@ -29,65 +29,49 @@ systemd.__context__ = {} @skipIf(NO_MOCK, NO_MOCK_REASON) class SystemdTestCase(TestCase): ''' - Test case for salt.modules.systemd + Test case for salt.modules.systemd ''' - def test_systemctl_reload(self): - ''' - Test to Reloads systemctl - ''' - mock = MagicMock(side_effect=[1, 0]) - with patch.dict(systemd.__salt__, {'cmd.retcode': mock}): - self.assertFalse(systemd.systemctl_reload()) - - self.assertTrue(systemd.systemctl_reload()) - def test_get_enabled(self): ''' - Test to return a list of all enabled services + Test to return a list of all enabled services ''' - def sysv(name): - if name in ['d', 'e']: - return True - return False + _service_is_sysv = lambda x: True if x in ('d', 'e') else False + _sysv_is_enabled = lambda x: False if x in ('e',) else True - def sysve(name): - if name in ['e']: - return True - return False - - mock = MagicMock(return_value={"a": "enabled", "b": "enabled", - "c": "disabled"}) - lmock = MagicMock(return_value={"d": "disabled", - "a": "disabled", - "b": "disabled", - "e": "disabled"}) - with patch.object(systemd, "_sysv_is_disabled", sysve): - with patch.object(systemd, "_service_is_sysv", sysv): - with patch.object(systemd, '_get_all_unit_files', mock): - with patch.object(systemd, '_get_all_units', lmock): + unit_files = MagicMock(return_value={'a': 'enabled', + 'b': 'enabled', + 'c': 'disabled'}) + all_units = MagicMock(return_value={'a': 'disabled', + 'b': 'disabled', + 'd': 'disabled', + 'e': 'disabled'}) + with patch.object(systemd, '_sysv_is_enabled', _sysv_is_enabled): + with patch.object(systemd, '_service_is_sysv', _service_is_sysv): + with patch.object(systemd, '_get_all_unit_files', unit_files): + with patch.object(systemd, '_get_all_units', all_units): self.assertListEqual( - systemd.get_enabled(), ["a", "b", "d"]) + systemd.get_enabled(), ['a', 'b', 'd']) def test_get_disabled(self): ''' - Test to return a list of all disabled services + Test to return a list of all disabled services ''' - mock = MagicMock(return_value={"a": "enabled", "b": "enabled", - "c": "disabled"}) + mock = MagicMock(return_value={'a': 'enabled', 'b': 'enabled', + 'c': 'disabled'}) with patch.object(systemd, '_get_all_unit_files', mock): mock = MagicMock(return_value={}) with patch.object(systemd, '_get_all_legacy_init_scripts', mock): - self.assertListEqual(systemd.get_disabled(), ["c"]) + self.assertListEqual(systemd.get_disabled(), ['c']) def test_get_all(self): ''' - Test to return a list of all available services + Test to return a list of all available services ''' - mock = MagicMock(return_value={"a": "enabled", "b": "enabled", - "c": "disabled"}) + mock = MagicMock(return_value={'a': 'enabled', 'b': 'enabled', + 'c': 'disabled'}) with patch.object(systemd, '_get_all_units', mock): - mock = MagicMock(return_value={"a1": "enabled", "b1": "disabled", - "c1": "enabled"}) + mock = MagicMock(return_value={'a1': 'enabled', 'b1': 'disabled', + 'c1': 'enabled'}) with patch.object(systemd, '_get_all_unit_files', mock): mock = MagicMock(return_value={}) with patch.object(systemd, @@ -97,165 +81,50 @@ class SystemdTestCase(TestCase): def test_available(self): ''' - Test to check that the given service is available + Test to check that the given service is available ''' - name = 'thing1' + available = ( + 'sshd.service - OpenSSH server daemon\n' + ' Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)\n' + ' Active: inactive (dead) since Thu 2015-12-17 15:33:06 CST; 19h ago\n' + ' Main PID: 230 (code=exited, status=0/SUCCESS)\n' + ) + not_available = ( + 'asdf.service\n' + ' Loaded: not-found (Reason: No such file or directory)\n' + ' Active: inactive (dead)\n' + ) + mock_stdout = MagicMock(return_value=available) + with patch.dict(systemd.__salt__, {'cmd.run': mock_stdout}): + self.assertTrue(systemd.available('sshd')) - mock_stdout = MagicMock(return_value=name) - with patch.dict(systemd.__salt__, {'cmd.run_stdout': mock_stdout}): - self.assertTrue(systemd.available(name)) - - mock_stdout = MagicMock(side_effect=['', '']) - with patch.dict(systemd.__salt__, {'cmd.run_stdout': mock_stdout}): - self.assertFalse(systemd.available(name)) - - mock_stdout = MagicMock(side_effect=['', name]) - with patch.dict(systemd.__salt__, {'cmd.run_stdout': mock_stdout}): - self.assertTrue(systemd.available(name)) - - def test_missing(self): - ''' - Test to the inverse of service.available. - ''' - mock = MagicMock(return_value=True) - with patch.object(systemd, 'available', mock): - self.assertFalse(systemd.missing("sshd")) - - def test_unmask(self): - ''' - Test to unmask the specified service with systemd - ''' - mock = MagicMock(return_value=False) - with patch.object(systemd, '_untracked_custom_unit_found', mock): - with patch.object(systemd, '_unit_file_changed', mock): - with patch.dict(systemd.__salt__, {'cmd.retcode': mock}): - self.assertTrue(systemd.unmask("sshd")) - - def test_start(self): - ''' - Test to start the specified service with systemd - ''' - mock = MagicMock(return_value=False) - with patch.object(systemd, '_untracked_custom_unit_found', mock): - with patch.object(systemd, '_unit_file_changed', mock): - with patch.dict(systemd.__salt__, {'cmd.retcode': mock}): - self.assertTrue(systemd.start("sshd")) - - def test_stop(self): - ''' - Test to stop the specified service with systemd - ''' - mock = MagicMock(return_value=False) - with patch.object(systemd, '_untracked_custom_unit_found', mock): - with patch.object(systemd, '_unit_file_changed', mock): - with patch.dict(systemd.__salt__, {'cmd.retcode': mock}): - self.assertTrue(systemd.stop("sshd")) - - def test_restart(self): - ''' - Test to restart the specified service with systemd - ''' - mock = MagicMock(return_value=False) - with patch.object(systemd, '_untracked_custom_unit_found', mock): - with patch.object(systemd, '_unit_file_changed', mock): - with patch.dict(systemd.__salt__, {'cmd.retcode': mock}): - self.assertTrue(systemd.restart("sshd")) - - def test_reload_(self): - ''' - Test to Reload the specified service with systemd - ''' - mock = MagicMock(return_value=False) - with patch.object(systemd, '_untracked_custom_unit_found', mock): - with patch.object(systemd, '_unit_file_changed', mock): - with patch.dict(systemd.__salt__, {'cmd.retcode': mock}): - self.assertTrue(systemd.reload_("sshd")) - - def test_force_reload(self): - ''' - Test to force-reload the specified service with systemd - ''' - mock = MagicMock(return_value=False) - with patch.object(systemd, '_untracked_custom_unit_found', mock): - with patch.object(systemd, '_unit_file_changed', mock): - with patch.dict(systemd.__salt__, {'cmd.retcode': mock}): - self.assertTrue(systemd.force_reload("sshd")) - - def test_status(self): - ''' - Test to return the status for a service via systemd - ''' - mock = MagicMock(return_value=False) - with patch.object(systemd, '_untracked_custom_unit_found', mock): - with patch.object(systemd, '_unit_file_changed', mock): - with patch.dict(systemd.__salt__, {'cmd.retcode': mock}): - self.assertTrue(systemd.status("sshd")) - - def test_enable(self): - ''' - Test to enable the named service to start when the system boots - ''' - exe = MagicMock(return_value='foo') - tmock = MagicMock(return_value=True) - mock = MagicMock(return_value=False) - with patch.object(systemd, '_untracked_custom_unit_found', mock): - with patch.object(systemd, '_unit_file_changed', mock): - with patch.dict(systemd.__salt__, {'cmd.retcode': mock}): - with patch.object(systemd, "_service_is_sysv", mock): - self.assertTrue(systemd.enable("sshd")) - with patch.object(systemd, "_get_service_exec", exe): - with patch.object(systemd, "_service_is_sysv", tmock): - self.assertTrue(systemd.enable("sshd")) - - def test_disable(self): - ''' - Test to disable the named service to not - start when the system boots - ''' - exe = MagicMock(return_value='foo') - tmock = MagicMock(return_value=True) - mock = MagicMock(return_value=False) - with patch.object(systemd, '_untracked_custom_unit_found', mock): - with patch.object(systemd, '_unit_file_changed', mock): - with patch.dict(systemd.__salt__, {'cmd.retcode': mock}): - with patch.object(systemd, "_service_is_sysv", mock): - self.assertTrue(systemd.disable("sshd")) - with patch.object(systemd, "_get_service_exec", exe): - with patch.object(systemd, "_service_is_sysv", tmock): - self.assertTrue(systemd.disable("sshd")) - - def test_enabled(self): - ''' - Test to return if the named service is enabled to start on boot - ''' - mock = MagicMock(return_value=True) - with patch.object(systemd, '_enabled', mock): - self.assertTrue(systemd.enabled("sshd")) - - def test_disabled(self): - ''' - Test to Return if the named service is disabled to start on boot - ''' - mock = MagicMock(return_value=True) - with patch.object(systemd, '_enabled', mock): - self.assertFalse(systemd.disabled("sshd")) + mock_stdout = MagicMock(return_value=not_available) + with patch.dict(systemd.__salt__, {'cmd.run': mock_stdout}): + self.assertFalse(systemd.available('asdf')) def test_show(self): ''' - Test to show properties of one or more units/jobs or the manager + Test to show properties of one or more units/jobs or the manager ''' - mock = MagicMock(return_value="a = b , c = d") + show_output = 'a=b\nc=d\ne={ f=g ; h=i }\nWants=foo.service bar.service\n' + mock = MagicMock(return_value=show_output) with patch.dict(systemd.__salt__, {'cmd.run': mock}): - self.assertDictEqual(systemd.show("sshd"), {'a ': ' b , c = d'}) + self.assertDictEqual( + systemd.show('sshd'), + {'a': 'b', + 'c': 'd', + 'e': {'f': 'g', 'h': 'i'}, + 'Wants': ['foo.service', 'bar.service']} + ) def test_execs(self): ''' - Test to return a list of all files specified as ``ExecStart`` - for all services + Test to return a list of all files specified as ``ExecStart`` for all + services ''' - mock = MagicMock(return_value=["a", "b"]) + mock = MagicMock(return_value=['a', 'b']) with patch.object(systemd, 'get_all', mock): - mock = MagicMock(return_value={"ExecStart": {"path": "c"}}) + mock = MagicMock(return_value={'ExecStart': {'path': 'c'}}) with patch.object(systemd, 'show', mock): self.assertDictEqual(systemd.execs(), {'a': 'c', 'b': 'c'})