| 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.module_utils import version |
| 7 |
from ansible_collections.community.rabbitmq.plugins.modules import rabbitmq_user |
| 8 |
from ansible.module_utils import six |
| 9 |
from itertools import chain |
| 10 |
|
| 11 |
if six.PY3: |
| 12 |
from itertools import zip_longest |
| 13 |
else: |
| 14 |
from itertools import izip_longest as zip_longest |
| 15 |
|
| 16 |
from ansible_collections.community.rabbitmq.tests.unit.compat.mock import patch |
| 17 |
from ansible_collections.community.rabbitmq.tests.unit.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args |
| 18 |
|
| 19 |
from ansible_collections.community.rabbitmq.tests.unit.modules.rabbitmq_user_fixtures import ( |
| 20 |
rabbitmq_3_6_status, |
| 21 |
rabbitmq_3_7_status, |
| 22 |
rabbitmq_3_8_status) |
| 23 |
|
| 24 |
|
| 25 |
def flatten(args): |
| 26 |
return list(chain(*args)) |
| 27 |
|
| 28 |
|
| 29 |
def lists_equal(l1, l2): |
| 30 |
return all(map(lambda t: t[0] == t[1], zip_longest(l1, l2))) |
| 31 |
|
| 32 |
|
| 33 |
class TestRabbitMQUserModule(ModuleTestCase): |
| 34 |
def setUp(self): |
| 35 |
super(TestRabbitMQUserModule, self).setUp() |
| 36 |
self.module = rabbitmq_user |
| 37 |
|
| 38 |
def tearDown(self): |
| 39 |
super(TestRabbitMQUserModule, self).tearDown() |
| 40 |
|
| 41 |
def _assert(self, exc, attribute, expected_value, msg=''): |
| 42 |
value = exc.message[attribute] if hasattr(exc, attribute) else exc.args[0][attribute] |
| 43 |
assert value == expected_value, msg |
| 44 |
|
| 45 |
def test_without_required_parameters(self): |
| 46 |
"""Failure must occur when all parameters are missing.""" |
| 47 |
with self.assertRaises(AnsibleFailJson): |
| 48 |
with set_module_args({}): |
| 49 |
self.module.main() |
| 50 |
|
| 51 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 52 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._check_version') |
| 53 |
def test_permissions_with_same_vhost(self, _check_version, get_bin_path): |
| 54 |
with set_module_args({ |
| 55 |
'user': 'someuser', |
| 56 |
'password': 'somepassword', |
| 57 |
'state': 'present', |
| 58 |
'permissions': [{'vhost': '/'}, {'vhost': '/'}], |
| 59 |
}): |
| 60 |
_check_version.return_value = version.StrictVersion('3.6.10') |
| 61 |
get_bin_path.return_value = '/rabbitmqctl' |
| 62 |
try: |
| 63 |
self.module.main() |
| 64 |
except AnsibleFailJson as e: |
| 65 |
self._assert(e, 'failed', True) |
| 66 |
self._assert(e, 'msg', "Error parsing vhost " |
| 67 |
"permissions: You can't have two permission dicts for the same vhost") |
| 68 |
|
| 69 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 70 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._check_version') |
| 71 |
def test_topic_permissions_with_same_vhost(self, _check_version, get_bin_path): |
| 72 |
with set_module_args({ |
| 73 |
'user': 'someuser', |
| 74 |
'password': 'somepassword', |
| 75 |
'state': 'present', |
| 76 |
'topic_permissions': [{'vhost': '/', 'exchange': 'amq.topic'}, {'vhost': '/', 'exchange': 'amq.topic'}], |
| 77 |
}): |
| 78 |
_check_version.return_value = version.StrictVersion('3.6.10') |
| 79 |
get_bin_path.return_value = '/rabbitmqctl' |
| 80 |
try: |
| 81 |
self.module.main() |
| 82 |
except AnsibleFailJson as e: |
| 83 |
self._assert(e, 'failed', True) |
| 84 |
self._assert(e, 'msg', "Error parsing vhost topic_permissions: " |
| 85 |
"You can't have two topic permission dicts for " |
| 86 |
"the same vhost and the same exchange") |
| 87 |
|
| 88 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 89 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser.get') |
| 90 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._check_version') |
| 91 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser.check_password') |
| 92 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser.has_tags_modifications') |
| 93 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser.has_permissions_modifications') |
| 94 |
def test_password_changes_only_when_needed(self, |
| 95 |
has_permissions_modifications, |
| 96 |
has_tags_modifications, |
| 97 |
check_password, |
| 98 |
_check_version, |
| 99 |
get, |
| 100 |
get_bin_path): |
| 101 |
with set_module_args({ |
| 102 |
'user': 'someuser', |
| 103 |
'password': 'somepassword', |
| 104 |
'state': 'present', |
| 105 |
'update_password': 'always', |
| 106 |
}): |
| 107 |
get.return_value = True |
| 108 |
_check_version.return_value = version.StrictVersion('3.6.10') |
| 109 |
get_bin_path.return_value = '/rabbitmqctl' |
| 110 |
check_password.return_value = True |
| 111 |
has_tags_modifications.return_value = False |
| 112 |
has_permissions_modifications.return_value = False |
| 113 |
try: |
| 114 |
self.module.main() |
| 115 |
except AnsibleExitJson as e: |
| 116 |
self._assert(e, 'changed', False) |
| 117 |
self._assert(e, 'state', 'present') |
| 118 |
|
| 119 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 120 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._exec') |
| 121 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._check_version') |
| 122 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._get_permissions') |
| 123 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser.has_tags_modifications') |
| 124 |
def test_same_permissions_not_changing(self, |
| 125 |
has_tags_modifications, |
| 126 |
_get_permissions, |
| 127 |
_check_version, |
| 128 |
_exec, |
| 129 |
get_bin_path): |
| 130 |
with set_module_args({ |
| 131 |
'user': 'someuser', |
| 132 |
'password': 'somepassword', |
| 133 |
'state': 'present', |
| 134 |
'permissions': [{'vhost': '/', 'configure_priv': '.*', 'write_priv': '.*', 'read_priv': '.*'}], |
| 135 |
}): |
| 136 |
_get_permissions.return_value = {'/': {'read': '.*', 'write': '.*', 'configure': '.*', 'vhost': '/'}} |
| 137 |
_exec.return_value = 'someuser\t[]' |
| 138 |
_check_version.return_value = version.StrictVersion('3.6.10') |
| 139 |
get_bin_path.return_value = '/rabbitmqctl' |
| 140 |
has_tags_modifications.return_value = False |
| 141 |
try: |
| 142 |
self.module.main() |
| 143 |
except AnsibleExitJson as e: |
| 144 |
self._assert(e, 'changed', False) |
| 145 |
self._assert(e, 'state', 'present') |
| 146 |
|
| 147 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 148 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._exec') |
| 149 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._check_version') |
| 150 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._get_topic_permissions') |
| 151 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._get_permissions') |
| 152 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser.has_tags_modifications') |
| 153 |
def test_same_topic_permissions_not_changing(self, |
| 154 |
has_tags_modifications, |
| 155 |
_get_permissions, |
| 156 |
_get_topic_permissions, |
| 157 |
_check_version, |
| 158 |
_exec, |
| 159 |
get_bin_path): |
| 160 |
with set_module_args({ |
| 161 |
'user': 'someuser', |
| 162 |
'password': 'somepassword', |
| 163 |
'state': 'present', |
| 164 |
'topic_permissions': [{'vhost': '/', 'exchange': 'amq.topic', 'write': '.*', 'read': '.*'}], |
| 165 |
}): |
| 166 |
_get_permissions.return_value = {'/': {'configure': '^$', 'read': '^$', 'write': '^$', 'vhost': '/'}} |
| 167 |
_get_topic_permissions.return_value = {('/', 'amq.topic'): {'read': '.*', 'write': '.*', 'vhost': '/', 'exchange': 'amq.topic'}} |
| 168 |
_exec.return_value = 'someuser\t[]' |
| 169 |
_check_version.return_value = version.StrictVersion('3.6.10') |
| 170 |
get_bin_path.return_value = '/rabbitmqctl' |
| 171 |
has_tags_modifications.return_value = False |
| 172 |
try: |
| 173 |
self.module.main() |
| 174 |
except AnsibleExitJson as e: |
| 175 |
self._assert(e, 'changed', False) |
| 176 |
self._assert(e, 'state', 'present') |
| 177 |
|
| 178 |
@patch('ansible.module_utils.basic.AnsibleModule') |
| 179 |
def test_status_can_be_parsed(self, module): |
| 180 |
"""Test correct parsing of the output of the status command.""" |
| 181 |
module.get_bin_path.return_value = '/rabbitmqctl' |
| 182 |
module.check_mode = False |
| 183 |
|
| 184 |
versions = ['3.6.10', '3.6.16'] |
| 185 |
for version_num in versions: |
| 186 |
def side_effect(args): |
| 187 |
assert '-q' in args |
| 188 |
if '--formatter' in args: |
| 189 |
return 64, '', '' |
| 190 |
return 0, rabbitmq_3_6_status.replace('version_num', version_num), '' |
| 191 |
|
| 192 |
module.run_command.side_effect = side_effect |
| 193 |
user_controller = rabbitmq_user.RabbitMqUser(module, 'someuser', 'somepassword', list(), list(), list(), 'rabbit') |
| 194 |
self.assertEqual(len(module.run_command.call_args_list), 2) |
| 195 |
last_call_args = flatten(module.run_command.call_args_list[-1][0]) |
| 196 |
self.assertTrue('-q' in last_call_args) |
| 197 |
self.assertTrue('--formatter' not in last_call_args) |
| 198 |
self.assertEqual(user_controller._version, version.StrictVersion(version_num)) |
| 199 |
module.run_command.reset_mock() |
| 200 |
|
| 201 |
versions = ['3.7.6', '3.7.7', '3.7.8', '3.7.9', '3.7.10', '3.7.11', '3.7.12', '3.7.13', '3.7.14', '3.7.15', |
| 202 |
'3.7.16', '3.7.17', '3.7.18', '3.7.19', '3.7.20', '3.7.21', '3.7.22', '3.7.23'] |
| 203 |
for version_num in versions: |
| 204 |
def side_effect(args): |
| 205 |
self.assertTrue('-q' in args) |
| 206 |
self.assertTrue('--formatter' in args) |
| 207 |
self.assertTrue('json' in args) |
| 208 |
return 0, rabbitmq_3_7_status.replace('version_num', str([ord(c) for c in version_num])), '' |
| 209 |
|
| 210 |
module.run_command.side_effect = side_effect |
| 211 |
user_controller = rabbitmq_user.RabbitMqUser(module, 'someuser', 'somepassword', list(), list(), list(), 'rabbit') |
| 212 |
self.assertEqual(1, module.run_command.call_count) |
| 213 |
self.assertEqual(user_controller._version, version.StrictVersion(version_num)) |
| 214 |
module.run_command.reset_mock() |
| 215 |
|
| 216 |
versions = ['3.8.0', '3.8.1', '3.8.2'] |
| 217 |
for version_num in versions: |
| 218 |
def side_effect(args): |
| 219 |
self.assertTrue('-q' in args) |
| 220 |
self.assertTrue('--formatter' in args) |
| 221 |
self.assertTrue('json' in args) |
| 222 |
return 0, rabbitmq_3_8_status.replace('version_num', version_num), '' |
| 223 |
|
| 224 |
module.run_command.side_effect = side_effect |
| 225 |
user_controller = rabbitmq_user.RabbitMqUser(module, 'someuser', 'somepassword', list(), list(), list(), 'rabbit') |
| 226 |
self.assertEqual(1, module.run_command.call_count) |
| 227 |
self.assertEqual(user_controller._version, version.StrictVersion(version_num)) |
| 228 |
module.run_command.reset_mock() |
| 229 |
|
| 230 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 231 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._exec') |
| 232 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._check_version') |
| 233 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._get_permissions') |
| 234 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser.has_tags_modifications') |
| 235 |
def test_permissions_are_fixed(self, |
| 236 |
has_tags_modifications, |
| 237 |
_get_permissions, |
| 238 |
_check_version, |
| 239 |
_exec, |
| 240 |
get_bin_path): |
| 241 |
"""Test changes in permissions are fixed. |
| 242 |
|
| 243 |
Ensure that permissions that do not need to be changed are not, permissions with differences are |
| 244 |
fixed and permissions are cleared when needed, with the minimum number of operations. The |
| 245 |
permissions are fed into the module using the pre-3.7 version format. |
| 246 |
""" |
| 247 |
with set_module_args({ |
| 248 |
'user': 'someuser', |
| 249 |
'password': 'somepassword', |
| 250 |
'state': 'present', |
| 251 |
'permissions': [ |
| 252 |
{'vhost': '/', 'configure_priv': '.*', 'write_priv': '.*', 'read_priv': '.*'}, |
| 253 |
{'vhost': '/ok', 'configure': '^$', 'write': '^$', 'read': '^$'} |
| 254 |
], |
| 255 |
}): |
| 256 |
get_bin_path.return_value = '/rabbitmqctl' |
| 257 |
has_tags_modifications.return_value = False |
| 258 |
_check_version.return_value = version.StrictVersion('3.6.10') |
| 259 |
_get_permissions.return_value = { |
| 260 |
'/wrong_vhost': {'vhost': '/wrong_vhost', 'configure': '', 'write': '', 'read': ''}, |
| 261 |
'/ok': {'vhost': '/ok', 'configure': '^$', 'write': '^$', 'read': '^$'} |
| 262 |
} |
| 263 |
|
| 264 |
def side_effect(args): |
| 265 |
if 'list_users' in args: |
| 266 |
self.assertTrue('--formatter' not in args) |
| 267 |
self.assertTrue('json' not in args) |
| 268 |
return 'someuser\t[administrator, management]' |
| 269 |
if 'clear_permissions' in args: |
| 270 |
self.assertTrue('someuser' in args) |
| 271 |
self.assertTrue('/wrong_vhost' in args) |
| 272 |
return '' |
| 273 |
if 'set_permissions' in args: |
| 274 |
self.assertTrue('someuser' in args) |
| 275 |
self.assertTrue('/' in args) |
| 276 |
self.assertTrue(['.*', '.*', '.*'] == args[-3:]) |
| 277 |
return '' |
| 278 |
_exec.side_effect = side_effect |
| 279 |
|
| 280 |
try: |
| 281 |
self.module.main() |
| 282 |
except AnsibleExitJson as e: |
| 283 |
self._assert(e, 'changed', True) |
| 284 |
self._assert(e, 'state', 'present') |
| 285 |
self.assertEqual(_exec.call_count, 3) |
| 286 |
self.assertTrue(['clear_permissions', '-p', '/wrong_vhost', 'someuser'] == |
| 287 |
flatten(_exec.call_args_list[-2][0])) |
| 288 |
self.assertTrue(['set_permissions', '-p', '/', 'someuser', '.*', '.*', '.*'] == |
| 289 |
flatten(_exec.call_args_list[-1][0])) |
| 290 |
|
| 291 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 292 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._exec') |
| 293 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._check_version') |
| 294 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._get_topic_permissions') |
| 295 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._get_permissions') |
| 296 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser.has_tags_modifications') |
| 297 |
def test_topic_permissions_are_fixed(self, |
| 298 |
has_tags_modifications, |
| 299 |
_get_permissions, |
| 300 |
_get_topic_permissions, |
| 301 |
_check_version, |
| 302 |
_exec, |
| 303 |
get_bin_path): |
| 304 |
"""Test changes in topic permissions are fixed. |
| 305 |
|
| 306 |
Ensure that topic permissions that do not need to be changed are not, topic permissions with differences are |
| 307 |
fixed and topic permissions are cleared when needed, with the minimum number of operations. |
| 308 |
""" |
| 309 |
with set_module_args({ |
| 310 |
'user': 'someuser', |
| 311 |
'password': 'somepassword', |
| 312 |
'state': 'present', |
| 313 |
'topic_permissions': [ |
| 314 |
{'vhost': '/', 'exchange': 'amq.topic', 'write_priv': '.*', 'read_priv': '.*'}, |
| 315 |
{'vhost': '/ok', 'exchange': 'amq.topic', 'write': '^$', 'read': '^$'} |
| 316 |
], |
| 317 |
}): |
| 318 |
get_bin_path.return_value = '/rabbitmqctl' |
| 319 |
has_tags_modifications.return_value = False |
| 320 |
_check_version.return_value = version.StrictVersion('3.6.10') |
| 321 |
_get_permissions.return_value = {} |
| 322 |
_get_topic_permissions.return_value = { |
| 323 |
('/wrong_vhost', 'amq.topic'): {'vhost': '/wrong_vhost', 'exchange': 'amq.topic', 'write': '', 'read': ''}, |
| 324 |
('/ok', 'amq.topic'): {'vhost': '/ok', 'exchange': 'amq.topic', 'write': '^$', 'read': '^$'} |
| 325 |
} |
| 326 |
|
| 327 |
def side_effect(args): |
| 328 |
if 'list_users' in args: |
| 329 |
self.assertTrue('--formatter' not in args) |
| 330 |
self.assertTrue('json' not in args) |
| 331 |
return 'someuser\t[administrator, management]' |
| 332 |
if 'clear_topic_permissions' in args: |
| 333 |
self.assertTrue('someuser' in args) |
| 334 |
self.assertTrue('/wrong_vhost' in args) |
| 335 |
return '' |
| 336 |
if 'set_topic_permissions' in args: |
| 337 |
self.assertTrue('someuser' in args) |
| 338 |
self.assertTrue('/' in args, args) |
| 339 |
self.assertTrue(['amq.topic', '.*', '.*'] == args[-3:]) |
| 340 |
return '' |
| 341 |
_exec.side_effect = side_effect |
| 342 |
|
| 343 |
try: |
| 344 |
self.module.main() |
| 345 |
except AnsibleExitJson as e: |
| 346 |
self._assert(e, 'changed', True) |
| 347 |
self._assert(e, 'state', 'present') |
| 348 |
self.assertEqual(_exec.call_count, 4) |
| 349 |
self.assertTrue(['clear_topic_permissions', '-p', '/wrong_vhost', 'someuser', 'amq.topic'] == |
| 350 |
flatten(_exec.call_args_list[-2][0])) |
| 351 |
self.assertTrue(['set_topic_permissions', '-p', '/', 'someuser', 'amq.topic', '.*', '.*'] == |
| 352 |
flatten(_exec.call_args_list[-1][0])) |
| 353 |
|
| 354 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 355 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._exec') |
| 356 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._check_version') |
| 357 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._get_topic_permissions') |
| 358 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._get_permissions') |
| 359 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser.has_tags_modifications') |
| 360 |
def test_topic_permissions_defaults(self, |
| 361 |
has_tags_modifications, |
| 362 |
_get_permissions, |
| 363 |
_get_topic_permissions, |
| 364 |
_check_version, |
| 365 |
_exec, |
| 366 |
get_bin_path): |
| 367 |
"""Test that the topic permissions defaults are set.""" |
| 368 |
with set_module_args({ |
| 369 |
'user': 'someuser', |
| 370 |
'password': 'somepassword', |
| 371 |
'state': 'present', |
| 372 |
'topic_permissions': [ |
| 373 |
{'write_priv': '.*', 'read_priv': '.*'}, |
| 374 |
], |
| 375 |
}): |
| 376 |
get_bin_path.return_value = '/rabbitmqctl' |
| 377 |
has_tags_modifications.return_value = False |
| 378 |
_check_version.return_value = version.StrictVersion('3.6.10') |
| 379 |
_get_permissions.return_value = {} |
| 380 |
_get_topic_permissions.return_value = {} |
| 381 |
|
| 382 |
def side_effect(args): |
| 383 |
if 'list_users' in args: |
| 384 |
self.assertTrue('--formatter' not in args) |
| 385 |
self.assertTrue('json' not in args) |
| 386 |
return 'someuser\t[administrator, management]' |
| 387 |
if 'set_topic_permissions' in args: |
| 388 |
self.assertTrue('someuser' in args) |
| 389 |
self.assertTrue('/' in args, args) |
| 390 |
self.assertTrue(['amq.topic', '.*', '.*'] == args[-3:]) |
| 391 |
return '' |
| 392 |
_exec.side_effect = side_effect |
| 393 |
|
| 394 |
try: |
| 395 |
self.module.main() |
| 396 |
except AnsibleExitJson as e: |
| 397 |
self._assert(e, 'changed', True) |
| 398 |
self._assert(e, 'state', 'present') |
| 399 |
self.assertEqual(_exec.call_count, 3) |
| 400 |
self.assertTrue(['set_topic_permissions', '-p', '/', 'someuser', 'amq.topic', '.*', '.*'] == |
| 401 |
flatten(_exec.call_args_list[-1][0])) |
| 402 |
|
| 403 |
@patch('ansible.module_utils.basic.AnsibleModule.get_bin_path') |
| 404 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._exec') |
| 405 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._check_version') |
| 406 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._get_permissions') |
| 407 |
def test_tags_are_fixed(self, _get_permissions, _check_version, _exec, get_bin_path): |
| 408 |
"""Test user tags are fixed.""" |
| 409 |
with set_module_args({ |
| 410 |
'user': 'someuser', |
| 411 |
'password': 'somepassword', |
| 412 |
'state': 'present', |
| 413 |
'tags': 'tag1,tags2', |
| 414 |
}): |
| 415 |
get_bin_path.return_value = '/rabbitmqctl' |
| 416 |
_check_version.return_value = version.StrictVersion('3.6.10') |
| 417 |
_get_permissions.return_value = {'/': {'vhost': '/', 'configure': '^$', 'write': '^$', 'read': '^$'}} |
| 418 |
|
| 419 |
def side_effect(args): |
| 420 |
if 'list_users' in args: |
| 421 |
self.assertTrue('--formatter' not in args) |
| 422 |
self.assertTrue('json' not in args) |
| 423 |
return 'someuser\t[tag1, tag3]' |
| 424 |
return '' |
| 425 |
_exec.side_effect = side_effect |
| 426 |
|
| 427 |
try: |
| 428 |
self.module.main() |
| 429 |
except AnsibleExitJson as e: |
| 430 |
self._assert(e, 'changed', True) |
| 431 |
self._assert(e, 'state', 'present') |
| 432 |
self.assertEqual(_exec.call_count, 2) |
| 433 |
self.assertTrue(lists_equal(['set_user_tags', 'someuser', 'tag1', 'tags2'], |
| 434 |
flatten(_exec.call_args_list[-1][0]))) |
| 435 |
|
| 436 |
@patch('ansible.module_utils.basic.AnsibleModule') |
| 437 |
def test_user_json_data_can_be_parsed(self, module): |
| 438 |
"""Ensure that user json data can be parsed. |
| 439 |
|
| 440 |
From version 3.7 onwards `rabbitmqctl` can output the user data in proper json format. Check that parsing |
| 441 |
works correctly. |
| 442 |
""" |
| 443 |
|
| 444 |
def side_effect(args): |
| 445 |
self.assertTrue('-q' in args) |
| 446 |
self.assertTrue('--formatter' in args) |
| 447 |
self.assertTrue('json' in args) |
| 448 |
if 'status' in args: |
| 449 |
return 0, rabbitmq_3_8_status.replace('version_num', '3.8.1'), '' |
| 450 |
if 'list_users' in args: |
| 451 |
return 0, '''[ |
| 452 |
{"user":"someuser","tags":["administrator","management"]} |
| 453 |
]''', '' |
| 454 |
if 'list_user_permissions' in args: |
| 455 |
return 0, '''[ |
| 456 |
{"vhost":"/test","configure":"^$","write":"^$","read":"^$"} |
| 457 |
,{"vhost":"/","configure":"^$","write":"^$","read":"^$"} |
| 458 |
]''', '' |
| 459 |
if 'list_user_topic_permissions' in args: |
| 460 |
return 0, '''[ |
| 461 |
{"vhost":"/test","exchange":"amq.topic","write":"^$","read":"^$"} |
| 462 |
,{"vhost":"/","exchange":"amq.topic","write":"^$","read":"^$"} |
| 463 |
]''', '' |
| 464 |
return 100, '', '' |
| 465 |
|
| 466 |
module.run_command.side_effect = side_effect |
| 467 |
user_controller = rabbitmq_user.RabbitMqUser( |
| 468 |
module, 'someuser', 'somepassword', list(), |
| 469 |
[{'vhost': '/', 'configure': '^$', 'write': '^$', 'read': '^$'}], |
| 470 |
[{'vhost': '/', 'exchange': 'amq.topic', 'write': '^$', 'read': '^$'}], |
| 471 |
'rabbit', bulk_permissions=True) |
| 472 |
self.assertTrue(user_controller.get()) |
| 473 |
self.assertTrue(user_controller._version, version.StrictVersion('3.8.1')) |
| 474 |
self.assertTrue(user_controller.existing_tags, ["administrator", "management"]) |
| 475 |
self.assertTrue(user_controller.existing_permissions == { |
| 476 |
'/test': {'vhost': '/test', 'configure': '^$', 'write': '^$', 'read': '^$'}, |
| 477 |
'/': {'vhost': '/', 'configure': '^$', 'write': '^$', 'read': '^$'}}) |
| 478 |
self.assertTrue(user_controller.existing_topic_permissions == { |
| 479 |
('/test', 'amq.topic'): {'vhost': '/test', 'exchange': 'amq.topic', 'write': '^$', 'read': '^$'}, |
| 480 |
('/', 'amq.topic'): {'vhost': '/', 'exchange': 'amq.topic', 'write': '^$', 'read': '^$'}}) |
| 481 |
self.assertEqual(module.run_command.call_count, 4) |
| 482 |
|
| 483 |
@patch('ansible.module_utils.basic.AnsibleModule') |
| 484 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._exec') |
| 485 |
@patch('ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_user.RabbitMqUser._check_version') |
| 486 |
def test_non_bulk_permissions_are_parsed_and_set(self, _check_version, _exec, module): |
| 487 |
"""Test that non bulk permissions are parsed correctly. |
| 488 |
|
| 489 |
Non-bulk permissions mean that only the permissions of the VHost specified will be changed if needed. |
| 490 |
If the same user has permissions in other VHosts, these will not be modified. |
| 491 |
""" |
| 492 |
module.get_bin_path.return_value = '/rabbitmqctl' |
| 493 |
module.check_mode = False |
| 494 |
_check_version.return_value = version.StrictVersion('3.8.0') |
| 495 |
|
| 496 |
def side_effect(args): |
| 497 |
self.assertTrue('--formatter' in args, args) |
| 498 |
self.assertTrue('json' in args, args) |
| 499 |
if 'list_users' in args: |
| 500 |
return '''[ |
| 501 |
{"user":"someuser","tags":["administrator","management"]} |
| 502 |
]''' |
| 503 |
if 'list_user_permissions' in args: |
| 504 |
self.assertTrue('someuser' in args, args) |
| 505 |
return '''[ |
| 506 |
{"vhost":"/test","configure":"^$","write":"^$","read":"^$"} |
| 507 |
,{"vhost":"/","configure":"^$","write":"^$","read":"^$"} |
| 508 |
]''' |
| 509 |
if 'list_user_topic_permissions' in args: |
| 510 |
self.assertTrue('someuser' in args, args) |
| 511 |
return '''[ |
| 512 |
{"vhost":"/test","exchange":"amq.topic","write":"^$","read":"^$"} |
| 513 |
,{"vhost":"/","exchange":"amq.topic","write":"^$","read":"^$"} |
| 514 |
]''' |
| 515 |
raise Exception('wrong command: ' + str(args)) |
| 516 |
|
| 517 |
_exec.side_effect = side_effect |
| 518 |
user_controller = rabbitmq_user.RabbitMqUser( |
| 519 |
module, 'someuser', 'somepassword', list(), [{ |
| 520 |
'vhost': '/', |
| 521 |
'configure_priv': '.*', |
| 522 |
'write_priv': '.*', |
| 523 |
'read_priv': '.*' |
| 524 |
}], [], 'rabbit' |
| 525 |
) |
| 526 |
user_controller.get() |
| 527 |
|
| 528 |
self.assertEqual(_exec.call_count, 3) |
| 529 |
self.assertListEqual(list(user_controller.existing_permissions.keys()), ['/']) |
| 530 |
self.assertEqual(user_controller.existing_permissions['/']['write'], '^$') |
| 531 |
self.assertEqual(user_controller.existing_permissions['/']['read'], '^$') |
| 532 |
self.assertEqual(user_controller.existing_permissions['/']['configure'], '^$') |
| 533 |
self.assertTrue(user_controller.has_permissions_modifications()) |