| 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 |
import copy |
| 7 |
from unittest.mock import MagicMock |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from ansible_collections.amazon.aws.plugins.modules import rds_cluster |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture(name="ansible_module") |
| 15 |
def fixture_ansible_module(): |
| 16 |
module = MagicMock() |
| 17 |
module.check_mode = False |
| 18 |
module.params = {} |
| 19 |
module.fail_json.side_effect = SystemExit(1) |
| 20 |
module.fail_json_aws.side_effect = SystemExit(1) |
| 21 |
|
| 22 |
return module |
| 23 |
|
| 24 |
|
| 25 |
class TestChangingClusterOptions: |
| 26 |
def test_module_state(self, ansible_module): |
| 27 |
apply_immediately = MagicMock() |
| 28 |
db_cluster_id = MagicMock() |
| 29 |
master_user_passwd = MagicMock() |
| 30 |
|
| 31 |
modify_params = { |
| 32 |
"ApplyImmediately": apply_immediately, |
| 33 |
"DBClusterIdentifier": db_cluster_id, |
| 34 |
"MasterUserPassword": master_user_passwd, |
| 35 |
} |
| 36 |
current_cluster = { |
| 37 |
"DBClusterIdentifier": db_cluster_id, |
| 38 |
} |
| 39 |
|
| 40 |
# 'present' or 'absent' |
| 41 |
rds_cluster.module = ansible_module |
| 42 |
for state in ("present", "absent"): |
| 43 |
rds_cluster.module.params.update({"state": state}) |
| 44 |
assert modify_params == rds_cluster.changing_cluster_options(copy.deepcopy(modify_params), current_cluster) |
| 45 |
|
| 46 |
# 'stopped'/'started' |
| 47 |
for state in ("started", "stopped"): |
| 48 |
rds_cluster.module.params.update({"state": state}) |
| 49 |
# 'mysql' or 'postgres' engine |
| 50 |
for engine in ("mysql", "postgres"): |
| 51 |
current_cluster.update({"Engine": engine}) |
| 52 |
with pytest.raises(SystemExit): |
| 53 |
rds_cluster.changing_cluster_options(copy.deepcopy(modify_params), current_cluster) |
| 54 |
rds_cluster.module.fail_json.assert_called_once_with(f"Only aurora clusters can use the state {state}") |
| 55 |
rds_cluster.module.fail_json.reset_mock() |
| 56 |
# 'aurora' engine |
| 57 |
current_cluster.update({"Engine": "aurora"}) |
| 58 |
assert {"DBClusterIdentifier": db_cluster_id} == rds_cluster.changing_cluster_options( |
| 59 |
copy.deepcopy(modify_params), current_cluster |
| 60 |
) |
| 61 |
|
| 62 |
@pytest.mark.parametrize( |
| 63 |
"params,expected", |
| 64 |
[ |
| 65 |
({"StorageType": "10B"}, {"StorageType": "10B"}), |
| 66 |
({"StorageType": "10B", "DeletionProtection": True}, {"StorageType": "10B", "DeletionProtection": True}), |
| 67 |
({"DeletionProtection": True}, {"DeletionProtection": True}), |
| 68 |
({"DeletionProtection": False}, {}), |
| 69 |
({"Domain": "domain-2"}, {"Domain": "domain-2"}), |
| 70 |
({"Domain": "domain-1", "DomainIAMRoleName": "ansible-test-units-001"}, {}), |
| 71 |
( |
| 72 |
{"Domain": "domain-2", "DomainIAMRoleName": "ansible-test-units-001"}, |
| 73 |
{"Domain": "domain-2", "DomainIAMRoleName": "ansible-test-units-001"}, |
| 74 |
), |
| 75 |
( |
| 76 |
{"Domain": "domain-2", "DomainIAMRoleName": "ansible-test-units-002"}, |
| 77 |
{"Domain": "domain-2", "DomainIAMRoleName": "ansible-test-units-002"}, |
| 78 |
), |
| 79 |
], |
| 80 |
) |
| 81 |
def test_modify_params(self, ansible_module, params, expected): |
| 82 |
apply_immediately = MagicMock() |
| 83 |
db_cluster_id = MagicMock() |
| 84 |
|
| 85 |
modify_params = { |
| 86 |
"ApplyImmediately": apply_immediately, |
| 87 |
"DBClusterIdentifier": db_cluster_id, |
| 88 |
} |
| 89 |
if expected: |
| 90 |
expected.update(modify_params) |
| 91 |
modify_params.update(params) |
| 92 |
|
| 93 |
current_cluster = { |
| 94 |
"DBClusterIdentifier": db_cluster_id, |
| 95 |
"DeletionProtection": False, |
| 96 |
"DomainMemberships": [ |
| 97 |
{ |
| 98 |
"Domain": "domain-1", |
| 99 |
"Status": "joined", |
| 100 |
"FQDN": "testing.ansible.units", |
| 101 |
"IAMRoleName": "ansible-test-units-001", |
| 102 |
}, |
| 103 |
], |
| 104 |
} |
| 105 |
|
| 106 |
# state is set to 'present' |
| 107 |
ansible_module.params.update({"state": "present"}) |
| 108 |
rds_cluster.module = ansible_module |
| 109 |
assert expected == rds_cluster.changing_cluster_options(modify_params, current_cluster) |
| 110 |
|
| 111 |
@pytest.mark.parametrize( |
| 112 |
"modify_vpc_security_group_ids, current_vpc_security_group_ids, expected_vpc_security_group_ids", |
| 113 |
[ |
| 114 |
(["id-1", "id-1", "id-2"], ["id-3"], ["id-1", "id-2"]), |
| 115 |
(["id-1", "id-2"], ["id-1", "id-2"], ["id-1", "id-2"]), |
| 116 |
], |
| 117 |
) |
| 118 |
def test_vpc_security_group_ids_purge_true( |
| 119 |
self, |
| 120 |
ansible_module, |
| 121 |
modify_vpc_security_group_ids, |
| 122 |
current_vpc_security_group_ids, |
| 123 |
expected_vpc_security_group_ids, |
| 124 |
): |
| 125 |
db_cluster_id = MagicMock() |
| 126 |
apply_immediately = MagicMock() |
| 127 |
modify_params = { |
| 128 |
"DBClusterIdentifier": db_cluster_id, |
| 129 |
"ApplyImmediately": apply_immediately, |
| 130 |
} |
| 131 |
expected = {} |
| 132 |
if expected_vpc_security_group_ids: |
| 133 |
expected.update(modify_params) |
| 134 |
expected["VpcSecurityGroupIds"] = expected_vpc_security_group_ids |
| 135 |
modify_params.update({"VpcSecurityGroupIds": modify_vpc_security_group_ids}) |
| 136 |
|
| 137 |
current_cluster = {"VpcSecurityGroups": [{"VpcSecurityGroupId": x} for x in current_vpc_security_group_ids]} |
| 138 |
|
| 139 |
# state is set to 'present' |
| 140 |
ansible_module.params.update({"state": "present", "purge_security_groups": True}) |
| 141 |
rds_cluster.module = ansible_module |
| 142 |
assert expected == rds_cluster.changing_cluster_options(modify_params, current_cluster) |
| 143 |
|
| 144 |
@pytest.mark.parametrize( |
| 145 |
"modify_vpc_security_group_ids, current_vpc_security_group_ids, expected_vpc_security_group_ids", |
| 146 |
[ |
| 147 |
(["id-1", "id-1", "id-2"], ["id-3"], ["id-1", "id-2", "id-3"]), |
| 148 |
(["id-1", "id-2"], ["id-2", "id-1"], []), |
| 149 |
(["id-1", "id-2"], ["id-2", "id-1", "id-3"], []), |
| 150 |
(["id-1", "id-1", "id-2"], [], ["id-1", "id-2"]), |
| 151 |
], |
| 152 |
) |
| 153 |
def test_vpc_security_group_ids_purge_false( |
| 154 |
self, |
| 155 |
ansible_module, |
| 156 |
modify_vpc_security_group_ids, |
| 157 |
current_vpc_security_group_ids, |
| 158 |
expected_vpc_security_group_ids, |
| 159 |
): |
| 160 |
db_cluster_id = MagicMock() |
| 161 |
apply_immediately = MagicMock() |
| 162 |
modify_params = { |
| 163 |
"DBClusterIdentifier": db_cluster_id, |
| 164 |
"ApplyImmediately": apply_immediately, |
| 165 |
} |
| 166 |
expected = {} |
| 167 |
if expected_vpc_security_group_ids: |
| 168 |
expected.update(modify_params) |
| 169 |
expected["VpcSecurityGroupIds"] = expected_vpc_security_group_ids |
| 170 |
modify_params.update({"VpcSecurityGroupIds": modify_vpc_security_group_ids}) |
| 171 |
|
| 172 |
current_cluster = {"VpcSecurityGroups": [{"VpcSecurityGroupId": x} for x in current_vpc_security_group_ids]} |
| 173 |
|
| 174 |
# state is set to 'present' |
| 175 |
ansible_module.params.update({"state": "present", "purge_security_groups": False}) |
| 176 |
rds_cluster.module = ansible_module |
| 177 |
assert expected == rds_cluster.changing_cluster_options(modify_params, current_cluster) |
| 178 |
|
| 179 |
@pytest.mark.parametrize( |
| 180 |
"purge_cloudwatch_logs_exports, modify_enable_cloudwatch_logs_exports, current_enabled_cloudwatch_logs_exports, enable_log_types, disable_log_types", |
| 181 |
[ |
| 182 |
(True, ["log-3", "log-2"], ["log-1"], ["log-2", "log-3"], ["log-1"]), |
| 183 |
(False, ["log-3", "log-2"], ["log-1"], ["log-2", "log-3"], []), |
| 184 |
(True, ["log-1", "log-2"], ["log-1", "log-2", "log-3"], [], ["log-3"]), |
| 185 |
(False, ["log-1", "log-2"], ["log-1", "log-2", "log-3"], [], []), |
| 186 |
], |
| 187 |
) |
| 188 |
def test_enable_cloudwatch_logs_export( |
| 189 |
self, |
| 190 |
ansible_module, |
| 191 |
purge_cloudwatch_logs_exports, |
| 192 |
modify_enable_cloudwatch_logs_exports, |
| 193 |
current_enabled_cloudwatch_logs_exports, |
| 194 |
enable_log_types, |
| 195 |
disable_log_types, |
| 196 |
): |
| 197 |
db_cluster_id = MagicMock() |
| 198 |
apply_immediately = MagicMock() |
| 199 |
modify_params = { |
| 200 |
"DBClusterIdentifier": db_cluster_id, |
| 201 |
"ApplyImmediately": apply_immediately, |
| 202 |
"EnableCloudwatchLogsExports": modify_enable_cloudwatch_logs_exports, |
| 203 |
} |
| 204 |
expected = None |
| 205 |
if enable_log_types or disable_log_types: |
| 206 |
expected = {"EnableLogTypes": enable_log_types, "DisableLogTypes": disable_log_types} |
| 207 |
|
| 208 |
current_cluster = {"EnabledCloudwatchLogsExports": current_enabled_cloudwatch_logs_exports} |
| 209 |
|
| 210 |
# state is set to 'present' |
| 211 |
ansible_module.params.update( |
| 212 |
{"state": "present", "purge_cloudwatch_logs_exports": purge_cloudwatch_logs_exports} |
| 213 |
) |
| 214 |
rds_cluster.module = ansible_module |
| 215 |
assert expected == rds_cluster.changing_cluster_options(modify_params, current_cluster).get( |
| 216 |
"CloudwatchLogsExportConfiguration" |
| 217 |
) |
| 218 |
|
| 219 |
@pytest.mark.parametrize( |
| 220 |
"new_cluster_id, current_cluster_id, expected_change", |
| 221 |
[ |
| 222 |
("", "cluster-id-1", False), |
| 223 |
(None, "cluster-id-1", False), |
| 224 |
("cluster-id-1", "cluster-id-1", False), |
| 225 |
("cluster-id-2", "cluster-id-1", True), |
| 226 |
], |
| 227 |
) |
| 228 |
def test_modify_cluster_id(self, ansible_module, new_cluster_id, current_cluster_id, expected_change): |
| 229 |
db_cluster_id = MagicMock() |
| 230 |
apply_immediately = MagicMock() |
| 231 |
modify_params = { |
| 232 |
"DBClusterIdentifier": db_cluster_id, |
| 233 |
"NewDBClusterIdentifier": new_cluster_id, |
| 234 |
"ApplyImmediately": apply_immediately, |
| 235 |
} |
| 236 |
expected = {} |
| 237 |
if expected_change: |
| 238 |
expected.update(modify_params) |
| 239 |
|
| 240 |
current_cluster = {"DBClusterIdentifier": current_cluster_id} |
| 241 |
|
| 242 |
# state is set to 'present' |
| 243 |
ansible_module.params.update({"state": "present"}) |
| 244 |
rds_cluster.module = ansible_module |
| 245 |
assert expected == rds_cluster.changing_cluster_options(modify_params, current_cluster) |
| 246 |
|
| 247 |
@pytest.mark.parametrize( |
| 248 |
"new_option_group_name, cluster_option_group_memberships, expected_change", |
| 249 |
[ |
| 250 |
("", [], False), |
| 251 |
(None, [], False), |
| 252 |
("opt-3", ["opt-1", "opt-2", "opt-3"], False), |
| 253 |
("opt-3", ["opt-1", "opt-2"], True), |
| 254 |
], |
| 255 |
) |
| 256 |
def test_option_group_name( |
| 257 |
self, ansible_module, new_option_group_name, cluster_option_group_memberships, expected_change |
| 258 |
): |
| 259 |
db_cluster_id = MagicMock() |
| 260 |
apply_immediately = MagicMock() |
| 261 |
modify_params = { |
| 262 |
"DBClusterIdentifier": db_cluster_id, |
| 263 |
"OptionGroupName": new_option_group_name, |
| 264 |
"ApplyImmediately": apply_immediately, |
| 265 |
} |
| 266 |
expected = {} |
| 267 |
if expected_change: |
| 268 |
expected.update(modify_params) |
| 269 |
|
| 270 |
current_cluster = { |
| 271 |
"DBClusterOptionGroupMemberships": [ |
| 272 |
{"DBClusterOptionGroupName": x} for x in cluster_option_group_memberships |
| 273 |
] |
| 274 |
} |
| 275 |
|
| 276 |
# state is set to 'present' |
| 277 |
ansible_module.params.update({"state": "present"}) |
| 278 |
rds_cluster.module = ansible_module |
| 279 |
assert expected == rds_cluster.changing_cluster_options(modify_params, current_cluster) |
| 280 |
|
| 281 |
@pytest.mark.parametrize( |
| 282 |
"new_db_cluster_parameter_group_name, current_db_cluster_parameter_group_name, expected_change", |
| 283 |
[ |
| 284 |
("", "param-group-0", False), |
| 285 |
(None, "param-group-0", False), |
| 286 |
("param-group-0", "param-group-0", False), |
| 287 |
("param-group-1", "param-group-0", True), |
| 288 |
], |
| 289 |
) |
| 290 |
def test_db_cluster_parameter_group_name( |
| 291 |
self, |
| 292 |
ansible_module, |
| 293 |
new_db_cluster_parameter_group_name, |
| 294 |
current_db_cluster_parameter_group_name, |
| 295 |
expected_change, |
| 296 |
): |
| 297 |
db_cluster_id = MagicMock() |
| 298 |
apply_immediately = MagicMock() |
| 299 |
modify_params = { |
| 300 |
"DBClusterIdentifier": db_cluster_id, |
| 301 |
"DBClusterParameterGroupName": new_db_cluster_parameter_group_name, |
| 302 |
"ApplyImmediately": apply_immediately, |
| 303 |
} |
| 304 |
expected = {} |
| 305 |
if expected_change: |
| 306 |
expected.update(modify_params) |
| 307 |
|
| 308 |
current_cluster = {"DBClusterParameterGroup": current_db_cluster_parameter_group_name} |
| 309 |
|
| 310 |
# state is set to 'present' |
| 311 |
ansible_module.params.update({"state": "present"}) |
| 312 |
rds_cluster.module = ansible_module |
| 313 |
assert expected == rds_cluster.changing_cluster_options(modify_params, current_cluster) |