Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_ss_3par_cpg.py

raw
1 # Copyright (c) 2018, Hewlett Packard Enterprise Development LP
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import sys
8 from unittest import mock
9
10 sys.modules["hpe3par_sdk"] = mock.Mock()
11 sys.modules["hpe3par_sdk.client"] = mock.Mock()
12 sys.modules["hpe3parclient"] = mock.Mock()
13 sys.modules["hpe3parclient.exceptions"] = mock.Mock()
14 from ansible_collections.community.general.plugins.module_utils import _hpe3par as hpe3par
15 from ansible_collections.community.general.plugins.modules import ss_3par_cpg
16
17
18 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.client")
19 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.AnsibleModule")
20 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.create_cpg")
21 def test_module_args(mock_create_cpg, mock_module, mock_client):
22 """
23 hpe3par CPG - test module arguments
24 """
25
26 PARAMS_FOR_PRESENT = {
27 "storage_system_ip": "192.168.0.1",
28 "storage_system_username": "USER",
29 "storage_system_password": "PASS",
30 "cpg_name": "test_cpg",
31 "domain": "test_domain",
32 "growth_increment": 32768,
33 "growth_increment_unit": "MiB",
34 "growth_limit": 32768,
35 "growth_limit_unit": "MiB",
36 "growth_warning": 32768,
37 "growth_warning_unit": "MiB",
38 "raid_type": "R6",
39 "set_size": 8,
40 "high_availability": "MAG",
41 "disk_type": "FC",
42 "state": "present",
43 "secure": False,
44 }
45 mock_module.params = PARAMS_FOR_PRESENT
46 mock_module.return_value = mock_module
47 mock_client.HPE3ParClient.login.return_value = True
48 mock_create_cpg.return_value = (True, True, "Created CPG successfully.")
49 ss_3par_cpg.main()
50 mock_module.assert_called_with(
51 argument_spec=hpe3par.cpg_argument_spec(), required_together=[["raid_type", "set_size"]]
52 )
53
54
55 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.client")
56 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.AnsibleModule")
57 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.create_cpg")
58 def test_main_exit_functionality_present_success_without_issue_attr_dict(mock_create_cpg, mock_module, mock_client):
59 """
60 hpe3par flash cache - success check
61 """
62 PARAMS_FOR_PRESENT = {
63 "storage_system_ip": "192.168.0.1",
64 "storage_system_name": "3PAR",
65 "storage_system_username": "USER",
66 "storage_system_password": "PASS",
67 "cpg_name": "test_cpg",
68 "domain": "test_domain",
69 "growth_increment": 32768,
70 "growth_increment_unit": "MiB",
71 "growth_limit": 32768,
72 "growth_limit_unit": "MiB",
73 "growth_warning": 32768,
74 "growth_warning_unit": "MiB",
75 "raid_type": "R6",
76 "set_size": 8,
77 "high_availability": "MAG",
78 "disk_type": "FC",
79 "state": "present",
80 "secure": False,
81 }
82 # This creates a instance of the AnsibleModule mock.
83 mock_module.params = PARAMS_FOR_PRESENT
84 mock_module.return_value = mock_module
85 instance = mock_module.return_value
86 mock_client.HPE3ParClient.login.return_value = True
87 mock_create_cpg.return_value = (True, True, "Created CPG successfully.")
88 ss_3par_cpg.main()
89 # AnsibleModule.exit_json should be called
90 instance.exit_json.assert_called_with(changed=True, msg="Created CPG successfully.")
91 # AnsibleModule.fail_json should not be called
92 assert instance.fail_json.call_count == 0
93
94
95 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.client")
96 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.AnsibleModule")
97 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.delete_cpg")
98 def test_main_exit_functionality_absent_success_without_issue_attr_dict(mock_delete_cpg, mock_module, mock_client):
99 """
100 hpe3par flash cache - success check
101 """
102 PARAMS_FOR_DELETE = {
103 "storage_system_ip": "192.168.0.1",
104 "storage_system_name": "3PAR",
105 "storage_system_username": "USER",
106 "storage_system_password": "PASS",
107 "cpg_name": "test_cpg",
108 "domain": None,
109 "growth_increment": None,
110 "growth_increment_unit": None,
111 "growth_limit": None,
112 "growth_limit_unit": None,
113 "growth_warning": None,
114 "growth_warning_unit": None,
115 "raid_type": None,
116 "set_size": None,
117 "high_availability": None,
118 "disk_type": None,
119 "state": "absent",
120 "secure": False,
121 }
122 # This creates a instance of the AnsibleModule mock.
123 mock_module.params = PARAMS_FOR_DELETE
124 mock_module.return_value = mock_module
125 instance = mock_module.return_value
126 mock_delete_cpg.return_value = (True, True, "Deleted CPG test_cpg successfully.")
127 mock_client.HPE3ParClient.login.return_value = True
128 ss_3par_cpg.main()
129 # AnsibleModule.exit_json should be called
130 instance.exit_json.assert_called_with(changed=True, msg="Deleted CPG test_cpg successfully.")
131 # AnsibleModule.fail_json should not be called
132 assert instance.fail_json.call_count == 0
133
134
135 def test_convert_to_binary_multiple():
136 assert hpe3par.convert_to_binary_multiple(None) == -1
137 assert hpe3par.convert_to_binary_multiple("-1.0 MiB") == -1
138 assert hpe3par.convert_to_binary_multiple("-1.0GiB") == -1
139 assert hpe3par.convert_to_binary_multiple("1.0 MiB") == 1
140 assert hpe3par.convert_to_binary_multiple("1.5GiB") == 1.5 * 1024
141 assert hpe3par.convert_to_binary_multiple("1.5 TiB") == 1.5 * 1024 * 1024
142 assert hpe3par.convert_to_binary_multiple(" 1.5 TiB ") == 1.5 * 1024 * 1024
143
144
145 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.client")
146 def test_validate_set_size(mock_client):
147 mock_client.HPE3ParClient.RAID_MAP = {
148 "R0": {"raid_value": 1, "set_sizes": [1]},
149 "R1": {"raid_value": 2, "set_sizes": [2, 3, 4]},
150 "R5": {"raid_value": 3, "set_sizes": [3, 4, 5, 6, 7, 8, 9]},
151 "R6": {"raid_value": 4, "set_sizes": [6, 8, 10, 12, 16]},
152 }
153 raid_type = "R0"
154 set_size = 1
155 assert ss_3par_cpg.validate_set_size(raid_type, set_size)
156
157 set_size = 2
158 assert not ss_3par_cpg.validate_set_size(raid_type, set_size)
159
160 raid_type = None
161 assert not ss_3par_cpg.validate_set_size(raid_type, set_size)
162
163
164 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.client")
165 def test_cpg_ldlayout_map(mock_client):
166 mock_client.HPE3ParClient.PORT = 1
167 mock_client.HPE3ParClient.RAID_MAP = {
168 "R0": {"raid_value": 1, "set_sizes": [1]},
169 "R1": {"raid_value": 2, "set_sizes": [2, 3, 4]},
170 "R5": {"raid_value": 3, "set_sizes": [3, 4, 5, 6, 7, 8, 9]},
171 "R6": {"raid_value": 4, "set_sizes": [6, 8, 10, 12, 16]},
172 }
173 ldlayout_dict = {"RAIDType": "R6", "HA": "PORT"}
174 assert ss_3par_cpg.cpg_ldlayout_map(ldlayout_dict) == {"RAIDType": 4, "HA": 1}
175
176
177 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.client")
178 def test_create_cpg(mock_client):
179 ss_3par_cpg.validate_set_size = mock.Mock(return_value=True)
180 ss_3par_cpg.cpg_ldlayout_map = mock.Mock(return_value={"RAIDType": 4, "HA": 1})
181
182 mock_client.HPE3ParClient.login.return_value = True
183 mock_client.HPE3ParClient.cpgExists.return_value = False
184 mock_client.HPE3ParClient.FC = 1
185 mock_client.HPE3ParClient.createCPG.return_value = True
186
187 assert ss_3par_cpg.create_cpg(
188 mock_client.HPE3ParClient,
189 "test_cpg",
190 "test_domain",
191 "32768 MiB",
192 "32768 MiB",
193 "32768 MiB",
194 "R6",
195 8,
196 "MAG",
197 "FC",
198 ) == (True, True, "Created CPG test_cpg successfully.")
199
200 mock_client.HPE3ParClient.cpgExists.return_value = True
201 assert ss_3par_cpg.create_cpg(
202 mock_client.HPE3ParClient,
203 "test_cpg",
204 "test_domain",
205 "32768.0 MiB",
206 "32768.0 MiB",
207 "32768.0 MiB",
208 "R6",
209 8,
210 "MAG",
211 "FC",
212 ) == (True, False, "CPG already present")
213
214 ss_3par_cpg.validate_set_size = mock.Mock(return_value=False)
215 assert ss_3par_cpg.create_cpg(
216 mock_client.HPE3ParClient,
217 "test_cpg",
218 "test_domain",
219 "32768.0 MiB",
220 "32768 MiB",
221 "32768.0 MiB",
222 "R6",
223 3,
224 "MAG",
225 "FC",
226 ) == (False, False, "Set size 3 not part of RAID set R6")
227
228
229 @mock.patch("ansible_collections.community.general.plugins.modules.ss_3par_cpg.client")
230 def test_delete_cpg(mock_client):
231 mock_client.HPE3ParClient.login.return_value = True
232 mock_client.HPE3ParClient.cpgExists.return_value = True
233 mock_client.HPE3ParClient.FC = 1
234 mock_client.HPE3ParClient.deleteCPG.return_value = True
235
236 assert ss_3par_cpg.delete_cpg(mock_client.HPE3ParClient, "test_cpg") == (
237 True,
238 True,
239 "Deleted CPG test_cpg successfully.",
240 )
241
242 mock_client.HPE3ParClient.cpgExists.return_value = False
243
244 assert ss_3par_cpg.delete_cpg(mock_client.HPE3ParClient, "test_cpg") == (True, False, "CPG does not exist")
245 assert ss_3par_cpg.delete_cpg(mock_client.HPE3ParClient, None) == (True, False, "CPG does not exist")