diff --git a/tests/unit/plugins/modules/s3_bucket/test_handle_bucket_accelerate.py b/tests/unit/plugins/modules/s3_bucket/test_handle_bucket_accelerate.py index 8cb8db594c..65594b504f 100644 --- a/tests/unit/plugins/modules/s3_bucket/test_handle_bucket_accelerate.py +++ b/tests/unit/plugins/modules/s3_bucket/test_handle_bucket_accelerate.py @@ -2,19 +2,28 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from unittest.mock import MagicMock -from unittest.mock import patch from unittest.mock import sentinel import botocore import pytest +import ansible_collections.amazon.aws.plugins.module_utils.s3 as s3_utils from ansible_collections.amazon.aws.plugins.modules.s3_bucket import handle_bucket_accelerate -module_name = "ansible_collections.amazon.aws.plugins.modules.s3_bucket" +SKIPABLE_ERRORS = [ + ("NotImplemented", "Fetching bucket transfer acceleration state is not supported", s3_utils.AnsibleS3SupportError), + ("XNotImplemented", "Fetching bucket transfer acceleration state is not supported", s3_utils.AnsibleS3SupportError), + ("UnsupportedArgument", "Argument not supported in the current Region", s3_utils.AnsibleS3RegionSupportError), + ("AccessDenied", "Permission denied fetching transfer acceleration for bucket", s3_utils.AnsibleS3PermissionsError), +] +BAD_ERRORS = [ + ("GenericBadClientError", "Failed to fetch bucket transfer acceleration state", s3_utils.AnsibleS3Error), +] -def a_botocore_exception(message): - return botocore.exceptions.ClientError({"Error": {"Code": message}}, sentinel.BOTOCORE_ACTION) + +def a_botocore_exception(code, message): + return botocore.exceptions.ClientError({"Error": {"Code": code, "Message": message}}, sentinel.BOTOCORE_ACTION) @pytest.fixture(name="ansible_module") @@ -22,69 +31,46 @@ def fixure_ansible_module(): mock = MagicMock() mock.params = {"accelerate_enabled": sentinel.ACCELERATE_ENABLED} mock.fail_json_aws.side_effect = SystemExit(1) + mock.fail_json.side_effect = SystemExit(1) return mock -@pytest.mark.parametrize( - "code,message", - [ - ("NotImplemented", "Fetching bucket transfer acceleration state is not supported"), - ("XNotImplemented", "Fetching bucket transfer acceleration state is not supported"), - ("AccessDenied", "Permission denied fetching transfer acceleration for bucket"), - (sentinel.BOTO_CLIENT_ERROR, "Failed to fetch bucket transfer acceleration state"), - ], -) -@patch(module_name + ".get_bucket_accelerate_status") -def test_failure(m_get_bucket_accelerate_status, ansible_module, code, message): +# Test that we fail "gracefully" if we don't support bucket acceleration +@pytest.mark.parametrize("code,message,exception", [*BAD_ERRORS, *SKIPABLE_ERRORS]) +def test_failure(ansible_module, code, message, exception): bucket_name = sentinel.BUCKET_NAME + exc = a_botocore_exception(code, message) client = MagicMock() - exc = a_botocore_exception(code) - m_get_bucket_accelerate_status.side_effect = exc - with pytest.raises(SystemExit): + client.get_bucket_accelerate_configuration.side_effect = exc + with pytest.raises(exception): handle_bucket_accelerate(client, ansible_module, bucket_name) - ansible_module.fail_json_aws.assert_called_once_with(exc, msg=message) -@patch(module_name + ".get_bucket_accelerate_status") -def test_unsupported(m_get_bucket_accelerate_status, ansible_module): +# Test that we only emit a warning if we don't support bucket acceleration but aren't trying to +# manage the acceleration configuration +@pytest.mark.parametrize("code,message,exception", SKIPABLE_ERRORS) +def test_no_argument(ansible_module, code, message, exception): bucket_name = sentinel.BUCKET_NAME + exc = a_botocore_exception(code, message) client = MagicMock() - m_get_bucket_accelerate_status.side_effect = a_botocore_exception("UnsupportedArgument") + client.get_bucket_accelerate_configuration.side_effect = exc + + ansible_module.params["accelerate_enabled"] = None changed, result = handle_bucket_accelerate(client, ansible_module, bucket_name) assert changed is False - assert result is False + assert result is None ansible_module.warn.assert_called_once() -@pytest.mark.parametrize("accelerate_enabled", [True, False]) -@patch(module_name + ".delete_bucket_accelerate_configuration") -@patch(module_name + ".get_bucket_accelerate_status") -def test_delete( - m_get_bucket_accelerate_status, m_delete_bucket_accelerate_configuration, ansible_module, accelerate_enabled -): - bucket_name = sentinel.BUCKET_NAME - client = MagicMock() - ansible_module.params.update({"accelerate_enabled": accelerate_enabled}) - m_get_bucket_accelerate_status.return_value = True - if not accelerate_enabled: - assert (True, False) == handle_bucket_accelerate(client, ansible_module, bucket_name) - m_delete_bucket_accelerate_configuration.assert_called_once_with(client, bucket_name) - else: - assert (False, True) == handle_bucket_accelerate(client, ansible_module, bucket_name) - m_delete_bucket_accelerate_configuration.assert_not_called() - - -@pytest.mark.parametrize("accelerate_enabled", [True, False]) -@patch(module_name + ".put_bucket_accelerate_configuration") -@patch(module_name + ".get_bucket_accelerate_status") -def test_put(m_get_bucket_accelerate_status, m_put_bucket_accelerate_configuration, ansible_module, accelerate_enabled): +# Test that we're not being "too" relaxed with our handling, we should only be ignoring errors +# caused by a total lack of support from the platform +@pytest.mark.parametrize("code,message,exception", BAD_ERRORS) +def test_no_argument_fatal(ansible_module, code, message, exception): bucket_name = sentinel.BUCKET_NAME + exc = a_botocore_exception(code, message) client = MagicMock() - ansible_module.params.update({"accelerate_enabled": accelerate_enabled}) - m_get_bucket_accelerate_status.return_value = False - if accelerate_enabled: - assert (True, True) == handle_bucket_accelerate(client, ansible_module, bucket_name) - m_put_bucket_accelerate_configuration.assert_called_once_with(client, bucket_name) - else: - assert (False, False) == handle_bucket_accelerate(client, ansible_module, bucket_name) - m_put_bucket_accelerate_configuration.assert_not_called() + client.get_bucket_accelerate_configuration.side_effect = exc + + ansible_module.params["accelerate_enabled"] = None + with pytest.raises(exception): + handle_bucket_accelerate(client, ansible_module, bucket_name)