Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Apr 27, 2023
1 parent 5344792 commit ca689b8
Show file tree
Hide file tree
Showing 20 changed files with 154 additions and 112 deletions.
2 changes: 1 addition & 1 deletion tests/integration/targets/lambda/files/mini_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def handler(event, context):

extra = os.environ.get("EXTRA_MESSAGE")
if extra is not None and len(extra) > 0:
greeting = "hello {0}. {1}".format(name, extra)
greeting = f"hello {name}. {extra}"
else:
greeting = "hello " + name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def handler(event, context):

extra = os.environ.get("EXTRA_MESSAGE")
if extra is not None and len(extra) > 0:
greeting = "hello {0}. {1}".format(name, extra)
greeting = f"hello {name}. {extra}"
else:
greeting = "hello " + name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def handler(event, context):

extra = os.environ.get("EXTRA_MESSAGE")
if extra is not None and len(extra) > 0:
greeting = "hello {0}. {1}".format(name, extra)
greeting = f"hello {name}. {extra}"
else:
greeting = "hello " + name

Expand Down
12 changes: 6 additions & 6 deletions tests/unit/module_utils/arn/test_parse_aws_arn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from ansible_collections.amazon.aws.plugins.module_utils.arn import parse_aws_arn

arn_bad_values = [
("arn:aws:outpost:us-east-1: 123456789012:outpost/op-1234567890abcdef0"),
("arn:aws:out post:us-east-1:123456789012:outpost/op-1234567890abcdef0"),
("arn:aws:outpost:us east 1:123456789012:outpost/op-1234567890abcdef0"),
("invalid:aws:outpost:us-east-1:123456789012:outpost/op-1234567890abcdef0"),
("arn:junk:outpost:us-east-1:123456789012:outpost/op-1234567890abcdef0"),
("arn:aws:outpost:us-east-1:junk:outpost/op-1234567890abcdef0"),
"arn:aws:outpost:us-east-1: 123456789012:outpost/op-1234567890abcdef0",
"arn:aws:out post:us-east-1:123456789012:outpost/op-1234567890abcdef0",
"arn:aws:outpost:us east 1:123456789012:outpost/op-1234567890abcdef0",
"invalid:aws:outpost:us-east-1:123456789012:outpost/op-1234567890abcdef0",
"arn:junk:outpost:us-east-1:123456789012:outpost/op-1234567890abcdef0",
"arn:aws:outpost:us-east-1:junk:outpost/op-1234567890abcdef0",
]

