| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
from __future__ import (absolute_import, division, print_function) |
| 4 |
__metaclass__ = type |
| 5 |
|
| 6 |
from ansible_collections.community.rabbitmq.plugins.modules import rabbitmq_upgrade |
| 7 |
|
| 8 |
from ansible_collections.community.rabbitmq.tests.unit.compat.mock import patch |
| 9 |
from ansible_collections.community.rabbitmq.tests.unit.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args |
| 10 |
|
| 11 |
|
| 12 |
class TestRabbitMQUpgradeModule(ModuleTestCase): |
| 13 |
def setUp(self): |
| 14 |
super(TestRabbitMQUpgradeModule, self).setUp() |
| 15 |
self.module = rabbitmq_upgrade |
| 16 |
|
| 17 |
def tearDown(self): |
| 18 |
super(TestRabbitMQUpgradeModule, self).tearDown() |
| 19 |
|
| 20 |
def _assert(self, exc, attribute, expected_value, msg=''): |
| 21 |
value = exc.message[attribute] if hasattr(exc, attribute) else exc.args[0][attribute] |
| 22 |
assert value == expected_value, msg |
| 23 |
|
| 24 |
def test_without_required_parameters(self): |
| 25 |
"""Failure must occur when all parameters are missing.""" |
| 26 |
with self.assertRaises(AnsibleFailJson): |
| 27 |
with set_module_args({}): |
| 28 |
self.module.main() |
| 29 |
|
| 30 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 31 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_upgrade.RabbitMqUpgrade._exec') |
| 32 |
def test_without_maitenance_mode_status_feature_flag(self, _exec, get_bin_path): |
| 33 |
"""Failure must occur when maintenance_mode_status feature_flag is disabled/not available""" |
| 34 |
with self.assertRaises(AnsibleFailJson): |
| 35 |
with set_module_args({ |
| 36 |
'action': 'drain', |
| 37 |
'node': 'rabbit@node-1', |
| 38 |
}): |
| 39 |
get_bin_path.return_value = '/rabbitmqctl' |
| 40 |
|
| 41 |
def side_effect(*args, **kwargs): |
| 42 |
if args[0] == 'rabbitmq-diagnostics': |
| 43 |
out = '{"active_plugins": ["rabbitmq_management", "amqp_client", "rabbitmq_web_dispatch", "cowboy",'\ |
| 44 |
'"cowlib", "rabbitmq_management_agent"], "is_under_maintenance": false}' |
| 45 |
elif args[0] == 'rabbitmqctl': |
| 46 |
out = 'name\tstate\nmaintenance_mode_status\tdisabled' |
| 47 |
else: |
| 48 |
out = '' |
| 49 |
return out.splitlines() |
| 50 |
|
| 51 |
_exec.side_effect = side_effect |
| 52 |
self.module.main() |
| 53 |
|
| 54 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 55 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_upgrade.RabbitMqUpgrade._exec') |
| 56 |
def test_drain_node(self, _exec, get_bin_path): |
| 57 |
"""Execute action: drain on active node""" |
| 58 |
with set_module_args({ |
| 59 |
'action': 'drain', |
| 60 |
'node': 'rabbit@node-1', |
| 61 |
}): |
| 62 |
get_bin_path.return_value = '/rabbitmqctl' |
| 63 |
|
| 64 |
def side_effect(*args, **kwargs): |
| 65 |
if args[0] == 'rabbitmq-diagnostics': |
| 66 |
out = '{"active_plugins": ["rabbitmq_management", "amqp_client", "rabbitmq_web_dispatch", "cowboy",'\ |
| 67 |
'"cowlib", "rabbitmq_management_agent"], "is_under_maintenance": false}' |
| 68 |
elif args[0] == 'rabbitmqctl': |
| 69 |
out = 'name\tstate\nmaintenance_mode_status\tenabled' |
| 70 |
else: |
| 71 |
out = '' |
| 72 |
return out.splitlines() |
| 73 |
|
| 74 |
_exec.side_effect = side_effect |
| 75 |
try: |
| 76 |
self.module.main() |
| 77 |
except AnsibleExitJson as e: |
| 78 |
self._assert(e, 'changed', True) |
| 79 |
|
| 80 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 81 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_upgrade.RabbitMqUpgrade._exec') |
| 82 |
def test_no_change_drain_node(self, _exec, get_bin_path): |
| 83 |
"""Execute action: drain on already disabled node""" |
| 84 |
with set_module_args({ |
| 85 |
'action': 'drain', |
| 86 |
'node': 'rabbit@node-1', |
| 87 |
}): |
| 88 |
get_bin_path.return_value = '/rabbitmqctl' |
| 89 |
|
| 90 |
def side_effect(*args, **kwargs): |
| 91 |
if args[0] == 'rabbitmq-diagnostics': |
| 92 |
out = '{"active_plugins": ["rabbitmq_management", "amqp_client", "rabbitmq_web_dispatch", "cowboy",'\ |
| 93 |
'"cowlib", "rabbitmq_management_agent"], "is_under_maintenance": true}' |
| 94 |
elif args[0] == 'rabbitmqctl': |
| 95 |
out = 'name\tstate\nmaintenance_mode_status\tenabled' |
| 96 |
else: |
| 97 |
out = '' |
| 98 |
return out.splitlines() |
| 99 |
|
| 100 |
_exec.side_effect = side_effect |
| 101 |
try: |
| 102 |
self.module.main() |
| 103 |
except AnsibleExitJson as e: |
| 104 |
self._assert(e, 'changed', False) |
| 105 |
|
| 106 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 107 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_upgrade.RabbitMqUpgrade._exec') |
| 108 |
def test_revive_node(self, _exec, get_bin_path): |
| 109 |
"""Execute action: revive on disabled node""" |
| 110 |
with set_module_args({ |
| 111 |
'action': 'revive', |
| 112 |
'node': 'rabbit@node-1', |
| 113 |
}): |
| 114 |
get_bin_path.return_value = '/rabbitmqctl' |
| 115 |
|
| 116 |
def side_effect(*args, **kwargs): |
| 117 |
if args[0] == 'rabbitmq-diagnostics': |
| 118 |
out = '{"active_plugins": ["rabbitmq_management", "amqp_client", "rabbitmq_web_dispatch", "cowboy",'\ |
| 119 |
'"cowboy", "cowlib", "rabbitmq_management_agent"], "is_under_maintenance": true}' |
| 120 |
elif args[0] == 'rabbitmqctl': |
| 121 |
out = 'name\tstate\nmaintenance_mode_status\tenabled' |
| 122 |
else: |
| 123 |
out = '' |
| 124 |
return out.splitlines() |
| 125 |
|
| 126 |
_exec.side_effect = side_effect |
| 127 |
try: |
| 128 |
self.module.main() |
| 129 |
except AnsibleExitJson as e: |
| 130 |
self._assert(e, 'changed', True) |
| 131 |
|
| 132 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 133 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_upgrade.RabbitMqUpgrade._exec') |
| 134 |
def test_no_change_revive_node(self, _exec, get_bin_path): |
| 135 |
"""Execute action: revive on active node""" |
| 136 |
with set_module_args({ |
| 137 |
'action': 'revive', |
| 138 |
'node': 'rabbit@node-1', |
| 139 |
}): |
| 140 |
get_bin_path.return_value = '/rabbitmqctl' |
| 141 |
|
| 142 |
def side_effect(*args, **kwargs): |
| 143 |
if args[0] == 'rabbitmq-diagnostics': |
| 144 |
out = '{"active_plugins": ["rabbitmq_management", "amqp_client", "rabbitmq_web_dispatch", "cowboy",'\ |
| 145 |
'"cowlib", "rabbitmq_management_agent"], "is_under_maintenance": false}' |
| 146 |
elif args[0] == 'rabbitmqctl': |
| 147 |
out = 'name\tstate\nmaintenance_mode_status\tenabled' |
| 148 |
else: |
| 149 |
out = '' |
| 150 |
return out.splitlines() |
| 151 |
|
| 152 |
_exec.side_effect = side_effect |
| 153 |
try: |
| 154 |
self.module.main() |
| 155 |
except AnsibleExitJson as e: |
| 156 |
self._assert(e, 'changed', False) |
| 157 |
|
| 158 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 159 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_upgrade.RabbitMqUpgrade._exec') |
| 160 |
def test_await_online_quorum_plus_one(self, _exec, get_bin_path): |
| 161 |
"""Execute action: await_online_quorum_plus_one""" |
| 162 |
with set_module_args({ |
| 163 |
'action': 'await_online_quorum_plus_one', |
| 164 |
'node': 'rabbit@node-1', |
| 165 |
}): |
| 166 |
get_bin_path.return_value = '/rabbitmqctl' |
| 167 |
|
| 168 |
try: |
| 169 |
self.module.main() |
| 170 |
except AnsibleExitJson as e: |
| 171 |
self._assert(e, 'changed', True) |
| 172 |
|
| 173 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 174 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_upgrade.RabbitMqUpgrade._exec') |
| 175 |
def test_await_online_synchronized_mirror(self, _exec, get_bin_path): |
| 176 |
"""Execute action: await_online_synchronized_mirror""" |
| 177 |
with set_module_args({ |
| 178 |
'action': 'await_online_synchronized_mirror', |
| 179 |
'node': 'rabbit@node-1', |
| 180 |
}): |
| 181 |
get_bin_path.return_value = '/rabbitmqctl' |
| 182 |
|
| 183 |
try: |
| 184 |
self.module.main() |
| 185 |
except AnsibleExitJson as e: |
| 186 |
self._assert(e, 'changed', True) |
| 187 |
|
| 188 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 189 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_upgrade.RabbitMqUpgrade._exec') |
| 190 |
def test_post_upgrade(self, _exec, get_bin_path): |
| 191 |
"""Execute action: post_upgrade""" |
| 192 |
with set_module_args({ |
| 193 |
'action': 'post_upgrade', |
| 194 |
'node': 'rabbit@node-1', |
| 195 |
}): |
| 196 |
get_bin_path.return_value = '/rabbitmqctl' |
| 197 |
|
| 198 |
try: |
| 199 |
self.module.main() |
| 200 |
except AnsibleExitJson as e: |
| 201 |
self._assert(e, 'changed', True) |