Skip to content

Commit

Permalink
add unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jatorcasso committed Mar 21, 2022
1 parent 75c016d commit 0e34d25
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 10 deletions.
17 changes: 9 additions & 8 deletions plugins/module_utils/rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
pass

from ansible.module_utils._text import to_text
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict

from .ec2 import AWSRetry
Expand Down Expand Up @@ -318,7 +319,7 @@ def compare_iam_roles(existing_roles, target_roles, purge_roles):
for target_role in target_roles:
found = False
for existing_role in existing_roles:
if target_role['role_arn'] == existing_role['RoleArn'] and target_role['feature_name'] == existing_role['FeatureName']:
if target_role['role_arn'] == existing_role['role_arn'] and target_role['feature_name'] == existing_role['feature_name']:
found = True
break
if not found:
Expand All @@ -328,7 +329,7 @@ def compare_iam_roles(existing_roles, target_roles, purge_roles):
for existing_role in existing_roles:
found = False
for target_role in target_roles:
if target_role['role_arn'] == existing_role['RoleArn'] and target_role['feature_name'] == existing_role['FeatureName']:
if target_role['role_arn'] == existing_role['role_arn'] and target_role['feature_name'] == existing_role['feature_name']:
found = True
break
if not found:
Expand All @@ -337,15 +338,15 @@ def compare_iam_roles(existing_roles, target_roles, purge_roles):
return roles_to_add, roles_to_remove


def ensure_iam_roles(client, module, instance, instance_id, iam_roles, purge_iam_roles):
if iam_roles is None:
iam_roles = []
roles_to_add, roles_to_remove = compare_iam_roles(instance['AssociatedRoles'], iam_roles, purge_iam_roles)
def ensure_iam_roles(client, module, instance_id, existing_roles, target_roles, purge_iam_roles):
if target_roles is None:
target_roles = []
roles_to_add, roles_to_remove = compare_iam_roles(existing_roles, target_roles, purge_iam_roles)
changed = bool(roles_to_add or roles_to_remove)
for role in roles_to_remove:
params = {'DBInstanceIdentifier': instance_id,
'RoleArn': role['RoleArn'],
'FeatureName': role['FeatureName']}
'RoleArn': role['role_arn'],
'FeatureName': role['feature_name']}
result, changed = call_method(client, module, method_name='remove_role_from_db_instance', parameters=params)
for role in roles_to_add:
params = {'DBInstanceIdentifier': instance_id,
Expand Down
82 changes: 80 additions & 2 deletions tests/unit/module_utils/test_rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

__metaclass__ = type

from ansible_collections.amazon.aws.tests.unit.compat.mock import MagicMock
from ansible_collections.amazon.aws.plugins.module_utils import rds

from ansible_collections.amazon.aws.tests.unit.compat import unittest
from ansible_collections.amazon.aws.tests.unit.compat.mock import MagicMock

from contextlib import nullcontext
import pytest
Expand Down Expand Up @@ -308,3 +308,81 @@ def test__handle_errors_failed(method_name, exception, expected, error):
rds.handle_errors(module, exception, method_name, {"Engine": "fake_engine"})
module.fail_json_aws.assert_called_once
module.fail_json_aws.call_args[1]["msg"] == expected


class RdsUtils(unittest.TestCase):

# ========================================================
# Setup some initial data that we can use within our tests
# ========================================================
def setUp(self):
self.target_role_list = [
{
'role_arn': 'role_won',
'feature_name': 's3Export'
},
{
'role_arn': 'role_too',
'feature_name': 'Lambda'
},
{
'role_arn': 'role_thrie',
'feature_name': 's3Import'
}
]

# ========================================================
# rds.compare_iam_roles
# ========================================================

def test_compare_iam_roles_equal(self):
existing_list = self.target_role_list
roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=False)
self.assertEqual([], roles_to_add)
self.assertEqual([], roles_to_delete)
roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=True)
self.assertEqual([], roles_to_add)
self.assertEqual([], roles_to_delete)

def test_compare_iam_roles_empty_arr_existing(self):
roles_to_add, roles_to_delete = rds.compare_iam_roles([], self.target_role_list, purge_roles=False)
self.assertEqual(self.target_role_list, roles_to_add)
self.assertEqual([], roles_to_delete)
roles_to_add, roles_to_delete = rds.compare_iam_roles([], self.target_role_list, purge_roles=True)
self.assertEqual(self.target_role_list, roles_to_add)
self.assertEqual([], roles_to_delete)

def test_compare_iam_roles_empty_arr_target(self):
existing_list = self.target_role_list
roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, [], purge_roles=False)
self.assertEqual([], roles_to_add)
self.assertEqual([], roles_to_delete)
roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, [], purge_roles=True)
self.assertEqual([], roles_to_add)
self.assertEqual(self.target_role_list, roles_to_delete)

def test_compare_iam_roles_different(self):
existing_list = [
{
'role_arn': 'role_wonn',
'feature_name': 's3Export'
}]
roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=False)
self.assertEqual(self.target_role_list, roles_to_add)
self.assertEqual([], roles_to_delete)
roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=True)
self.assertEqual(self.target_role_list, roles_to_add)
self.assertEqual(existing_list, roles_to_delete)

existing_list = self.target_role_list.copy()
self.target_role_list = [
{
'role_arn': 'role_wonn',
'feature_name': 's3Export'
}]
roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=False)
self.assertEqual(self.target_role_list, roles_to_add)
self.assertEqual([], roles_to_delete)
roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=True)
self.assertEqual(self.target_role_list, roles_to_add)
self.assertEqual(existing_list, roles_to_delete)

0 comments on commit 0e34d25

Please sign in to comment.