From a6d89079f704da80fc9f20498b81e192d07146ae Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 31 Oct 2022 20:42:18 -0700 Subject: [PATCH 1/7] refactor module, remove get_eni_info(interface) method --- plugins/modules/ec2_eni_info.py | 79 ++++++++++++--------------------- 1 file changed, 28 insertions(+), 51 deletions(-) diff --git a/plugins/modules/ec2_eni_info.py b/plugins/modules/ec2_eni_info.py index 6eb24c22f95..a2c377999f0 100644 --- a/plugins/modules/ec2_eni_info.py +++ b/plugins/modules/ec2_eni_info.py @@ -205,24 +205,32 @@ from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_tag_list_to_ansible_dict -def list_eni(connection, module): +def build_request_args(eni_id, filters): + request_args = { + 'NetworkInterfaceIds': [eni_id] if eni_id else '', + 'Filters': ansible_dict_to_boto3_filter_list(filters), + } - params = {} - # Options are mutually exclusive - if module.params.get("eni_id"): - params['NetworkInterfaceIds'] = [module.params.get("eni_id")] - elif module.params.get("filters"): - params['Filters'] = ansible_dict_to_boto3_filter_list(module.params.get("filters")) - else: - params['Filters'] = [] + request_args = {k: v for k, v in request_args.items() if v} + return request_args + + +def get_network_interfaces(connection, module, request_args): try: - network_interfaces_result = connection.describe_network_interfaces(aws_retry=True, **params)['NetworkInterfaces'] + network_interfaces_result = connection.describe_network_interfaces(aws_retry=True, **request_args)['NetworkInterfaces'] except is_boto3_error_code('InvalidNetworkInterfaceID.NotFound'): module.exit_json(network_interfaces=[]) except (ClientError, NoCredentialsError) as e: # pylint: disable=duplicate-except module.fail_json_aws(e) + return network_interfaces_result + + +def list_eni(connection, module, request_args): + + network_interfaces_result = get_network_interfaces(connection, module, request_args) + # Modify boto3 tags list to be ansible friendly dict and then camel_case camel_network_interfaces = [] for network_interface in network_interfaces_result: @@ -234,51 +242,13 @@ def list_eni(connection, module): network_interface['Id'] = network_interface['NetworkInterfaceId'] camel_network_interfaces.append(camel_dict_to_snake_dict(network_interface, ignore_list=['Tags', 'TagSet'])) - module.exit_json(network_interfaces=camel_network_interfaces) - - -def get_eni_info(interface): - - # Private addresses - private_addresses = [] - for ip in interface.private_ip_addresses: - private_addresses.append({'private_ip_address': ip.private_ip_address, 'primary_address': ip.primary}) - - interface_info = {'id': interface.id, - 'subnet_id': interface.subnet_id, - 'vpc_id': interface.vpc_id, - 'description': interface.description, - 'owner_id': interface.owner_id, - 'status': interface.status, - 'mac_address': interface.mac_address, - 'private_ip_address': interface.private_ip_address, - 'source_dest_check': interface.source_dest_check, - 'groups': dict((group.id, group.name) for group in interface.groups), - 'private_ip_addresses': private_addresses - } - - if hasattr(interface, 'publicDnsName'): - interface_info['association'] = {'public_ip_address': interface.publicIp, - 'public_dns_name': interface.publicDnsName, - 'ip_owner_id': interface.ipOwnerId - } - - if interface.attachment is not None: - interface_info['attachment'] = {'attachment_id': interface.attachment.id, - 'instance_id': interface.attachment.instance_id, - 'device_index': interface.attachment.device_index, - 'status': interface.attachment.status, - 'attach_time': interface.attachment.attach_time, - 'delete_on_termination': interface.attachment.delete_on_termination, - } - - return interface_info + return camel_network_interfaces def main(): argument_spec = dict( eni_id=dict(type='str'), - filters=dict(default=None, type='dict') + filters=dict(default={}, type='dict'), ) mutually_exclusive = [ ['eni_id', 'filters'] @@ -292,7 +262,14 @@ def main(): connection = module.client('ec2', retry_decorator=AWSRetry.jittered_backoff()) - list_eni(connection, module) + request_args = build_request_args( + eni_id=module.params["eni_id"], + filters=module.params["filters"], + ) + + result = list_eni(connection, module, request_args) + + module.exit_json(network_interfaces=result) if __name__ == '__main__': From 74befaa26bd6bd1edef7f8564daf3398e47949a3 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 1 Nov 2022 20:35:55 -0700 Subject: [PATCH 2/7] Initial unit tests --- .../unit/plugins/modules/test_ec2_eni_info.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/unit/plugins/modules/test_ec2_eni_info.py diff --git a/tests/unit/plugins/modules/test_ec2_eni_info.py b/tests/unit/plugins/modules/test_ec2_eni_info.py new file mode 100644 index 00000000000..27e3c9bda57 --- /dev/null +++ b/tests/unit/plugins/modules/test_ec2_eni_info.py @@ -0,0 +1,26 @@ +# (c) 2022 Red Hat Inc. + +# This file is part of Ansible +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from unittest.mock import MagicMock, patch, ANY +import pytest + +from ansible_collections.amazon.aws.plugins.modules import ec2_eni_info + +module_name = "ansible_collections.amazon.aws.plugins.modules.ec2_eni_info" + + +@pytest.mark.parametrize("eni_id,filters,expected", [('', {}, {}), ('eni-1234567890', {}, {'NetworkInterfaceIds': ['eni-1234567890']})]) +def test_build_request_args(eni_id, filters, expected): + assert ec2_eni_info.build_request_args(eni_id, filters) == expected + + +@patch(module_name + ".AnsibleAWSModule") +def test_main_success(m_AnsibleAWSModule): + m_module = MagicMock() + m_AnsibleAWSModule.return_value = m_module + + ec2_eni_info.main() + + m_module.client.assert_called_with("ec2", retry_decorator=ANY) From cc35ec65a78267951488a197074f5d5787e3b74e Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 1 Nov 2022 22:48:26 -0700 Subject: [PATCH 3/7] Add unit test for get_network_interfaces() --- plugins/modules/ec2_eni_info.py | 4 +- .../unit/plugins/modules/test_ec2_eni_info.py | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/plugins/modules/ec2_eni_info.py b/plugins/modules/ec2_eni_info.py index a2c377999f0..216c9540af4 100644 --- a/plugins/modules/ec2_eni_info.py +++ b/plugins/modules/ec2_eni_info.py @@ -218,7 +218,7 @@ def build_request_args(eni_id, filters): def get_network_interfaces(connection, module, request_args): try: - network_interfaces_result = connection.describe_network_interfaces(aws_retry=True, **request_args)['NetworkInterfaces'] + network_interfaces_result = connection.describe_network_interfaces(aws_retry=True, **request_args) except is_boto3_error_code('InvalidNetworkInterfaceID.NotFound'): module.exit_json(network_interfaces=[]) except (ClientError, NoCredentialsError) as e: # pylint: disable=duplicate-except @@ -233,7 +233,7 @@ def list_eni(connection, module, request_args): # Modify boto3 tags list to be ansible friendly dict and then camel_case camel_network_interfaces = [] - for network_interface in network_interfaces_result: + for network_interface in network_interfaces_result['NetworkInterfaces']: network_interface['TagSet'] = boto3_tag_list_to_ansible_dict(network_interface['TagSet']) network_interface['Tags'] = network_interface['TagSet'] if 'Name' in network_interface['Tags']: diff --git a/tests/unit/plugins/modules/test_ec2_eni_info.py b/tests/unit/plugins/modules/test_ec2_eni_info.py index 27e3c9bda57..da6b24c514b 100644 --- a/tests/unit/plugins/modules/test_ec2_eni_info.py +++ b/tests/unit/plugins/modules/test_ec2_eni_info.py @@ -16,6 +16,52 @@ def test_build_request_args(eni_id, filters, expected): assert ec2_eni_info.build_request_args(eni_id, filters) == expected +def test_get_network_interfaces(): + connection = MagicMock() + module = MagicMock() + + connection.describe_network_interfaces.return_value = { + 'NetworkInterfaces': [ + { + "AvailabilityZone": "us-east-2b", + "Description": "", + "Groups": [ + { + "GroupId": "sg-05db20a1234567890", + "GroupName": "TestGroup1" + } + ], + "InterfaceType": "interface", + "Ipv6Addresses": [], + "MacAddress": "00:11:66:77:11:bb", + "NetworkInterfaceId": "eni-1234567890", + "OwnerId": "1234567890", + "PrivateIpAddress": "11.22.33.44", + "PrivateIpAddresses": [ + { + "Primary": "True", + "PrivateIpAddress": "11.22.33.44" + } + ], + "RequesterManaged": False, + "SourceDestCheck": True, + "Status": "available", + "SubnetId": "subnet-07d906b8358869bda", + "TagSet": [], + "VpcId": "vpc-0cb60952be96c9cd8" + }, + ] + } + + request_args = {'NetworkInterfaceIds': ['eni-1234567890']} + + network_interfaces_result = ec2_eni_info.get_network_interfaces(connection, module, request_args) + + connection.describe_network_interfaces.call_count == 1 + connection.describe_network_interfaces.assert_called_with(aws_retry=True, **request_args) + assert len(network_interfaces_result['NetworkInterfaces']) == 1 + + @patch(module_name + ".AnsibleAWSModule") def test_main_success(m_AnsibleAWSModule): m_module = MagicMock() From 39963e12e3c79e5f68d8763be270dc72d855a61f Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 1 Nov 2022 23:28:36 -0700 Subject: [PATCH 4/7] Add test for list_eni() --- .../unit/plugins/modules/test_ec2_eni_info.py | 156 ++++++++++++++---- 1 file changed, 128 insertions(+), 28 deletions(-) diff --git a/tests/unit/plugins/modules/test_ec2_eni_info.py b/tests/unit/plugins/modules/test_ec2_eni_info.py index da6b24c514b..648b5511e77 100644 --- a/tests/unit/plugins/modules/test_ec2_eni_info.py +++ b/tests/unit/plugins/modules/test_ec2_eni_info.py @@ -3,7 +3,7 @@ # This file is part of Ansible # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from unittest.mock import MagicMock, patch, ANY +from unittest.mock import MagicMock, patch, ANY, call import pytest from ansible_collections.amazon.aws.plugins.modules import ec2_eni_info @@ -23,33 +23,33 @@ def test_get_network_interfaces(): connection.describe_network_interfaces.return_value = { 'NetworkInterfaces': [ { - "AvailabilityZone": "us-east-2b", - "Description": "", - "Groups": [ - { - "GroupId": "sg-05db20a1234567890", - "GroupName": "TestGroup1" - } - ], - "InterfaceType": "interface", - "Ipv6Addresses": [], - "MacAddress": "00:11:66:77:11:bb", - "NetworkInterfaceId": "eni-1234567890", - "OwnerId": "1234567890", - "PrivateIpAddress": "11.22.33.44", - "PrivateIpAddresses": [ - { - "Primary": "True", - "PrivateIpAddress": "11.22.33.44" - } - ], - "RequesterManaged": False, - "SourceDestCheck": True, - "Status": "available", - "SubnetId": "subnet-07d906b8358869bda", - "TagSet": [], - "VpcId": "vpc-0cb60952be96c9cd8" - }, + "AvailabilityZone": "us-east-2b", + "Description": "", + "Groups": [ + { + "GroupId": "sg-05db20a1234567890", + "GroupName": "TestGroup1" + } + ], + "InterfaceType": "interface", + "Ipv6Addresses": [], + "MacAddress": "00:11:66:77:11:bb", + "NetworkInterfaceId": "eni-1234567890", + "OwnerId": "1234567890", + "PrivateIpAddress": "11.22.33.44", + "PrivateIpAddresses": [ + { + "Primary": "True", + "PrivateIpAddress": "11.22.33.44" + } + ], + "RequesterManaged": False, + "SourceDestCheck": True, + "Status": "available", + "SubnetId": "subnet-07d906b8358869bda", + "TagSet": [], + "VpcId": "vpc-0cb60952be96c9cd8" + } ] } @@ -62,6 +62,106 @@ def test_get_network_interfaces(): assert len(network_interfaces_result['NetworkInterfaces']) == 1 +@patch(module_name + '.get_network_interfaces') +@patch(module_name + '.build_request_args') +def test_list_eni(m_build_request_args, m_get_network_interfaces): + connection = MagicMock() + module = MagicMock() + + m_build_request_args.return_value ={ + 'Filters': [{ + 'Name': 'owner-id', + 'Values': [ + '1234567890' + ] + }] + } + + m_get_network_interfaces.return_value = { + 'NetworkInterfaces': [ + { + "AvailabilityZone": "us-east-2b", + "Description": "", + "Groups": [ + { + "GroupId": "sg-05db20a1234567890", + "GroupName": "TestGroup1" + } + ], + "InterfaceType": "interface", + "Ipv6Addresses": [], + "MacAddress": "00:11:66:77:11:bb", + "NetworkInterfaceId": "eni-1234567890", + "OwnerId": "1234567890", + "PrivateIpAddress": "11.22.33.44", + "PrivateIpAddresses": [ + { + "Primary": "True", + "PrivateIpAddress": "11.22.33.44" + } + ], + "RequesterManaged": False, + "SourceDestCheck": True, + "Status": "available", + "SubnetId": "subnet-07d906b8358869bda", + "TagSet": [], + "VpcId": "vpc-0cb60952be96c9cd8" + }, + { + "AvailabilityZone": "us-east-2b", + "Description": "", + "Groups": [ + { + "GroupId": "sg-05db20a1234567890", + "GroupName": "TestGroup1" + } + ], + "InterfaceType": "interface", + "Ipv6Addresses": [], + "MacAddress": "00:11:66:77:11:bb", + "NetworkInterfaceId": "eni-0987654321", + "OwnerId": "1234567890", + "PrivateIpAddress": "11.22.33.44", + "PrivateIpAddresses": [ + { + "Primary": "True", + "PrivateIpAddress": "11.22.33.44" + } + ], + "RequesterManaged": False, + "SourceDestCheck": True, + "Status": "available", + "SubnetId": "subnet-07d906b8358869bda", + "TagSet": [{ + 'Key': 'Name', + 'Value': 'my-test-eni-name' + },], + "VpcId": "vpc-0cb60952be96c9cd8" + }, + ] + } + + request_args = ec2_eni_info.build_request_args() + + camel_network_interfaces = ec2_eni_info.list_eni(connection, module, request_args) + + m_get_network_interfaces.call_count == 1 + m_get_network_interfaces.assert_has_calls( + [ + call(connection, module, request_args), + ] + ) + assert len(camel_network_interfaces) == 2 + + assert camel_network_interfaces[0].get('id', None) == 'eni-1234567890' + assert camel_network_interfaces[0].get('tags', None) == {} + assert camel_network_interfaces[0].get('name', None) == None + + assert camel_network_interfaces[1].get('id', None) == 'eni-0987654321' + assert camel_network_interfaces[1].get('tags', None) == {'Name' : 'my-test-eni-name'} + assert camel_network_interfaces[1].get('name', None) == 'my-test-eni-name' + + @patch(module_name + ".AnsibleAWSModule") def test_main_success(m_AnsibleAWSModule): m_module = MagicMock() From 599bcdac9a784d3d3bfad6ef17cb7d73952a0991 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 1 Nov 2022 23:36:47 -0700 Subject: [PATCH 5/7] added changelogs fragment --- changelogs/fragments/unit-tests_test_ec2_eni_info_only.yaml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/unit-tests_test_ec2_eni_info_only.yaml diff --git a/changelogs/fragments/unit-tests_test_ec2_eni_info_only.yaml b/changelogs/fragments/unit-tests_test_ec2_eni_info_only.yaml new file mode 100644 index 00000000000..3ce084db8c2 --- /dev/null +++ b/changelogs/fragments/unit-tests_test_ec2_eni_info_only.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: +- "ec2_eni_info - Add unit-tests coverage (https://github.com/ansible-collections/amazon.aws/pull/1236)." From 0833386904ebd18c26dfb95993d982bfc12ad42a Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 2 Nov 2022 11:10:46 -0700 Subject: [PATCH 6/7] sanity fixes --- .../unit/plugins/modules/test_ec2_eni_info.py | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/unit/plugins/modules/test_ec2_eni_info.py b/tests/unit/plugins/modules/test_ec2_eni_info.py index 648b5511e77..6110b891347 100644 --- a/tests/unit/plugins/modules/test_ec2_eni_info.py +++ b/tests/unit/plugins/modules/test_ec2_eni_info.py @@ -27,8 +27,8 @@ def test_get_network_interfaces(): "Description": "", "Groups": [ { - "GroupId": "sg-05db20a1234567890", - "GroupName": "TestGroup1" + "GroupId": "sg-05db20a1234567890", + "GroupName": "TestGroup1" } ], "InterfaceType": "interface", @@ -39,8 +39,8 @@ def test_get_network_interfaces(): "PrivateIpAddress": "11.22.33.44", "PrivateIpAddresses": [ { - "Primary": "True", - "PrivateIpAddress": "11.22.33.44" + "Primary": "True", + "PrivateIpAddress": "11.22.33.44" } ], "RequesterManaged": False, @@ -68,7 +68,7 @@ def test_list_eni(m_build_request_args, m_get_network_interfaces): connection = MagicMock() module = MagicMock() - m_build_request_args.return_value ={ + m_build_request_args.return_value = { 'Filters': [{ 'Name': 'owner-id', 'Values': [ @@ -84,8 +84,8 @@ def test_list_eni(m_build_request_args, m_get_network_interfaces): "Description": "", "Groups": [ { - "GroupId": "sg-05db20a1234567890", - "GroupName": "TestGroup1" + "GroupId": "sg-05db20a1234567890", + "GroupName": "TestGroup1" } ], "InterfaceType": "interface", @@ -96,8 +96,8 @@ def test_list_eni(m_build_request_args, m_get_network_interfaces): "PrivateIpAddress": "11.22.33.44", "PrivateIpAddresses": [ { - "Primary": "True", - "PrivateIpAddress": "11.22.33.44" + "Primary": "True", + "PrivateIpAddress": "11.22.33.44" } ], "RequesterManaged": False, @@ -112,8 +112,8 @@ def test_list_eni(m_build_request_args, m_get_network_interfaces): "Description": "", "Groups": [ { - "GroupId": "sg-05db20a1234567890", - "GroupName": "TestGroup1" + "GroupId": "sg-05db20a1234567890", + "GroupName": "TestGroup1" } ], "InterfaceType": "interface", @@ -124,8 +124,8 @@ def test_list_eni(m_build_request_args, m_get_network_interfaces): "PrivateIpAddress": "11.22.33.44", "PrivateIpAddresses": [ { - "Primary": "True", - "PrivateIpAddress": "11.22.33.44" + "Primary": "True", + "PrivateIpAddress": "11.22.33.44" } ], "RequesterManaged": False, @@ -133,9 +133,9 @@ def test_list_eni(m_build_request_args, m_get_network_interfaces): "Status": "available", "SubnetId": "subnet-07d906b8358869bda", "TagSet": [{ - 'Key': 'Name', - 'Value': 'my-test-eni-name' - },], + 'Key': 'Name', + 'Value': 'my-test-eni-name' + }, ], "VpcId": "vpc-0cb60952be96c9cd8" }, ] @@ -155,10 +155,10 @@ def test_list_eni(m_build_request_args, m_get_network_interfaces): assert camel_network_interfaces[0].get('id', None) == 'eni-1234567890' assert camel_network_interfaces[0].get('tags', None) == {} - assert camel_network_interfaces[0].get('name', None) == None + assert camel_network_interfaces[0].get('name', None) is None assert camel_network_interfaces[1].get('id', None) == 'eni-0987654321' - assert camel_network_interfaces[1].get('tags', None) == {'Name' : 'my-test-eni-name'} + assert camel_network_interfaces[1].get('tags', None) == {'Name': 'my-test-eni-name'} assert camel_network_interfaces[1].get('name', None) == 'my-test-eni-name' From 7faa0111b32af9f51c33854c70b7f593d3934341 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Thu, 3 Nov 2022 13:03:53 -0700 Subject: [PATCH 7/7] Modified based on feedback --- plugins/modules/ec2_eni_info.py | 3 +- .../unit/plugins/modules/test_ec2_eni_info.py | 76 ++++--------------- 2 files changed, 17 insertions(+), 62 deletions(-) diff --git a/plugins/modules/ec2_eni_info.py b/plugins/modules/ec2_eni_info.py index 216c9540af4..bf05b017e2e 100644 --- a/plugins/modules/ec2_eni_info.py +++ b/plugins/modules/ec2_eni_info.py @@ -206,8 +206,9 @@ def build_request_args(eni_id, filters): + request_args = { - 'NetworkInterfaceIds': [eni_id] if eni_id else '', + 'NetworkInterfaceIds': [eni_id] if eni_id else [], 'Filters': ansible_dict_to_boto3_filter_list(filters), } diff --git a/tests/unit/plugins/modules/test_ec2_eni_info.py b/tests/unit/plugins/modules/test_ec2_eni_info.py index 6110b891347..a0997ea6518 100644 --- a/tests/unit/plugins/modules/test_ec2_eni_info.py +++ b/tests/unit/plugins/modules/test_ec2_eni_info.py @@ -25,18 +25,7 @@ def test_get_network_interfaces(): { "AvailabilityZone": "us-east-2b", "Description": "", - "Groups": [ - { - "GroupId": "sg-05db20a1234567890", - "GroupName": "TestGroup1" - } - ], - "InterfaceType": "interface", - "Ipv6Addresses": [], - "MacAddress": "00:11:66:77:11:bb", "NetworkInterfaceId": "eni-1234567890", - "OwnerId": "1234567890", - "PrivateIpAddress": "11.22.33.44", "PrivateIpAddresses": [ { "Primary": "True", @@ -63,37 +52,16 @@ def test_get_network_interfaces(): @patch(module_name + '.get_network_interfaces') -@patch(module_name + '.build_request_args') -def test_list_eni(m_build_request_args, m_get_network_interfaces): +def test_list_eni(m_get_network_interfaces): connection = MagicMock() module = MagicMock() - m_build_request_args.return_value = { - 'Filters': [{ - 'Name': 'owner-id', - 'Values': [ - '1234567890' - ] - }] - } - m_get_network_interfaces.return_value = { 'NetworkInterfaces': [ { "AvailabilityZone": "us-east-2b", "Description": "", - "Groups": [ - { - "GroupId": "sg-05db20a1234567890", - "GroupName": "TestGroup1" - } - ], - "InterfaceType": "interface", - "Ipv6Addresses": [], - "MacAddress": "00:11:66:77:11:bb", "NetworkInterfaceId": "eni-1234567890", - "OwnerId": "1234567890", - "PrivateIpAddress": "11.22.33.44", "PrivateIpAddresses": [ { "Primary": "True", @@ -110,18 +78,7 @@ def test_list_eni(m_build_request_args, m_get_network_interfaces): { "AvailabilityZone": "us-east-2b", "Description": "", - "Groups": [ - { - "GroupId": "sg-05db20a1234567890", - "GroupName": "TestGroup1" - } - ], - "InterfaceType": "interface", - "Ipv6Addresses": [], - "MacAddress": "00:11:66:77:11:bb", "NetworkInterfaceId": "eni-0987654321", - "OwnerId": "1234567890", - "PrivateIpAddress": "11.22.33.44", "PrivateIpAddresses": [ { "Primary": "True", @@ -141,7 +98,14 @@ def test_list_eni(m_build_request_args, m_get_network_interfaces): ] } - request_args = ec2_eni_info.build_request_args() + request_args = { + 'Filters': [{ + 'Name': 'owner-id', + 'Values': [ + '1234567890' + ] + }] + } camel_network_interfaces = ec2_eni_info.list_eni(connection, module, request_args) @@ -153,20 +117,10 @@ def test_list_eni(m_build_request_args, m_get_network_interfaces): ) assert len(camel_network_interfaces) == 2 - assert camel_network_interfaces[0].get('id', None) == 'eni-1234567890' - assert camel_network_interfaces[0].get('tags', None) == {} - assert camel_network_interfaces[0].get('name', None) is None - - assert camel_network_interfaces[1].get('id', None) == 'eni-0987654321' - assert camel_network_interfaces[1].get('tags', None) == {'Name': 'my-test-eni-name'} - assert camel_network_interfaces[1].get('name', None) == 'my-test-eni-name' - - -@patch(module_name + ".AnsibleAWSModule") -def test_main_success(m_AnsibleAWSModule): - m_module = MagicMock() - m_AnsibleAWSModule.return_value = m_module - - ec2_eni_info.main() + assert camel_network_interfaces[0]['id'] == 'eni-1234567890' + assert camel_network_interfaces[0]['tags'] == {} + assert camel_network_interfaces[0].get('name') is None - m_module.client.assert_called_with("ec2", retry_decorator=ANY) + assert camel_network_interfaces[1]['id'] == 'eni-0987654321' + assert camel_network_interfaces[1]['tags'] == {'Name': 'my-test-eni-name'} + assert camel_network_interfaces[1]['name'] == 'my-test-eni-name'