Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Nov 16, 2022
1 parent 91e5039 commit a38d6aa
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 175 deletions.
17 changes: 17 additions & 0 deletions tests/unit/plugin_utils/base/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ def test_client_wrapper(monkeypatch):
region=sentinel.CONN_REGION,
endpoint=sentinel.CONN_URL,)

# Check that we can override parameters
wrapped_conn = base_plugin.client(sentinel.PARAM_SERVICE, sentinel.PARAM_WRAPPER, region=sentinel.PARAM_REGION)
assert wrapped_conn.client is sentinel.BOTO3_CONN
assert wrapped_conn.retry is sentinel.PARAM_WRAPPER
assert get_aws_connection_info.call_args == call(base_plugin)
assert boto3_conn.call_args == call(base_plugin, conn_type='client',
resource=sentinel.PARAM_SERVICE,
region=sentinel.PARAM_REGION,
endpoint=sentinel.CONN_URL,)


def test_resource(monkeypatch):
get_aws_connection_info = MagicMock(name='get_aws_connection_info')
Expand All @@ -137,3 +147,10 @@ def test_resource(monkeypatch):
resource=sentinel.PARAM_SERVICE,
region=sentinel.CONN_REGION,
endpoint=sentinel.CONN_URL,)

assert base_plugin.resource(sentinel.PARAM_SERVICE, region=sentinel.PARAM_REGION) is sentinel.BOTO3_CONN
assert get_aws_connection_info.call_args == call(base_plugin)
assert boto3_conn.call_args == call(base_plugin, conn_type='resource',
resource=sentinel.PARAM_SERVICE,
region=sentinel.PARAM_REGION,
endpoint=sentinel.CONN_URL,)
143 changes: 0 additions & 143 deletions tests/unit/plugin_utils/inventory/test_inventory.py

This file was deleted.

31 changes: 31 additions & 0 deletions tests/unit/plugin_utils/inventory/test_inventory_base.py
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 tests/unit/plugin_utils/inventory/test_inventory_clients.py
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)
10 changes: 5 additions & 5 deletions tests/unit/plugins/inventory/test_aws_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ def test_inventory_get_instances_by_region(m_describe_ec2_instances, inventory,
(MagicMock(), "us-east-1"), (MagicMock(), "us-east-2")
]

inventory._boto3_conn = MagicMock()
inventory._boto3_conn.return_value = boto3_conn
inventory.all_clients = MagicMock()
inventory.all_clients.return_value = boto3_conn

m_describe_ec2_instances.side_effect = [
{
Expand Down Expand Up @@ -526,7 +526,7 @@ def test_inventory_get_instances_by_region(m_describe_ec2_instances, inventory,
regions = ["us-east-2", "us-east-4"]

assert inventory._get_instances_by_region(regions, filters, False) == expected
inventory._boto3_conn.assert_called_with(regions, "ec2")
inventory.all_clients.assert_called_with("ec2")

if any((f['Name'] == 'instance-state-name' for f in filters)):
filters.append(default_filter)
Expand Down Expand Up @@ -564,8 +564,8 @@ def test_inventory_get_instances_by_region(m_describe_ec2_instances, inventory,
@patch("ansible_collections.amazon.aws.plugins.inventory.aws_ec2._describe_ec2_instances")
def test_inventory_get_instances_by_region_failures(m_describe_ec2_instances, inventory, strict, error):

inventory._boto3_conn = MagicMock()
inventory._boto3_conn.return_value = [(MagicMock(), "us-west-2")]
inventory.all_clients = MagicMock()
inventory.all_clients.return_value = [(MagicMock(), "us-west-2")]
inventory.fail_aws = MagicMock()
inventory.fail_aws.side_effect = SystemExit(1)

Expand Down
Loading

0 comments on commit a38d6aa

Please sign in to comment.