| 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_collections.amazon.aws.plugins.modules import ec2_eni_info |
| 13 |
|
| 14 |
module_name = "ansible_collections.amazon.aws.plugins.modules.ec2_eni_info" |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.parametrize( |
| 18 |
"eni_id,filters,expected", [("", {}, {}), ("eni-1234567890", {}, {"NetworkInterfaceIds": ["eni-1234567890"]})] |
| 19 |
) |
| 20 |
def test_build_request_args(eni_id, filters, expected): |
| 21 |
assert ec2_eni_info.build_request_args(eni_id, filters) == expected |
| 22 |
|
| 23 |
|
| 24 |
@patch(module_name + ".describe_network_interfaces") |
| 25 |
def test_get_network_interfaces(m_describe_network_interfaces): |
| 26 |
connection = MagicMock() |
| 27 |
module = MagicMock() |
| 28 |
|
| 29 |
m_describe_network_interfaces.return_value = { |
| 30 |
"NetworkInterfaces": [ |
| 31 |
{ |
| 32 |
"AvailabilityZone": "us-east-2b", |
| 33 |
"Description": "", |
| 34 |
"NetworkInterfaceId": "eni-1234567890", |
| 35 |
"PrivateIpAddresses": [{"Primary": "True", "PrivateIpAddress": "11.22.33.44"}], |
| 36 |
"RequesterManaged": False, |
| 37 |
"SourceDestCheck": True, |
| 38 |
"Status": "available", |
| 39 |
"SubnetId": "subnet-07d906b8358869bda", |
| 40 |
"TagSet": [], |
| 41 |
"VpcId": "vpc-0cb60952be96c9cd8", |
| 42 |
} |
| 43 |
] |
| 44 |
} |
| 45 |
|
| 46 |
request_args = {"NetworkInterfaceIds": ["eni-1234567890"]} |
| 47 |
|
| 48 |
network_interfaces_result = ec2_eni_info.get_network_interfaces(connection, module, request_args) |
| 49 |
|
| 50 |
assert m_describe_network_interfaces.call_count == 1 |
| 51 |
m_describe_network_interfaces.assert_called_with(connection, **request_args) |
| 52 |
assert len(network_interfaces_result["NetworkInterfaces"]) == 1 |
| 53 |
|
| 54 |
|
| 55 |
@patch(module_name + ".get_network_interfaces") |
| 56 |
def test_list_eni(m_get_network_interfaces): |
| 57 |
connection = MagicMock() |
| 58 |
module = MagicMock() |
| 59 |
|
| 60 |
m_get_network_interfaces.return_value = [ |
| 61 |
{ |
| 62 |
"AvailabilityZone": "us-east-2b", |
| 63 |
"Description": "", |
| 64 |
"NetworkInterfaceId": "eni-1234567890", |
| 65 |
"PrivateIpAddresses": [{"Primary": "True", "PrivateIpAddress": "11.22.33.44"}], |
| 66 |
"RequesterManaged": False, |
| 67 |
"SourceDestCheck": True, |
| 68 |
"Status": "available", |
| 69 |
"SubnetId": "subnet-07d906b8358869bda", |
| 70 |
"TagSet": [], |
| 71 |
"VpcId": "vpc-0cb60952be96c9cd8", |
| 72 |
}, |
| 73 |
{ |
| 74 |
"AvailabilityZone": "us-east-2b", |
| 75 |
"Description": "", |
| 76 |
"NetworkInterfaceId": "eni-0987654321", |
| 77 |
"PrivateIpAddresses": [{"Primary": "True", "PrivateIpAddress": "11.22.33.44"}], |
| 78 |
"RequesterManaged": False, |
| 79 |
"SourceDestCheck": True, |
| 80 |
"Status": "available", |
| 81 |
"SubnetId": "subnet-07d906b8358869bda", |
| 82 |
"TagSet": [ |
| 83 |
{"Key": "Name", "Value": "my-test-eni-name"}, |
| 84 |
], |
| 85 |
"VpcId": "vpc-0cb60952be96c9cd8", |
| 86 |
}, |
| 87 |
] |
| 88 |
|
| 89 |
request_args = {"Filters": [{"Name": "owner-id", "Values": ["1234567890"]}]} |
| 90 |
|
| 91 |
camel_network_interfaces = ec2_eni_info.list_eni(connection, module, request_args) |
| 92 |
|
| 93 |
assert m_get_network_interfaces.call_count == 1 |
| 94 |
m_get_network_interfaces.assert_has_calls( |
| 95 |
[ |
| 96 |
call(connection, module, request_args), |
| 97 |
] |
| 98 |
) |
| 99 |
assert len(camel_network_interfaces) == 2 |
| 100 |
|
| 101 |
assert camel_network_interfaces[0]["id"] == "eni-1234567890" |
| 102 |
assert camel_network_interfaces[0]["tags"] == {} |
| 103 |
assert camel_network_interfaces[0].get("name") is None |
| 104 |
|
| 105 |
assert camel_network_interfaces[1]["id"] == "eni-0987654321" |
| 106 |
assert camel_network_interfaces[1]["tags"] == {"Name": "my-test-eni-name"} |
| 107 |
assert camel_network_interfaces[1]["name"] == "my-test-eni-name" |