-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
175 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
This file was deleted.
Oops, something went wrong.
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,31 @@ | ||
# (c) 2022 Red Hat Inc. | ||
# | ||
# This file is part of Ansible | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from unittest.mock import call | ||
from unittest.mock import MagicMock | ||
from unittest.mock import patch | ||
from unittest.mock import sentinel | ||
|
||
import ansible_collections.amazon.aws.plugins.plugin_utils.inventory as utils_inventory | ||
|
||
|
||
@patch("ansible.plugins.inventory.BaseInventoryPlugin.parse", MagicMock) | ||
def test_parse(monkeypatch): | ||
require_aws_sdk = MagicMock(name='require_aws_sdk') | ||
require_aws_sdk.return_value = sentinel.RETURNED_SDK | ||
config_data = MagicMock(name='_read_config_data') | ||
config_data.return_value = sentinel.RETURNED_OPTIONS | ||
frozen_credentials = MagicMock(name='_set_frozen_credentials') | ||
frozen_credentials.return_value = sentinel.RETURNED_CREDENTIALS | ||
|
||
inventory_plugin = utils_inventory.AWSInventoryBase() | ||
monkeypatch.setattr(inventory_plugin, 'require_aws_sdk', require_aws_sdk) | ||
monkeypatch.setattr(inventory_plugin, '_read_config_data', config_data) | ||
monkeypatch.setattr(inventory_plugin, '_set_frozen_credentials', frozen_credentials) | ||
|
||
inventory_plugin.parse(sentinel.PARAM_INVENTORY, sentinel.PARAM_LOADER, sentinel.PARAM_PATH) | ||
assert require_aws_sdk.call_args == call(botocore_version=None, boto3_version=None) | ||
assert config_data.call_args == call(sentinel.PARAM_PATH) | ||
assert frozen_credentials.call_args == call() |
112 changes: 112 additions & 0 deletions
112
tests/unit/plugin_utils/inventory/test_inventory_clients.py
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,112 @@ | ||
# (c) 2022 Red Hat Inc. | ||
# | ||
# This file is part of Ansible | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from unittest.mock import call | ||
from unittest.mock import MagicMock | ||
from unittest.mock import sentinel | ||
|
||
import ansible_collections.amazon.aws.plugins.plugin_utils.inventory as utils_inventory | ||
import ansible_collections.amazon.aws.plugins.plugin_utils.base as utils_base | ||
# import ansible_collections.amazon.aws.plugins.module_utils. | ||
|
||
|
||
def test_client(monkeypatch): | ||
super_client = MagicMock(name="client") | ||
super_client.return_value = sentinel.SUPER_CLIENT | ||
monkeypatch.setattr(utils_base.AWSPluginBase, "client", super_client) | ||
inventory_plugin = utils_inventory.AWSInventoryBase() | ||
|
||
client = inventory_plugin.client(sentinel.SERVICE_NAME) | ||
assert super_client.call_args == call(sentinel.SERVICE_NAME) | ||
assert client is sentinel.SUPER_CLIENT | ||
|
||
client = inventory_plugin.client(sentinel.SERVICE_NAME, extra_arg=sentinel.EXTRA_ARG) | ||
assert super_client.call_args == call(sentinel.SERVICE_NAME, extra_arg=sentinel.EXTRA_ARG) | ||
assert client is sentinel.SUPER_CLIENT | ||
|
||
frozen_creds = {"credential_one": sentinel.CREDENTIAL_ONE} | ||
inventory_plugin._frozen_credentials = frozen_creds | ||
|
||
client = inventory_plugin.client(sentinel.SERVICE_NAME) | ||
assert super_client.call_args == call( | ||
sentinel.SERVICE_NAME, | ||
credential_one=sentinel.CREDENTIAL_ONE | ||
) | ||
assert client is sentinel.SUPER_CLIENT | ||
|
||
client = inventory_plugin.client(sentinel.SERVICE_NAME, extra_arg=sentinel.EXTRA_ARG) | ||
assert super_client.call_args == call( | ||
sentinel.SERVICE_NAME, | ||
credential_one=sentinel.CREDENTIAL_ONE, | ||
extra_arg=sentinel.EXTRA_ARG | ||
) | ||
assert client is sentinel.SUPER_CLIENT | ||
|
||
client = inventory_plugin.client(sentinel.SERVICE_NAME, credential_one=sentinel.CREDENTIAL_ARG) | ||
assert super_client.call_args == call( | ||
sentinel.SERVICE_NAME, | ||
credential_one=sentinel.CREDENTIAL_ARG, | ||
) | ||
assert client is sentinel.SUPER_CLIENT | ||
|
||
|
||
def test_resource(monkeypatch): | ||
super_resource = MagicMock(name="resource") | ||
super_resource.return_value = sentinel.SUPER_RESOURCE | ||
monkeypatch.setattr(utils_base.AWSPluginBase, "resource", super_resource) | ||
inventory_plugin = utils_inventory.AWSInventoryBase() | ||
|
||
resource = inventory_plugin.resource(sentinel.SERVICE_NAME) | ||
assert super_resource.call_args == call(sentinel.SERVICE_NAME) | ||
assert resource is sentinel.SUPER_RESOURCE | ||
|
||
resource = inventory_plugin.resource(sentinel.SERVICE_NAME, extra_arg=sentinel.EXTRA_ARG) | ||
assert super_resource.call_args == call(sentinel.SERVICE_NAME, extra_arg=sentinel.EXTRA_ARG) | ||
assert resource is sentinel.SUPER_RESOURCE | ||
|
||
frozen_creds = {"credential_one": sentinel.CREDENTIAL_ONE} | ||
inventory_plugin._frozen_credentials = frozen_creds | ||
|
||
resource = inventory_plugin.resource(sentinel.SERVICE_NAME) | ||
assert super_resource.call_args == call( | ||
sentinel.SERVICE_NAME, | ||
credential_one=sentinel.CREDENTIAL_ONE | ||
) | ||
assert resource is sentinel.SUPER_RESOURCE | ||
|
||
resource = inventory_plugin.resource(sentinel.SERVICE_NAME, extra_arg=sentinel.EXTRA_ARG) | ||
assert super_resource.call_args == call( | ||
sentinel.SERVICE_NAME, | ||
credential_one=sentinel.CREDENTIAL_ONE, | ||
extra_arg=sentinel.EXTRA_ARG | ||
) | ||
assert resource is sentinel.SUPER_RESOURCE | ||
|
||
resource = inventory_plugin.resource(sentinel.SERVICE_NAME, credential_one=sentinel.CREDENTIAL_ARG) | ||
assert super_resource.call_args == call( | ||
sentinel.SERVICE_NAME, | ||
credential_one=sentinel.CREDENTIAL_ARG, | ||
) | ||
assert resource is sentinel.SUPER_RESOURCE | ||
|
||
|
||
def test_all_clients(monkeypatch): | ||
test_regions = ['us-east-1', 'us-east-2'] | ||
inventory_plugin = utils_inventory.AWSInventoryBase() | ||
mock_client = MagicMock(name="client") | ||
mock_client.return_value = sentinel.RETURN_CLIENT | ||
monkeypatch.setattr(inventory_plugin, "client", mock_client) | ||
boto3_regions = MagicMock(name="_boto3_regions") | ||
boto3_regions.return_value = test_regions | ||
monkeypatch.setattr(inventory_plugin, "_boto3_regions", boto3_regions) | ||
|
||
regions = [] | ||
for (client, region) in inventory_plugin.all_clients(sentinel.ARG_SERVICE): | ||
assert boto3_regions.call_args == call(service=sentinel.ARG_SERVICE) | ||
assert mock_client.call_args == call(sentinel.ARG_SERVICE, region=region) | ||
assert client is sentinel.RETURN_CLIENT | ||
regions.append(region) | ||
|
||
assert set(regions) == set(test_regions) |
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
Oops, something went wrong.