| 1 |
# (c) 2024 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 |
from unittest.mock import MagicMock |
| 7 |
from unittest.mock import patch |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from ansible_collections.amazon.aws.plugins.module_utils.rds import AnsibleRDSError |
| 12 |
from ansible_collections.amazon.aws.plugins.modules import rds_instance |
| 13 |
from ansible_collections.amazon.aws.plugins.modules.rds_instance import get_instance |
| 14 |
|
| 15 |
mod_name = "ansible_collections.amazon.aws.plugins.modules.rds_instance" |
| 16 |
|
| 17 |
|
| 18 |
@pytest.mark.parametrize( |
| 19 |
"instances, expected", |
| 20 |
[ |
| 21 |
([], {}), |
| 22 |
( |
| 23 |
[ |
| 24 |
{ |
| 25 |
"DBInstanceIdentifier": "my-instance", |
| 26 |
"DBInstanceArn": "arn:aws:rds:us-east-1:123456789012:og:my-instance", |
| 27 |
"TagList": [], |
| 28 |
} |
| 29 |
], |
| 30 |
{ |
| 31 |
"DBInstanceIdentifier": "my-instance", |
| 32 |
"DBInstanceArn": "arn:aws:rds:us-east-1:123456789012:og:my-instance", |
| 33 |
"Tags": {}, |
| 34 |
}, |
| 35 |
), |
| 36 |
( |
| 37 |
[ |
| 38 |
{ |
| 39 |
"DBInstanceIdentifier": "my-instance", |
| 40 |
"DBInstanceArn": "arn:aws:rds:us-east-1:123456789012:og:my-instance", |
| 41 |
"TagList": [{"Key": "My Tag", "Value": "My Value"}], |
| 42 |
"ProcessorFeatures": [{"Name": "coreCount", "Value": "1"}], |
| 43 |
"PendingModifiedValues": { |
| 44 |
"ProcessorFeatures": [{"Name": "coreCount", "Value": "2"}], |
| 45 |
}, |
| 46 |
} |
| 47 |
], |
| 48 |
{ |
| 49 |
"DBInstanceIdentifier": "my-instance", |
| 50 |
"DBInstanceArn": "arn:aws:rds:us-east-1:123456789012:og:my-instance", |
| 51 |
"Tags": {"My Tag": "My Value"}, |
| 52 |
"ProcessorFeatures": {"coreCount": "1"}, |
| 53 |
"PendingModifiedValues": {"ProcessorFeatures": {"coreCount": "2"}}, |
| 54 |
}, |
| 55 |
), |
| 56 |
( |
| 57 |
[ |
| 58 |
{ |
| 59 |
"DBInstanceIdentifier": "my-instance", |
| 60 |
"DBInstanceArn": "arn:aws:rds:us-east-1:123456789012:og:my-instance", |
| 61 |
"TagList": [{"Key": "My Tag", "Value": "My Value"}], |
| 62 |
"Engine": "oracle-ee-cdb", |
| 63 |
"MultiTenant": "True", |
| 64 |
} |
| 65 |
], |
| 66 |
{ |
| 67 |
"DBInstanceIdentifier": "my-instance", |
| 68 |
"DBInstanceArn": "arn:aws:rds:us-east-1:123456789012:og:my-instance", |
| 69 |
"Tags": {"My Tag": "My Value"}, |
| 70 |
"Engine": "oracle-ee-cdb", |
| 71 |
"MultiTenant": "True", |
| 72 |
}, |
| 73 |
), |
| 74 |
], |
| 75 |
) |
| 76 |
@patch(mod_name + ".describe_db_instances") |
| 77 |
def test_get_instance_success(m_describe_db_instances, instances, expected): |
| 78 |
client = MagicMock() |
| 79 |
module = MagicMock() |
| 80 |
m_describe_db_instances.return_value = instances |
| 81 |
assert get_instance(client, module, "my-instance") == expected |
| 82 |
|
| 83 |
|
| 84 |
@patch(mod_name + ".describe_db_instances") |
| 85 |
def test_get_instance_failure(m_describe_db_instances): |
| 86 |
client = MagicMock() |
| 87 |
module = MagicMock() |
| 88 |
e = AnsibleRDSError() |
| 89 |
m_describe_db_instances.side_effect = e |
| 90 |
get_instance(client, module, "my-instance") |
| 91 |
module.fail_json_aws.assert_called_once_with(e, msg="Failed to get DB instance my-instance") |
| 92 |
|
| 93 |
|
| 94 |
@patch(mod_name + ".describe_db_instances") |
| 95 |
@patch(mod_name + ".get_changing_options_with_inconsistent_keys") |
| 96 |
def test_get_options_with_changing_values(m_get_changing_options_with_inconsistent_keys, m_describe_db_instances): |
| 97 |
module = MagicMock() |
| 98 |
client = MagicMock() |
| 99 |
instance = {} |
| 100 |
|
| 101 |
m_describe_db_instances.return_value = [ |
| 102 |
{ |
| 103 |
"DBInstanceIdentifier": "my-instance", |
| 104 |
"DBInstanceArn": "arn:aws:rds:us-east-1:123456789012:og:my-instance", |
| 105 |
"TagList": [{"Key": "My Tag", "Value": "My Value"}], |
| 106 |
"Engine": "oracle-ee-cdb", |
| 107 |
"MultiTenant": True, |
| 108 |
"Endpoint": {"Port": "3000"}, |
| 109 |
"DBSubnetGroup": { |
| 110 |
"DBSubnetGroupDescription": "default", |
| 111 |
"DBSubnetGroupName": "default", |
| 112 |
}, |
| 113 |
} |
| 114 |
] |
| 115 |
|
| 116 |
module.params = { |
| 117 |
"db_instance_identifier": "my-instance", |
| 118 |
"purge_cloudwatch_logs_exports": None, |
| 119 |
"force_update_password": None, |
| 120 |
"port": None, |
| 121 |
"enable_cloudwatch_logs_exports": None, |
| 122 |
"storage_type": None, |
| 123 |
"purge_security_groups": None, |
| 124 |
"ca_certificate_identifier": None, |
| 125 |
"db_instance_arn": "arn:aws:rds:us-east-1:123456789012:og:my-instance", |
| 126 |
"engine": "oracle-ee-cdb", |
| 127 |
"multi_tenant": False, |
| 128 |
"db_subnet_group": { |
| 129 |
"db_subnet_group_description": "default", |
| 130 |
"db_subnet_group_name": "default", |
| 131 |
"subnet_group_status": "Complete", |
| 132 |
}, |
| 133 |
} |
| 134 |
m_get_changing_options_with_inconsistent_keys.return_value = {} |
| 135 |
rds_instance.get_options_with_changing_values(client, module, instance) |
| 136 |
assert module.require_botocore_at_least.call_count == 0 |
| 137 |
module.fail_json.assert_called_with( |
| 138 |
msg="A DB which is configured to be a multi tenant cannot be modified to use single tenant configuration." |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
def test_validate_options(): |
| 143 |
module = MagicMock() |
| 144 |
client = MagicMock() |
| 145 |
instance = "" |
| 146 |
|
| 147 |
module.params = { |
| 148 |
"skip_final_snapshot": None, |
| 149 |
"final_db_snapshot_identifier": None, |
| 150 |
"new_db_instance_identifier": None, |
| 151 |
"tde_credential_password": False, |
| 152 |
"tde_credential_arn": False, |
| 153 |
"read_replica": None, |
| 154 |
"creation_source": None, |
| 155 |
"source_db_instance_identifier": None, |
| 156 |
"engine": "Aurora", |
| 157 |
"multi_tenant": True, |
| 158 |
"state": "present", |
| 159 |
} |
| 160 |
rds_instance.validate_options(client, module, instance) |
| 161 |
assert module.require_botocore_at_least.call_count == 0 |
| 162 |
module.fail_json.assert_called_with( |
| 163 |
msg="Multi Tenant parameter only applies to RDS for Oracle container database (CDB) engines and not to Aurora." |
| 164 |
) |