mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
refactored list_policies code and added more tests
This commit is contained in:
parent
eea81feb7b
commit
e043ea2833
2 changed files with 34 additions and 10 deletions
|
@ -19,6 +19,7 @@ import salt.utils.path
|
|||
import salt.utils.platform
|
||||
import salt.utils.user
|
||||
from salt.exceptions import CommandExecutionError, SaltInvocationError
|
||||
from salt.utils.versions import LooseVersion as _LooseVersion
|
||||
|
||||
# Import 3rd-party libs
|
||||
from salt.ext import six
|
||||
|
@ -829,13 +830,10 @@ def list_policies(vhost="/", runas=None):
|
|||
_check_response(res)
|
||||
output = res['stdout']
|
||||
|
||||
if __salt__['pkg.version']('rabbitmq-server'):
|
||||
version = __salt__['pkg.version']('rabbitmq-server').split('-')[0].split('.')
|
||||
if __grains__['os_family'] != 'FreeBSD':
|
||||
version = __salt__['pkg.version']('rabbitmq-server').split('-')[0]
|
||||
else:
|
||||
version = __salt__['pkg.version']('rabbitmq').split('-')[0].split('.')
|
||||
|
||||
major_version = int(version[0])
|
||||
minor_version = int(version[1])
|
||||
version = __salt__['pkg.version']('rabbitmq').split('-')[0]
|
||||
|
||||
for line in _output_lines_to_list(output):
|
||||
parts = line.split('\t')
|
||||
|
@ -848,7 +846,7 @@ def list_policies(vhost="/", runas=None):
|
|||
ret[vhost] = {}
|
||||
ret[vhost][name] = {}
|
||||
|
||||
if major_version >= 3 and minor_version >= 7:
|
||||
if _LooseVersion(version) >= _LooseVersion("3.7"):
|
||||
# in version 3.7 the position of apply_to and pattern has been
|
||||
# switched
|
||||
ret[vhost][name]['pattern'] = parts[2]
|
||||
|
|
|
@ -378,7 +378,7 @@ class RabbitmqTestCase(TestCase, LoaderModuleMockMixin):
|
|||
self.assertDictEqual(rabbitmq.list_queues_vhost('consumers'), {'saltstack': ['0'],
|
||||
'celeryev.234-234': ['10']})
|
||||
|
||||
# 'list_policies' function tests: 1
|
||||
# 'list_policies' function tests: 3
|
||||
|
||||
def test_list_policies(self):
|
||||
'''
|
||||
|
@ -386,7 +386,31 @@ class RabbitmqTestCase(TestCase, LoaderModuleMockMixin):
|
|||
and name based on the data returned from rabbitmqctl list_policies.
|
||||
'''
|
||||
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
||||
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
||||
mock_pkg = MagicMock(return_value='3.7')
|
||||
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run, 'pkg.version': mock_pkg}), \
|
||||
patch.dict(rabbitmq.__grains__, {'os_family': ''}):
|
||||
self.assertDictEqual(rabbitmq.list_policies(), {})
|
||||
|
||||
def test_list_policies_freebsd(self):
|
||||
'''
|
||||
Test if it return a dictionary of policies nested by vhost
|
||||
and name based on the data returned from rabbitmqctl list_policies.
|
||||
'''
|
||||
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
||||
mock_pkg = MagicMock(return_value='3.7')
|
||||
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run, 'pkg.version': mock_pkg}), \
|
||||
patch.dict(rabbitmq.__grains__, {'os_family': 'FreeBSD'}):
|
||||
self.assertDictEqual(rabbitmq.list_policies(), {})
|
||||
|
||||
def test_list_policies_old_version(self):
|
||||
'''
|
||||
Test if it return a dictionary of policies nested by vhost
|
||||
and name based on the data returned from rabbitmqctl list_policies.
|
||||
'''
|
||||
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
||||
mock_pkg = MagicMock(return_value='3.0')
|
||||
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run, 'pkg.version': mock_pkg}), \
|
||||
patch.dict(rabbitmq.__grains__, {'os_family': ''}):
|
||||
self.assertDictEqual(rabbitmq.list_policies(), {})
|
||||
|
||||
# 'set_policy' function tests: 1
|
||||
|
@ -420,7 +444,9 @@ class RabbitmqTestCase(TestCase, LoaderModuleMockMixin):
|
|||
based on rabbitmqctl list_policies.
|
||||
'''
|
||||
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
||||
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
||||
mock_pkg = MagicMock(return_value='3.0')
|
||||
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run, 'pkg.version': mock_pkg}), \
|
||||
patch.dict(rabbitmq.__grains__, {'os_family': ''}):
|
||||
self.assertFalse(rabbitmq.policy_exists('/', 'HA'))
|
||||
|
||||
# 'list_available_plugins' function tests: 2
|
||||
|
|
Loading…
Add table
Reference in a new issue