| 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 ANY |
| 5 |
from unittest.mock import MagicMock |
| 6 |
from unittest.mock import call |
| 7 |
from unittest.mock import patch |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from ansible_collections.amazon.aws.plugins.modules import ec2_ami |
| 12 |
|
| 13 |
module_name = "ansible_collections.amazon.aws.plugins.modules.ec2_ami" |
| 14 |
|
| 15 |
|
| 16 |
@patch(module_name + ".register_image") |
| 17 |
@patch(module_name + ".get_image_by_id") |
| 18 |
def test_create_image_uefi_data(m_get_image_by_id, m_register_image): |
| 19 |
module = MagicMock() |
| 20 |
connection = MagicMock() |
| 21 |
|
| 22 |
m_get_image_by_id.return_value = { |
| 23 |
"ImageId": "ami-0c7a795306730b288", |
| 24 |
"BootMode": "uefi", |
| 25 |
"TpmSupport": "v2.0", |
| 26 |
} |
| 27 |
|
| 28 |
module.params = { |
| 29 |
"name": "my-image", |
| 30 |
"boot_mode": "uefi", |
| 31 |
"tpm_support": "v2.0", |
| 32 |
"uefi_data": "QU1aTlVFRkk9xcN0AAAAAHj5a7fZ9+3aT2gcVRgA8Ek3NipiPST0pCiCIlTJtj20FzENCcQa", |
| 33 |
} |
| 34 |
|
| 35 |
ec2_ami.CreateImage.do(module, connection, None) |
| 36 |
assert m_register_image.call_count == 1 |
| 37 |
m_register_image.assert_has_calls( |
| 38 |
[ |
| 39 |
call( |
| 40 |
connection, |
| 41 |
Name="my-image", |
| 42 |
BootMode="uefi", |
| 43 |
TpmSupport="v2.0", |
| 44 |
UefiData="QU1aTlVFRkk9xcN0AAAAAHj5a7fZ9+3aT2gcVRgA8Ek3NipiPST0pCiCIlTJtj20FzENCcQa", |
| 45 |
) |
| 46 |
] |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
def test_get_block_device_mapping_virtual_name(): |
| 51 |
image = {"block_device_mappings": [{"device_name": "/dev/sdc", "virtual_name": "ephemeral0"}]} |
| 52 |
block_device = ec2_ami.get_block_device_mapping(image) |
| 53 |
assert block_device == {"/dev/sdc": {"virtual_name": "ephemeral0"}} |
| 54 |
|
| 55 |
|
| 56 |
@patch(module_name + ".describe_image_attribute") |
| 57 |
@patch(module_name + ".describe_images") |
| 58 |
def test_get_image_by_id_found(m_describe_images, m_describe_image_attribute): |
| 59 |
connection = MagicMock() |
| 60 |
|
| 61 |
m_describe_images.return_value = [{"ImageId": "ami-0c7a795306730b288"}] |
| 62 |
|
| 63 |
image = ec2_ami.get_image_by_id(connection, "ami-0c7a795306730b288") |
| 64 |
assert image["ImageId"] == "ami-0c7a795306730b288" |
| 65 |
assert m_describe_images.call_count == 1 |
| 66 |
assert m_describe_image_attribute.call_count == 2 |
| 67 |
m_describe_images.assert_has_calls( |
| 68 |
[ |
| 69 |
call( |
| 70 |
connection, |
| 71 |
ImageIds=["ami-0c7a795306730b288"], |
| 72 |
) |
| 73 |
] |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
@patch(module_name + ".describe_images") |
| 78 |
def test_get_image_by_too_many(m_describe_images): |
| 79 |
connection = MagicMock() |
| 80 |
|
| 81 |
m_describe_images.return_value = [ |
| 82 |
{"ImageId": "ami-0c7a795306730b288"}, |
| 83 |
{"ImageId": "ami-0c7a795306730b289"}, |
| 84 |
] |
| 85 |
|
| 86 |
with pytest.raises(ec2_ami.Ec2AmiFailure): |
| 87 |
ec2_ami.get_image_by_id(connection, "ami-0c7a795306730b288") |
| 88 |
m_describe_images.assert_called_once_with(connection, ImageIds=["ami-0c7a795306730b288"]) |
| 89 |
|
| 90 |
|
| 91 |
@patch(module_name + ".describe_images") |
| 92 |
def test_get_image_missing(m_describe_images): |
| 93 |
connection = MagicMock() |
| 94 |
|
| 95 |
m_describe_images.return_value = [] |
| 96 |
|
| 97 |
assert ec2_ami.get_image_by_id(connection, "ami-0c7a795306730b288") is None |
| 98 |
|
| 99 |
assert m_describe_images.call_count == 1 |
| 100 |
m_describe_images.assert_has_calls( |
| 101 |
[ |
| 102 |
call( |
| 103 |
connection, |
| 104 |
ImageIds=["ami-0c7a795306730b288"], |
| 105 |
) |
| 106 |
] |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
@patch( |
| 111 |
module_name + ".create_image", |
| 112 |
) |
| 113 |
@patch( |
| 114 |
module_name + ".get_image_by_id", |
| 115 |
) |
| 116 |
def test_create_image_minimal(m_get_image_by_id, m_create_image): |
| 117 |
module = MagicMock() |
| 118 |
connection = MagicMock() |
| 119 |
|
| 120 |
m_get_image_by_id.return_value = {"ImageId": "ami-0c7a795306730b288"} |
| 121 |
module.params = { |
| 122 |
"name": "my-image", |
| 123 |
"instance_id": "i-123456789", |
| 124 |
"image_id": "ami-0c7a795306730b288", |
| 125 |
} |
| 126 |
ec2_ami.CreateImage.do(module, connection, None) |
| 127 |
assert m_create_image.call_count == 1 |
| 128 |
m_create_image.assert_has_calls( |
| 129 |
[ |
| 130 |
call( |
| 131 |
connection, |
| 132 |
InstanceId="i-123456789", |
| 133 |
Name="my-image", |
| 134 |
) |
| 135 |
] |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
def test_validate_params(): |
| 140 |
module = MagicMock() |
| 141 |
|
| 142 |
ec2_ami.validate_params(module) |
| 143 |
module.fail_json.assert_any_call("one of the following is required: name, image_id") |
| 144 |
assert module.require_botocore_at_least.call_count == 0 |
| 145 |
|
| 146 |
module = MagicMock() |
| 147 |
ec2_ami.validate_params(module, tpm_support=True) |
| 148 |
assert module.require_botocore_at_least.call_count == 0 |
| 149 |
|
| 150 |
module = MagicMock() |
| 151 |
ec2_ami.validate_params(module, tpm_support=True, boot_mode="legacy-bios") |
| 152 |
assert module.require_botocore_at_least.call_count == 0 |
| 153 |
module.fail_json.assert_any_call("To specify 'tpm_support', 'boot_mode' must be 'uefi'.") |
| 154 |
|
| 155 |
module = MagicMock() |
| 156 |
ec2_ami.validate_params(module, state="present", name="bobby") |
| 157 |
assert module.require_botocore_at_least.call_count == 0 |
| 158 |
module.fail_json.assert_any_call( |
| 159 |
"The parameters instance_id or device_mapping (register from EBS snapshot) are required for a new image." |
| 160 |
) |
| 161 |
|
| 162 |
|
| 163 |
def test_rename_item_if_exists(): |
| 164 |
dict_object = { |
| 165 |
"Paris": True, |
| 166 |
"London": {"Heathrow Airport": False}, |
| 167 |
} |
| 168 |
ec2_ami.rename_item_if_exists(dict_object, "Paris", "NewYork") |
| 169 |
assert dict_object == {"London": {"Heathrow Airport": False}, "NewYork": True} |
| 170 |
|
| 171 |
dict_object = { |
| 172 |
"Cities": {}, |
| 173 |
"London": "bar", |
| 174 |
} |
| 175 |
|
| 176 |
ec2_ami.rename_item_if_exists(dict_object, "London", "Abidjan", "Cities") |
| 177 |
ec2_ami.rename_item_if_exists(dict_object, "Doesnt-exist", "Nowhere", "Cities") |
| 178 |
assert dict_object == {"Cities": {"Abidjan": "bar"}} |
| 179 |
|
| 180 |
|
| 181 |
@patch(module_name + ".delete_snapshot") |
| 182 |
def test_DeregisterImage_defer_purge_snapshots(m_delete_snapshot): |
| 183 |
image = {"BlockDeviceMappings": [{"Ebs": {"SnapshotId": "My_snapshot"}}, {}]} |
| 184 |
func = ec2_ami.DeregisterImage.defer_purge_snapshots(image) |
| 185 |
|
| 186 |
connection = MagicMock() |
| 187 |
assert list(func(connection)) == ["My_snapshot"] |
| 188 |
m_delete_snapshot.assert_called_with(connection, "My_snapshot") |
| 189 |
|
| 190 |
|
| 191 |
@patch(module_name + ".get_image_by_id") |
| 192 |
@patch(module_name + ".time.sleep") |
| 193 |
def test_DeregisterImage_timeout_success(m_sleep, m_get_image_by_id): |
| 194 |
connection = MagicMock() |
| 195 |
m_get_image_by_id.side_effect = [{"ImageId": "ami-0c7a795306730b288"}, None] |
| 196 |
|
| 197 |
ec2_ami.DeregisterImage.timeout(connection, "ami-0c7a795306730b288", 10) |
| 198 |
assert m_sleep.call_count == 1 |
| 199 |
|
| 200 |
|
| 201 |
@patch(module_name + ".get_image_by_id") |
| 202 |
@patch(module_name + ".time.time") |
| 203 |
@patch(module_name + ".time.sleep") |
| 204 |
def test_DeregisterImage_timeout_failure(m_sleep, m_time, m_get_image_by_id): |
| 205 |
connection = MagicMock() |
| 206 |
m_time.side_effect = list(range(1, 30)) |
| 207 |
m_get_image_by_id.return_value = {"ImageId": "ami-0c7a795306730b288"} |
| 208 |
|
| 209 |
with pytest.raises(ec2_ami.Ec2AmiFailure): |
| 210 |
ec2_ami.DeregisterImage.timeout(connection, "ami-0c7a795306730b288", 10) |
| 211 |
assert m_sleep.call_count == 9 |
| 212 |
|
| 213 |
|
| 214 |
def test_UpdateImage_set_launch_permission_check_mode_no_change(): |
| 215 |
connection = MagicMock() |
| 216 |
image = {"ImageId": "ami-0c7a795306730b288", "LaunchPermissions": {}} |
| 217 |
|
| 218 |
changed = ec2_ami.UpdateImage.set_launch_permission(connection, image, launch_permissions={}, check_mode=True) |
| 219 |
assert changed is False |
| 220 |
assert connection.modify_image_attribute.call_count == 0 |
| 221 |
|
| 222 |
|
| 223 |
def test_UpdateImage_set_launch_permission_check_mode_with_change(): |
| 224 |
connection = MagicMock() |
| 225 |
image = {"ImageId": "ami-0c7a795306730b288", "LaunchPermissions": {}} |
| 226 |
launch_permissions = {"user_ids": ["123456789012"], "group_names": ["foo", "bar"]} |
| 227 |
changed = ec2_ami.UpdateImage.set_launch_permission(connection, image, launch_permissions, check_mode=True) |
| 228 |
assert changed is True |
| 229 |
assert connection.modify_image_attribute.call_count == 0 |
| 230 |
|
| 231 |
|
| 232 |
@patch(module_name + ".modify_image_attribute") |
| 233 |
def test_UpdateImage_set_launch_permission_with_change(m_modify_image_attribute): |
| 234 |
connection = MagicMock() |
| 235 |
image = {"ImageId": "ami-0c7a795306730b288", "LaunchPermissions": {}} |
| 236 |
launch_permissions = {"user_ids": ["123456789012"], "group_names": ["foo", "bar"]} |
| 237 |
changed = ec2_ami.UpdateImage.set_launch_permission(connection, image, launch_permissions, check_mode=False) |
| 238 |
assert changed is True |
| 239 |
assert m_modify_image_attribute.call_count == 1 |
| 240 |
m_modify_image_attribute.assert_called_with( |
| 241 |
connection, |
| 242 |
image_id="ami-0c7a795306730b288", |
| 243 |
Attribute="launchPermission", |
| 244 |
LaunchPermission={ |
| 245 |
"Add": [{"Group": "bar"}, {"Group": "foo"}, {"UserId": "123456789012"}], |
| 246 |
"Remove": [], |
| 247 |
}, |
| 248 |
) |
| 249 |
|
| 250 |
|
| 251 |
@patch(module_name + ".modify_image_attribute") |
| 252 |
def test_UpdateImage_set_description(m_modify_image_attribute): |
| 253 |
connection = MagicMock() |
| 254 |
module = MagicMock() |
| 255 |
module.check_mode = False |
| 256 |
image = {"ImageId": "ami-0c7a795306730b288", "Description": "My description"} |
| 257 |
changed = ec2_ami.UpdateImage.set_description(connection, module, image, "My description") |
| 258 |
assert changed is False |
| 259 |
|
| 260 |
changed = ec2_ami.UpdateImage.set_description(connection, module, image, "New description") |
| 261 |
assert changed is True |
| 262 |
assert m_modify_image_attribute.call_count == 1 |
| 263 |
m_modify_image_attribute.assert_called_with( |
| 264 |
connection, |
| 265 |
image_id="ami-0c7a795306730b288", |
| 266 |
Attribute="Description", |
| 267 |
Description={"Value": "New description"}, |
| 268 |
) |
| 269 |
|
| 270 |
|
| 271 |
def test_UpdateImage_set_description_check_mode(): |
| 272 |
connection = MagicMock() |
| 273 |
module = MagicMock() |
| 274 |
module.check_mode = True |
| 275 |
image = {"ImageId": "ami-0c7a795306730b288", "Description": "My description"} |
| 276 |
changed = ec2_ami.UpdateImage.set_description(connection, module, image, "My description") |
| 277 |
assert changed is False |
| 278 |
|
| 279 |
changed = ec2_ami.UpdateImage.set_description(connection, module, image, "New description") |
| 280 |
assert changed is True |
| 281 |
assert connection.modify_image_attribute.call_count == 0 |
| 282 |
|
| 283 |
|
| 284 |
def test_CreateImage_build_block_device_mapping(): |
| 285 |
device_mapping = [ |
| 286 |
{ |
| 287 |
"device_name": "/dev/xvda", |
| 288 |
"volume_size": 8, |
| 289 |
"snapshot_id": "snap-xxxxxxxx", |
| 290 |
"delete_on_termination": True, |
| 291 |
"volume_type": "gp2", |
| 292 |
"no_device": False, |
| 293 |
}, |
| 294 |
{ |
| 295 |
"device_name": "/dev/xvdb", |
| 296 |
"no_device": True, |
| 297 |
}, |
| 298 |
] |
| 299 |
result = ec2_ami.CreateImage.build_block_device_mapping(device_mapping) |
| 300 |
assert result == [ |
| 301 |
{ |
| 302 |
"Ebs": { |
| 303 |
"DeleteOnTermination": True, |
| 304 |
"SnapshotId": "snap-xxxxxxxx", |
| 305 |
"VolumeSize": 8, |
| 306 |
"VolumeType": "gp2", |
| 307 |
}, |
| 308 |
"DeviceName": "/dev/xvda", |
| 309 |
}, |
| 310 |
{"DeviceName": "/dev/xvdb", "Ebs": {}, "NoDevice": ""}, |
| 311 |
] |
| 312 |
|
| 313 |
|
| 314 |
def test_CreateImage_do_check_mode_no_change(): |
| 315 |
module = MagicMock() |
| 316 |
|
| 317 |
module.params = {"name": "my-image"} |
| 318 |
connection = MagicMock() |
| 319 |
connection.describe_images.return_value = { |
| 320 |
"Images": [ |
| 321 |
{ |
| 322 |
"InstanceId": "i-123456789", |
| 323 |
"Name": "my-image", |
| 324 |
} |
| 325 |
] |
| 326 |
} |
| 327 |
|
| 328 |
ec2_ami.CreateImage.do_check_mode(module, connection, None) |
| 329 |
module.exit_json.assert_called_with( |
| 330 |
changed=False, |
| 331 |
msg="Error registering image: AMI name is already in use by another AMI", |
| 332 |
) |
| 333 |
|
| 334 |
|
| 335 |
@patch(module_name + ".describe_images") |
| 336 |
def test_CreateImage_do_check_mode_with_change(m_describe_images): |
| 337 |
module = MagicMock() |
| 338 |
|
| 339 |
module.params = {"name": "my-image"} |
| 340 |
connection = MagicMock() |
| 341 |
m_describe_images.return_value = [] |
| 342 |
|
| 343 |
ec2_ami.CreateImage.do_check_mode(module, connection, None) |
| 344 |
m_describe_images.assert_called_once_with(connection, Filters=[{"Name": "name", "Values": [module.params["name"]]}]) |
| 345 |
module.exit_json.assert_called_with(changed=True, msg="Would have created a AMI if not in check mode.") |
| 346 |
|
| 347 |
|
| 348 |
@patch(module_name + ".wait_for_resource_state") |
| 349 |
def test_CreateImage_wait(m_wait_for_resource_state): |
| 350 |
connection = MagicMock() |
| 351 |
m_waiter = MagicMock() |
| 352 |
m_wait_for_resource_state.return_value = m_waiter |
| 353 |
module = MagicMock() |
| 354 |
module.params = {"wait": True, "wait_timeout": 0} |
| 355 |
assert ec2_ami.CreateImage.wait(connection, module, image_id=ANY) is None |
| 356 |
m_wait_for_resource_state.assert_not_called() |
| 357 |
|
| 358 |
module.params = {"wait": True, "wait_timeout": 600} |
| 359 |
image_id = "ami-0c7a795306730b288" |
| 360 |
ec2_ami.CreateImage.wait(connection, module, image_id=image_id) |
| 361 |
assert m_wait_for_resource_state.call_count == 1 |
| 362 |
m_wait_for_resource_state.assert_called_with( |
| 363 |
connection, module, "image_available", delay=15, max_attempts=40, ImageIds=[image_id] |
| 364 |
) |
| 365 |
|
| 366 |
|
| 367 |
@patch(module_name + ".add_ec2_tags") |
| 368 |
@patch(module_name + ".get_image_by_id") |
| 369 |
def test_CreateImage_set_tags(m_get_image_by_id, m_add_ec2_tags): |
| 370 |
connection = MagicMock() |
| 371 |
module = MagicMock() |
| 372 |
|
| 373 |
m_get_image_by_id.return_value = { |
| 374 |
"ImageId": "ami-0c7a795306730b288", |
| 375 |
"BlockDeviceMappings": [ |
| 376 |
{"DeviceName": "/dev/sda1", "Ebs": {"VolumeSize": "50"}}, |
| 377 |
{ |
| 378 |
"DeviceName": "/dev/sdm", |
| 379 |
"Ebs": {"VolumeSize": "100", "SnapshotId": "snap-066877671789bd71b"}, |
| 380 |
}, |
| 381 |
{"DeviceName": "/dev/sda2"}, |
| 382 |
], |
| 383 |
} |
| 384 |
tags = {} |
| 385 |
ec2_ami.CreateImage.set_tags(connection, module, tags, image_id="ami-0c7a795306730b288") |
| 386 |
assert m_add_ec2_tags.call_count == 0 |
| 387 |
|
| 388 |
tags = {"metro": "LaSalle"} |
| 389 |
ec2_ami.CreateImage.set_tags(connection, module, tags, image_id="ami-0c7a795306730b288") |
| 390 |
assert m_add_ec2_tags.call_count == 3 |
| 391 |
m_add_ec2_tags.assert_called_with(connection, module, "snap-066877671789bd71b", tags) |
| 392 |
|
| 393 |
|
| 394 |
@patch(module_name + ".modify_image_attribute") |
| 395 |
def test_CreateInage_set_launch_permissions(m_modify_image_attribute): |
| 396 |
connection = MagicMock() |
| 397 |
launch_permissions = {"user_ids": ["123456789012"], "group_names": ["foo", "bar"]} |
| 398 |
image_id = "ami-0c7a795306730b288" |
| 399 |
ec2_ami.CreateImage.set_launch_permissions(connection, launch_permissions, image_id) |
| 400 |
|
| 401 |
assert m_modify_image_attribute.call_count == 1 |
| 402 |
m_modify_image_attribute.assert_called_with( |
| 403 |
connection, |
| 404 |
image_id="ami-0c7a795306730b288", |
| 405 |
Attribute="LaunchPermission", |
| 406 |
LaunchPermission={"Add": [{"Group": "foo"}, {"Group": "bar"}, {"UserId": "123456789012"}]}, |
| 407 |
) |