-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SUMMARY Refactor ec2_key, ec2_key_info modules ISSUE TYPE Feature Pull Request COMPONENT NAME ec2_key ec2_key_info Reviewed-by: Alina Buzachis
- Loading branch information
Showing
4 changed files
with
61 additions
and
88 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,4 @@ | ||
--- | ||
minor_changes: | ||
- ec2_key - refactored code to use ``AnsibleEC2Error`` as well as moving shared code into module_utils.ec2 (https://github.com/ansible-collections/amazon.aws/pull/2168). | ||
- ec2_key_info - refactored code to use ``AnsibleEC2Error`` as well as moving shared code into module_utils.ec2 (https://github.com/ansible-collections/amazon.aws/pull/2168). |
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
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
|
||
import copy | ||
import datetime | ||
from unittest.mock import ANY | ||
from unittest.mock import MagicMock | ||
from unittest.mock import patch | ||
|
||
|
@@ -40,7 +39,8 @@ def raise_botocore_exception_clienterror(action): | |
return botocore.exceptions.ClientError(params, action) | ||
|
||
|
||
def test__import_key_pair(): | ||
@patch(module_name + ".import_key_pair") | ||
def test__import_key_pair(m_import_key_pair): | ||
ec2_client = MagicMock() | ||
name = "my_keypair" | ||
key_material = "ssh-rsa AAAAB3NzaC1yc2EAA [email protected]" | ||
|
@@ -50,17 +50,17 @@ def test__import_key_pair(): | |
"PublicKeyMaterial": to_bytes(key_material), | ||
} | ||
|
||
ec2_client.import_key_pair.return_value = { | ||
m_import_key_pair.return_value = { | ||
"KeyFingerprint": "d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62", | ||
"KeyName": "my_keypair", | ||
"KeyPairId": "key-012345678905a208d", | ||
} | ||
|
||
result = ec2_key._import_key_pair(ec2_client, name, key_material) | ||
|
||
assert result == ec2_client.import_key_pair.return_value | ||
assert ec2_client.import_key_pair.call_count == 1 | ||
ec2_client.import_key_pair.assert_called_with(aws_retry=True, **expected_params) | ||
assert result == m_import_key_pair.return_value | ||
assert m_import_key_pair.call_count == 1 | ||
m_import_key_pair.assert_called_with(ec2_client, **expected_params) | ||
|
||
|
||
def test_api_failure__import_key_pair(): | ||
|
@@ -155,27 +155,26 @@ def test_get_key_fingerprint(m_find_key_pair, m_import_key_pair, m_delete_key_pa | |
assert m_delete_key_pair.call_count == 1 | ||
|
||
|
||
def test_find_key_pair(): | ||
@patch(module_name + ".describe_key_pairs") | ||
def test_find_key_pair(m_describe_key_pairs): | ||
ec2_client = MagicMock() | ||
name = "my_keypair" | ||
|
||
ec2_client.describe_key_pairs.return_value = { | ||
"KeyPairs": [ | ||
{ | ||
"CreateTime": datetime.datetime(2022, 9, 15, 20, 10, 15, tzinfo=tzutc()), | ||
"KeyFingerprint": "11:12:13:14:bb:26:85:b2:e8:39:27:bc:ee:aa:ff:ee:dd:cc:bb:aa", | ||
"KeyName": "my_keypair", | ||
"KeyPairId": "key-043046ef2a9a80b56", | ||
"KeyType": "rsa", | ||
"Tags": [], | ||
} | ||
], | ||
} | ||
m_describe_key_pairs.return_value = [ | ||
{ | ||
"CreateTime": datetime.datetime(2022, 9, 15, 20, 10, 15, tzinfo=tzutc()), | ||
"KeyFingerprint": "11:12:13:14:bb:26:85:b2:e8:39:27:bc:ee:aa:ff:ee:dd:cc:bb:aa", | ||
"KeyName": "my_keypair", | ||
"KeyPairId": "key-043046ef2a9a80b56", | ||
"KeyType": "rsa", | ||
"Tags": [], | ||
} | ||
] | ||
|
||
ec2_key.find_key_pair(ec2_client, name) | ||
|
||
assert ec2_client.describe_key_pairs.call_count == 1 | ||
ec2_client.describe_key_pairs.assert_called_with(aws_retry=True, KeyNames=[name]) | ||
assert m_describe_key_pairs.call_count == 1 | ||
m_describe_key_pairs.assert_called_with(ec2_client, KeyNames=[name]) | ||
|
||
|
||
def test_api_failure_find_key_pair(): | ||
|
@@ -199,15 +198,16 @@ def test_invalid_key_pair_find_key_pair(): | |
assert result is None | ||
|
||
|
||
def test__create_key_pair(): | ||
@patch(module_name + ".create_key_pair") | ||
def test__create_key_pair(m_create_key_pair): | ||
ec2_client = MagicMock() | ||
name = "my_keypair" | ||
tag_spec = None | ||
key_type = None | ||
|
||
expected_params = {"KeyName": name} | ||
|
||
ec2_client.create_key_pair.return_value = { | ||
m_create_key_pair.return_value = { | ||
"KeyFingerprint": "d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62", | ||
"KeyMaterial": ( | ||
"-----BEGIN RSA PRIVATE KEY-----\n" # gitleaks:allow | ||
|
@@ -220,9 +220,9 @@ def test__create_key_pair(): | |
|
||
result = ec2_key._create_key_pair(ec2_client, name, tag_spec, key_type) | ||
|
||
assert result == ec2_client.create_key_pair.return_value | ||
assert ec2_client.create_key_pair.call_count == 1 | ||
ec2_client.create_key_pair.assert_called_with(aws_retry=True, **expected_params) | ||
assert result == m_create_key_pair.return_value | ||
assert m_create_key_pair.call_count == 1 | ||
m_create_key_pair.assert_called_with(ec2_client, **expected_params) | ||
|
||
|
||
def test_api_failure__create_key_pair(): | ||
|
@@ -310,26 +310,6 @@ def test_create_new_key_pair_no_key_material(m_create_key_pair, m_extract_key_da | |
assert m_extract_key_data.call_count == 1 | ||
|
||
|
||
def test__delete_key_pair(): | ||
ec2_client = MagicMock() | ||
|
||
key_name = "my_keypair" | ||
ec2_key._delete_key_pair(ec2_client, key_name) | ||
|
||
assert ec2_client.delete_key_pair.call_count == 1 | ||
ec2_client.delete_key_pair.assert_called_with(aws_retry=True, KeyName=key_name) | ||
|
||
|
||
def test_api_failure__delete_key_pair(): | ||
ec2_client = MagicMock() | ||
name = "my_keypair" | ||
|
||
ec2_client.delete_key_pair.side_effect = raise_botocore_exception_clienterror("delete_key_pair") | ||
|
||
with pytest.raises(ec2_key.Ec2KeyFailure): | ||
ec2_key._delete_key_pair(ec2_client, name) | ||
|
||
|
||
@patch(module_name + ".extract_key_data") | ||
@patch(module_name + "._import_key_pair") | ||
@patch(module_name + ".delete_key_pair") | ||
|
@@ -577,7 +557,7 @@ def test_handle_existing_key_pair_else(m_extract_key_data): | |
assert m_extract_key_data.call_count == 1 | ||
|
||
|
||
@patch(module_name + "._delete_key_pair") | ||
@patch(module_name + ".delete_ec2_key_pair") | ||
@patch(module_name + ".find_key_pair") | ||
def test_delete_key_pair_key_exists(m_find_key_pair, m_delete_key_pair, tmp_path): | ||
module = MagicMock() | ||
|
@@ -609,7 +589,7 @@ def test_delete_key_pair_key_exists(m_find_key_pair, m_delete_key_pair, tmp_path | |
assert result == {"changed": True, "key": None, "msg": "key deleted"} | ||
|
||
|
||
@patch(module_name + "._delete_key_pair") | ||
@patch(module_name + ".delete_ec2_key_pair") | ||
@patch(module_name + ".find_key_pair") | ||
def test_delete_key_pair_key_not_exist(m_find_key_pair, m_delete_key_pair): | ||
module = MagicMock() | ||
|
@@ -656,4 +636,4 @@ def test_main_success(m_AnsibleAWSModule): | |
|
||
ec2_key.main() | ||
|
||
m_module.client.assert_called_with("ec2", retry_decorator=ANY) | ||
m_module.client.assert_called_with("ec2") |