Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In OpenRC exec module, make sure to ignore retcode on status #46101

Merged
merged 2 commits into from Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions salt/modules/gentoo_service.py
Expand Up @@ -37,9 +37,9 @@ def __virtual__():
'only available on Gentoo/Open-RC systems.')


def _ret_code(cmd):
def _ret_code(cmd, ignore_retcode=False):
log.debug('executing [{0}]'.format(cmd))
sts = __salt__['cmd.retcode'](cmd, python_shell=False)
sts = __salt__['cmd.retcode'](cmd, python_shell=False, ignore_retcode=ignore_retcode)
return sts


Expand Down Expand Up @@ -248,8 +248,9 @@ def status(name, sig=None):
'''
if sig:
return bool(__salt__['status.pid'](sig))

cmd = _service_cmd(name, 'status')
return not _ret_code(cmd)
return not _ret_code(cmd, ignore_retcode=True)


def enable(name, **kwargs):
Expand Down
105 changes: 80 additions & 25 deletions tests/unit/modules/test_gentoo_service.py
Expand Up @@ -151,7 +151,9 @@ def test_start(self):
mock = MagicMock(return_value=True)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': mock}):
self.assertFalse(gentoo_service.start('name'))
mock.assert_called_once_with('/etc/init.d/name start', python_shell=False)
mock.assert_called_once_with('/etc/init.d/name start',
ignore_retcode=False,
python_shell=False)

def test_stop(self):
'''
Expand All @@ -160,7 +162,9 @@ def test_stop(self):
mock = MagicMock(return_value=True)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': mock}):
self.assertFalse(gentoo_service.stop('name'))
mock.assert_called_once_with('/etc/init.d/name stop', python_shell=False)
mock.assert_called_once_with('/etc/init.d/name stop',
ignore_retcode=False,
python_shell=False)

def test_restart(self):
'''
Expand All @@ -169,7 +173,9 @@ def test_restart(self):
mock = MagicMock(return_value=True)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': mock}):
self.assertFalse(gentoo_service.restart('name'))
mock.assert_called_once_with('/etc/init.d/name restart', python_shell=False)
mock.assert_called_once_with('/etc/init.d/name restart',
ignore_retcode=False,
python_shell=False)

def test_reload_(self):
'''
Expand All @@ -178,7 +184,9 @@ def test_reload_(self):
mock = MagicMock(return_value=True)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': mock}):
self.assertFalse(gentoo_service.reload_('name'))
mock.assert_called_once_with('/etc/init.d/name reload', python_shell=False)
mock.assert_called_once_with('/etc/init.d/name reload',
ignore_retcode=False,
python_shell=False)

def test_zap(self):
'''
Expand All @@ -187,7 +195,9 @@ def test_zap(self):
mock = MagicMock(return_value=True)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': mock}):
self.assertFalse(gentoo_service.zap('name'))
mock.assert_called_once_with('/etc/init.d/name zap', python_shell=False)
mock.assert_called_once_with('/etc/init.d/name zap',
ignore_retcode=False,
python_shell=False)

def test_status(self):
'''
Expand All @@ -201,25 +211,33 @@ def test_status(self):
mock = MagicMock(return_value=0)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': mock}):
self.assertTrue(gentoo_service.status('name'))
mock.assert_called_once_with('/etc/init.d/name status', python_shell=False)
mock.assert_called_once_with('/etc/init.d/name status',
ignore_retcode=True,
python_shell=False)

# service is not running
mock = MagicMock(return_value=1)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': mock}):
self.assertFalse(gentoo_service.status('name'))
mock.assert_called_once_with('/etc/init.d/name status', python_shell=False)
mock.assert_called_once_with('/etc/init.d/name status',
ignore_retcode=True,
python_shell=False)

# service is stopped
mock = MagicMock(return_value=3)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': mock}):
self.assertFalse(gentoo_service.status('name'))
mock.assert_called_once_with('/etc/init.d/name status', python_shell=False)
mock.assert_called_once_with('/etc/init.d/name status',
ignore_retcode=True,
python_shell=False)

# service has crashed
mock = MagicMock(return_value=32)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': mock}):
self.assertFalse(gentoo_service.status('name'))
mock.assert_called_once_with('/etc/init.d/name status', python_shell=False)
mock.assert_called_once_with('/etc/init.d/name status',
ignore_retcode=True,
python_shell=False)

