forked from ansible-collections/community.aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ec2_eni_info/tests: add unit-tests (ansible-collections#1236)
ec2_eni_info/tests: add unit-tests SUMMARY Add the unit-test coverage of the ec2_eni_info module. break up list_eni to move connection.describe_network_interfaces to separate method. refactor ec2_eni_info module COMPONENT NAME ec2_eni_info Reviewed-by: Gonéri Le Bouder <[email protected]>
- Loading branch information
Showing
3 changed files
with
159 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
minor_changes: | ||
- "ec2_eni_info - Add unit-tests coverage (https://github.com/ansible-collections/amazon.aws/pull/1236)." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# (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, call | ||
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 | ||
|
||
|
||
def test_get_network_interfaces(): | ||
connection = MagicMock() | ||
module = MagicMock() | ||
|
||
connection.describe_network_interfaces.return_value = { | ||
'NetworkInterfaces': [ | ||
{ | ||
"AvailabilityZone": "us-east-2b", | ||
"Description": "", | ||
"NetworkInterfaceId": "eni-1234567890", | ||
"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 + '.get_network_interfaces') | ||
def test_list_eni(m_get_network_interfaces): | ||
connection = MagicMock() | ||
module = MagicMock() | ||
|
||
m_get_network_interfaces.return_value = { | ||
'NetworkInterfaces': [ | ||
{ | ||
"AvailabilityZone": "us-east-2b", | ||
"Description": "", | ||
"NetworkInterfaceId": "eni-1234567890", | ||
"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": "", | ||
"NetworkInterfaceId": "eni-0987654321", | ||
"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 = { | ||
'Filters': [{ | ||
'Name': 'owner-id', | ||
'Values': [ | ||
'1234567890' | ||
] | ||
}] | ||
} | ||
|
||
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]['id'] == 'eni-1234567890' | ||
assert camel_network_interfaces[0]['tags'] == {} | ||
assert camel_network_interfaces[0].get('name') is None | ||
|
||
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' |