2016-05-25 08:22:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python Libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2016-05-25 08:22:10 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.rabbitmq as rabbitmq
|
2016-05-25 08:22:10 +00:00
|
|
|
from salt.exceptions import CommandExecutionError
|
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 15:35:30 +00:00
|
|
|
class RabbitmqTestCase(TestCase, LoaderModuleMockMixin):
|
2016-05-25 08:22:10 +00:00
|
|
|
'''
|
|
|
|
Test cases for salt.modules.rabbitmq
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {rabbitmq: {'__context__': {'rabbitmqctl': None, 'rabbitmq-plugins': None}}}
|
2017-02-19 15:35:30 +00:00
|
|
|
|
2016-05-24 10:05:39 +00:00
|
|
|
# 'list_users_rabbitmq2' function tests: 1
|
2016-05-25 08:22:10 +00:00
|
|
|
|
2016-05-24 10:05:39 +00:00
|
|
|
def test_list_users_rabbitmq2(self):
|
2016-05-25 08:22:10 +00:00
|
|
|
'''
|
|
|
|
Test if it return a list of users based off of rabbitmqctl user_list.
|
|
|
|
'''
|
2016-12-29 08:22:47 +00:00
|
|
|
mock_run = MagicMock(return_value={
|
|
|
|
'retcode': 0,
|
|
|
|
'stdout': 'Listing users ...\nguest\t[administrator, user]\njustAnAdmin\t[administrator]\n',
|
|
|
|
'stderr': '',
|
|
|
|
})
|
2016-09-29 13:38:42 +00:00
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-12-29 08:22:47 +00:00
|
|
|
self.assertDictEqual(rabbitmq.list_users(),
|
|
|
|
{'guest': ['administrator', 'user'], 'justAnAdmin': ['administrator']})
|
2016-05-25 08:22:10 +00:00
|
|
|
|
2016-05-24 10:05:39 +00:00
|
|
|
# 'list_users_rabbitmq3' function tests: 1
|
2016-05-25 08:22:10 +00:00
|
|
|
|
2016-05-24 10:05:39 +00:00
|
|
|
def test_list_users_rabbitmq3(self):
|
|
|
|
'''
|
|
|
|
Test if it return a list of users based off of rabbitmqctl user_list.
|
|
|
|
'''
|
2016-12-29 08:22:47 +00:00
|
|
|
mock_run = MagicMock(return_value={
|
|
|
|
'retcode': 0,
|
|
|
|
'stdout': 'guest\t[administrator user]\r\nother\t[a b]\r\n',
|
|
|
|
'stderr': ''
|
|
|
|
})
|
2016-09-29 13:38:42 +00:00
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-12-29 08:22:47 +00:00
|
|
|
self.assertDictEqual(rabbitmq.list_users(), {'guest': ['administrator', 'user'], 'other': ['a', 'b']})
|
2016-05-24 10:05:39 +00:00
|
|
|
|
|
|
|
# 'list_users_with_warning_rabbitmq2' function tests: 1
|
|
|
|
|
|
|
|
def test_list_users_with_warning_rabbitmq2(self):
|
|
|
|
'''
|
|
|
|
Test if having a leading WARNING returns the user_list anyway.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
rtn_stdout = '\n'.join([
|
2016-05-24 10:05:39 +00:00
|
|
|
'WARNING: ignoring /etc/rabbitmq/rabbitmq.conf -- location has moved to /etc/rabbitmq/rabbitmq-env.conf',
|
|
|
|
'Listing users ...',
|
|
|
|
'guest\t[administrator, user]\n',
|
|
|
|
])
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': rtn_stdout, 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 17:56:00 +00:00
|
|
|
self.assertDictEqual(rabbitmq.list_users(), {'guest': ['administrator', 'user']})
|
2016-05-24 10:05:39 +00:00
|
|
|
|
|
|
|
# 'list_users_with_warning_rabbitmq3' function tests: 1
|
|
|
|
|
|
|
|
def test_list_users_with_warning_rabbitmq3(self):
|
2016-05-25 08:22:10 +00:00
|
|
|
'''
|
|
|
|
Test if having a leading WARNING returns the user_list anyway.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
rtn_stdout = '\n'.join([
|
2016-05-25 08:22:10 +00:00
|
|
|
'WARNING: ignoring /etc/rabbitmq/rabbitmq.conf -- location has moved to /etc/rabbitmq/rabbitmq-env.conf',
|
|
|
|
'Listing users ...',
|
2016-05-24 10:05:39 +00:00
|
|
|
'guest\t[administrator user]\n',
|
2016-05-25 08:22:10 +00:00
|
|
|
])
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': rtn_stdout, 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 17:56:00 +00:00
|
|
|
self.assertDictEqual(rabbitmq.list_users(), {'guest': ['administrator', 'user']})
|
2016-05-25 08:22:10 +00:00
|
|
|
|
2016-12-29 08:22:47 +00:00
|
|
|
# 'list_vhosts' function tests: 2
|
2016-05-25 08:22:10 +00:00
|
|
|
|
|
|
|
def test_list_vhosts(self):
|
|
|
|
'''
|
|
|
|
Test if it return a list of vhost based on rabbitmqctl list_vhosts.
|
|
|
|
'''
|
2016-12-29 08:22:47 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': '/\nsaltstack\n...', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
|
|
|
self.assertListEqual(rabbitmq.list_vhosts(), ['/', 'saltstack', '...'])
|
|
|
|
|
|
|
|
def test_list_vhosts_with_warning(self):
|
|
|
|
'''
|
|
|
|
Test if it return a list of vhost based on rabbitmqctl list_vhosts even with a leading WARNING.
|
|
|
|
'''
|
|
|
|
rtn_stdout = '\n'.join([
|
|
|
|
'WARNING: ignoring /etc/rabbitmq/rabbitmq.conf -- location has moved to /etc/rabbitmq/rabbitmq-env.conf',
|
|
|
|
'Listing users ...',
|
|
|
|
'/',
|
|
|
|
'saltstack',
|
|
|
|
'...\n',
|
|
|
|
])
|
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': rtn_stdout, 'stderr': ''})
|
2016-09-29 13:38:42 +00:00
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-12-29 08:22:47 +00:00
|
|
|
self.assertListEqual(rabbitmq.list_vhosts(), ['/', 'saltstack', '...'])
|
2016-05-25 08:22:10 +00:00
|
|
|
|
|
|
|
# 'user_exists' function tests: 2
|
|
|
|
|
2016-12-29 08:22:47 +00:00
|
|
|
def test_user_exists(self):
|
2016-05-25 08:22:10 +00:00
|
|
|
'''
|
2016-12-29 08:22:47 +00:00
|
|
|
Test whether a given rabbitmq-internal user exists based
|
2016-05-25 08:22:10 +00:00
|
|
|
on rabbitmqctl list_users.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'Listing users ...\nsaltstack\t[administrator]\n...done', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-12-29 08:22:47 +00:00
|
|
|
self.assertTrue(rabbitmq.user_exists('saltstack'))
|
2016-05-25 08:22:10 +00:00
|
|
|
|
2016-12-29 08:22:47 +00:00
|
|
|
def test_user_exists_negative(self):
|
2016-05-25 08:22:10 +00:00
|
|
|
'''
|
2016-12-29 08:22:47 +00:00
|
|
|
Negative test of whether rabbitmq-internal user exists based
|
2016-05-25 08:22:10 +00:00
|
|
|
on rabbitmqctl list_users.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'Listing users ...\nsaltstack\t[administrator]\n...done', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-12-29 08:22:47 +00:00
|
|
|
self.assertFalse(rabbitmq.user_exists('salt'))
|
2016-05-25 08:22:10 +00:00
|
|
|
|
2016-12-29 08:22:47 +00:00
|
|
|
# 'vhost_exists' function tests: 2
|
2016-05-25 08:22:10 +00:00
|
|
|
|
|
|
|
def test_vhost_exists(self):
|
|
|
|
'''
|
|
|
|
Test if it return whether the vhost exists based
|
|
|
|
on rabbitmqctl list_vhosts.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'Listing vhosts ...\nsaltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertTrue(rabbitmq.vhost_exists('saltstack'))
|
|
|
|
|
2016-12-29 08:22:47 +00:00
|
|
|
def test_vhost_exists_negative(self):
|
|
|
|
'''
|
|
|
|
Test if it return whether the vhost exists based
|
|
|
|
on rabbitmqctl list_vhosts.
|
|
|
|
'''
|
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'Listing vhosts ...\nsaltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
|
|
|
self.assertFalse(rabbitmq.vhost_exists('salt'))
|
|
|
|
|
2016-05-25 08:22:10 +00:00
|
|
|
# 'add_user' function tests: 1
|
|
|
|
|
|
|
|
def test_add_user(self):
|
|
|
|
'''
|
|
|
|
Test if it add a rabbitMQ user via rabbitmqctl
|
|
|
|
user_add <user> <password>
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.add_user('saltstack'),
|
|
|
|
{'Added': 'saltstack'})
|
|
|
|
|
|
|
|
mock_run = MagicMock(return_value='Error')
|
2016-09-29 13:38:42 +00:00
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
with patch.object(rabbitmq, 'clear_password',
|
|
|
|
return_value={'Error': 'Error', 'retcode': 1}):
|
|
|
|
self.assertRaises(CommandExecutionError, rabbitmq.add_user, 'saltstack')
|
|
|
|
|
|
|
|
# 'delete_user' function tests: 1
|
|
|
|
|
|
|
|
def test_delete_user(self):
|
|
|
|
'''
|
|
|
|
Test if it deletes a user via rabbitmqctl delete_user.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.delete_user('saltstack'),
|
|
|
|
{'Deleted': 'saltstack'})
|
|
|
|
|
|
|
|
# 'change_password' function tests: 1
|
|
|
|
|
|
|
|
def test_change_password(self):
|
|
|
|
'''
|
|
|
|
Test if it changes a user's password.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.change_password('saltstack',
|
|
|
|
'salt@123'),
|
|
|
|
{'Password Changed': 'saltstack'})
|
|
|
|
|
|
|
|
# 'clear_password' function tests: 1
|
|
|
|
|
|
|
|
def test_clear_password(self):
|
|
|
|
'''
|
|
|
|
Test if it removes a user's password.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.clear_password('saltstack'),
|
|
|
|
{'Password Cleared': 'saltstack'})
|
|
|
|
|
|
|
|
# 'add_vhost' function tests: 1
|
|
|
|
|
|
|
|
def test_add_vhost(self):
|
|
|
|
'''
|
|
|
|
Test if it adds a vhost via rabbitmqctl add_vhost.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.add_vhost('saltstack'),
|
|
|
|
{'Added': 'saltstack'})
|
|
|
|
|
|
|
|
# 'delete_vhost' function tests: 1
|
|
|
|
|
|
|
|
def test_delete_vhost(self):
|
|
|
|
'''
|
|
|
|
Test if it deletes a vhost rabbitmqctl delete_vhost.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.delete_vhost('saltstack'),
|
|
|
|
{'Deleted': 'saltstack'})
|
|
|
|
|
|
|
|
# 'set_permissions' function tests: 1
|
|
|
|
|
|
|
|
def test_set_permissions(self):
|
|
|
|
'''
|
|
|
|
Test if it sets permissions for vhost via rabbitmqctl set_permissions.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.set_permissions('myvhost', 'myuser'),
|
|
|
|
{'Permissions Set': 'saltstack'})
|
|
|
|
|
2016-12-29 08:22:47 +00:00
|
|
|
# 'list_permissions' function tests: 1
|
|
|
|
|
|
|
|
def test_list_permissions(self):
|
|
|
|
'''
|
|
|
|
Test if it lists permissions for a vhost
|
|
|
|
via rabbitmqctl list_permissions.
|
|
|
|
'''
|
|
|
|
mock_run = MagicMock(return_value={
|
|
|
|
'retcode': 0,
|
|
|
|
'stdout': 'Listing stuff ...\nsaltstack\tsaltstack\t.*\t1\nguest\t0\tone\n...done',
|
|
|
|
'stderr': '',
|
|
|
|
})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
|
|
|
self.assertDictEqual(rabbitmq.list_user_permissions('myuser'),
|
|
|
|
{'saltstack': ['saltstack', '.*', '1'], 'guest': ['0', 'one']})
|
|
|
|
|
2016-05-25 08:22:10 +00:00
|
|
|
# 'list_user_permissions' function tests: 1
|
|
|
|
|
|
|
|
def test_list_user_permissions(self):
|
|
|
|
'''
|
|
|
|
Test if it list permissions for a user
|
|
|
|
via rabbitmqctl list_user_permissions.
|
|
|
|
'''
|
2016-12-29 08:22:47 +00:00
|
|
|
mock_run = MagicMock(return_value={
|
|
|
|
'retcode': 0,
|
|
|
|
'stdout': 'Listing stuff ...\nsaltstack\tsaltstack\t0\t1\nguest\t0\tone\n...done',
|
|
|
|
'stderr': '',
|
|
|
|
})
|
2016-09-29 13:38:42 +00:00
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.list_user_permissions('myuser'),
|
2016-12-29 08:22:47 +00:00
|
|
|
{'saltstack': ['saltstack', '0', '1'], 'guest': ['0', 'one']})
|
2016-05-25 08:22:10 +00:00
|
|
|
|
|
|
|
# 'set_user_tags' function tests: 1
|
|
|
|
|
|
|
|
def test_set_user_tags(self):
|
|
|
|
'''
|
|
|
|
Test if it add user tags via rabbitmqctl set_user_tags.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.set_user_tags('myadmin', 'admin'),
|
|
|
|
{'Tag(s) set': 'saltstack'})
|
|
|
|
|
|
|
|
# 'status' function tests: 1
|
|
|
|
|
|
|
|
def test_status(self):
|
|
|
|
'''
|
|
|
|
Test if it return rabbitmq status.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertEqual(rabbitmq.status(), 'saltstack')
|
|
|
|
|
|
|
|
# 'cluster_status' function tests: 1
|
|
|
|
|
|
|
|
def test_cluster_status(self):
|
|
|
|
'''
|
|
|
|
Test if it return rabbitmq cluster_status.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertEqual(rabbitmq.cluster_status(), 'saltstack')
|
|
|
|
|
|
|
|
# 'join_cluster' function tests: 1
|
|
|
|
|
|
|
|
def test_join_cluster(self):
|
|
|
|
'''
|
|
|
|
Test if it join a rabbit cluster.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.join_cluster('rabbit.example.com'),
|
|
|
|
{'Join': 'saltstack'})
|
|
|
|
|
|
|
|
# 'stop_app' function tests: 1
|
|
|
|
|
|
|
|
def test_stop_app(self):
|
|
|
|
'''
|
|
|
|
Test if it stops the RabbitMQ application,
|
|
|
|
leaving the Erlang node running.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertEqual(rabbitmq.stop_app(), 'saltstack')
|
|
|
|
|
|
|
|
# 'start_app' function tests: 1
|
|
|
|
|
|
|
|
def test_start_app(self):
|
|
|
|
'''
|
|
|
|
Test if it start the RabbitMQ application.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertEqual(rabbitmq.start_app(), 'saltstack')
|
|
|
|
|
|
|
|
# 'reset' function tests: 1
|
|
|
|
|
|
|
|
def test_reset(self):
|
|
|
|
'''
|
|
|
|
Test if it return a RabbitMQ node to its virgin state
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertEqual(rabbitmq.reset(), 'saltstack')
|
|
|
|
|
|
|
|
# 'force_reset' function tests: 1
|
|
|
|
|
|
|
|
def test_force_reset(self):
|
|
|
|
'''
|
|
|
|
Test if it forcefully Return a RabbitMQ node to its virgin state
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertEqual(rabbitmq.force_reset(), 'saltstack')
|
|
|
|
|
|
|
|
# 'list_queues' function tests: 1
|
|
|
|
|
|
|
|
def test_list_queues(self):
|
|
|
|
'''
|
|
|
|
Test if it returns queue details of the / virtual host
|
|
|
|
'''
|
2016-12-29 08:22:47 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack\t0\nceleryev.234-234\t10', 'stderr': ''})
|
2016-09-29 13:38:42 +00:00
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-12-29 08:22:47 +00:00
|
|
|
self.assertDictEqual(rabbitmq.list_queues(), {'saltstack': ['0'], 'celeryev.234-234': ['10']})
|
2016-05-25 08:22:10 +00:00
|
|
|
|
|
|
|
# 'list_queues_vhost' function tests: 1
|
|
|
|
|
|
|
|
def test_list_queues_vhost(self):
|
|
|
|
'''
|
|
|
|
Test if it returns queue details of specified virtual host.
|
|
|
|
'''
|
2016-12-29 08:22:47 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack\t0\nceleryev.234-234\t10', 'stderr': ''})
|
2016-09-29 13:38:42 +00:00
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-12-29 08:22:47 +00:00
|
|
|
self.assertDictEqual(rabbitmq.list_queues_vhost('consumers'), {'saltstack': ['0'], 'celeryev.234-234': ['10']})
|
2016-05-25 08:22:10 +00:00
|
|
|
|
|
|
|
# 'list_policies' function tests: 1
|
|
|
|
|
|
|
|
def test_list_policies(self):
|
|
|
|
'''
|
|
|
|
Test if it return a dictionary of policies nested by vhost
|
|
|
|
and name based on the data returned from rabbitmqctl list_policies.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.list_policies(), {})
|
|
|
|
|
|
|
|
# 'set_policy' function tests: 1
|
|
|
|
|
|
|
|
def test_set_policy(self):
|
|
|
|
'''
|
|
|
|
Test if it set a policy based on rabbitmqctl set_policy.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.set_policy('/', 'HA', '.*',
|
|
|
|
'{"ha-mode": "all"}'),
|
|
|
|
{'Set': 'saltstack'})
|
|
|
|
|
|
|
|
# 'delete_policy' function tests: 1
|
|
|
|
|
|
|
|
def test_delete_policy(self):
|
|
|
|
'''
|
|
|
|
Test if it delete a policy based on rabbitmqctl clear_policy.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertDictEqual(rabbitmq.delete_policy('/', 'HA'),
|
|
|
|
{'Deleted': 'saltstack'})
|
|
|
|
|
|
|
|
# 'policy_exists' function tests: 1
|
|
|
|
|
|
|
|
def test_policy_exists(self):
|
|
|
|
'''
|
|
|
|
Test if it return whether the policy exists
|
|
|
|
based on rabbitmqctl list_policies.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run}):
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertFalse(rabbitmq.policy_exists('/', 'HA'))
|
|
|
|
|
2016-12-29 08:22:47 +00:00
|
|
|
# 'list_available_plugins' function tests: 2
|
|
|
|
|
|
|
|
def test_list_available_plugins(self):
|
|
|
|
'''
|
|
|
|
Test if it returns a list of plugins.
|
|
|
|
'''
|
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack\nsalt\nother', 'stderr': ''})
|
|
|
|
mock_pkg = MagicMock(return_value='')
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run,
|
|
|
|
'pkg.version': mock_pkg}):
|
|
|
|
self.assertListEqual(rabbitmq.list_available_plugins(), ['saltstack', 'salt', 'other'])
|
|
|
|
|
|
|
|
def test_list_available_plugins_space_delimited(self):
|
|
|
|
'''
|
|
|
|
Test if it returns a list of plugins.
|
|
|
|
'''
|
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack salt other', 'stderr': ''})
|
|
|
|
mock_pkg = MagicMock(return_value='')
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run,
|
|
|
|
'pkg.version': mock_pkg}):
|
|
|
|
self.assertListEqual(rabbitmq.list_available_plugins(), ['saltstack', 'salt', 'other'])
|
|
|
|
|
|
|
|
# 'list_enabled_plugins' function tests: 2
|
|
|
|
|
|
|
|
def test_list_enabled_plugins(self):
|
|
|
|
'''
|
|
|
|
Test if it returns a list of plugins.
|
|
|
|
'''
|
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack\nsalt\nother', 'stderr': ''})
|
|
|
|
mock_pkg = MagicMock(return_value='')
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run,
|
|
|
|
'pkg.version': mock_pkg}):
|
|
|
|
self.assertListEqual(rabbitmq.list_enabled_plugins(), ['saltstack', 'salt', 'other'])
|
|
|
|
|
|
|
|
def test_list_enabled_plugins_space_delimited(self):
|
|
|
|
'''
|
|
|
|
Test if it returns a list of plugins.
|
|
|
|
'''
|
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack salt other', 'stderr': ''})
|
|
|
|
mock_pkg = MagicMock(return_value='')
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run,
|
|
|
|
'pkg.version': mock_pkg}):
|
|
|
|
self.assertListEqual(rabbitmq.list_enabled_plugins(), ['saltstack', 'salt', 'other'])
|
|
|
|
|
|
|
|
# 'plugin_is_enabled' function tests: 2
|
2016-05-25 08:22:10 +00:00
|
|
|
|
|
|
|
def test_plugin_is_enabled(self):
|
|
|
|
'''
|
2016-12-29 08:22:47 +00:00
|
|
|
Test if it returns true for an enabled plugin.
|
2016-05-25 08:22:10 +00:00
|
|
|
'''
|
2016-12-29 08:22:47 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack\nsalt\nother', 'stderr': ''})
|
2016-05-25 08:22:10 +00:00
|
|
|
mock_pkg = MagicMock(return_value='')
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run,
|
|
|
|
'pkg.version': mock_pkg}):
|
2016-12-29 08:22:47 +00:00
|
|
|
self.assertTrue(rabbitmq.plugin_is_enabled('saltstack'))
|
2016-05-25 08:22:10 +00:00
|
|
|
self.assertTrue(rabbitmq.plugin_is_enabled('salt'))
|
2016-12-29 08:22:47 +00:00
|
|
|
self.assertTrue(rabbitmq.plugin_is_enabled('other'))
|
|
|
|
|
|
|
|
def test_plugin_is_enabled_negative(self):
|
|
|
|
'''
|
|
|
|
Test if it returns false for a disabled plugin.
|
|
|
|
'''
|
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack\nother', 'stderr': ''})
|
|
|
|
mock_pkg = MagicMock(return_value='')
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run,
|
|
|
|
'pkg.version': mock_pkg}):
|
|
|
|
self.assertFalse(rabbitmq.plugin_is_enabled('salt'))
|
|
|
|
self.assertFalse(rabbitmq.plugin_is_enabled('stack'))
|
|
|
|
self.assertFalse(rabbitmq.plugin_is_enabled('random'))
|
2016-05-25 08:22:10 +00:00
|
|
|
|
|
|
|
# 'enable_plugin' function tests: 1
|
|
|
|
|
|
|
|
def test_enable_plugin(self):
|
|
|
|
'''
|
|
|
|
Test if it enable a RabbitMQ plugin via the rabbitmq-plugins command.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
2016-05-25 08:22:10 +00:00
|
|
|
mock_pkg = MagicMock(return_value='')
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run,
|
|
|
|
'pkg.version': mock_pkg}):
|
|
|
|
self.assertDictEqual(rabbitmq.enable_plugin('salt'),
|
|
|
|
{'Enabled': 'saltstack'})
|
|
|
|
|
|
|
|
# 'disable_plugin' function tests: 1
|
|
|
|
|
|
|
|
def test_disable_plugin(self):
|
|
|
|
'''
|
|
|
|
Test if it disable a RabbitMQ plugin via the rabbitmq-plugins command.
|
|
|
|
'''
|
2016-09-29 13:38:42 +00:00
|
|
|
mock_run = MagicMock(return_value={'retcode': 0, 'stdout': 'saltstack', 'stderr': ''})
|
2016-05-25 08:22:10 +00:00
|
|
|
mock_pkg = MagicMock(return_value='')
|
|
|
|
with patch.dict(rabbitmq.__salt__, {'cmd.run_all': mock_run,
|
|
|
|
'pkg.version': mock_pkg}):
|
|
|
|
self.assertDictEqual(rabbitmq.disable_plugin('salt'),
|
|
|
|
{'Disabled': 'saltstack'})
|