def test_enable(self):
'''
Expand All @@ -228,7 +246,9 @@ def test_enable(self):
rc_update_mock = MagicMock(return_value=0)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name'))
rc_update_mock.assert_called_once_with('rc-update add name', python_shell=False)
rc_update_mock.assert_called_once_with('rc-update add name',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

# move service from 'l1' to 'l2' runlevel
Expand All @@ -238,8 +258,12 @@ def test_enable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels='l2'))
rc_update_mock.assert_has_calls([call('rc-update delete name l1', python_shell=False),
call('rc-update add name l2', python_shell=False)])
rc_update_mock.assert_has_calls([call('rc-update delete name l1',
ignore_retcode=False,
python_shell=False),
call('rc-update add name l2',
ignore_retcode=False,
python_shell=False)])
rc_update_mock.reset_mock()

# requested levels are the same as the current ones
Expand All @@ -260,7 +284,9 @@ def test_enable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels=['l2', 'l1']))
rc_update_mock.assert_called_once_with('rc-update add name l2', python_shell=False)
rc_update_mock.assert_called_once_with('rc-update add name l2',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

# remove service from 'l1' runlevel
Expand All @@ -269,15 +295,21 @@ def test_enable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels=['l2']))
rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False)
rc_update_mock.assert_called_once_with('rc-update delete name l1',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

# move service from 'l2' add to 'l3', leaving at l1
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels=['l1', 'l3']))
rc_update_mock.assert_has_calls([call('rc-update delete name l2', python_shell=False),
call('rc-update add name l3', python_shell=False)])
rc_update_mock.assert_has_calls([call('rc-update delete name l2',
ignore_retcode=False,
python_shell=False),
call('rc-update add name l3',
ignore_retcode=False,
python_shell=False)])
rc_update_mock.reset_mock()

# remove from l1, l3, and add to l2, l4, and leave at l5
Expand All @@ -286,15 +318,21 @@ def test_enable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels=['l2', 'l4', 'l5']))
rc_update_mock.assert_has_calls([call('rc-update delete name l1 l3', python_shell=False),
call('rc-update add name l2 l4', python_shell=False)])
rc_update_mock.assert_has_calls([call('rc-update delete name l1 l3',
ignore_retcode=False,
python_shell=False),
call('rc-update add name l2 l4',
ignore_retcode=False,
python_shell=False)])
rc_update_mock.reset_mock()

# rc-update failed
rc_update_mock = MagicMock(return_value=1)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.enable('name'))
rc_update_mock.assert_called_once_with('rc-update add name', python_shell=False)
rc_update_mock.assert_called_once_with('rc-update add name',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

# move service delete failed
Expand All @@ -303,7 +341,9 @@ def test_enable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.enable('name', runlevels='l2'))
rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False)
rc_update_mock.assert_called_once_with('rc-update delete name l1',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

# move service delete succeeds. add fails
Expand All @@ -312,8 +352,12 @@ def test_enable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.enable('name', runlevels='l2'))
rc_update_mock.assert_has_calls([call('rc-update delete name l1', python_shell=False),
call('rc-update add name l2', python_shell=False)])
rc_update_mock.assert_has_calls([call('rc-update delete name l1',
ignore_retcode=False,
python_shell=False),
call('rc-update add name l2',
ignore_retcode=False,
python_shell=False)])
rc_update_mock.reset_mock()

def test_disable(self):
Expand All @@ -323,7 +367,9 @@ def test_disable(self):
rc_update_mock = MagicMock(return_value=0)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name'))
rc_update_mock.assert_called_once_with('rc-update delete name', python_shell=False)
rc_update_mock.assert_called_once_with('rc-update delete name',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

# disable service
Expand All @@ -334,6 +380,7 @@ def test_disable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name', runlevels='l1'))
rc_update_mock.assert_called_once_with('rc-update delete name l1',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

Expand All @@ -344,6 +391,7 @@ def test_disable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name', runlevels=['l1']))
rc_update_mock.assert_called_once_with('rc-update delete name l1',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

Expand All @@ -354,6 +402,7 @@ def test_disable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name', runlevels=['l1']))
rc_update_mock.assert_called_once_with('rc-update delete name l1',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

Expand All @@ -373,14 +422,17 @@ def test_disable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name', runlevels=['l1', 'l3']))
rc_update_mock.assert_called_once_with('rc-update delete name l1 l3',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

# rc-update failed
rc_update_mock = MagicMock(return_value=1)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.disable('name'))
rc_update_mock.assert_called_once_with('rc-update delete name', python_shell=False)
rc_update_mock.assert_called_once_with('rc-update delete name',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

# move service delete failed
Expand All @@ -389,7 +441,9 @@ def test_disable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.disable('name', runlevels='l1'))
rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False)
rc_update_mock.assert_called_once_with('rc-update delete name l1',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

# move service delete succeeds. add fails
Expand All @@ -399,6 +453,7 @@ def test_disable(self):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.disable('name', runlevels=['l1', 'l3']))
rc_update_mock.assert_called_once_with('rc-update delete name l1 l3',
ignore_retcode=False,
python_shell=False)
rc_update_mock.reset_mock()

Expand Down