arn_good_values = [
Expand Down
28 changes: 16 additions & 12 deletions tests/unit/module_utils/botocore/test_is_boto3_error_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ def _make_denied_exception(self):
{
"Error": {
"Code": "AccessDenied",
"Message": "User: arn:aws:iam::123456789012:user/ExampleUser "
+ "is not authorized to perform: iam:GetUser on resource: user ExampleUser",
"Message": (
"User: arn:aws:iam::123456789012:user/ExampleUser "
+ "is not authorized to perform: iam:GetUser on resource: user ExampleUser"
),
},
"ResponseMetadata": {"RequestId": "01234567-89ab-cdef-0123-456789abcdef"},
},
Expand All @@ -47,16 +49,18 @@ def _make_encoded_exception(self):
{
"Error": {
"Code": "PermissionDenied",
"Message": "You are not authorized to perform this operation. Encoded authorization failure message: "
+ "fEwXX6llx3cClm9J4pURgz1XPnJPrYexEbrJcLhFkwygMdOgx_-aEsj0LqRM6Kxt2HVI6prUhDwbJqBo9U2V7iRKZ"
+ "T6ZdJvHH02cXmD0Jwl5vrTsf0PhBcWYlH5wl2qME7xTfdolEUr4CzumCiti7ETiO-RDdHqWlasBOW5bWsZ4GSpPdU"
+ "06YAX0TfwVBs48uU5RpCHfz1uhSzez-3elbtp9CmTOHLt5pzJodiovccO55BQKYLPtmJcs6S9YLEEogmpI4Cb1D26"
+ "fYahDh51jEmaohPnW5pb1nQe2yPEtuIhtRzNjhFCOOMwY5DBzNsymK-Gj6eJLm7FSGHee4AHLU_XmZMe_6bcLAiOx"
+ "6Zdl65Kdd0hLcpwVxyZMi27HnYjAdqRlV3wuCW2PkhAW14qZQLfiuHZDEwnPe2PBGSlFcCmkQvJvX-YLoA7Uyc2wf"
+ "NX5RJm38STwfiJSkQaNDhHKTWKiLOsgY4Gze6uZoG7zOcFXFRyaA4cbMmI76uyBO7j-9uQUCtBYqYto8x_9CUJcxI"
+ "VC5SPG_C1mk-WoDMew01f0qy-bNaCgmJ9TOQGd08FyuT1SaMpCC0gX6mHuOnEgkFw3veBIowMpp9XcM-yc42fmIOp"
+ "FOdvQO6uE9p55Qc-uXvsDTTvT3A7EeFU8a_YoAIt9UgNYM6VTvoprLz7dBI_P6C-bdPPZCY2amm-dJNVZelT6TbJB"
+ "H_Vxh0fzeiSUBersy_QzB0moc-vPWgnB-IkgnYLV-4L3K0L2",
"Message": (
"You are not authorized to perform this operation. Encoded authorization failure message: "
+ "fEwXX6llx3cClm9J4pURgz1XPnJPrYexEbrJcLhFkwygMdOgx_-aEsj0LqRM6Kxt2HVI6prUhDwbJqBo9U2V7iRKZ"
+ "T6ZdJvHH02cXmD0Jwl5vrTsf0PhBcWYlH5wl2qME7xTfdolEUr4CzumCiti7ETiO-RDdHqWlasBOW5bWsZ4GSpPdU"
+ "06YAX0TfwVBs48uU5RpCHfz1uhSzez-3elbtp9CmTOHLt5pzJodiovccO55BQKYLPtmJcs6S9YLEEogmpI4Cb1D26"
+ "fYahDh51jEmaohPnW5pb1nQe2yPEtuIhtRzNjhFCOOMwY5DBzNsymK-Gj6eJLm7FSGHee4AHLU_XmZMe_6bcLAiOx"
+ "6Zdl65Kdd0hLcpwVxyZMi27HnYjAdqRlV3wuCW2PkhAW14qZQLfiuHZDEwnPe2PBGSlFcCmkQvJvX-YLoA7Uyc2wf"
+ "NX5RJm38STwfiJSkQaNDhHKTWKiLOsgY4Gze6uZoG7zOcFXFRyaA4cbMmI76uyBO7j-9uQUCtBYqYto8x_9CUJcxI"
+ "VC5SPG_C1mk-WoDMew01f0qy-bNaCgmJ9TOQGd08FyuT1SaMpCC0gX6mHuOnEgkFw3veBIowMpp9XcM-yc42fmIOp"
+ "FOdvQO6uE9p55Qc-uXvsDTTvT3A7EeFU8a_YoAIt9UgNYM6VTvoprLz7dBI_P6C-bdPPZCY2amm-dJNVZelT6TbJB"
+ "H_Vxh0fzeiSUBersy_QzB0moc-vPWgnB-IkgnYLV-4L3K0L2"
),
},
"ResponseMetadata": {"RequestId": "01234567-89ab-cdef-0123-456789abcdef"},
},
Expand Down
28 changes: 16 additions & 12 deletions tests/unit/module_utils/botocore/test_is_boto3_error_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ def _make_denied_exception(self):
{
"Error": {
"Code": "AccessDenied",
"Message": "User: arn:aws:iam::123456789012:user/ExampleUser "
+ "is not authorized to perform: iam:GetUser on resource: user ExampleUser",
"Message": (
"User: arn:aws:iam::123456789012:user/ExampleUser "
+ "is not authorized to perform: iam:GetUser on resource: user ExampleUser"
),
},
"ResponseMetadata": {"RequestId": "01234567-89ab-cdef-0123-456789abcdef"},
},
Expand All @@ -47,16 +49,18 @@ def _make_encoded_exception(self):
{
"Error": {
"Code": "AccessDenied",
"Message": "You are not authorized to perform this operation. Encoded authorization failure message: "
+ "fEwXX6llx3cClm9J4pURgz1XPnJPrYexEbrJcLhFkwygMdOgx_-aEsj0LqRM6Kxt2HVI6prUhDwbJqBo9U2V7iRKZ"
+ "T6ZdJvHH02cXmD0Jwl5vrTsf0PhBcWYlH5wl2qME7xTfdolEUr4CzumCiti7ETiO-RDdHqWlasBOW5bWsZ4GSpPdU"
+ "06YAX0TfwVBs48uU5RpCHfz1uhSzez-3elbtp9CmTOHLt5pzJodiovccO55BQKYLPtmJcs6S9YLEEogmpI4Cb1D26"
+ "fYahDh51jEmaohPnW5pb1nQe2yPEtuIhtRzNjhFCOOMwY5DBzNsymK-Gj6eJLm7FSGHee4AHLU_XmZMe_6bcLAiOx"
+ "6Zdl65Kdd0hLcpwVxyZMi27HnYjAdqRlV3wuCW2PkhAW14qZQLfiuHZDEwnPe2PBGSlFcCmkQvJvX-YLoA7Uyc2wf"
+ "NX5RJm38STwfiJSkQaNDhHKTWKiLOsgY4Gze6uZoG7zOcFXFRyaA4cbMmI76uyBO7j-9uQUCtBYqYto8x_9CUJcxI"
+ "VC5SPG_C1mk-WoDMew01f0qy-bNaCgmJ9TOQGd08FyuT1SaMpCC0gX6mHuOnEgkFw3veBIowMpp9XcM-yc42fmIOp"
+ "FOdvQO6uE9p55Qc-uXvsDTTvT3A7EeFU8a_YoAIt9UgNYM6VTvoprLz7dBI_P6C-bdPPZCY2amm-dJNVZelT6TbJB"
+ "H_Vxh0fzeiSUBersy_QzB0moc-vPWgnB-IkgnYLV-4L3K0L2",
"Message": (
"You are not authorized to perform this operation. Encoded authorization failure message: "
+ "fEwXX6llx3cClm9J4pURgz1XPnJPrYexEbrJcLhFkwygMdOgx_-aEsj0LqRM6Kxt2HVI6prUhDwbJqBo9U2V7iRKZ"
+ "T6ZdJvHH02cXmD0Jwl5vrTsf0PhBcWYlH5wl2qME7xTfdolEUr4CzumCiti7ETiO-RDdHqWlasBOW5bWsZ4GSpPdU"
+ "06YAX0TfwVBs48uU5RpCHfz1uhSzez-3elbtp9CmTOHLt5pzJodiovccO55BQKYLPtmJcs6S9YLEEogmpI4Cb1D26"
+ "fYahDh51jEmaohPnW5pb1nQe2yPEtuIhtRzNjhFCOOMwY5DBzNsymK-Gj6eJLm7FSGHee4AHLU_XmZMe_6bcLAiOx"
+ "6Zdl65Kdd0hLcpwVxyZMi27HnYjAdqRlV3wuCW2PkhAW14qZQLfiuHZDEwnPe2PBGSlFcCmkQvJvX-YLoA7Uyc2wf"
+ "NX5RJm38STwfiJSkQaNDhHKTWKiLOsgY4Gze6uZoG7zOcFXFRyaA4cbMmI76uyBO7j-9uQUCtBYqYto8x_9CUJcxI"
+ "VC5SPG_C1mk-WoDMew01f0qy-bNaCgmJ9TOQGd08FyuT1SaMpCC0gX6mHuOnEgkFw3veBIowMpp9XcM-yc42fmIOp"
+ "FOdvQO6uE9p55Qc-uXvsDTTvT3A7EeFU8a_YoAIt9UgNYM6VTvoprLz7dBI_P6C-bdPPZCY2amm-dJNVZelT6TbJB"
+ "H_Vxh0fzeiSUBersy_QzB0moc-vPWgnB-IkgnYLV-4L3K0L2"
),
},
"ResponseMetadata": {"RequestId": "01234567-89ab-cdef-0123-456789abcdef"},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/module_utils/cloud/test_cloud_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, status):
self.status = status

def __str__(self):
return "TestException with status: {0}".format(self.status)
return f"TestException with status: {self.status}"

class UnitTestsRetry(CloudRetry):
base_class = Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def setup_method(self):
self.CAMEL_RESPONSE = camel_dict_to_snake_dict(self.EXAMPLE_EXCEPTION_DATA.get("ResponseMetadata"))
self.CAMEL_ERROR = camel_dict_to_snake_dict(self.EXAMPLE_EXCEPTION_DATA.get("Error"))
# ClientError(EXAMPLE_EXCEPTION_DATA, "testCall") will generate this
self.EXAMPLE_MSG = "An error occurred (InvalidParameterValue) when calling the testCall operation: The filter 'exampleFilter' is invalid"
self.EXAMPLE_MSG = (
"An error occurred (InvalidParameterValue) when calling the testCall operation: The filter 'exampleFilter'"
" is invalid"
)
self.DEFAULT_CORE_MSG = "An unspecified error occurred"
self.FAIL_MSG = "I Failed!"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test_require_botocore_at_least_with_reason(
# The message is generated by Ansible, don't test for an exact
# message
assert desired_version in return_val.get("msg")
assert " {0}".format(reason) in return_val.get("msg")
assert f" {reason}" in return_val.get("msg")
assert "botocore" in return_val.get("msg")
assert return_val.get("boto3_version") == DUMMY_VERSION
assert return_val.get("botocore_version") == compare_version
Expand Down Expand Up @@ -227,7 +227,7 @@ def test_require_boto3_at_least_with_reason(
# The message is generated by Ansible, don't test for an exact
# message
assert desired_version in return_val.get("msg")
assert " {0}".format(reason) in return_val.get("msg")
assert f" {reason}" in return_val.get("msg")
assert "boto3" in return_val.get("msg")
assert return_val.get("botocore_version") == DUMMY_VERSION
assert return_val.get("boto3_version") == compare_version
12 changes: 6 additions & 6 deletions tests/unit/module_utils/test_acm.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_acm_service_manager_get_domain_of_cert_failure(acm_service_mgr):
with pytest.raises(SystemExit):
acm_service_mgr.get_domain_of_cert(arn=arn)

error = "Couldn't obtain certificate data for arn %s" % arn
error = f"Couldn't obtain certificate data for arn {arn}"
acm_service_mgr.module.fail_json_aws.assert_called_with(boto_err, msg=error)
acm_service_mgr.module.fail.assert_not_called()

Expand Down Expand Up @@ -152,7 +152,7 @@ def test_acm_service_manager_import_certificate_failure_at_tagging(acm_service_m

with pytest.raises(SystemExit):
acm_service_mgr.import_certificate(certificate=MagicMock(), private_key=MagicMock())
acm_service_mgr.module.fail_json_aws.assert_called_with(boto_err, msg="Couldn't tag certificate %s" % arn)
acm_service_mgr.module.fail_json_aws.assert_called_with(boto_err, msg=f"Couldn't tag certificate {arn}")


def test_acm_service_manager_import_certificate_failure_at_deletion(acm_service_mgr):
Expand All @@ -166,7 +166,7 @@ def test_acm_service_manager_import_certificate_failure_at_deletion(acm_service_
with pytest.raises(SystemExit):
acm_service_mgr.import_certificate(certificate=MagicMock(), private_key=MagicMock())
acm_service_mgr.module.warn.assert_called_with(
"Certificate %s exists, and is not tagged. So Ansible will not see it on the next run." % arn
f"Certificate {arn} exists, and is not tagged. So Ansible will not see it on the next run."
)


Expand All @@ -180,7 +180,7 @@ def test_acm_service_manager_import_certificate_failure_with_arn_change(acm_serv
with pytest.raises(SystemExit):
acm_service_mgr.import_certificate(certificate=MagicMock(), private_key=MagicMock(), arn=original_arn)
acm_service_mgr.module.fail_json.assert_called_with(
msg="ARN changed with ACM update, from %s to %s" % (original_arn, arn)
msg=f"ARN changed with ACM update, from {original_arn} to {arn}"
)


Expand All @@ -199,7 +199,7 @@ def test_acm_service_manager_delete_certificate_keyword_arn(acm_service_mgr):
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012"
acm_service_mgr.delete_certificate_with_backoff = MagicMock()
acm_service_mgr.delete_certificate(arn=arn)
err = "Couldn't delete certificate %s" % arn
err = f"Couldn't delete certificate {arn}"
acm_service_mgr.delete_certificate_with_backoff.assert_called_with(arn, module=acm_service_mgr.module, error=err)


Expand All @@ -209,7 +209,7 @@ def test_acm_service_manager_delete_certificate_positional_arn(acm_service_mgr):
module = MagicMock()
client = MagicMock()
acm_service_mgr.delete_certificate(module, client, arn)
err = "Couldn't delete certificate %s" % arn
err = f"Couldn't delete certificate {arn}"
acm_service_mgr.delete_certificate_with_backoff.assert_called_with(arn, module=acm_service_mgr.module, error=err)


Expand Down
14 changes: 7 additions & 7 deletions tests/unit/module_utils/test_cloudfront_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def raise_botocore_error(operation="getCloudFront"):
def test_unsupported_api(cloudfront_facts_service):
with pytest.raises(CloudFrontFactsServiceManagerFailure) as err:
cloudfront_facts_service._unsupported_api()
assert "Method {0} is not currently supported".format("_unsupported_api") in err
assert "Method _unsupported_api is not currently supported" in err


def test_get_distribution(cloudfront_facts_service):
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_get_invalidation_failure(cloudfront_facts_service):
@patch(MOCK_CLOUDFRONT_FACTS_KEYED_LIST_HELPER)
def test_list_distributions_by_web_acl_id(m_cloudfront_facts_keyed_list_helper, cloudfront_facts_service):
web_acl_id = MagicMock()
distribution_webacl = {"DistributionList": {"Items": ["webacl_%d" % d for d in range(10)]}}
distribution_webacl = {"DistributionList": {"Items": [f"webacl_{int(d)}" for d in range(10)]}}
cloudfront_facts_service.client.list_distributions_by_web_acl_id.return_value = distribution_webacl
m_cloudfront_facts_keyed_list_helper.return_value = distribution_webacl["DistributionList"]["Items"]

Expand All @@ -124,7 +124,7 @@ def test_list_distributions_by_web_acl_id(m_cloudfront_facts_keyed_list_helper,
def test_list_origin_access_identities(
m_cloudfront_paginate_build_full_result, m_cloudfront_facts_keyed_list_helper, cloudfront_facts_service
):
items = ["item_%d" % d for d in range(10)]
items = [f"item_{int(d)}" for d in range(10)]
result = {"CloudFrontOriginAccessIdentityList": {"Items": items}}

m_cloudfront_paginate_build_full_result.return_value = result
Expand All @@ -137,7 +137,7 @@ def test_list_origin_access_identities(
def test_list_distributions(
m_cloudfront_paginate_build_full_result, m_cloudfront_facts_keyed_list_helper, cloudfront_facts_service
):
items = ["item_%d" % d for d in range(10)]
items = [f"item_{int(d)}" for d in range(10)]
result = {"DistributionList": {"Items": items}}

m_cloudfront_paginate_build_full_result.return_value = result
Expand All @@ -152,7 +152,7 @@ def test_list_distributions(
def test_list_invalidations(
m_cloudfront_paginate_build_full_result, m_cloudfront_facts_keyed_list_helper, cloudfront_facts_service
):
items = ["item_%d" % d for d in range(10)]
items = [f"item_{int(d)}" for d in range(10)]
result = {"InvalidationList": {"Items": items}}
distribution_id = MagicMock()

Expand Down Expand Up @@ -438,8 +438,8 @@ def test_summary_get_distribution_list_failure(cloudfront_facts_service, streami

def test_summary(cloudfront_facts_service):
cloudfront_facts_service.summary_get_distribution_list = MagicMock()
cloudfront_facts_service.summary_get_distribution_list.side_effect = (
lambda x: {"called_with_true": True} if x else {"called_with_false": False}
cloudfront_facts_service.summary_get_distribution_list.side_effect = lambda x: (
{"called_with_true": True} if x else {"called_with_false": False}
)

cloudfront_facts_service.summary_get_origin_access_identity_list = MagicMock()
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/module_utils/test_elbv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
}
],
},
"TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-tg-58045486/5b231e04f663ae21",
"TargetGroupArn": (
"arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-tg-58045486/5b231e04f663ae21"
),
"Type": "forward",
}
]
Expand Down Expand Up @@ -76,7 +78,9 @@ def setup_method(self):
"SecurityGroups": ["sg-5943793c"],
"LoadBalancerName": "my-load-balancer",
"State": {"Code": "active"},
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188",
"LoadBalancerArn": (
"arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188"
),
}
self.paginate.build_full_result.return_value = {"LoadBalancers": [self.loadbalancer]}

Expand Down
Loading

0 comments on commit ca689b8

Please sign in to comment.