| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2023 Ali AlKhalidi <@doteast> |
| 4 |
# |
| 5 |
# This file is part of Ansible |
| 6 |
# |
| 7 |
# Ansible is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Ansible is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import copy |
| 21 |
import random |
| 22 |
import string |
| 23 |
from unittest.mock import MagicMock |
| 24 |
from unittest.mock import call |
| 25 |
from unittest.mock import patch |
| 26 |
|
| 27 |
import pytest |
| 28 |
|
| 29 |
try: |
| 30 |
import botocore |
| 31 |
except ImportError: |
| 32 |
# Handled by HAS_BOTO3 |
| 33 |
pass |
| 34 |
|
| 35 |
from ansible.errors import AnsibleError |
| 36 |
|
| 37 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3 |
| 38 |
|
| 39 |
from ansible_collections.community.aws.plugins.inventory.aws_mq import InventoryModule |
| 40 |
from ansible_collections.community.aws.plugins.inventory.aws_mq import _add_details_to_hosts |
| 41 |
from ansible_collections.community.aws.plugins.inventory.aws_mq import _find_hosts_matching_statuses |
| 42 |
from ansible_collections.community.aws.plugins.inventory.aws_mq import _get_broker_host_tags |
| 43 |
|
| 44 |
if not HAS_BOTO3: |
| 45 |
pytestmark = pytest.mark.skip("test_aws_mq.py requires the python modules 'boto3' and 'botocore'") |
| 46 |
|
| 47 |
|
| 48 |
def make_clienterror_exception(code="AccessDenied"): |
| 49 |
return botocore.exceptions.ClientError( |
| 50 |
{ |
| 51 |
"Error": {"Code": code, "Message": "User is not authorized to perform: xxx on resource: user yyyy"}, |
| 52 |
"ResponseMetadata": {"RequestId": "01234567-89ab-cdef-0123-456789abcdef"}, |
| 53 |
}, |
| 54 |
"getXXX", |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
@pytest.fixture(name="inventory") |
| 59 |
def fixture_inventory(): |
| 60 |
inventory = InventoryModule() |
| 61 |
inventory.inventory = MagicMock() |
| 62 |
inventory.inventory.set_variable = MagicMock() |
| 63 |
|
| 64 |
inventory.all_clients = MagicMock() |
| 65 |
inventory.get_option = MagicMock() |
| 66 |
|
| 67 |
inventory._populate_host_vars = MagicMock() |
| 68 |
inventory._set_composite_vars = MagicMock() |
| 69 |
inventory._add_host_to_composed_groups = MagicMock() |
| 70 |
inventory._add_host_to_keyed_groups = MagicMock() |
| 71 |
|
| 72 |
inventory.get_cache_key = MagicMock() |
| 73 |
|
| 74 |
inventory._cache = {} |
| 75 |
|
| 76 |
return inventory |
| 77 |
|
| 78 |
|
| 79 |
@pytest.fixture(name="connection") |
| 80 |
def fixture_connection(): |
| 81 |
conn = MagicMock() |
| 82 |
return conn |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.parametrize( |
| 86 |
"suffix,result", |
| 87 |
[ |
| 88 |
("aws_mq.yml", True), |
| 89 |
("aws_mq.yaml", True), |
| 90 |
("aws_MQ.yml", False), |
| 91 |
("AWS_mq.yaml", False), |
| 92 |
], |
| 93 |
) |
| 94 |
def test_inventory_verify_file_suffix(inventory, suffix, result, tmp_path): |
| 95 |
test_dir = tmp_path / "test_aws_mq" |
| 96 |
test_dir.mkdir() |
| 97 |
inventory_file = "inventory" + suffix |
| 98 |
inventory_file = test_dir / inventory_file |
| 99 |
inventory_file.write_text("my inventory") |
| 100 |
assert result == inventory.verify_file(str(inventory_file)) |
| 101 |
|
| 102 |
|
| 103 |
def test_inventory_verify_file_with_missing_file(inventory): |
| 104 |
inventory_file = "this_file_does_not_exist_aws_mq.yml" |
| 105 |
assert not inventory.verify_file(inventory_file) |
| 106 |
|
| 107 |
|
| 108 |
def generate_random_string(with_digits=True, with_punctuation=True, length=16): |
| 109 |
data = string.ascii_letters |
| 110 |
if with_digits: |
| 111 |
data += string.digits |
| 112 |
if with_punctuation: |
| 113 |
data += string.punctuation |
| 114 |
return "".join([random.choice(data) for i in range(length)]) |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.parametrize( |
| 118 |
"hosts,statuses,expected", |
| 119 |
[ |
| 120 |
( |
| 121 |
[ |
| 122 |
{"host": "host1", "BrokerState": "DELETION_IN_PROGRESS"}, |
| 123 |
{"host": "host2", "BrokerState": "RUNNING"}, |
| 124 |
{"host": "host3", "BrokerState": "REBOOT_IN_PROGRESS"}, |
| 125 |
{"host": "host4", "BrokerState": "CRITICAL_ACTION_REQUIRED"}, |
| 126 |
{"host": "host5", "BrokerState": "CREATION_FAILED"}, |
| 127 |
{"host": "host6", "BrokerState": "CREATION_IN_PROGRESS"}, |
| 128 |
], |
| 129 |
["RUNNING"], |
| 130 |
[{"host": "host2", "BrokerState": "RUNNING"}], |
| 131 |
), |
| 132 |
( |
| 133 |
[ |
| 134 |
{"host": "host1", "BrokerState": "DELETION_IN_PROGRESS"}, |
| 135 |
{"host": "host2", "BrokerState": "RUNNING"}, |
| 136 |
{"host": "host3", "BrokerState": "REBOOT_IN_PROGRESS"}, |
| 137 |
{"host": "host4", "BrokerState": "CRITICAL_ACTION_REQUIRED"}, |
| 138 |
{"host": "host5", "BrokerState": "CREATION_FAILED"}, |
| 139 |
{"host": "host6", "BrokerState": "CREATION_IN_PROGRESS"}, |
| 140 |
], |
| 141 |
["all"], |
| 142 |
[ |
| 143 |
{"host": "host1", "BrokerState": "DELETION_IN_PROGRESS"}, |
| 144 |
{"host": "host2", "BrokerState": "RUNNING"}, |
| 145 |
{"host": "host3", "BrokerState": "REBOOT_IN_PROGRESS"}, |
| 146 |
{"host": "host4", "BrokerState": "CRITICAL_ACTION_REQUIRED"}, |
| 147 |
{"host": "host5", "BrokerState": "CREATION_FAILED"}, |
| 148 |
{"host": "host6", "BrokerState": "CREATION_IN_PROGRESS"}, |
| 149 |
], |
| 150 |
), |
| 151 |
( |
| 152 |
[ |
| 153 |
{"host": "host1", "BrokerState": "DELETION_IN_PROGRESS"}, |
| 154 |
{"host": "host2", "BrokerState": "RUNNING"}, |
| 155 |
{"host": "host3", "BrokerState": "CREATION_FAILED"}, |
| 156 |
{"host": "host4", "BrokerState": "CRITICAL_ACTION_REQUIRED"}, |
| 157 |
{"host": "host5", "BrokerState": "RUNNING"}, |
| 158 |
{"host": "host6", "BrokerState": "CREATION_IN_PROGRESS"}, |
| 159 |
], |
| 160 |
["RUNNING"], |
| 161 |
[ |
| 162 |
{"host": "host2", "BrokerState": "RUNNING"}, |
| 163 |
{"host": "host5", "BrokerState": "RUNNING"}, |
| 164 |
], |
| 165 |
), |
| 166 |
], |
| 167 |
) |
| 168 |
def test_find_hosts_matching_statuses(hosts, statuses, expected): |
| 169 |
assert expected == _find_hosts_matching_statuses(hosts, statuses) |
| 170 |
|
| 171 |
|
| 172 |
@pytest.mark.parametrize("hosts", ["", "host1", "host2,host3", "host2,host3,host1"]) |
| 173 |
@patch("ansible_collections.community.aws.plugins.inventory.aws_mq._get_mq_hostname") |
| 174 |
def test_inventory_format_inventory(m_get_mq_hostname, inventory, hosts): |
| 175 |
hosts_vars = { |
| 176 |
"host1": {"var10": "value10"}, |
| 177 |
"host2": {"var20": "value20", "var21": "value21"}, |
| 178 |
"host3": {"var30": "value30", "var31": "value31", "var32": "value32"}, |
| 179 |
} |
| 180 |
|
| 181 |
m_get_mq_hostname.side_effect = lambda h: h["name"] |
| 182 |
|
| 183 |
class _inventory_host(object): |
| 184 |
def __init__(self, name, host_vars): |
| 185 |
self.name = name |
| 186 |
self.vars = host_vars |
| 187 |
|
| 188 |
inventory.inventory = MagicMock() |
| 189 |
inventory.inventory.get_host.side_effect = lambda x: _inventory_host(name=x, host_vars=hosts_vars.get(x)) |
| 190 |
|
| 191 |
hosts = [{"name": x} for x in hosts.split(",") if x] |
| 192 |
expected = { |
| 193 |
"_meta": {"hostvars": {x["name"]: hosts_vars.get(x["name"]) for x in hosts}}, |
| 194 |
"aws_mq": {"hosts": [x["name"] for x in hosts]}, |
| 195 |
} |
| 196 |
|
| 197 |
assert expected == inventory._format_inventory(hosts) |
| 198 |
if hosts == []: |
| 199 |
m_get_mq_hostname.assert_not_called() |
| 200 |
|
| 201 |
|
| 202 |
@pytest.mark.parametrize("length", range(0, 10, 2)) |
| 203 |
def test_inventory_populate(inventory, length): |
| 204 |
group = "aws_mq" |
| 205 |
hosts = [f"host_{int(i)}" for i in range(length)] |
| 206 |
|
| 207 |
inventory._add_hosts = MagicMock() |
| 208 |
inventory._populate(hosts=hosts) |
| 209 |
|
| 210 |
inventory.inventory.add_group.assert_called_with("aws_mq") |
| 211 |
|
| 212 |
if len(hosts) == 0: |
| 213 |
inventory.inventory._add_hosts.assert_not_called() |
| 214 |
inventory.inventory.add_child.assert_not_called() |
| 215 |
else: |
| 216 |
inventory._add_hosts.assert_called_with(hosts=hosts, group=group) |
| 217 |
inventory.inventory.add_child.assert_called_with("all", group) |
| 218 |
|
| 219 |
|
| 220 |
def test_inventory_populate_from_cache(inventory): |
| 221 |
cache_data = { |
| 222 |
"_meta": { |
| 223 |
"hostvars": { |
| 224 |
"broker_A": {"var10": "value10"}, |
| 225 |
"broker_B": {"var2": "value2"}, |
| 226 |
"broker_C": {"var3": ["value30", "value31", "value32"]}, |
| 227 |
} |
| 228 |
}, |
| 229 |
"all": {"hosts": ["broker_A", "broker_D", "broker_B", "broker_C"]}, |
| 230 |
"aws_broker_group_A": {"hosts": ["broker_A", "broker_D"]}, |
| 231 |
"aws_broker_group_B": {"hosts": ["broker_B"]}, |
| 232 |
"aws_broker_group_C": {"hosts": ["broker_C"]}, |
| 233 |
} |
| 234 |
|
| 235 |
inventory._populate_from_cache(cache_data) |
| 236 |
inventory.inventory.add_group.assert_has_calls( |
| 237 |
[ |
| 238 |
call("aws_broker_group_A"), |
| 239 |
call("aws_broker_group_B"), |
| 240 |
call("aws_broker_group_C"), |
| 241 |
], |
| 242 |
any_order=True, |
| 243 |
) |
| 244 |
inventory.inventory.add_child.assert_has_calls( |
| 245 |
[ |
| 246 |
call("all", "aws_broker_group_A"), |
| 247 |
call("all", "aws_broker_group_B"), |
| 248 |
call("all", "aws_broker_group_C"), |
| 249 |
], |
| 250 |
any_order=True, |
| 251 |
) |
| 252 |
|
| 253 |
inventory._populate_host_vars.assert_has_calls( |
| 254 |
[ |
| 255 |
call(["broker_A"], {"var10": "value10"}, "aws_broker_group_A"), |
| 256 |
call(["broker_D"], {}, "aws_broker_group_A"), |
| 257 |
call(["broker_B"], {"var2": "value2"}, "aws_broker_group_B"), |
| 258 |
call(["broker_C"], {"var3": ["value30", "value31", "value32"]}, "aws_broker_group_C"), |
| 259 |
], |
| 260 |
any_order=True, |
| 261 |
) |
| 262 |
|
| 263 |
|
| 264 |
@pytest.mark.parametrize("detail", [{}, {"Tags": {"tag1": "value1", "tag2": "value2", "Tag3": "Value2"}}]) |
| 265 |
def test_get_broker_host_tags(detail): |
| 266 |
expected_tags = [ |
| 267 |
{"Key": "tag1", "Value": "value1"}, |
| 268 |
{"Key": "tag2", "Value": "value2"}, |
| 269 |
{"Key": "Tag3", "Value": "Value2"}, |
| 270 |
] |
| 271 |
|
| 272 |
tags = _get_broker_host_tags(detail) |
| 273 |
|
| 274 |
if not detail: |
| 275 |
assert tags == [] |
| 276 |
else: |
| 277 |
assert tags == expected_tags |
| 278 |
|
| 279 |
|
| 280 |
@pytest.mark.parametrize("strict", [True, False]) |
| 281 |
def test_add_details_to_hosts_with_no_hosts(connection, strict): |
| 282 |
hosts = [] |
| 283 |
|
| 284 |
_add_details_to_hosts(connection, hosts, strict) |
| 285 |
connection.describe_broker.assert_not_called() |
| 286 |
|
| 287 |
|
| 288 |
def test_add_details_to_hosts_with_failure_not_strict(connection): |
| 289 |
hosts = [{"BrokerId": "1"}] |
| 290 |
|
| 291 |
connection.describe_broker.side_effect = make_clienterror_exception() |
| 292 |
|
| 293 |
_add_details_to_hosts(connection, hosts, strict=False) |
| 294 |
|
| 295 |
assert hosts == [{"BrokerId": "1"}] |
| 296 |
|
| 297 |
|
| 298 |
def test_add_details_to_hosts_with_failure_strict(connection): |
| 299 |
hosts = [{"BrokerId": "1"}] |
| 300 |
|
| 301 |
connection.describe_broker.side_effect = make_clienterror_exception() |
| 302 |
|
| 303 |
with pytest.raises(AnsibleError): |
| 304 |
_add_details_to_hosts(connection, hosts, strict=True) |
| 305 |
|
| 306 |
|
| 307 |
def test_add_details_to_hosts_with_hosts(connection): |
| 308 |
hosts = [{"BrokerId": "1"}, {"BrokerId": "2"}] |
| 309 |
broker_hosts_tags = { |
| 310 |
"1": {"Tags": {"tag10": "value10", "tag11": "value11"}}, |
| 311 |
"2": {"Tags": {"tag20": "value20", "tag21": "value21", "tag22": "value22"}}, |
| 312 |
} |
| 313 |
connection.describe_broker.side_effect = lambda **kwargs: broker_hosts_tags.get(kwargs.get("BrokerId")) |
| 314 |
|
| 315 |
_add_details_to_hosts(connection, hosts, strict=False) |
| 316 |
|
| 317 |
assert hosts == [ |
| 318 |
{ |
| 319 |
"BrokerId": "1", |
| 320 |
"Tags": [ |
| 321 |
{"Key": "tag10", "Value": "value10"}, |
| 322 |
{"Key": "tag11", "Value": "value11"}, |
| 323 |
], |
| 324 |
}, |
| 325 |
{ |
| 326 |
"BrokerId": "2", |
| 327 |
"Tags": [ |
| 328 |
{"Key": "tag20", "Value": "value20"}, |
| 329 |
{"Key": "tag21", "Value": "value21"}, |
| 330 |
{"Key": "tag22", "Value": "value22"}, |
| 331 |
], |
| 332 |
}, |
| 333 |
] |
| 334 |
|
| 335 |
|
| 336 |
ADD_DETAILS_TO_HOSTS = "ansible_collections.community.aws.plugins.inventory.aws_mq._add_details_to_hosts" |
| 337 |
|
| 338 |
|
| 339 |
@patch(ADD_DETAILS_TO_HOSTS) |
| 340 |
def test_get_broker_hosts(m_add_details_to_hosts, inventory, connection): |
| 341 |
broker = { |
| 342 |
"BrokerArn": "arn:xxx:xxxx", |
| 343 |
"BrokerId": "resource_id", |
| 344 |
"BrokerName": "brk1", |
| 345 |
"BrokerState": "RUNNING", |
| 346 |
"EngineType": "RABBITMQ", |
| 347 |
"DeploymentMode": "CLUSTER_MULTI_AZ", |
| 348 |
} |
| 349 |
|
| 350 |
conn_paginator = MagicMock() |
| 351 |
paginate = MagicMock() |
| 352 |
|
| 353 |
connection.get_paginator.return_value = conn_paginator |
| 354 |
conn_paginator.paginate.return_value = paginate |
| 355 |
|
| 356 |
paginate.build_full_result.side_effect = lambda **kwargs: {"BrokerSummaries": [broker]} |
| 357 |
|
| 358 |
connection.describe_broker.return_value = {} |
| 359 |
connection.list_brokers.return_value = {"BrokerSummaries": [broker]} |
| 360 |
|
| 361 |
strict = False |
| 362 |
|
| 363 |
result = inventory._get_broker_hosts(connection=connection, strict=strict)(paginate.build_full_result) |
| 364 |
|
| 365 |
assert result == [broker] |
| 366 |
|
| 367 |
m_add_details_to_hosts.assert_called_with(connection, result, strict) |
| 368 |
|
| 369 |
|
| 370 |
@pytest.mark.parametrize("strict", [True, False]) |
| 371 |
@patch(ADD_DETAILS_TO_HOSTS) |
| 372 |
def test_get_broker_hosts_with_access_denied(m_add_details_to_hosts, inventory, connection, strict): |
| 373 |
conn_paginator = MagicMock() |
| 374 |
paginate = MagicMock() |
| 375 |
|
| 376 |
connection.get_paginator.return_value = conn_paginator |
| 377 |
conn_paginator.paginate.return_value = paginate |
| 378 |
|
| 379 |
paginate.build_full_result.side_effect = make_clienterror_exception() |
| 380 |
|
| 381 |
if strict: |
| 382 |
with pytest.raises(AnsibleError): |
| 383 |
inventory._get_broker_hosts(connection=connection, strict=strict)(paginate.build_full_result) |
| 384 |
else: |
| 385 |
assert inventory._get_broker_hosts(connection=connection, strict=strict)(paginate.build_full_result) == [] |
| 386 |
|
| 387 |
m_add_details_to_hosts.assert_not_called() |
| 388 |
|
| 389 |
|
| 390 |
@patch(ADD_DETAILS_TO_HOSTS) |
| 391 |
def test_get_broker_hosts_with_client_error(m_add_details_to_hosts, inventory, connection): |
| 392 |
conn_paginator = MagicMock() |
| 393 |
paginate = MagicMock() |
| 394 |
|
| 395 |
connection.get_paginator.return_value = conn_paginator |
| 396 |
conn_paginator.paginate.return_value = paginate |
| 397 |
|
| 398 |
paginate.build_full_result.side_effect = make_clienterror_exception(code="Unknown") |
| 399 |
|
| 400 |
with pytest.raises(AnsibleError): |
| 401 |
inventory._get_broker_hosts(connection=connection, strict=False)(paginate.build_full_result) |
| 402 |
|
| 403 |
m_add_details_to_hosts.assert_not_called() |
| 404 |
|
| 405 |
|
| 406 |
FIND_HOSTS_MATCHING_STATUSES = ( |
| 407 |
"ansible_collections.community.aws.plugins.inventory.aws_mq._find_hosts_matching_statuses" |
| 408 |
) |
| 409 |
|
| 410 |
|
| 411 |
@pytest.mark.parametrize("regions", range(1, 5)) |
| 412 |
@patch(FIND_HOSTS_MATCHING_STATUSES) |
| 413 |
def test_inventory_get_all_hosts(m_find_hosts, inventory, regions): |
| 414 |
params = { |
| 415 |
"regions": [f"us-east-{int(i)}" for i in range(regions)], |
| 416 |
"strict": random.choice((True, False)), |
| 417 |
"statuses": [ |
| 418 |
random.choice( |
| 419 |
[ |
| 420 |
"RUNNING", |
| 421 |
"CREATION_IN_PROGRESS", |
| 422 |
"REBOOT_IN_PROGRESS", |
| 423 |
"DELETION_IN_PROGRESS", |
| 424 |
"CRITICAL_ACTION_REQUIRED", |
| 425 |
] |
| 426 |
) |
| 427 |
for i in range(3) |
| 428 |
], |
| 429 |
} |
| 430 |
|
| 431 |
connections = [MagicMock() for i in range(regions)] |
| 432 |
|
| 433 |
inventory.all_clients.return_value = [(connections[i], f"us-east-{int(i)}") for i in range(regions)] |
| 434 |
|
| 435 |
ids = list(reversed(range(regions))) |
| 436 |
broker_hosts = [{"BrokerName": f"broker_00{int(i)}"} for i in ids] |
| 437 |
|
| 438 |
inventory._get_broker_hosts = MagicMock() |
| 439 |
inventory._get_broker_hosts._boto3_paginate_wrapper = MagicMock() |
| 440 |
inventory._get_broker_hosts._boto3_paginate_wrapper.side_effect = [[i] for i in broker_hosts] |
| 441 |
inventory._get_broker_hosts.return_value = inventory._get_broker_hosts._boto3_paginate_wrapper |
| 442 |
|
| 443 |
result = list(sorted(broker_hosts, key=lambda x: x["BrokerName"])) |
| 444 |
|
| 445 |
m_find_hosts.return_value = result |
| 446 |
|
| 447 |
assert result == inventory._get_all_hosts(**params) |
| 448 |
inventory.all_clients.assert_called_with("mq") |
| 449 |
inventory._get_broker_hosts.assert_has_calls( |
| 450 |
[call(connections[i], params["strict"]) for i in range(regions)], any_order=True |
| 451 |
) |
| 452 |
|
| 453 |
m_find_hosts.assert_called_with(result, params["statuses"]) |
| 454 |
|
| 455 |
|
| 456 |
@pytest.mark.parametrize("hostvars_prefix", [True]) |
| 457 |
@pytest.mark.parametrize("hostvars_suffix", [True]) |
| 458 |
@patch("ansible_collections.community.aws.plugins.inventory.aws_mq._get_mq_hostname") |
| 459 |
def test_inventory_add_hosts(m_get_mq_hostname, inventory, hostvars_prefix, hostvars_suffix): |
| 460 |
_options = { |
| 461 |
"strict": random.choice((False, True)), |
| 462 |
"compose": random.choice((False, True)), |
| 463 |
"keyed_groups": "keyed_group_test_inventory_add_hosts", |
| 464 |
"groups": ["all", "test_inventory_add_hosts"], |
| 465 |
} |
| 466 |
|
| 467 |
if hostvars_prefix: |
| 468 |
_options["hostvars_prefix"] = f"prefix_{generate_random_string(length=8, with_punctuation=False)}" |
| 469 |
if hostvars_suffix: |
| 470 |
_options["hostvars_suffix"] = f"suffix_{generate_random_string(length=8, with_punctuation=False)}" |
| 471 |
|
| 472 |
def _get_option_side_effect(x): |
| 473 |
return _options.get(x) |
| 474 |
|
| 475 |
inventory.get_option.side_effect = _get_option_side_effect |
| 476 |
|
| 477 |
m_get_mq_hostname.side_effect = lambda h: h["BrokerName"] |
| 478 |
|
| 479 |
hosts = [ |
| 480 |
{ |
| 481 |
"BrokerName": "broker_i_001", |
| 482 |
"Tags": [{"Key": "Name", "Value": "broker_001"}, {"Key": "RunningEngine", "Value": "ActiveMQ"}], |
| 483 |
"availability_zone": "us-east-1a", |
| 484 |
}, |
| 485 |
{ |
| 486 |
"BrokerName": "broker_i_002", |
| 487 |
"Tags": [{"Key": "ClusterName", "Value": "test_cluster"}, {"Key": "RunningOS", "Value": "CoreOS"}], |
| 488 |
}, |
| 489 |
{ |
| 490 |
"BrokerName": "test_cluster", |
| 491 |
"Tags": [{"Key": "CluserVersionOrigin", "Value": "2.0"}, {"Key": "Provider", "Value": "RedHat"}], |
| 492 |
}, |
| 493 |
{ |
| 494 |
"BrokerName": "another_cluster", |
| 495 |
"Tags": [{"Key": "TestingPurpose", "Value": "Ansible"}], |
| 496 |
"availability_zones": ["us-west-1a", "us-east-1b"], |
| 497 |
}, |
| 498 |
] |
| 499 |
|
| 500 |
group = f"test_add_hosts_group_{generate_random_string(length=10, with_punctuation=False)}" |
| 501 |
inventory._add_hosts(hosts, group) |
| 502 |
|
| 503 |
m_get_mq_hostname.assert_has_calls([call(h) for h in hosts], any_order=True) |
| 504 |
|
| 505 |
hosts_names = ["broker_i_001", "broker_i_002", "test_cluster", "another_cluster"] |
| 506 |
inventory.inventory.add_host.assert_has_calls([call(name, group=group) for name in hosts_names], any_order=True) |
| 507 |
|
| 508 |
camel_hosts = [ |
| 509 |
{ |
| 510 |
"broker_name": "broker_i_001", |
| 511 |
"tags": {"Name": "broker_001", "RunningEngine": "ActiveMQ"}, |
| 512 |
"availability_zone": "us-east-1a", |
| 513 |
}, |
| 514 |
{"broker_name": "broker_i_002", "tags": {"ClusterName": "test_cluster", "RunningOS": "CoreOS"}}, |
| 515 |
{"broker_name": "test_cluster", "tags": {"CluserVersionOrigin": "2.0", "Provider": "RedHat"}}, |
| 516 |
{ |
| 517 |
"broker_name": "another_cluster", |
| 518 |
"tags": {"TestingPurpose": "Ansible"}, |
| 519 |
"availability_zones": ["us-west-1a", "us-east-1b"], |
| 520 |
}, |
| 521 |
] |
| 522 |
|
| 523 |
set_variable_calls = [] |
| 524 |
for i in range(len(camel_hosts)): |
| 525 |
for var, value in camel_hosts[i].items(): |
| 526 |
if hostvars_prefix: |
| 527 |
var = _options["hostvars_prefix"] + var |
| 528 |
if hostvars_suffix: |
| 529 |
var += _options["hostvars_suffix"] |
| 530 |
set_variable_calls.append(call(hosts_names[i], var, value)) |
| 531 |
|
| 532 |
inventory.get_option.assert_has_calls([call("hostvars_prefix"), call("hostvars_suffix")]) |
| 533 |
inventory.inventory.set_variable.assert_has_calls(set_variable_calls) |
| 534 |
|
| 535 |
if hostvars_prefix or hostvars_suffix: |
| 536 |
tmp = [] |
| 537 |
for host in camel_hosts: |
| 538 |
new_host = copy.deepcopy(host) |
| 539 |
for key, value in host.items(): |
| 540 |
new_key = key |
| 541 |
if hostvars_prefix: |
| 542 |
new_key = _options["hostvars_prefix"] + new_key |
| 543 |
if hostvars_suffix: |
| 544 |
new_key += _options["hostvars_suffix"] |
| 545 |
new_host[new_key] = value |
| 546 |
tmp.append(new_host) |
| 547 |
camel_hosts = tmp |
| 548 |
|
| 549 |
inventory._set_composite_vars.assert_has_calls( |
| 550 |
[ |
| 551 |
call(_options["compose"], camel_hosts[i], hosts_names[i], strict=_options["strict"]) |
| 552 |
for i in range(len(camel_hosts)) |
| 553 |
], |
| 554 |
any_order=True, |
| 555 |
) |
| 556 |
inventory._add_host_to_composed_groups.assert_has_calls( |
| 557 |
[ |
| 558 |
call(_options["groups"], camel_hosts[i], hosts_names[i], strict=_options["strict"]) |
| 559 |
for i in range(len(camel_hosts)) |
| 560 |
], |
| 561 |
any_order=True, |
| 562 |
) |
| 563 |
inventory._add_host_to_keyed_groups.assert_has_calls( |
| 564 |
[ |
| 565 |
call(_options["keyed_groups"], camel_hosts[i], hosts_names[i], strict=_options["strict"]) |
| 566 |
for i in range(len(camel_hosts)) |
| 567 |
], |
| 568 |
any_order=True, |
| 569 |
) |
| 570 |
|
| 571 |
|
| 572 |
BASE_INVENTORY_PARSE = "ansible_collections.community.aws.plugins.inventory.aws_mq.AWSInventoryBase.parse" |
| 573 |
|
| 574 |
|
| 575 |
@pytest.mark.parametrize("user_cache_directive", [True, False]) |
| 576 |
@pytest.mark.parametrize("cache", [True, False]) |
| 577 |
@pytest.mark.parametrize("cache_hit", [True, False]) |
| 578 |
@patch(BASE_INVENTORY_PARSE) |
| 579 |
def test_inventory_parse(m_parse, inventory, user_cache_directive, cache, cache_hit): |
| 580 |
inventory_data = MagicMock() |
| 581 |
loader = MagicMock() |
| 582 |
path = generate_random_string(with_punctuation=False, with_digits=False) |
| 583 |
|
| 584 |
options = {} |
| 585 |
options["regions"] = [f"us-east-{d}" for d in range(random.randint(1, 5))] |
| 586 |
options["strict_permissions"] = random.choice((True, False)) |
| 587 |
options["statuses"] = generate_random_string(with_punctuation=False) |
| 588 |
|
| 589 |
options["cache"] = user_cache_directive |
| 590 |
|
| 591 |
def get_option_side_effect(v): |
| 592 |
return options.get(v) |
| 593 |
|
| 594 |
inventory.get_option.side_effect = get_option_side_effect |
| 595 |
|
| 596 |
cache_key = path + generate_random_string() |
| 597 |
inventory.get_cache_key.return_value = cache_key |
| 598 |
|
| 599 |
cache_key_value = generate_random_string() |
| 600 |
if cache_hit: |
| 601 |
inventory._cache[cache_key] = cache_key_value |
| 602 |
|
| 603 |
inventory._populate = MagicMock() |
| 604 |
inventory._populate_from_cache = MagicMock() |
| 605 |
inventory._get_all_hosts = MagicMock() |
| 606 |
all_hosts = [ |
| 607 |
{"host": f"host_{int(random.randint(1, 1000))}"}, |
| 608 |
{"host": f"host_{int(random.randint(1, 1000))}"}, |
| 609 |
{"host": f"host_{int(random.randint(1, 1000))}"}, |
| 610 |
{"host": f"host_{int(random.randint(1, 1000))}"}, |
| 611 |
] |
| 612 |
inventory._get_all_hosts.return_value = all_hosts |
| 613 |
|
| 614 |
format_cache_key_value = f"format_inventory_{all_hosts}" |
| 615 |
inventory._format_inventory = MagicMock() |
| 616 |
inventory._format_inventory.return_value = format_cache_key_value |
| 617 |
|
| 618 |
inventory.parse(inventory_data, loader, path, cache) |
| 619 |
|
| 620 |
m_parse.assert_called_with(inventory_data, loader, path, cache=cache) |
| 621 |
|
| 622 |
if not cache or not user_cache_directive or (cache and user_cache_directive and not cache_hit): |
| 623 |
inventory._get_all_hosts.assert_called_with( |
| 624 |
options["regions"], |
| 625 |
options["strict_permissions"], |
| 626 |
options["statuses"], |
| 627 |
) |
| 628 |
inventory._populate.assert_called_with(all_hosts) |
| 629 |
inventory._format_inventory.assert_called_with(all_hosts) |
| 630 |
else: |
| 631 |
inventory._get_all_hosts.assert_not_called() |
| 632 |
|
| 633 |
if cache and user_cache_directive and cache_hit: |
| 634 |
inventory._populate_from_cache.assert_called_with(cache_key_value) |
| 635 |
|
| 636 |
if cache and user_cache_directive and not cache_hit or (not cache and user_cache_directive): |
| 637 |
# validate that cache was populated |
| 638 |
assert inventory._cache[cache_key] == format_cache_key_value |