Merge pull request #30038 from rallytime/bp-30017

Back-port #30017 to 2015.8
This commit is contained in:
Mike Place 2015-12-28 13:41:45 -07:00
commit 6cdca314c7
3 changed files with 25 additions and 4 deletions

View file

@ -80,6 +80,27 @@ def show_current(name):
return False
def check_exists(name, path):
'''
Check if the given path is an alternative for a name.
.. versionadded:: 2015.8.4
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))