From ca689b864a46c8e8148d7febe07b196fd93ff8b0 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Thu, 27 Apr 2023 10:36:24 +0200 Subject: [PATCH] tests --- .../targets/lambda/files/mini_lambda.py | 2 +- .../targets/lambda_alias/files/mini_lambda.py | 2 +- .../targets/lambda_event/files/mini_lambda.py | 2 +- .../module_utils/arn/test_parse_aws_arn.py | 12 +++---- .../botocore/test_is_boto3_error_code.py | 28 +++++++++------- .../botocore/test_is_boto3_error_message.py | 28 +++++++++------- .../module_utils/cloud/test_cloud_retry.py | 2 +- .../ansible_aws_module/test_fail_json_aws.py | 5 ++- .../test_require_at_least.py | 4 +-- tests/unit/module_utils/test_acm.py | 12 +++---- .../module_utils/test_cloudfront_facts.py | 14 ++++---- tests/unit/module_utils/test_elbv2.py | 8 +++-- tests/unit/module_utils/test_iam.py | 32 ++++++++++-------- tests/unit/module_utils/test_rds.py | 33 +++++++++++++++---- tests/unit/module_utils/test_s3.py | 32 +++++++++--------- tests/unit/plugins/inventory/test_aws_ec2.py | 6 ++-- tests/unit/plugins/inventory/test_aws_rds.py | 32 +++++++++--------- tests/unit/plugins/modules/test_ec2_key.py | 4 ++- .../unit/plugins/modules/test_lambda_layer.py | 4 ++- tests/unit/utils/amazon_placebo_fixtures.py | 4 +-- 20 files changed, 154 insertions(+), 112 deletions(-) diff --git a/tests/integration/targets/lambda/files/mini_lambda.py b/tests/integration/targets/lambda/files/mini_lambda.py index f1252c585c6..e21d27b9025 100644 --- a/tests/integration/targets/lambda/files/mini_lambda.py +++ b/tests/integration/targets/lambda/files/mini_lambda.py @@ -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 diff --git a/tests/integration/targets/lambda_alias/files/mini_lambda.py b/tests/integration/targets/lambda_alias/files/mini_lambda.py index f1252c585c6..e21d27b9025 100644 --- a/tests/integration/targets/lambda_alias/files/mini_lambda.py +++ b/tests/integration/targets/lambda_alias/files/mini_lambda.py @@ -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 diff --git a/tests/integration/targets/lambda_event/files/mini_lambda.py b/tests/integration/targets/lambda_event/files/mini_lambda.py index f1252c585c6..e21d27b9025 100644 --- a/tests/integration/targets/lambda_event/files/mini_lambda.py +++ b/tests/integration/targets/lambda_event/files/mini_lambda.py @@ -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 diff --git a/tests/unit/module_utils/arn/test_parse_aws_arn.py b/tests/unit/module_utils/arn/test_parse_aws_arn.py index 372e4498a49..49375a855de 100644 --- a/tests/unit/module_utils/arn/test_parse_aws_arn.py +++ b/tests/unit/module_utils/arn/test_parse_aws_arn.py @@ -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 = [ diff --git a/tests/unit/module_utils/botocore/test_is_boto3_error_code.py b/tests/unit/module_utils/botocore/test_is_boto3_error_code.py index 737f3793265..ad2b111ca97 100644 --- a/tests/unit/module_utils/botocore/test_is_boto3_error_code.py +++ b/tests/unit/module_utils/botocore/test_is_boto3_error_code.py @@ -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"}, }, @@ -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"}, }, diff --git a/tests/unit/module_utils/botocore/test_is_boto3_error_message.py b/tests/unit/module_utils/botocore/test_is_boto3_error_message.py index 169314d5834..6085c4b71b5 100644 --- a/tests/unit/module_utils/botocore/test_is_boto3_error_message.py +++ b/tests/unit/module_utils/botocore/test_is_boto3_error_message.py @@ -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"}, }, @@ -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"}, }, diff --git a/tests/unit/module_utils/cloud/test_cloud_retry.py b/tests/unit/module_utils/cloud/test_cloud_retry.py index 204ba918644..9feba78c585 100644 --- a/tests/unit/module_utils/cloud/test_cloud_retry.py +++ b/tests/unit/module_utils/cloud/test_cloud_retry.py @@ -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 diff --git a/tests/unit/module_utils/modules/ansible_aws_module/test_fail_json_aws.py b/tests/unit/module_utils/modules/ansible_aws_module/test_fail_json_aws.py index 5b76566c606..c044068540c 100644 --- a/tests/unit/module_utils/modules/ansible_aws_module/test_fail_json_aws.py +++ b/tests/unit/module_utils/modules/ansible_aws_module/test_fail_json_aws.py @@ -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!" diff --git a/tests/unit/module_utils/modules/ansible_aws_module/test_require_at_least.py b/tests/unit/module_utils/modules/ansible_aws_module/test_require_at_least.py index c389f13d51a..741f3406576 100644 --- a/tests/unit/module_utils/modules/ansible_aws_module/test_require_at_least.py +++ b/tests/unit/module_utils/modules/ansible_aws_module/test_require_at_least.py @@ -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 @@ -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 diff --git a/tests/unit/module_utils/test_acm.py b/tests/unit/module_utils/test_acm.py index 97cc0ad64e0..2669b594c34 100644 --- a/tests/unit/module_utils/test_acm.py +++ b/tests/unit/module_utils/test_acm.py @@ -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() @@ -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): @@ -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." ) @@ -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}" ) @@ -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) @@ -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) diff --git a/tests/unit/module_utils/test_cloudfront_facts.py b/tests/unit/module_utils/test_cloudfront_facts.py index 12852ff1cb8..8166e94573a 100644 --- a/tests/unit/module_utils/test_cloudfront_facts.py +++ b/tests/unit/module_utils/test_cloudfront_facts.py @@ -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): @@ -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"] @@ -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 @@ -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 @@ -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() @@ -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() diff --git a/tests/unit/module_utils/test_elbv2.py b/tests/unit/module_utils/test_elbv2.py index 24f15a700a7..d7293f0cead 100644 --- a/tests/unit/module_utils/test_elbv2.py +++ b/tests/unit/module_utils/test_elbv2.py @@ -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", } ] @@ -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]} diff --git a/tests/unit/module_utils/test_iam.py b/tests/unit/module_utils/test_iam.py index c8fa16eaf44..44f80064754 100644 --- a/tests/unit/module_utils/test_iam.py +++ b/tests/unit/module_utils/test_iam.py @@ -26,10 +26,12 @@ def _make_denied_exception(self, partition): { "Error": { "Code": "AccessDenied", - "Message": "User: arn:" - + partition - + ":iam::123456789012:user/ExampleUser " - + "is not authorized to perform: iam:GetUser on resource: user ExampleUser", + "Message": ( + "User: arn:" + + partition + + ":iam::123456789012:user/ExampleUser " + + "is not authorized to perform: iam:GetUser on resource: user ExampleUser" + ), }, "ResponseMetadata": {"RequestId": "01234567-89ab-cdef-0123-456789abcdef"}, }, @@ -50,16 +52,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"}, }, diff --git a/tests/unit/module_utils/test_rds.py b/tests/unit/module_utils/test_rds.py index 76dfb672383..3de8021470a 100644 --- a/tests/unit/module_utils/test_rds.py +++ b/tests/unit/module_utils/test_rds.py @@ -196,7 +196,10 @@ def test__wait_for_cluster_snapshot_status_failed(input, expected): {"wait": True}, *error( NotImplementedError, - match="method fake_method hasn't been added to the list of accepted methods to use a waiter in module_utils/rds.py", + match=( + "method fake_method hasn't been added to the list of accepted methods to use a waiter in" + " module_utils/rds.py" + ), ), ), ], @@ -360,7 +363,10 @@ def test__get_rds_method_attribute_cluster(method_name, params, expected, error) {"wait": True}, *error( NotImplementedError, - match="method fake_method hasn't been added to the list of accepted methods to use a waiter in module_utils/rds.py", + match=( + "method fake_method hasn't been added to the list of accepted methods to use a waiter in" + " module_utils/rds.py" + ), ), ), ], @@ -505,7 +511,10 @@ def test__get_rds_method_attribute_instance(method_name, params, expected, error {"wait": True}, *error( NotImplementedError, - match="method fake_method hasn't been added to the list of accepted methods to use a waiter in module_utils/rds.py", + match=( + "method fake_method hasn't been added to the list of accepted methods to use a waiter in" + " module_utils/rds.py" + ), ), ), ], @@ -627,7 +636,8 @@ def test__handle_errors(method_name, exception, expected): message="ModifyDbCluster API", ), *expected( - "It appears you are trying to modify attributes that are managed at the cluster level. Please see rds_cluster" + "It appears you are trying to modify attributes that are managed at the cluster level. Please see" + " rds_cluster" ), ), ( @@ -635,7 +645,10 @@ def test__handle_errors(method_name, exception, expected): build_exception("modify_db_instance", code="InvalidParameterCombination"), *error( NotImplementedError, - match="method modify_db_instance hasn't been added to the list of accepted methods to use a waiter in module_utils/rds.py", + match=( + "method modify_db_instance hasn't been added to the list of accepted methods to use a waiter in" + " module_utils/rds.py" + ), ), ), ( @@ -643,7 +656,10 @@ def test__handle_errors(method_name, exception, expected): build_exception("promote_read_replica", code="InvalidDBInstanceState"), *error( NotImplementedError, - match="method promote_read_replica hasn't been added to the list of accepted methods to use a waiter in module_utils/rds.py", + match=( + "method promote_read_replica hasn't been added to the list of accepted methods to use a waiter in" + " module_utils/rds.py" + ), ), ), ( @@ -651,7 +667,10 @@ def test__handle_errors(method_name, exception, expected): build_exception("promote_read_replica_db_cluster", code="InvalidDBClusterStateFault"), *error( NotImplementedError, - match="method promote_read_replica_db_cluster hasn't been added to the list of accepted methods to use a waiter in module_utils/rds.py", + match=( + "method promote_read_replica_db_cluster hasn't been added to the list of accepted methods to use a" + " waiter in module_utils/rds.py" + ), ), ), ( diff --git a/tests/unit/module_utils/test_s3.py b/tests/unit/module_utils/test_s3.py index 38c0d9cc078..d70c9bd1b75 100644 --- a/tests/unit/module_utils/test_s3.py +++ b/tests/unit/module_utils/test_s3.py @@ -30,8 +30,8 @@ def generate_random_string(size, include_digits=True): def test_s3_head_objects(parts, version): client = MagicMock() - s3bucket_name = "s3-bucket-%s" % (generate_random_string(8, False)) - s3bucket_object = "s3-bucket-object-%s" % (generate_random_string(8, False)) + s3bucket_name = f"s3-bucket-{generate_random_string(8, False)}" + s3bucket_object = f"s3-bucket-object-{generate_random_string(8, False)}" versionId = None if version: versionId = random.randint(0, 1000) @@ -69,7 +69,7 @@ def test_calculate_checksum(m_s3_head_objects, m_s3_md5, use_file, parts, tmp_pa mock_md5.digest.return_value = b"1" mock_md5.hexdigest.return_value = "".join(["f" for i in range(32)]) - m_s3_head_objects.return_value = [{"ContentLength": "%d" % (i + 1)} for i in range(parts)] + m_s3_head_objects.return_value = [{"ContentLength": f"{int(i + 1)}"} for i in range(parts)] content = b'"f20e84ac3d0c33cea77b3f29e3323a09"' test_function = s3.calculate_checksum_with_content @@ -82,13 +82,13 @@ def test_calculate_checksum(m_s3_head_objects, m_s3_md5, use_file, parts, tmp_pa content = str(etag_file) - s3bucket_name = "s3-bucket-%s" % (generate_random_string(8, False)) - s3bucket_object = "s3-bucket-object-%s" % (generate_random_string(8, False)) + s3bucket_name = f"s3-bucket-{generate_random_string(8, False)}" + s3bucket_object = f"s3-bucket-object-{generate_random_string(8, False)}" version = random.randint(0, 1000) result = test_function(client, parts, s3bucket_name, s3bucket_object, version, content) - expected = '"{0}-{1}"'.format(mock_md5.hexdigest.return_value, parts) + expected = f'"{mock_md5.hexdigest.return_value}-{parts}"' assert result == expected mock_md5.digest.assert_has_calls([call() for i in range(parts)]) @@ -106,8 +106,8 @@ def test_calculate_etag(m_checksum_file, etag_multipart): module.fail_json_aws.side_effect = SystemExit(2) module.md5.return_value = generate_random_string(32) - s3bucket_name = "s3-bucket-%s" % (generate_random_string(8, False)) - s3bucket_object = "s3-bucket-object-%s" % (generate_random_string(8, False)) + s3bucket_name = f"s3-bucket-{generate_random_string(8, False)}" + s3bucket_object = f"s3-bucket-object-{generate_random_string(8, False)}" version = random.randint(0, 1000) parts = 3 @@ -118,10 +118,10 @@ def test_calculate_etag(m_checksum_file, etag_multipart): if not etag_multipart: result = s3.calculate_etag(module, file_name, etag, client, s3bucket_name, s3bucket_object, version) - assert result == '"{0}"'.format(module.md5.return_value) + assert result == f'"{module.md5.return_value}"' module.md5.assert_called_once_with(file_name) else: - etag = '"f20e84ac3d0c33cea77b3f29e3323a09-{0}"'.format(parts) + etag = f'"f20e84ac3d0c33cea77b3f29e3323a09-{parts}"' m_checksum_file.return_value = digest assert digest == s3.calculate_etag(module, file_name, etag, client, s3bucket_name, s3bucket_object, version) @@ -136,8 +136,8 @@ def test_calculate_etag_content(m_checksum_content, etag_multipart): module.fail_json_aws.side_effect = SystemExit(2) - s3bucket_name = "s3-bucket-%s" % (generate_random_string(8, False)) - s3bucket_object = "s3-bucket-object-%s" % (generate_random_string(8, False)) + s3bucket_name = f"s3-bucket-{generate_random_string(8, False)}" + s3bucket_object = f"s3-bucket-object-{generate_random_string(8, False)}" version = random.randint(0, 1000) parts = 3 @@ -150,7 +150,7 @@ def test_calculate_etag_content(m_checksum_content, etag_multipart): module, content, etag, client, s3bucket_name, s3bucket_object, version ) else: - etag = '"f20e84ac3d0c33cea77b3f29e3323a09-{0}"'.format(parts) + etag = f'"f20e84ac3d0c33cea77b3f29e3323a09-{parts}"' m_checksum_content.return_value = digest result = s3.calculate_etag_content(module, content, etag, client, s3bucket_name, s3bucket_object, version) assert result == digest @@ -167,12 +167,12 @@ def test_calculate_etag_failure(m_checksum_file, m_checksum_content, using_file) module.fail_json_aws.side_effect = SystemExit(2) - s3bucket_name = "s3-bucket-%s" % (generate_random_string(8, False)) - s3bucket_object = "s3-bucket-object-%s" % (generate_random_string(8, False)) + s3bucket_name = f"s3-bucket-{generate_random_string(8, False)}" + s3bucket_object = f"s3-bucket-object-{generate_random_string(8, False)}" version = random.randint(0, 1000) parts = 3 - etag = '"f20e84ac3d0c33cea77b3f29e3323a09-{0}"'.format(parts) + etag = f'"f20e84ac3d0c33cea77b3f29e3323a09-{parts}"' content = "some content or file name" if using_file: diff --git a/tests/unit/plugins/inventory/test_aws_ec2.py b/tests/unit/plugins/inventory/test_aws_ec2.py index 22a5b2ebd22..72da05a1fcc 100644 --- a/tests/unit/plugins/inventory/test_aws_ec2.py +++ b/tests/unit/plugins/inventory/test_aws_ec2.py @@ -74,7 +74,7 @@ def inventory(): ({}, None), ({"GroupId": "test01"}, "test01"), ({"GroupId": ["test01"]}, "test01"), - ({"GroupId": ("test01")}, "test01"), + ({"GroupId": "test01"}, "test01"), ({"GroupId": ["test01", "test02"]}, ["test01", "test02"]), ([{"GroupId": ["test01", "test02"]}], ["test01", "test02"]), ([{"GroupId": ["test01"]}], "test01"), @@ -415,10 +415,10 @@ def test_inventory_query(inventory, include_filters, exclude_filters, instances_ } for u in include_filters: - params["include_filters"].append({"Name": "in_filters_%d" % u, "Values": [u]}) + params["include_filters"].append({"Name": f"in_filters_{int(u)}", "Values": [u]}) for u in exclude_filters: - params["exclude_filters"].append({"Name": "ex_filters_%d" % u, "Values": [u]}) + params["exclude_filters"].append({"Name": f"ex_filters_{int(u)}", "Values": [u]}) assert inventory._query(**params) == {"aws_ec2": instances} if not instances_by_region: diff --git a/tests/unit/plugins/inventory/test_aws_rds.py b/tests/unit/plugins/inventory/test_aws_rds.py index cba2c7923c0..96ca05bb8cb 100644 --- a/tests/unit/plugins/inventory/test_aws_rds.py +++ b/tests/unit/plugins/inventory/test_aws_rds.py @@ -208,7 +208,7 @@ def __init__(self, name, host_vars): @pytest.mark.parametrize("length", range(0, 10, 2)) def test_inventory_populate(inventory, length): group = "aws_rds" - hosts = ["host_%d" % i for i in range(length)] + hosts = [f"host_{int(i)}" for i in range(length)] inventory._add_hosts = MagicMock() inventory._populate(hosts=hosts) @@ -420,7 +420,7 @@ def test_inventory_get_all_db_hosts( ): params = { "gather_clusters": gather_clusters, - "regions": ["us-east-%d" % i for i in range(regions)], + "regions": [f"us-east-{int(i)}" for i in range(regions)], "instance_filters": generate_random_string(), "cluster_filters": generate_random_string(), "strict": random.choice((True, False)), @@ -429,11 +429,11 @@ def test_inventory_get_all_db_hosts( connections = [MagicMock() for i in range(regions)] - inventory.all_clients.return_value = [(connections[i], "us-east-%d" % i) for i in range(regions)] + inventory.all_clients.return_value = [(connections[i], f"us-east-{int(i)}") for i in range(regions)] ids = list(reversed(range(regions))) - db_instances = [{"DBInstanceIdentifier": "db_00%d" % i} for i in ids] - db_clusters = [{"DBClusterIdentifier": "cluster_00%d" % i} for i in ids] + db_instances = [{"DBInstanceIdentifier": f"db_00{int(i)}"} for i in ids] + db_clusters = [{"DBClusterIdentifier": f"cluster_00{int(i)}"} for i in ids] m_describe_db_instances.side_effect = [[i] for i in db_instances] m_describe_db_clusters.side_effect = [[i] for i in db_clusters] @@ -470,17 +470,17 @@ def test_inventory_add_hosts(m_get_rds_hostname, inventory, hostvars_prefix, hos } if hostvars_prefix: - _options["hostvars_prefix"] = "prefix_%s" % generate_random_string(length=8, with_punctuation=False) + _options["hostvars_prefix"] = f"prefix_{generate_random_string(length=8, with_punctuation=False)}" if hostvars_suffix: - _options["hostvars_suffix"] = "suffix_%s" % generate_random_string(length=8, with_punctuation=False) + _options["hostvars_suffix"] = f"suffix_{generate_random_string(length=8, with_punctuation=False)}" def _get_option_side_effect(x): return _options.get(x) inventory.get_option.side_effect = _get_option_side_effect - m_get_rds_hostname.side_effect = ( - lambda h: h["DBInstanceIdentifier"] if "DBInstanceIdentifier" in h else h["DBClusterIdentifier"] + m_get_rds_hostname.side_effect = lambda h: ( + h["DBInstanceIdentifier"] if "DBInstanceIdentifier" in h else h["DBClusterIdentifier"] ) hosts = [ @@ -504,7 +504,7 @@ def _get_option_side_effect(x): }, ] - group = "test_add_hosts_group_%s" % generate_random_string(length=10, with_punctuation=False) + group = f"test_add_hosts_group_{generate_random_string(length=10, with_punctuation=False)}" inventory._add_hosts(hosts, group) m_get_rds_hostname.assert_has_calls([call(h) for h in hosts], any_order=True) @@ -600,7 +600,7 @@ def test_inventory_parse( options["include_clusters"] = include_clusters options["filters"] = { "db-instance-id": [ - "arn:db:%s" % generate_random_string(with_punctuation=False) for i in range(random.randint(1, 10)) + f"arn:db:{generate_random_string(with_punctuation=False)}" for i in range(random.randint(1, 10)) ], "dbi-resource-id": generate_random_string(with_punctuation=False), "domain": generate_random_string(with_digits=False, with_punctuation=False), @@ -608,7 +608,7 @@ def test_inventory_parse( } if filter_db_cluster_id: options["filters"]["db-cluster-id"] = [ - "arn:cluster:%s" % generate_random_string(with_punctuation=False) for i in range(random.randint(1, 10)) + f"arn:cluster:{generate_random_string(with_punctuation=False)}" for i in range(random.randint(1, 10)) ] options["cache"] = user_cache_directive @@ -629,10 +629,10 @@ def get_option_side_effect(v): inventory._populate_from_source = MagicMock() inventory._get_all_db_hosts = MagicMock() all_db_hosts = [ - {"host": "host_%d" % random.randint(1, 1000)}, - {"host": "host_%d" % random.randint(1, 1000)}, - {"host": "host_%d" % random.randint(1, 1000)}, - {"host": "host_%d" % random.randint(1, 1000)}, + {"host": f"host_{int(random.randint(1, 1000))}"}, + {"host": f"host_{int(random.randint(1, 1000))}"}, + {"host": f"host_{int(random.randint(1, 1000))}"}, + {"host": f"host_{int(random.randint(1, 1000))}"}, ] inventory._get_all_db_hosts.return_value = all_db_hosts diff --git a/tests/unit/plugins/modules/test_ec2_key.py b/tests/unit/plugins/modules/test_ec2_key.py index 54d95d9b5c1..4305c8a7bf4 100644 --- a/tests/unit/plugins/modules/test_ec2_key.py +++ b/tests/unit/plugins/modules/test_ec2_key.py @@ -207,7 +207,9 @@ def test__create_key_pair(): ec2_client.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-----\nMIIEXm7/Bi9wba2m0Qtclu\nCXQw2paSIZb\n-----END RSA PRIVATE KEY-----", + "KeyMaterial": ( + "-----BEGIN RSA PRIVATE KEY-----\nMIIEXm7/Bi9wba2m0Qtclu\nCXQw2paSIZb\n-----END RSA PRIVATE KEY-----" + ), "KeyName": "my_keypair", "KeyPairId": "key-012345678905a208d", } diff --git a/tests/unit/plugins/modules/test_lambda_layer.py b/tests/unit/plugins/modules/test_lambda_layer.py index 763bfbef07b..6480647b964 100644 --- a/tests/unit/plugins/modules/test_lambda_layer.py +++ b/tests/unit/plugins/modules/test_lambda_layer.py @@ -223,7 +223,9 @@ def test_create_layer(m_list_layer, b_s3content, tmp_path): "Content": { "CodeSha256": "tv9jJO+rPbXUUXuRKi7CwHzKtLDkDRJLB3cC3Z/ouXo=", "CodeSize": 169, - "Location": "https://awslambda-us-west-2-layers.s3.us-west-2.amazonaws.com/snapshots/123456789012/my-layer-4aaa2fbb", + "Location": ( + "https://awslambda-us-west-2-layers.s3.us-west-2.amazonaws.com/snapshots/123456789012/my-layer-4aaa2fbb" + ), }, "CreatedDate": "2018-11-14T23:03:52.894+0000", "Description": "ansible units testing sample layer", diff --git a/tests/unit/utils/amazon_placebo_fixtures.py b/tests/unit/utils/amazon_placebo_fixtures.py index 28935fcbe04..3e06f97eb8d 100644 --- a/tests/unit/utils/amazon_placebo_fixtures.py +++ b/tests/unit/utils/amazon_placebo_fixtures.py @@ -67,7 +67,7 @@ def placeboify(request, monkeypatch): if not os.getenv("PLACEBO_RECORD"): if not os.path.isdir(recordings_path): - raise NotImplementedError("Missing Placebo recordings in directory: %s" % recordings_path) + raise NotImplementedError(f"Missing Placebo recordings in directory: {recordings_path}") else: try: # make sure the directory for placebo test recordings is available @@ -85,7 +85,7 @@ def placeboify(request, monkeypatch): def boto3_middleman_connection(module, conn_type, resource, region="us-west-2", **kwargs): if conn_type != "client": # TODO support resource-based connections - raise ValueError("Mocker only supports client, not %s" % conn_type) + raise ValueError(f"Mocker only supports client, not {conn_type}") return session.client(resource, region_name=region) import ansible_collections.amazon.aws.plugins.module_utils.ec2