Clean-up/fixes to rabbitmq_user state and test adjustments

This commit is contained in:
rallytime 2015-10-26 13:48:50 -04:00
parent 3e0e8fc8c6
commit 19b8b868a3
3 changed files with 44 additions and 35 deletions

View file

@ -50,7 +50,7 @@ def _check_perms_changes(name, newperms, runas=None, existing=None):
if existing is None:
try:
existing_perms = __salt__['rabbitmq.list_user_permissions'](name, runas=runas)
existing = __salt__['rabbitmq.list_user_permissions'](name, runas=runas)
except CommandExecutionError as err:
log.error('Error: {0}'.format(err))
return False
@ -58,8 +58,8 @@ def _check_perms_changes(name, newperms, runas=None, existing=None):
perm_need_change = False
for vhost_perms in newperms:
for vhost, perms in vhost_perms.iteritems():
if vhost in existing_perms:
if perms != existing_perms[vhost]:
if vhost in existing:
if perms != existing[vhost]:
perm_need_change = True
else:
perm_need_change = True
@ -116,19 +116,24 @@ def present(name,
if user and not any((force, perms, tags)):
log.debug('RabbitMQ user \'{0}\' exists and force is not set.'.format(name))
ret['comment'] = 'User \'{0}\' is already present.'.format(name)
ret['result'] = True
return ret
if not user:
if not __opts__['test']:
log.debug('RabbitMQ user \'{0}\' doesn\'t exist - Creating.'.foramt(name))
try:
__salt__['rabbitmq.add_user'](name, password, runas=runas)
except CommandExecutionError as err:
ret['comment'] = 'Error: {0}'.format(err)
return ret
ret['changes'].update({'user':
{'old': '',
'new': name}})
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'User \'{0}\' is set to be created.'.format(name)
return ret
log.debug('RabbitMQ user \'{0}\' doesn\'t exist - Creating.'.format(name))
try:
__salt__['rabbitmq.add_user'](name, password, runas=runas)
except CommandExecutionError as err:
ret['comment'] = 'Error: {0}'.format(err)
return ret
else:
log.debug('RabbitMQ user \'{0}\' exists'.format(name))
if force:
@ -166,7 +171,7 @@ def present(name,
{'old': tags,
'new': new_tags}})
try:
existing_perms = __salt__['rabbitmq.list_user_permissions'](name, runas=runas)
existing_perms = __salt__['rabbitmq.list_user_permissions'](name, runas=runas)[0]
except CommandExecutionError as err:
ret['comment'] = 'Error: {0}'.format(err)
return ret
@ -182,11 +187,14 @@ def present(name,
except CommandExecutionError as err:
ret['comment'] = 'Error: {0}'.format(err)
return ret
if ret['changes'].get('perms') is None:
ret['changes'].update({'perms':
{'old': existing_perms,
'new': ''}})
ret['changes']['perms']['new'].update({vhost: perm})
new_perms = {vhost: perm}
if existing_perms != new_perms:
if ret['changes'].get('perms') is None:
ret['changes'].update({'perms':
{'old': {},
'new': {}}})
ret['changes']['perms']['old'].update(existing_perms)
ret['changes']['perms']['new'].update(new_perms)
ret['result'] = True
if ret['changes'] == {}:

View file

@ -21,6 +21,7 @@ ensure_in_syspath('../../')
# Import Salt Libs
from salt.modules import rabbitmq
from salt.exceptions import CommandExecutionError
# Globals
rabbitmq.__salt__ = {}
@ -114,9 +115,7 @@ class RabbitmqTestCase(TestCase):
with patch.dict(rabbitmq.__salt__, {'cmd.run': mock_run}):
with patch.object(rabbitmq, 'clear_password',
return_value={'Error': 'Error', 'retcode': 1}):
self.assertDictEqual(rabbitmq.add_user('saltstack'),
{'Error': {'Error': 'Error',
'retcode': 1}})
self.assertRaises(CommandExecutionError, rabbitmq.add_user, 'saltstack')
# 'delete_user' function tests: 1

View file

@ -21,7 +21,7 @@ ensure_in_syspath('../../')
# Import Salt Libs
from salt.states import rabbitmq_user
rabbitmq_user.__opts__ = {}
rabbitmq_user.__opts__ = {'test': False}
rabbitmq_user.__salt__ = {}
@ -56,38 +56,40 @@ class RabbitmqUserTestCase(TestCase):
'rabbitmq.list_users': mock_dct,
'rabbitmq.list_user_permissions': mock_pr,
'rabbitmq.set_user_tags': mock_add}):
comt = ('User foo already presents')
ret.update({'comment': comt})
comment = 'User \'foo\' is already present.'
ret.update({'comment': comment})
self.assertDictEqual(rabbitmq_user.present(name), ret)
with patch.dict(rabbitmq_user.__opts__, {'test': True}):
comt = ('User foo is set to be created')
ret.update({'comment': comt, 'result': None})
comment = 'User \'foo\' is set to be created.'
changes = {'user': {'new': 'foo', 'old': ''}}
ret.update({'comment': comment, 'result': None, 'changes': changes})
self.assertDictEqual(rabbitmq_user.present(name), ret)
comt = ("User foo's password is set to be updated")
ret.update({'comment': comt})
comment = 'Configuration for \'foo\' will change.'
changes = {'password': {'new': 'Set password.', 'old': ''}}
ret.update({'comment': comment, 'changes': changes})
self.assertDictEqual(rabbitmq_user.present(name,
password=passwd,
force=True), ret)
comt = ("User foo's password is set to be removed")
ret.update({'comment': comt})
changes = {'password': {'new': '', 'old': 'Removed password.'}}
ret.update({'changes': changes})
self.assertDictEqual(rabbitmq_user.present(name, force=True),
ret)
comt = ('Tags for user foo is set to be changed')
ret.update({'comment': comt})
changes = {'tags': {'new': set(['e', 'r', 's', 'u']), 'old': 'user'}}
ret.update({'changes': changes})
self.assertDictEqual(rabbitmq_user.present(name, tags=tag), ret)
comt = ('Permissions for user foo is set to be changed')
ret.update({'comment': comt})
comment = '\'foo\' is already in the desired state.'
ret.update({'changes': {}, 'comment': comment, 'result': True})
self.assertDictEqual(rabbitmq_user.present(name, perms=perms),
ret)
with patch.dict(rabbitmq_user.__opts__, {'test': False}):
ret.update({'comment': name, 'result': True,
'changes': {'new': 'Set tags: user\n', 'old': ''}})
ret.update({'comment': '\'foo\' was configured.', 'result': True,
'changes': {'tags': {'new': set(['e', 'r', 's', 'u']), 'old': 'user'}}})
self.assertDictEqual(rabbitmq_user.present(name, tags=tag), ret)
# 'absent' function tests: 1
@ -101,7 +103,7 @@ class RabbitmqUserTestCase(TestCase):
ret = {'name': name,
'changes': {},
'result': True,
'comment': 'User {0} is not present'.format(name)}
'comment': 'The user \'foo\' is not present.'.format(name)}
mock = MagicMock(return_value=False)
with patch.dict(rabbitmq_user.__salt__, {'rabbitmq.user_exists': mock}):