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.
This commit is contained in:
Erik Johnson 2015-12-18 15:13:41 -06:00
parent b219998b4b
commit 15047918a9

View File

@ -29,65 +29,49 @@ systemd.__context__ = {}
@skipIf(NO_MOCK, NO_MOCK_REASON) @skipIf(NO_MOCK, NO_MOCK_REASON)
class SystemdTestCase(TestCase): 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): 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): _service_is_sysv = lambda x: True if x in ('d', 'e') else False
if name in ['d', 'e']: _sysv_is_enabled = lambda x: False if x in ('e',) else True
return True
return False
def sysve(name): unit_files = MagicMock(return_value={'a': 'enabled',
if name in ['e']: 'b': 'enabled',
return True 'c': 'disabled'})
return False all_units = MagicMock(return_value={'a': 'disabled',
'b': 'disabled',
mock = MagicMock(return_value={"a": "enabled", "b": "enabled", 'd': 'disabled',
"c": "disabled"}) 'e': 'disabled'})
lmock = MagicMock(return_value={"d": "disabled", with patch.object(systemd, '_sysv_is_enabled', _sysv_is_enabled):
"a": "disabled", with patch.object(systemd, '_service_is_sysv', _service_is_sysv):
"b": "disabled", with patch.object(systemd, '_get_all_unit_files', unit_files):
"e": "disabled"}) with patch.object(systemd, '_get_all_units', all_units):
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):
self.assertListEqual( self.assertListEqual(
systemd.get_enabled(), ["a", "b", "d"]) systemd.get_enabled(), ['a', 'b', 'd'])
def test_get_disabled(self): 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", mock = MagicMock(return_value={'a': 'enabled', 'b': 'enabled',
"c": "disabled"}) 'c': 'disabled'})
with patch.object(systemd, '_get_all_unit_files', mock): with patch.object(systemd, '_get_all_unit_files', mock):
mock = MagicMock(return_value={}) mock = MagicMock(return_value={})
with patch.object(systemd, '_get_all_legacy_init_scripts', mock): 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): 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", mock = MagicMock(return_value={'a': 'enabled', 'b': 'enabled',
"c": "disabled"}) 'c': 'disabled'})
with patch.object(systemd, '_get_all_units', mock): with patch.object(systemd, '_get_all_units', mock):
mock = MagicMock(return_value={"a1": "enabled", "b1": "disabled", mock = MagicMock(return_value={'a1': 'enabled', 'b1': 'disabled',
"c1": "enabled"}) 'c1': 'enabled'})
with patch.object(systemd, '_get_all_unit_files', mock): with patch.object(systemd, '_get_all_unit_files', mock):
mock = MagicMock(return_value={}) mock = MagicMock(return_value={})
with patch.object(systemd, with patch.object(systemd,
@ -97,165 +81,50 @@ class SystemdTestCase(TestCase):
def test_available(self): 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) mock_stdout = MagicMock(return_value=not_available)
with patch.dict(systemd.__salt__, {'cmd.run_stdout': mock_stdout}): with patch.dict(systemd.__salt__, {'cmd.run': mock_stdout}):
self.assertTrue(systemd.available(name)) self.assertFalse(systemd.available('asdf'))
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"))
def test_show(self): 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}): 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): def test_execs(self):
''' '''
Test to return a list of all files specified as ``ExecStart`` Test to return a list of all files specified as ``ExecStart`` for all
for all services services
''' '''
mock = MagicMock(return_value=["a", "b"]) mock = MagicMock(return_value=['a', 'b'])
with patch.object(systemd, 'get_all', mock): 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): with patch.object(systemd, 'show', mock):
self.assertDictEqual(systemd.execs(), {'a': 'c', 'b': 'c'}) self.assertDictEqual(systemd.execs(), {'a': 'c', 'b': 'c'})