Debian Codegraph @

ansible_collections/amazon/aws/tests/unit/plugins/module_utils/test_rds.py

raw
1 # (c) 2021 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 contextlib import nullcontext
7 from unittest.mock import MagicMock
8 from unittest.mock import patch
9
10 import pytest
11
12 try:
13 import botocore
14 except ImportError:
15 # Handled by HAS_BOTO3
16 pass
17
18 from ansible_collections.amazon.aws.plugins.module_utils import rds
19 from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3
20
21 if not HAS_BOTO3:
22 pytestmark = pytest.mark.skip("test_rds.py requires the python modules 'boto3' and 'botocore'")
23
24 mod_name = "ansible_collections.amazon.aws.plugins.module_utils.rds"
25
26
27 def helper_expected(x):
28 return x, nullcontext()
29
30
31 def helper_error(*args, **kwargs):
32 return MagicMock(), pytest.raises(*args, **kwargs)
33
34
35 def build_exception(operation_name, code=None, message=None, http_status_code=None, error=True):
36 # Support skipping the test is botocore isn't installed
37 # (called by parametrize before skip is evaluated)
38 if not HAS_BOTO3:
39 return Exception("MissingBotoCore")
40 response = {}
41 if error or code or message:
42 response["Error"] = {}
43 if code:
44 response["Error"]["Code"] = code
45 if message:
46 response["Error"]["Message"] = message
47 if http_status_code:
48 response["ResponseMetadata"] = {"HTTPStatusCode": http_status_code}
49
50 return botocore.exceptions.ClientError(response, operation_name)
51
52
53 @pytest.mark.parametrize("waiter_name", ["", "db_snapshot_available"])
54 def test__wait_for_instance_snapshot_status(waiter_name):
55 rds.wait_for_instance_snapshot_status(MagicMock(), MagicMock(), "test", waiter_name)
56
57
58 @pytest.mark.parametrize("waiter_name", ["", "db_cluster_snapshot_available"])
59 def test__wait_for_cluster_snapshot_status(waiter_name):
60 rds.wait_for_cluster_snapshot_status(MagicMock(), MagicMock(), "test", waiter_name)
61
62
63 @pytest.mark.parametrize(
64 "waiter_name, expected",
65 [
66 (
67 "db_snapshot_available",
68 "Failed to wait for DB snapshot test to be available",
69 ),
70 ("db_snapshot_deleted", "Failed to wait for DB snapshot test to be deleted"),
71 ],
72 )
73 def test__wait_for_instance_snapshot_status_failed(waiter_name, expected):
74 spec = {"get_waiter.side_effect": [botocore.exceptions.WaiterError(None, None, None)]}
75 client = MagicMock(**spec)
76 module = MagicMock()
77
78 rds.wait_for_instance_snapshot_status(client, module, "test", waiter_name)
79 module.fail_json_aws.assert_called_once()
80 assert module.fail_json_aws.call_args[1]["msg"] == expected
81
82
83 @pytest.mark.parametrize(
84 "waiter_name, expected",
85 [
86 (
87 "db_cluster_snapshot_available",
88 "Failed to wait for DB cluster snapshot test to be available",
89 ),
90 (
91 "db_cluster_snapshot_deleted",
92 "Failed to wait for DB cluster snapshot test to be deleted",
93 ),
94 ],
95 )
96 def test__wait_for_cluster_snapshot_status_failed(waiter_name, expected):
97 spec = {"get_waiter.side_effect": [botocore.exceptions.WaiterError(None, None, None)]}
98 client = MagicMock(**spec)
99 module = MagicMock()
100
101 rds.wait_for_cluster_snapshot_status(client, module, "test", waiter_name)
102 module.fail_json_aws.assert_called_once()
103 assert module.fail_json_aws.call_args[1]["msg"] == expected
104
105
106 @pytest.mark.parametrize(
107 "method_name, params, expected, error",
108 [
109 (
110 "delete_db_cluster",
111 {
112 "new_db_cluster_identifier": "test",
113 },
114 *helper_expected(
115 rds.Boto3ClientMethod(
116 name="delete_db_cluster",
117 waiter="cluster_deleted",
118 operation_description="delete DB cluster",
119 resource="cluster",
120 retry_codes=["InvalidDBClusterState"],
121 )
122 ),
123 ),
124 (
125 "create_db_cluster",
126 {
127 "new_db_cluster_identifier": "test",
128 },
129 *helper_expected(
130 rds.Boto3ClientMethod(
131 name="create_db_cluster",
132 waiter="cluster_available",
133 operation_description="create DB cluster",
134 resource="cluster",
135 retry_codes=["InvalidDBClusterState"],
136 )
137 ),
138 ),
139 (
140 "start_db_cluster",
141 {
142 "new_db_cluster_identifier": "test",
143 },
144 *helper_expected(
145 rds.Boto3ClientMethod(
146 name="start_db_cluster",
147 waiter="cluster_available",
148 operation_description="start DB cluster",
149 resource="cluster",
150 retry_codes=["InvalidDBClusterState"],
151 )
152 ),
153 ),
154 (
155 "stop_db_cluster",
156 {
157 "new_db_cluster_identifier": "test",
158 },
159 *helper_expected(
160 rds.Boto3ClientMethod(
161 name="stop_db_cluster",
162 waiter="cluster_available",
163 operation_description="stop DB cluster",
164 resource="cluster",
165 retry_codes=["InvalidDBClusterState"],
166 )
167 ),
168 ),
169 (
170 "restore_db_cluster_from_snapshot",
171 {
172 "new_db_cluster_identifier": "test",
173 },
174 *helper_expected(
175 rds.Boto3ClientMethod(
176 name="restore_db_cluster_from_snapshot",
177 waiter="cluster_available",
178 operation_description="restore DB cluster from snapshot",
179 resource="cluster",
180 retry_codes=["InvalidDBClusterSnapshotState"],
181 )
182 ),
183 ),
184 (
185 "modify_db_cluster",
186 {
187 "new_db_cluster_identifier": "test",
188 },
189 *helper_expected(
190 rds.Boto3ClientMethod(
191 name="modify_db_cluster",
192 waiter="cluster_available",
193 operation_description="modify DB cluster",
194 resource="cluster",
195 retry_codes=["InvalidDBClusterState"],
196 )
197 ),
198 ),
199 (
200 "list_tags_for_resource",
201 {
202 "new_db_cluster_identifier": "test",
203 },
204 *helper_expected(
205 rds.Boto3ClientMethod(
206 name="list_tags_for_resource",
207 waiter="cluster_available",
208 operation_description="list tags for resource",
209 resource="cluster",
210 retry_codes=["InvalidDBClusterState"],
211 )
212 ),
213 ),
214 (
215 "fake_method",
216 {"wait": False},
217 *helper_expected(
218 rds.Boto3ClientMethod(
219 name="fake_method", waiter="", operation_description="fake method", resource="", retry_codes=[]
220 )
221 ),
222 ),
223 (
224 "fake_method",
225 {"wait": True},
226 *helper_error(
227 NotImplementedError,
228 match=(
229 "method fake_method hasn't been added to the list of accepted methods to use a waiter in"
230 " module_utils/rds.py"
231 ),
232 ),
233 ),
234 ],
235 )
236 def test__get_rds_method_attribute_cluster(method_name, params, expected, error):
237 module = MagicMock()
238 module.params = params
239 with error:
240 assert rds.get_rds_method_attribute(method_name, module) == expected
241
242
243 @pytest.mark.parametrize(
244 "method_name, params, expected, error",
245 [
246 (
247 "delete_db_instance",
248 {
249 "new_db_instance_identifier": "test",
250 },
251 *helper_expected(
252 rds.Boto3ClientMethod(
253 name="delete_db_instance",
254 waiter="db_instance_deleted",
255 operation_description="delete DB instance",
256 resource="instance",
257 retry_codes=["InvalidDBInstanceState", "InvalidDBSecurityGroupState"],
258 )
259 ),
260 ),
261 (
262 "create_db_instance",
263 {
264 "new_db_instance_identifier": "test",
265 },
266 *helper_expected(
267 rds.Boto3ClientMethod(
268 name="create_db_instance",
269 waiter="db_instance_available",
270 operation_description="create DB instance",
271 resource="instance",
272 retry_codes=["InvalidDBInstanceState", "InvalidDBSecurityGroupState"],
273 )
274 ),
275 ),
276 (
277 "stop_db_instance",
278 {
279 "new_db_instance_identifier": "test",
280 },
281 *helper_expected(
282 rds.Boto3ClientMethod(
283 name="stop_db_instance",
284 waiter="db_instance_stopped",
285 operation_description="stop DB instance",
286 resource="instance",
287 retry_codes=["InvalidDBInstanceState", "InvalidDBSecurityGroupState"],
288 )
289 ),
290 ),
291 (
292 "promote_read_replica",
293 {
294 "new_db_instance_identifier": "test",
295 },
296 *helper_expected(
297 rds.Boto3ClientMethod(
298 name="promote_read_replica",
299 waiter="read_replica_promoted",
300 operation_description="promote read replica",
301 resource="instance",
302 retry_codes=["InvalidDBInstanceState", "InvalidDBSecurityGroupState"],
303 )
304 ),
305 ),
306 (
307 "restore_db_instance_from_db_snapshot",
308 {
309 "new_db_instance_identifier": "test",
310 },
311 *helper_expected(
312 rds.Boto3ClientMethod(
313 name="restore_db_instance_from_db_snapshot",
314 waiter="db_instance_available",
315 operation_description="restore DB instance from DB snapshot",
316 resource="instance",
317 retry_codes=["InvalidDBSnapshotState"],
318 )
319 ),
320 ),
321 (
322 "modify_db_instance",
323 {
324 "new_db_instance_identifier": "test",
325 },
326 *helper_expected(
327 rds.Boto3ClientMethod(
328 name="modify_db_instance",
329 waiter="db_instance_available",
330 operation_description="modify DB instance",
331 resource="instance",
332 retry_codes=["InvalidDBInstanceState", "InvalidDBSecurityGroupState"],
333 )
334 ),
335 ),
336 (
337 "add_role_to_db_instance",
338 {
339 "new_db_instance_identifier": "test",
340 },
341 *helper_expected(
342 rds.Boto3ClientMethod(
343 name="add_role_to_db_instance",
344 waiter="role_associated",
345 operation_description="add role to DB instance",
346 resource="instance",
347 retry_codes=["InvalidDBInstanceState", "InvalidDBSecurityGroupState"],
348 )
349 ),
350 ),
351 (
352 "remove_role_from_db_instance",
353 {
354 "new_db_instance_identifier": "test",
355 },
356 *helper_expected(
357 rds.Boto3ClientMethod(
358 name="remove_role_from_db_instance",
359 waiter="role_disassociated",
360 operation_description="remove role from DB instance",
361 resource="instance",
362 retry_codes=["InvalidDBInstanceState", "InvalidDBSecurityGroupState"],
363 )
364 ),
365 ),
366 (
367 "list_tags_for_resource",
368 {
369 "new_db_instance_identifier": "test",
370 },
371 *helper_expected(
372 rds.Boto3ClientMethod(
373 name="list_tags_for_resource",
374 waiter="db_instance_available",
375 operation_description="list tags for resource",
376 resource="instance",
377 retry_codes=["InvalidDBInstanceState", "InvalidDBSecurityGroupState"],
378 )
379 ),
380 ),
381 (
382 "fake_method",
383 {"wait": False},
384 *helper_expected(
385 rds.Boto3ClientMethod(
386 name="fake_method", waiter="", operation_description="fake method", resource="", retry_codes=[]
387 )
388 ),
389 ),
390 (
391 "fake_method",
392 {"wait": True},
393 *helper_error(
394 NotImplementedError,
395 match=(
396 "method fake_method hasn't been added to the list of accepted methods to use a waiter in"
397 " module_utils/rds.py"
398 ),
399 ),
400 ),
401 ],
402 )
403 def test__get_rds_method_attribute_instance(method_name, params, expected, error):
404 module = MagicMock()
405 module.params = params
406 with error:
407 assert rds.get_rds_method_attribute(method_name, module) == expected
408
409
410 @pytest.mark.parametrize(
411 "method_name, params, expected, error",
412 [
413 (
414 "delete_db_snapshot",
415 {
416 "db_snapshot_identifier": "test",
417 },
418 *helper_expected(
419 rds.Boto3ClientMethod(
420 name="delete_db_snapshot",
421 waiter="db_snapshot_deleted",
422 operation_description="delete DB snapshot",
423 resource="instance_snapshot",
424 retry_codes=["InvalidDBSnapshotState"],
425 )
426 ),
427 ),
428 (
429 "create_db_snapshot",
430 {
431 "db_snapshot_identifier": "test",
432 },
433 *helper_expected(
434 rds.Boto3ClientMethod(
435 name="create_db_snapshot",
436 waiter="db_snapshot_available",
437 operation_description="create DB snapshot",
438 resource="instance_snapshot",
439 retry_codes=["InvalidDBInstanceState"],
440 )
441 ),
442 ),
443 (
444 "copy_db_snapshot",
445 {"source_db_snapshot_identifier": "test", "db_snapshot_identifier": "test-copy"},
446 *helper_expected(
447 rds.Boto3ClientMethod(
448 name="copy_db_snapshot",
449 waiter="db_snapshot_available",
450 operation_description="copy DB snapshot",
451 resource="instance_snapshot",
452 retry_codes=["InvalidDBSnapshotState"],
453 )
454 ),
455 ),
456 (
457 "list_tags_for_resource",
458 {
459 "db_snapshot_identifier": "test",
460 },
461 *helper_expected(
462 rds.Boto3ClientMethod(
463 name="list_tags_for_resource",
464 waiter="db_snapshot_available",
465 operation_description="list tags for resource",
466 resource="instance_snapshot",
467 retry_codes=["InvalidDBSnapshotState"],
468 )
469 ),
470 ),
471 (
472 "delete_db_cluster_snapshot",
473 {
474 "db_cluster_snapshot_identifier": "test",
475 },
476 *helper_expected(
477 rds.Boto3ClientMethod(
478 name="delete_db_cluster_snapshot",
479 waiter="db_cluster_snapshot_deleted",
480 operation_description="delete DB cluster snapshot",
481 resource="cluster_snapshot",
482 retry_codes=["InvalidDBClusterSnapshotState"],
483 )
484 ),
485 ),
486 (
487 "create_db_cluster_snapshot",
488 {
489 "db_cluster_snapshot_identifier": "test",
490 },
491 *helper_expected(
492 rds.Boto3ClientMethod(
493 name="create_db_cluster_snapshot",
494 waiter="db_cluster_snapshot_available",
495 operation_description="create DB cluster snapshot",
496 resource="cluster_snapshot",
497 retry_codes=["InvalidDBClusterState"],
498 )
499 ),
500 ),
501 (
502 "copy_db_cluster_snapshot",
503 {"source_db_cluster_snapshot_identifier": "test", "db_cluster_snapshot_identifier": "test-copy"},
504 *helper_expected(
505 rds.Boto3ClientMethod(
506 name="copy_db_cluster_snapshot",
507 waiter="db_cluster_snapshot_available",
508 operation_description="copy DB cluster snapshot",
509 resource="cluster_snapshot",
510 retry_codes=["InvalidDBClusterSnapshotState"],
511 )
512 ),
513 ),
514 (
515 "list_tags_for_resource",
516 {
517 "db_cluster_snapshot_identifier": "test",
518 },
519 *helper_expected(
520 rds.Boto3ClientMethod(
521 name="list_tags_for_resource",
522 waiter="db_cluster_snapshot_available",
523 operation_description="list tags for resource",
524 resource="cluster_snapshot",
525 retry_codes=["InvalidDBClusterSnapshotState"],
526 )
527 ),
528 ),
529 (
530 "fake_method",
531 {"wait": False},
532 *helper_expected(
533 rds.Boto3ClientMethod(
534 name="fake_method", waiter="", operation_description="fake method", resource="", retry_codes=[]
535 )
536 ),
537 ),
538 (
539 "fake_method",
540 {"wait": True},
541 *helper_error(
542 NotImplementedError,
543 match=(
544 "method fake_method hasn't been added to the list of accepted methods to use a waiter in"
545 " module_utils/rds.py"
546 ),
547 ),
548 ),
549 ],
550 )
551 def test__get_rds_method_attribute_snapshot(method_name, params, expected, error):
552 module = MagicMock()
553 module.params = params
554 with error:
555 assert rds.get_rds_method_attribute(method_name, module) == expected
556
557
558 @pytest.mark.parametrize(
559 "method_name, params, expected",
560 [
561 ("create_db_snapshot", {"db_snapshot_identifier": "test"}, "test"),
562 (
563 "create_db_snapshot",
564 {"db_snapshot_identifier": "test", "apply_immediately": True},
565 "test",
566 ),
567 (
568 "create_db_instance",
569 {
570 "db_instance_identifier": "test",
571 "new_db_instance_identifier": "test_updated",
572 },
573 "test",
574 ),
575 (
576 "create_db_snapshot",
577 {"db_snapshot_identifier": "test", "apply_immediately": True},
578 "test",
579 ),
580 (
581 "create_db_instance",
582 {
583 "db_instance_identifier": "test",
584 "new_db_instance_identifier": "test_updated",
585 "apply_immediately": True,
586 },
587 "test_updated",
588 ),
589 (
590 "create_db_cluster",
591 {
592 "db_cluster_identifier": "test",
593 "new_db_cluster_identifier": "test_updated",
594 },
595 "test",
596 ),
597 (
598 "create_db_snapshot",
599 {"db_snapshot_identifier": "test", "apply_immediately": True},
600 "test",
601 ),
602 (
603 "create_db_cluster",
604 {
605 "db_cluster_identifier": "test",
606 "new_db_cluster_identifier": "test_updated",
607 "apply_immediately": True,
608 },
609 "test_updated",
610 ),
611 ],
612 )
613 def test__get_final_identifier(method_name, params, expected):
614 module = MagicMock()
615 module.params = params
616 module.check_mode = False
617
618 assert rds.get_final_identifier(method_name, module) == expected
619
620
621 @pytest.mark.parametrize(
622 "method_name, exception, expected",
623 [
624 (
625 "modify_db_instance",
626 build_exception(
627 "modify_db_instance",
628 code="InvalidParameterCombination",
629 message="No modifications were requested",
630 ),
631 False,
632 ),
633 (
634 "promote_read_replica",
635 build_exception(
636 "promote_read_replica",
637 code="InvalidDBInstanceState",
638 message="DB Instance is not a read replica",
639 ),
640 False,
641 ),
642 (
643 "promote_read_replica_db_cluster",
644 build_exception(
645 "promote_read_replica_db_cluster",
646 code="InvalidDBClusterStateFault",
647 message="DB Cluster that is not a read replica",
648 ),
649 False,
650 ),
651 ],
652 )
653 def test__handle_errors(method_name, exception, expected):
654 assert rds.handle_errors(MagicMock(), exception, method_name, {}) == expected
655
656
657 @pytest.mark.parametrize(
658 "method_name, exception, expected, error",
659 [
660 (
661 "modify_db_instance",
662 build_exception(
663 "modify_db_instance",
664 code="InvalidParameterCombination",
665 message="ModifyDbCluster API",
666 ),
667 *helper_expected(
668 "It appears you are trying to modify attributes that are managed at the cluster level. Please see"
669 " rds_cluster"
670 ),
671 ),
672 (
673 "modify_db_instance",
674 build_exception("modify_db_instance", code="InvalidParameterCombination"),
675 *helper_error(
676 NotImplementedError,
677 match=(
678 "method modify_db_instance hasn't been added to the list of accepted methods to use a waiter in"
679 " module_utils/rds.py"
680 ),
681 ),
682 ),
683 (
684 "promote_read_replica",
685 build_exception("promote_read_replica", code="InvalidDBInstanceState"),
686 *helper_error(
687 NotImplementedError,
688 match=(
689 "method promote_read_replica hasn't been added to the list of accepted methods to use a waiter in"
690 " module_utils/rds.py"
691 ),
692 ),
693 ),
694 (
695 "promote_read_replica_db_cluster",
696 build_exception("promote_read_replica_db_cluster", code="InvalidDBClusterStateFault"),
697 *helper_error(
698 NotImplementedError,
699 match=(
700 "method promote_read_replica_db_cluster hasn't been added to the list of accepted methods to use a"
701 " waiter in module_utils/rds.py"
702 ),
703 ),
704 ),
705 (
706 "create_db_cluster",
707 build_exception("create_db_cluster", code="InvalidParameterValue"),
708 *helper_expected(
709 "DB engine fake_engine should be one of ['aurora', 'aurora-mysql', 'aurora-postgresql', 'mysql', 'postgres']"
710 ),
711 ),
712 ],
713 )
714 def test__handle_errors_failed(method_name, exception, expected, error):
715 module = MagicMock()
716
717 with error:
718 rds.handle_errors(module, exception, method_name, {"Engine": "fake_engine"})
719 module.fail_json_aws.assert_called_once()
720 assert module.fail_json_aws.call_args[1]["msg"] == expected
721
722
723 @pytest.mark.parametrize(
724 "snapshots, snapshot_type, convert_tags, expected",
725 [
726 ([], "cluster", False, {}),
727 ([], "instance", True, {}),
728 (
729 [{"DBSnapshotIdentifier": "my-snapshot", "DBInstanceIdentifier": "my-instance", "TagList": []}],
730 "instance",
731 False,
732 {"DBSnapshotIdentifier": "my-snapshot", "DBInstanceIdentifier": "my-instance", "TagList": []},
733 ),
734 (
735 [
736 {
737 "DBClusterSnapshotIdentifier": "my-cluster-snapshot",
738 "DBClusterIdentifier": "my-cluster",
739 "TagList": [],
740 }
741 ],
742 "cluster",
743 True,
744 {"DBClusterSnapshotIdentifier": "my-cluster-snapshot", "DBClusterIdentifier": "my-cluster", "Tags": {}},
745 ),
746 (
747 [
748 {
749 "DBClusterSnapshotIdentifier": "my-cluster-snapshot",
750 "DBClusterIdentifier": "my-cluster",
751 "TagList": [{"Key": "TagOne", "Value": "Value one"}, {"Key": "tag_two", "Value": "Value two"}],
752 }
753 ],
754 "cluster",
755 False,
756 {
757 "DBClusterSnapshotIdentifier": "my-cluster-snapshot",
758 "DBClusterIdentifier": "my-cluster",
759 "TagList": [{"Key": "TagOne", "Value": "Value one"}, {"Key": "tag_two", "Value": "Value two"}],
760 },
761 ),
762 (
763 [
764 {
765 "DBSnapshotIdentifier": "my-snapshot",
766 "DBInstanceIdentifier": "my-instance",
767 "TagList": [{"Key": "TagOne", "Value": "Value one"}, {"Key": "tag_two", "Value": "Value two"}],
768 }
769 ],
770 "instance",
771 True,
772 {
773 "DBSnapshotIdentifier": "my-snapshot",
774 "DBInstanceIdentifier": "my-instance",
775 "Tags": {"TagOne": "Value one", "tag_two": "Value two"},
776 },
777 ),
778 ],
779 )
780 @patch(mod_name + ".describe_db_snapshots")
781 @patch(mod_name + ".describe_db_cluster_snapshots")
782 def test_get_snapshot_success(
783 m_describe_db_cluster_snapshots, m_describe_db_snapshots, snapshots, snapshot_type, convert_tags, expected
784 ):
785 client = MagicMock()
786 m_describe_db_cluster_snapshots.return_value = snapshots
787 m_describe_db_snapshots.return_value = snapshots
788 assert rds.get_snapshot(client, "my-snapshot", snapshot_type, convert_tags) == expected
789
790
791 def test_get_snapshot_error():
792 client = MagicMock()
793 with pytest.raises(ValueError) as e:
794 rds.get_snapshot(client, "my-snapshot", "bad parameter")
795 assert "Invalid snapshot_type. Expected one of: ('cluster', 'instance')" in str(e)
796
797
798 @pytest.mark.parametrize(
799 "provided_params, format_tags, expected",
800 [
801 (
802 {"RequiredParameter": "Present"},
803 False,
804 {"RequiredParameter": "Present"},
805 ),
806 (
807 {"RequiredParameter": "Present", "OptionalParameter": None},
808 False,
809 {"RequiredParameter": "Present"},
810 ),
811 (
812 {"RequiredParameter": "Present", "IrrelevantParameter": "Not used by this method"},
813 False,
814 {"RequiredParameter": "Present"},
815 ),
816 (
817 {
818 "RequiredParameter": "Present",
819 "OptionalParameter": "Present",
820 "Tags": {"still_in": "ansible_dict_format"},
821 },
822 False,
823 {
824 "RequiredParameter": "Present",
825 "OptionalParameter": "Present",
826 "Tags": {"still_in": "ansible_dict_format"},
827 },
828 ),
829 (
830 {"RequiredParameter": "Present"},
831 True,
832 {"RequiredParameter": "Present"},
833 ),
834 (
835 {"RequiredParameter": "Present", "Tags": None},
836 True,
837 {"RequiredParameter": "Present"},
838 ),
839 (
840 {"RequiredParameter": "Present", "Tags": {}},
841 True,
842 {"RequiredParameter": "Present", "Tags": {}},
843 ),
844 (
845 {"RequiredParameter": "Present", "Tags": {"Now in": "boto3_list_format"}},
846 True,
847 {"RequiredParameter": "Present", "Tags": [{"Key": "Now in", "Value": "boto3_list_format"}]},
848 ),
849 ],
850 )
851 @patch(mod_name + ".get_boto3_client_method_parameters")
852 def test_format_rds_client_method_parameters_success(
853 m_get_boto3_client_method_parameters, provided_params, format_tags, expected
854 ):
855 module = MagicMock()
856 module.fail_json_aws = MagicMock()
857 client = MagicMock()
858 m_get_boto3_client_method_parameters.side_effect = [
859 ["RequiredParameter"],
860 ["RequiredParameter", "OptionalParameter", "Tags"],
861 ]
862
863 assert (
864 rds.format_rds_client_method_parameters(client, module, provided_params, "mock_method", format_tags) == expected
865 )
866
867
868 @pytest.mark.parametrize(
869 "provided_params",
870 [
871 ({"RequiredParameter": None}),
872 ({"OptionalParameter": "present"}),
873 ({}),
874 ],
875 )
876 @patch(mod_name + ".get_boto3_client_method_parameters")
877 @patch(mod_name + ".get_rds_method_attribute")
878 def test_format_rds_client_method_parameters_failure(
879 m_get_rds_method_attribute, m_get_boto3_client_method_parameters, provided_params
880 ):
881 module = MagicMock()
882 client = MagicMock()
883 m_get_boto3_client_method_parameters.return_value = ["RequiredParameter"]
884 m_get_rds_method_attribute.return_value = rds.Boto3ClientMethod("mock_method", None, "mock method", None, None)
885
886 rds.format_rds_client_method_parameters(client, module, provided_params, "mock_method", False)
887 module.fail_json.assert_called_with(msg="To mock method requires the parameters: ['RequiredParameter']")
888
889
890 class TestRdsUtils:
891 # ========================================================
892 # Setup some initial data that we can use within our tests
893 # ========================================================
894 def setup_method(self):
895 self.target_role_list = [
896 {"role_arn": "role_won", "feature_name": "s3Export"},
897 {"role_arn": "role_too", "feature_name": "Lambda"},
898 {"role_arn": "role_thrie", "feature_name": "s3Import"},
899 ]
900
901 # ========================================================
902 # rds.compare_iam_roles
903 # ========================================================
904
905 def test_compare_iam_roles_equal(self):
906 existing_list = self.target_role_list
907 roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=False)
908 assert [] == roles_to_add
909 assert [] == roles_to_delete
910 roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=True)
911 assert [] == roles_to_add
912 assert [] == roles_to_delete
913
914 def test_compare_iam_roles_empty_arr_existing(self):
915 roles_to_add, roles_to_delete = rds.compare_iam_roles([], self.target_role_list, purge_roles=False)
916 assert self.target_role_list == roles_to_add
917 assert [] == roles_to_delete
918 roles_to_add, roles_to_delete = rds.compare_iam_roles([], self.target_role_list, purge_roles=True)
919 assert self.target_role_list, roles_to_add
920 assert [] == roles_to_delete
921
922 def test_compare_iam_roles_empty_arr_target(self):
923 existing_list = self.target_role_list
924 roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, [], purge_roles=False)
925 assert [] == roles_to_add
926 assert [] == roles_to_delete
927 roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, [], purge_roles=True)
928 assert [] == roles_to_add
929 assert self.target_role_list == roles_to_delete
930
931 def test_compare_iam_roles_different(self):
932 existing_list = [{"role_arn": "role_wonn", "feature_name": "s3Export"}]
933 roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=False)
934 assert self.target_role_list == roles_to_add
935 assert [] == roles_to_delete
936 roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=True)
937 assert self.target_role_list == roles_to_add
938 assert existing_list == roles_to_delete
939
940 existing_list = self.target_role_list.copy()
941 self.target_role_list = [{"role_arn": "role_wonn", "feature_name": "s3Export"}]
942 roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=False)
943 assert self.target_role_list == roles_to_add
944 assert [] == roles_to_delete
945 roles_to_add, roles_to_delete = rds.compare_iam_roles(existing_list, self.target_role_list, purge_roles=True)
946 assert self.target_role_list == roles_to_add
947 assert existing_list == roles_to_delete