| 1 |
# (c) 2022 Red Hat Inc. |
| 2 |
|
| 3 |
# This file is part of Ansible |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from unittest.mock import MagicMock |
| 7 |
from unittest.mock import call |
| 8 |
from unittest.mock import patch |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 13 |
|
| 14 |
from ansible_collections.amazon.aws.plugins.modules import ec2_ami_info |
| 15 |
|
| 16 |
module_name = "ansible_collections.amazon.aws.plugins.modules.ec2_ami_info" |
| 17 |
|
| 18 |
|
| 19 |
@pytest.fixture(name="ec2_client") |
| 20 |
def fixture_ec2_client(): |
| 21 |
return MagicMock() |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.parametrize( |
| 25 |
"executable_users,filters,image_ids,owners,expected", |
| 26 |
[ |
| 27 |
([], {}, [], [], {}), |
| 28 |
([], {}, ["ami-1234567890"], [], {"ImageIds": ["ami-1234567890"]}), |
| 29 |
([], {}, [], ["1234567890"], {"Filters": [{"Name": "owner-id", "Values": ["1234567890"]}]}), |
| 30 |
( |
| 31 |
[], |
| 32 |
{"owner-alias": "test_ami_owner"}, |
| 33 |
[], |
| 34 |
["1234567890"], |
| 35 |
{ |
| 36 |
"Filters": [ |
| 37 |
{"Name": "owner-alias", "Values": ["test_ami_owner"]}, |
| 38 |
{"Name": "owner-id", "Values": ["1234567890"]}, |
| 39 |
] |
| 40 |
}, |
| 41 |
), |
| 42 |
([], {"is-public": True}, [], [], {"Filters": [{"Name": "is-public", "Values": ["true"]}]}), |
| 43 |
(["self"], {}, [], [], {"ExecutableUsers": ["self"]}), |
| 44 |
([], {}, [], ["self"], {"Owners": ["self"]}), |
| 45 |
], |
| 46 |
) |
| 47 |
def test_build_request_args(executable_users, filters, image_ids, owners, expected): |
| 48 |
assert ec2_ami_info.build_request_args(executable_users, filters, image_ids, owners) == expected |
| 49 |
|
| 50 |
|
| 51 |
@patch(module_name + ".describe_images") |
| 52 |
def test_get_images(m_describe_images): |
| 53 |
m_describe_images.return_value = { |
| 54 |
"Images": [ |
| 55 |
{ |
| 56 |
"Architecture": "x86_64", |
| 57 |
"BlockDeviceMappings": [ |
| 58 |
{ |
| 59 |
"DeviceName": "/dev/sda1", |
| 60 |
"Ebs": { |
| 61 |
"DeleteOnTermination": "True", |
| 62 |
"Encrypted": "False", |
| 63 |
"SnapshotId": "snap-0f00cba784af62428", |
| 64 |
"VolumeSize": 10, |
| 65 |
"VolumeType": "gp2", |
| 66 |
}, |
| 67 |
} |
| 68 |
], |
| 69 |
"ImageId": "ami-1234567890", |
| 70 |
"ImageLocation": "1234567890/test-ami-uefi-boot", |
| 71 |
"ImageType": "machine", |
| 72 |
"Name": "test-ami-uefi-boot", |
| 73 |
"OwnerId": "1234567890", |
| 74 |
"PlatformDetails": "Linux/UNIX", |
| 75 |
} |
| 76 |
], |
| 77 |
} |
| 78 |
|
| 79 |
request_args = {"ImageIds": ["ami-1234567890"]} |
| 80 |
ec2_client = MagicMock() |
| 81 |
get_images_result = ec2_ami_info.get_images(ec2_client, request_args) |
| 82 |
|
| 83 |
assert m_describe_images.call_count >= 1 |
| 84 |
m_describe_images.assert_called_with(ec2_client, **request_args) |
| 85 |
assert get_images_result == m_describe_images.return_value |
| 86 |
|
| 87 |
|
| 88 |
@patch(module_name + ".describe_image_attribute") |
| 89 |
@patch(module_name + ".get_images") |
| 90 |
def test_list_ec2_images(m_get_images, m_describe_image_attribute, ec2_client): |
| 91 |
module = MagicMock() |
| 92 |
|
| 93 |
m_get_images.return_value = [ |
| 94 |
{ |
| 95 |
"Architecture": "x86_64", |
| 96 |
"BlockDeviceMappings": [ |
| 97 |
{ |
| 98 |
"DeviceName": "/dev/sda1", |
| 99 |
"Ebs": { |
| 100 |
"DeleteOnTermination": "True", |
| 101 |
"Encrypted": "False", |
| 102 |
"SnapshotId": "snap-0f00cba784af62428", |
| 103 |
"VolumeSize": 10, |
| 104 |
"VolumeType": "gp2", |
| 105 |
}, |
| 106 |
} |
| 107 |
], |
| 108 |
"ImageId": "ami-1234567890", |
| 109 |
"ImageLocation": "1234567890/test-ami-uefi-boot", |
| 110 |
"ImageType": "machine", |
| 111 |
"Name": "test-ami-uefi-boot", |
| 112 |
"OwnerId": "1234567890", |
| 113 |
"OwnerAlias": "test_ami_owner", |
| 114 |
"PlatformDetails": "Linux/UNIX", |
| 115 |
}, |
| 116 |
{ |
| 117 |
"Architecture": "x86_64", |
| 118 |
"BlockDeviceMappings": [ |
| 119 |
{ |
| 120 |
"DeviceName": "/dev/sda1", |
| 121 |
"Ebs": { |
| 122 |
"DeleteOnTermination": "True", |
| 123 |
"Encrypted": "False", |
| 124 |
"SnapshotId": "snap-0f00cba784af62428", |
| 125 |
"VolumeSize": 10, |
| 126 |
"VolumeType": "gp2", |
| 127 |
}, |
| 128 |
} |
| 129 |
], |
| 130 |
"ImageId": "ami-1523498760", |
| 131 |
"ImageLocation": "1523498760/test-ami-uefi-boot", |
| 132 |
"ImageType": "machine", |
| 133 |
"Name": "test-ami-uefi-boot", |
| 134 |
"OwnerId": "1234567890", |
| 135 |
"OwnerAlias": "test_ami_owner", |
| 136 |
"PlatformDetails": "Linux/UNIX", |
| 137 |
}, |
| 138 |
] |
| 139 |
|
| 140 |
m_describe_image_attribute.return_value = { |
| 141 |
"ImageId": "ami-1234567890", |
| 142 |
"LaunchPermissions": [{"UserId": "1234567890"}, {"UserId": "0987654321"}], |
| 143 |
} |
| 144 |
|
| 145 |
images = m_get_images.return_value |
| 146 |
images = [camel_dict_to_snake_dict(image) for image in images] |
| 147 |
|
| 148 |
request_args = { |
| 149 |
"Filters": [ |
| 150 |
{"Name": "owner-alias", "Values": ["test_ami_owner"]}, |
| 151 |
{"Name": "owner-id", "Values": ["1234567890"]}, |
| 152 |
] |
| 153 |
} |
| 154 |
|
| 155 |
# needed for `assert m_get_image_attribute.call_count == 2` |
| 156 |
module.params = {"describe_image_attributes": True} |
| 157 |
|
| 158 |
list_ec2_images_result = ec2_ami_info.list_ec2_images(ec2_client, module, request_args) |
| 159 |
|
| 160 |
assert m_get_images.call_count == 1 |
| 161 |
m_get_images.assert_called_with(ec2_client, request_args) |
| 162 |
|
| 163 |
assert m_describe_image_attribute.call_count == 2 |
| 164 |
m_describe_image_attribute.assert_has_calls( |
| 165 |
[call(ec2_client, attribute="launchPermission", image_id=images[0]["image_id"])], |
| 166 |
[call(ec2_client, attribute="launchPermission", image_id=images[1]["image_id"])], |
| 167 |
) |
| 168 |
|
| 169 |
assert len(list_ec2_images_result) == 2 |
| 170 |
assert list_ec2_images_result[0]["image_id"] == "ami-1234567890" |
| 171 |
assert list_ec2_images_result[1]["image_id"] == "ami-1523498760" |
| 172 |
|
| 173 |
|
| 174 |
@patch(module_name + ".AnsibleAWSModule") |
| 175 |
def test_main_success(m_AnsibleAWSModule): |
| 176 |
m_module = MagicMock() |
| 177 |
m_AnsibleAWSModule.return_value = m_module |
| 178 |
|
| 179 |
ec2_ami_info.main() |
| 180 |
|
| 181 |
m_module.client.assert_called_with("ec2") |
| 182 |
m_module.exit_json.assert_called_with(images=[]) |
| 183 |
|
| 184 |
|
| 185 |
def a_ami_info_exception(): |
| 186 |
return ec2_ami_info.AnsibleEC2Error(message="Whoops") |
| 187 |
|
| 188 |
|
| 189 |
@patch(module_name + ".describe_images") |
| 190 |
def test_api_failure_get_images(m_describe_images, ec2_client): |
| 191 |
request_args = {} |
| 192 |
m_describe_images.side_effect = a_ami_info_exception() |
| 193 |
|
| 194 |
with pytest.raises(ec2_ami_info.AmiInfoFailure): |
| 195 |
ec2_ami_info.get_images(ec2_client, request_args) |