| 1 |
# This file is part of Ansible |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
from unittest.mock import MagicMock |
| 5 |
from unittest.mock import patch |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from ansible_collections.amazon.aws.plugins.modules import ec2_dedicated_host |
| 10 |
|
| 11 |
module_name = "ansible_collections.amazon.aws.plugins.modules.ec2_dedicated_host" |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture(name="ansible_module") |
| 15 |
def fixture_ansible_module(): |
| 16 |
module = MagicMock() |
| 17 |
module.params = {} |
| 18 |
module.exit_json = MagicMock() |
| 19 |
module.fail_json = MagicMock() |
| 20 |
module.fail_json.side_effect = SystemExit(1) |
| 21 |
return module |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.parametrize( |
| 25 |
"dedicated_hosts,expected", |
| 26 |
[ |
| 27 |
([], None), |
| 28 |
( |
| 29 |
[{"host_id": "h-01ca0a554a7f8787b", "mac_os_latest_supported_versions": ["15.3", "14.7.3", "13.7.3"]}], |
| 30 |
{"host_id": "h-01ca0a554a7f8787b", "mac_os_latest_supported_versions": ["15.3", "14.7.3", "13.7.3"]}, |
| 31 |
), |
| 32 |
], |
| 33 |
) |
| 34 |
@patch(module_name + ".describe_ec2_dedicated_hosts") |
| 35 |
def test_get_ec2_dedicated_host_with_lookup_host_id( |
| 36 |
m_describe_ec2_dedicated_hosts, ansible_module, dedicated_hosts, expected |
| 37 |
): |
| 38 |
ansible_module.params = {"host_id": "h-012345", "lookup": "host_id"} |
| 39 |
client = MagicMock() |
| 40 |
m_describe_ec2_dedicated_hosts.return_value = dedicated_hosts |
| 41 |
|
| 42 |
assert ec2_dedicated_host.get_ec2_dedicated_host(client, ansible_module) == expected |
| 43 |
expected_params = { |
| 44 |
"Filters": [{"Name": "state", "Values": ["available", "under-assessment", "permanent-failure"]}], |
| 45 |
"HostIds": ["h-012345"], |
| 46 |
} |
| 47 |
m_describe_ec2_dedicated_hosts.assert_called_once_with(client, **expected_params) |
| 48 |
|
| 49 |
|
| 50 |
@pytest.mark.parametrize( |
| 51 |
"dedicated_hosts,expected", |
| 52 |
[ |
| 53 |
([], None), |
| 54 |
([{"host_id": "host-1"}, {"host_id": "host-2", "Tags": {"foo": "bar"}}], None), |
| 55 |
([{"host_id": "host-1"}, {"host_id": "host-2", "Tags": {"phase": "test"}}], None), |
| 56 |
( |
| 57 |
[ |
| 58 |
{"host_id": "host-1"}, |
| 59 |
{"host_id": "host-2", "Tags": {"foo": "bar", "phase": "test", "session": "active"}}, |
| 60 |
], |
| 61 |
None, |
| 62 |
), |
| 63 |
( |
| 64 |
[{"host_id": "host-1"}, {"host_id": "host-2", "Tags": {"foo": "bar", "phase": "test"}}], |
| 65 |
{"host_id": "host-2", "Tags": {"foo": "bar", "phase": "test"}}, |
| 66 |
), |
| 67 |
( |
| 68 |
[ |
| 69 |
{"host_id": "host-1", "Tags": {"foo": "bar", "phase": "test"}}, |
| 70 |
{"host_id": "host-2", "Tags": {"foo": "bar", "phase": "test"}}, |
| 71 |
], |
| 72 |
[ |
| 73 |
{"host_id": "host-1", "Tags": {"foo": "bar", "phase": "test"}}, |
| 74 |
{"host_id": "host-2", "Tags": {"foo": "bar", "phase": "test"}}, |
| 75 |
], |
| 76 |
), |
| 77 |
], |
| 78 |
) |
| 79 |
@patch(module_name + ".boto3_tag_list_to_ansible_dict") |
| 80 |
@patch(module_name + ".describe_ec2_dedicated_hosts") |
| 81 |
def test_get_ec2_dedicated_host_with_lookup_tag( |
| 82 |
m_describe_ec2_dedicated_hosts, m_boto3_tag_list_to_ansible_dict, ansible_module, dedicated_hosts, expected |
| 83 |
): |
| 84 |
ansible_module.params = {"host_id": "h-012345", "lookup": "tag", "tags": {"foo": "bar", "phase": "test"}} |
| 85 |
client = MagicMock() |
| 86 |
m_describe_ec2_dedicated_hosts.return_value = dedicated_hosts |
| 87 |
m_boto3_tag_list_to_ansible_dict.side_effect = lambda x: x |
| 88 |
|
| 89 |
if expected and isinstance(expected, list) and len(expected) > 1: |
| 90 |
with pytest.raises(SystemExit): |
| 91 |
ec2_dedicated_host.get_ec2_dedicated_host(client, ansible_module) |
| 92 |
ansible_module.fail_json.assert_called_once_with( |
| 93 |
msg=f"Tags provided do not identify a unique dedicated host ({len(expected)} found)." |
| 94 |
) |
| 95 |
else: |
| 96 |
assert ec2_dedicated_host.get_ec2_dedicated_host(client, ansible_module) == expected |
| 97 |
expected_params = { |
| 98 |
"Filters": [{"Name": "state", "Values": ["available", "under-assessment", "permanent-failure"]}], |
| 99 |
} |
| 100 |
m_describe_ec2_dedicated_hosts.assert_called_once_with(client, **expected_params) |