change how alternatives states check for installed

This commit is contained in:
Andreas Lutro 2015-12-26 22:48:29 +01:00 committed by rallytime
parent e7fe24dc64
commit ca290ec3e1
3 changed files with 23 additions and 4 deletions

View file

@ -80,6 +80,25 @@ def show_current(name):
return False
def check_exists(name, path):
'''
Check if the given path is an alternative for a name.
CLI Example:
.. code-block:: bash
salt '*' alternatives.check_exists name path
'''
cmd = [_get_cmd(), '--list', name]
out = __salt__['cmd.run_all'](cmd, python_shell=False)
if out['retcode'] > 0 and out['stderr'] != '':
return False
return path in out['stdout'].splitlines()
def check_installed(name, path):
'''
Check if the current highest-priority match for a given alternatives link

View file

@ -62,7 +62,7 @@ def install(name, link, path, priority):
'changes': {},
'comment': ''}
isinstalled = __salt__['alternatives.check_installed'](name, path)
isinstalled = __salt__['alternatives.check_exists'](name, path)
if not isinstalled:
if __opts__['test']:
ret['comment'] = (
@ -103,7 +103,7 @@ def remove(name, path):
'changes': {},
'comment': ''}
isinstalled = __salt__['alternatives.check_installed'](name, path)
isinstalled = __salt__['alternatives.check_exists'](name, path)
if isinstalled:
if __opts__['test']:
ret['comment'] = ('Alternative for {0} will be removed'

View file

@ -52,7 +52,7 @@ class AlternativesTestCase(TestCase):
mock = MagicMock(side_effect=[True, False, False])
mock_bool = MagicMock(return_value=True)
with patch.dict(alternatives.__salt__,
{'alternatives.check_installed': mock,
{'alternatives.check_exists': mock,
'alternatives.install': mock_bool}):
comt = ('Alternatives for {0} is already set to {1}'
).format(name, path)
@ -96,7 +96,7 @@ class AlternativesTestCase(TestCase):
mock_bool = MagicMock(return_value=True)
mock_bol = MagicMock(side_effect=[False, True, True, False])
with patch.dict(alternatives.__salt__,
{'alternatives.check_installed': mock,
{'alternatives.check_exists': mock,
'alternatives.show_current': mock_bol,
'alternatives.remove': mock_bool}):
comt = ('Alternative for {0} will be removed'.format(name))