| 1 |
# (c) 2022 Red Hat Inc. |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
import warnings |
| 7 |
from unittest.mock import MagicMock |
| 8 |
from unittest.mock import call |
| 9 |
from unittest.mock import sentinel |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from ansible.errors import AnsibleError |
| 14 |
|
| 15 |
import ansible_collections.amazon.aws.plugins.plugin_utils.base as utils_base |
| 16 |
|
| 17 |
|
| 18 |
def test_debug(monkeypatch): |
| 19 |
monkeypatch.setattr(utils_base.display, "debug", warnings.warn) |
| 20 |
base_plugin = utils_base.AWSPluginBase() |
| 21 |
|
| 22 |
with pytest.warns(UserWarning, match="My debug message"): |
| 23 |
base_plugin.debug("My debug message") |
| 24 |
|
| 25 |
|
| 26 |
def test_warn(monkeypatch): |
| 27 |
monkeypatch.setattr(utils_base.display, "warning", warnings.warn) |
| 28 |
base_plugin = utils_base.AWSPluginBase() |
| 29 |
|
| 30 |
with pytest.warns(UserWarning, match="My warning message"): |
| 31 |
base_plugin.warn("My warning message") |
| 32 |
|
| 33 |
|
| 34 |
def test_do_fail(): |
| 35 |
base_plugin = utils_base.AWSPluginBase() |
| 36 |
|
| 37 |
with pytest.raises(AnsibleError, match="My exception message"): |
| 38 |
base_plugin._do_fail("My exception message") |
| 39 |
|
| 40 |
|
| 41 |
def test_fail_aws(): |
| 42 |
base_plugin = utils_base.AWSPluginBase() |
| 43 |
example_exception = Exception("My example exception") |
| 44 |
example_message = "My example failure message" |
| 45 |
|
| 46 |
with pytest.raises(AnsibleError, match="My example failure message"): |
| 47 |
base_plugin.fail_aws(example_message) |
| 48 |
|
| 49 |
with pytest.raises(AnsibleError, match="My example failure message"): |
| 50 |
base_plugin.fail_aws(message=example_message) |
| 51 |
|
| 52 |
# As long as example_example_exception is supported by to_native, we're good. |
| 53 |
with pytest.raises(AnsibleError, match="My example exception"): |
| 54 |
base_plugin.fail_aws(example_exception) |
| 55 |
|
| 56 |
with pytest.raises(AnsibleError, match="My example failure message: My example exception"): |
| 57 |
base_plugin.fail_aws(example_message, example_exception) |
| 58 |
|
| 59 |
with pytest.raises(AnsibleError, match="My example failure message: My example exception"): |
| 60 |
base_plugin.fail_aws(message=example_message, exception=example_exception) |
| 61 |
|
| 62 |
|
| 63 |
def test_region(monkeypatch): |
| 64 |
get_aws_region = MagicMock(name="get_aws_region") |
| 65 |
get_aws_region.return_value = sentinel.RETURNED_REGION |
| 66 |
monkeypatch.setattr(utils_base, "get_aws_region", get_aws_region) |
| 67 |
base_plugin = utils_base.AWSPluginBase() |
| 68 |
|
| 69 |
assert base_plugin.region is sentinel.RETURNED_REGION |
| 70 |
assert get_aws_region.call_args == call(base_plugin) |
| 71 |
|
| 72 |
|
| 73 |
def test_require_aws_sdk(monkeypatch): |
| 74 |
require_sdk = MagicMock(name="check_sdk_version_supported") |
| 75 |
require_sdk.return_value = sentinel.RETURNED_SDK |
| 76 |
monkeypatch.setattr(utils_base, "check_sdk_version_supported", require_sdk) |
| 77 |
|
| 78 |
base_plugin = utils_base.AWSPluginBase() |
| 79 |
assert base_plugin.require_aws_sdk() is sentinel.RETURNED_SDK |
| 80 |
assert require_sdk.call_args == call(botocore_version=None, boto3_version=None, warn=base_plugin.warn) |
| 81 |
|
| 82 |
base_plugin = utils_base.AWSPluginBase() |
| 83 |
assert ( |
| 84 |
base_plugin.require_aws_sdk(botocore_version=sentinel.PARAM_BOTOCORE, boto3_version=sentinel.PARAM_BOTO3) |
| 85 |
is sentinel.RETURNED_SDK |
| 86 |
) |
| 87 |
assert require_sdk.call_args == call( |
| 88 |
botocore_version=sentinel.PARAM_BOTOCORE, boto3_version=sentinel.PARAM_BOTO3, warn=base_plugin.warn |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
def test_client_no_wrapper(monkeypatch): |
| 93 |
get_aws_connection_info = MagicMock(name="get_aws_connection_info") |
| 94 |
sentinel.CONN_ARGS = dict() |
| 95 |
get_aws_connection_info.return_value = (sentinel.CONN_REGION, sentinel.CONN_URL, sentinel.CONN_ARGS) |
| 96 |
monkeypatch.setattr(utils_base, "get_aws_connection_info", get_aws_connection_info) |
| 97 |
boto3_conn = MagicMock(name="boto3_conn") |
| 98 |
boto3_conn.return_value = sentinel.BOTO3_CONN |
| 99 |
monkeypatch.setattr(utils_base, "boto3_conn", boto3_conn) |
| 100 |
|
| 101 |
base_plugin = utils_base.AWSPluginBase() |
| 102 |
assert base_plugin.client(sentinel.PARAM_SERVICE) is sentinel.BOTO3_CONN |
| 103 |
assert get_aws_connection_info.call_args == call(base_plugin) |
| 104 |
assert boto3_conn.call_args == call( |
| 105 |
base_plugin, |
| 106 |
conn_type="client", |
| 107 |
resource=sentinel.PARAM_SERVICE, |
| 108 |
region=sentinel.CONN_REGION, |
| 109 |
endpoint=sentinel.CONN_URL, |
| 110 |
) |
| 111 |
|
| 112 |
|
| 113 |
def test_client_wrapper(monkeypatch): |
| 114 |
get_aws_connection_info = MagicMock(name="get_aws_connection_info") |
| 115 |
sentinel.CONN_ARGS = dict() |
| 116 |
get_aws_connection_info.return_value = (sentinel.CONN_REGION, sentinel.CONN_URL, sentinel.CONN_ARGS) |
| 117 |
monkeypatch.setattr(utils_base, "get_aws_connection_info", get_aws_connection_info) |
| 118 |
boto3_conn = MagicMock(name="boto3_conn") |
| 119 |
boto3_conn.return_value = sentinel.BOTO3_CONN |
| 120 |
monkeypatch.setattr(utils_base, "boto3_conn", boto3_conn) |
| 121 |
|
| 122 |
base_plugin = utils_base.AWSPluginBase() |
| 123 |
wrapped_conn = base_plugin.client(sentinel.PARAM_SERVICE, sentinel.PARAM_WRAPPER) |
| 124 |
assert wrapped_conn.client is sentinel.BOTO3_CONN |
| 125 |
assert wrapped_conn.retry is sentinel.PARAM_WRAPPER |
| 126 |
assert get_aws_connection_info.call_args == call(base_plugin) |
| 127 |
assert boto3_conn.call_args == call( |
| 128 |
base_plugin, |
| 129 |
conn_type="client", |
| 130 |
resource=sentinel.PARAM_SERVICE, |
| 131 |
region=sentinel.CONN_REGION, |
| 132 |
endpoint=sentinel.CONN_URL, |
| 133 |
) |
| 134 |
|
| 135 |
# Check that we can override parameters |
| 136 |
wrapped_conn = base_plugin.client(sentinel.PARAM_SERVICE, sentinel.PARAM_WRAPPER, region=sentinel.PARAM_REGION) |
| 137 |
assert wrapped_conn.client is sentinel.BOTO3_CONN |
| 138 |
assert wrapped_conn.retry is sentinel.PARAM_WRAPPER |
| 139 |
assert get_aws_connection_info.call_args == call(base_plugin) |
| 140 |
assert boto3_conn.call_args == call( |
| 141 |
base_plugin, |
| 142 |
conn_type="client", |
| 143 |
resource=sentinel.PARAM_SERVICE, |
| 144 |
region=sentinel.PARAM_REGION, |
| 145 |
endpoint=sentinel.CONN_URL, |
| 146 |
) |
| 147 |
|
| 148 |
|
| 149 |
def test_resource(monkeypatch): |
| 150 |
get_aws_connection_info = MagicMock(name="get_aws_connection_info") |
| 151 |
sentinel.CONN_ARGS = dict() |
| 152 |
get_aws_connection_info.return_value = (sentinel.CONN_REGION, sentinel.CONN_URL, sentinel.CONN_ARGS) |
| 153 |
monkeypatch.setattr(utils_base, "get_aws_connection_info", get_aws_connection_info) |
| 154 |
boto3_conn = MagicMock(name="boto3_conn") |
| 155 |
boto3_conn.return_value = sentinel.BOTO3_CONN |
| 156 |
monkeypatch.setattr(utils_base, "boto3_conn", boto3_conn) |
| 157 |
|
| 158 |
base_plugin = utils_base.AWSPluginBase() |
| 159 |
assert base_plugin.resource(sentinel.PARAM_SERVICE) is sentinel.BOTO3_CONN |
| 160 |
assert get_aws_connection_info.call_args == call(base_plugin) |
| 161 |
assert boto3_conn.call_args == call( |
| 162 |
base_plugin, |
| 163 |
conn_type="resource", |
| 164 |
resource=sentinel.PARAM_SERVICE, |
| 165 |
region=sentinel.CONN_REGION, |
| 166 |
endpoint=sentinel.CONN_URL, |
| 167 |
) |
| 168 |
|
| 169 |
assert base_plugin.resource(sentinel.PARAM_SERVICE, region=sentinel.PARAM_REGION) is sentinel.BOTO3_CONN |
| 170 |
assert get_aws_connection_info.call_args == call(base_plugin) |
| 171 |
assert boto3_conn.call_args == call( |
| 172 |
base_plugin, |
| 173 |
conn_type="resource", |
| 174 |
resource=sentinel.PARAM_SERVICE, |
| 175 |
region=sentinel.PARAM_REGION, |
| 176 |
endpoint=sentinel.CONN_URL, |
| 177 |
) |