Debian Codegraph @

ansible_collections/amazon/aws/tests/unit/plugins/modules/test_ec2_snapshot_info.py

raw
1 # (c) 2022 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 call
8 from unittest.mock import patch
9
10 import pytest
11
12 from ansible_collections.amazon.aws.plugins.modules import ec2_snapshot_info
13
14 module_name = "ansible_collections.amazon.aws.plugins.modules.ec2_snapshot_info"
15
16
17 @pytest.mark.parametrize(
18 "snapshot_ids,owner_ids,restorable_by_user_ids,filters,max_results,next_token_id,expected",
19 [([], [], [], {}, None, None, {})],
20 )
21 def test_build_request_args(
22 snapshot_ids, owner_ids, restorable_by_user_ids, filters, max_results, next_token_id, expected
23 ):
24 assert (
25 ec2_snapshot_info.build_request_args(
26 snapshot_ids, owner_ids, restorable_by_user_ids, filters, max_results, next_token_id
27 )
28 == expected
29 )
30
31
32 @patch(module_name + ".build_request_args")
33 @patch(module_name + ".describe_snapshots")
34 def test_list_ec2_snapshots(m_describe_snapshots, m_build_request_args):
35 module = MagicMock()
36 connection = MagicMock()
37
38 m_describe_snapshots.return_value = {
39 "Snapshots": [
40 {
41 "Description": "Created by CreateImage(i-083b9dd1234567890) for ami-01486e111234567890",
42 "Encrypted": False,
43 "OwnerId": "123456789000",
44 "Progress": "100%",
45 "SnapshotId": "snap-0f00cba1234567890",
46 "StartTime": "2021-09-30T01:04:49.724000+00:00",
47 "State": "completed",
48 "StorageTier": "standard",
49 "Tags": [
50 {"Key": "TagKey", "Value": "TagValue"},
51 ],
52 "VolumeId": "vol-0ae6c5e1234567890",
53 "VolumeSize": 10,
54 }
55 ]
56 }
57
58 m_build_request_args.return_value = {"SnapshotIds": ["snap-0f00cba1234567890"]}
59
60 request_args = ec2_snapshot_info.build_request_args()
61
62 ec2_snapshot_info.list_ec2_snapshots(connection, module, request_args)
63
64 assert m_describe_snapshots.call_count == 1
65 m_describe_snapshots.assert_has_calls(
66 [
67 call(connection, **m_build_request_args.return_value),
68 ]
69 )
70
71
72 @patch(module_name + ".AnsibleAWSModule")
73 def test_main_success(m_AnsibleAWSModule):
74 m_module = MagicMock()
75 m_AnsibleAWSModule.return_value = m_module
76
77 ec2_snapshot_info.main()
78
79 m_module.client.assert_called_with("ec2")