Debian Codegraph @

ansible_collections/dellemc/openmanage/tests/unit/plugins/modules/test_ome_chassis_slots.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 #
4 # Dell OpenManage Ansible Modules
5 # Version 10.0.1
6 # Copyright (C) 2021-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
7
8 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
9 #
10
11 from __future__ import (absolute_import, division, print_function)
12
13 __metaclass__ = type
14
15 import json
16 import pytest
17 from ssl import SSLError
18 from io import StringIO
19 from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError
20 from ansible.module_utils.urls import ConnectionError, SSLValidationError
21 from ansible.module_utils._text import to_text
22 from ansible_collections.dellemc.openmanage.plugins.modules import ome_chassis_slots
23 from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule, AnsibleFailJSonException
24
25 DEVICE_REPEATED = "Duplicate device entry found for devices with identifiers {0}."
26 CHASSIS_REPEATED = "Duplicate chassis entry found for chassis with service tags {0}."
27 CHASSIS_TAG_INVALID = "Provided chassis {0} is invalid."
28 INVALID_SLOT_DEVICE = "Unable to rename one or more slots because either the specified device is invalid or slots " \
29 "cannot be configured. The devices for which the slots cannot be renamed are: {0}."
30 JOBS_TRIG_FAIL = "Unable to initiate the slot name rename jobs."
31 SUCCESS_MSG = "Successfully renamed the slot(s)."
32 SUCCESS_REFRESH_MSG = "The rename slot job(s) completed successfully. " \
33 "For changes to reflect, refresh the inventory task manually."
34 FAILED_MSG = "Failed to rename {0} of {1} slot names."
35 NO_CHANGES_MSG = "No changes found to be applied."
36 CHANGES_FOUND = "Changes found to be applied."
37 INVALID_SLOT_NUMBERS = "Unable to rename one or more slots because the slot number(s) are invalid: {0}."
38 SLOT_NUM_DUP = "Slot numbers are repeated for chassis {0}."
39
40 MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.ome_chassis_slots.'
41 MODULE_UTIL_PATH = 'ansible_collections.dellemc.openmanage.plugins.module_utils.ome.'
42
43
44 @pytest.fixture
45 def ome_connection_mock_for_chassis_slots(mocker, ome_response_mock):
46 connection_class_mock = mocker.patch(MODULE_PATH + 'RestOME')
47 ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value
48 ome_connection_mock_obj.invoke_request.return_value = ome_response_mock
49 return ome_connection_mock_obj
50
51
52 class TestOmeChassisSlots(FakeAnsibleModule):
53 module = ome_chassis_slots
54
55 def _run_config_error_test(self, params, ome_response_mock, ome_connection_mock, ome_default_args):
56 ome_response_mock.success = params.get("success", True)
57 ome_response_mock.json_data = params["json_data"]
58 ome_connection_mock.get_all_items_with_pagination.return_value = params["json_data"]
59 ome_default_args.update(params["mparams"])
60 return self._run_module(ome_default_args)
61
62 @pytest.mark.parametrize("params", [{'mparams': {"device_options": [
63 {"slot_name": "t1",
64 "device_service_tag": "ABCD1234"}]},
65 "invalid_list": set(["ABCD1234"]), "json_data": {
66 "value": [{"Id": 10053, "Identifier": "2H5DNX2", "SlotConfiguration": {"ChassisName": None}},
67 {"Id": 10054, "Type": 1000, "Identifier": "2H7HNX2",
68 "SlotConfiguration": {"DeviceType": "1000", "ChassisId": "10053", "SlotNumber": "1",
69 "SlotName": "my_840c", "SlotType": "2000"}}]},
70 'message': INVALID_SLOT_DEVICE, "success": True},
71 {'mparams': {"device_options": [{"slot_name": "s1", "device_id": 10054},
72 {"slot_name": "s2",
73 "device_service_tag": "ABCD1234"},
74 {"slot_name": "s1", "device_id": 10052},
75 ]},
76 "invalid_list": set(["ABCD1234"]),
77 "json_data":
78 {"value": [{"Id": 10053, "Identifier": "2H5DNX2",
79 "SlotConfiguration": {"ChassisName": None}},
80 {"Id": 10054, "Type": 1000, "Identifier": "ABCD1234",
81 "SlotConfiguration": {"DeviceType": "1000", "ChassisId": "10053", "SlotNumber": "1",
82 "SlotName": "my_840c", "SlotType": "2000"}}]}, 'message': DEVICE_REPEATED,
83 "success": True}, {
84 'mparams': {"device_options": [{"slot_name": "s1", "device_id": 10054},
85 {"slot_name": "s2",
86 "device_service_tag": "ABCD1234"},
87 {"slot_name": "s2",
88 "device_service_tag": "ABCD1234"},
89 {"slot_name": "s2",
90 "device_service_tag": "PQRS1234"}, ]},
91 "invalid_list": set(["ABCD1234"]), "json_data": {
92 "value": [{"Id": 10053, "Identifier": "2H5DNX2", "SlotConfiguration": {"ChassisName": None}},
93 {"Id": 10052, "Type": 1000, "Identifier": "PQRS1234",
94 "SlotConfiguration": {"DeviceType": "1000", "ChassisId": "10053", "SlotNumber": "1",
95 "SlotName": "my_840c", "SlotType": "2000"}},
96 {"Id": 10054, "Type": 1000, "Identifier": "ABCD1234",
97 "SlotConfiguration": {"DeviceType": "1000", "ChassisId": "10053", "SlotNumber": "1",
98 "SlotName": "my_840c", "SlotType": "2000"}}]}, 'message': DEVICE_REPEATED,
99 "success": True},
100 {
101 'mparams': {"device_options": []},
102 "invalid_list": set(["ABCD1234"]), "json_data": {
103 "value": [{"Id": 10053, "Identifier": "2H5DNX2", "SlotConfiguration": {"ChassisName": None}},
104 {"Id": 10052, "Type": 1000, "Identifier": "PQRS1234",
105 "SlotConfiguration": {"DeviceType": "1000", "ChassisId": "10053", "SlotNumber": "1",
106 "SlotName": "my_840c", "SlotType": "2000"}},
107 {"Id": 10054, "Type": 1000, "Identifier": "ABCD1234",
108 "SlotConfiguration": {"DeviceType": "1000", "ChassisId": "10053", "SlotNumber": "1",
109 "SlotName": "my_840c", "SlotType": "2000"}}]}, 'message': NO_CHANGES_MSG,
110 "success": True},
111 {
112 "mparams": {"slot_options": [{"chassis_service_tag": "ABC1234",
113 "slots": [{"slot_name": "t1", "slot_number": 1}, {"slot_name": "s1", "slot_number": 5}]},
114 {"chassis_service_tag": "ABC1234",
115 "slots": [{"slot_name": "t2", "slot_number": 2}, {"slot_name": "s2", "slot_number": 6}]}]},
116 "invalid_list": {"ABC1234"},
117 "json_data": {
118 "value": [{"Id": 10053, "Identifier": "2H5DNX2", "SlotConfiguration": {"ChassisName": None}},
119 {"Id": 10054, "Type": 1000, "Identifier": "ABCD1234",
120 "SlotConfiguration": {"DeviceType": "1000", "ChassisId": "10053", "SlotNumber": "1", "SlotName": "my_840c", "SlotType": "2000"}}]},
121 "message": CHASSIS_REPEATED,
122 "success": True},
123 {
124 "mparams": {
125 "slot_options": [
126 {
127 "chassis_service_tag": "ABC1234",
128 "slots": [
129 {"slot_name": "t1", "slot_number": 1},
130 {"slot_name": "s1", "slot_number": 5},
131 ],
132 },
133 {
134 "chassis_service_tag": "ABC1235",
135 "slots": [
136 {"slot_name": "t2", "slot_number": 2},
137 {"slot_name": "s2", "slot_number": 6},
138 ],
139 },
140 ]},
141 "invalid_list": {"ABC1234"},
142 "json_data": {
143 "value": [
144 {
145 "Id": 10053,
146 "Identifier": "2H5DNX2",
147 "SlotConfiguration": {"ChassisName": None},
148 },
149 {
150 "Id": 10054,
151 "Type": 1000,
152 "Identifier": "ABCD1234",
153 "SlotConfiguration": {
154 "DeviceType": "1000",
155 "ChassisId": "10053",
156 "SlotNumber": "1",
157 "SlotName": "my_840c",
158 "SlotType": "2000",
159 },
160 },
161 ]
162 },
163 "message": CHASSIS_TAG_INVALID,
164 "success": True},
165 {
166 'mparams': {"device_options": [{"slot_name": "s2", "device_service_tag": "PQRS1234"}]},
167 "invalid_list": set(["PQRS1234"]),
168 "json_data": {
169 "value": [{"Id": 10053, "Identifier": "2H5DNX2"},
170 {"Id": 10052, "Type": 1000, "Identifier": "PQRS1234"},
171 {"Id": 10054, "Type": 1000, "Identifier": "ABCD1234"}]},
172 'message': INVALID_SLOT_DEVICE,
173 "success": True},
174 {
175 'mparams': {"device_options": [{"slot_name": "s2", "device_id": 10054}]},
176 "invalid_list": set(["10054"]),
177 "json_data": {
178 "value": [{"Id": 10053, "Identifier": "2H5DNX2"},
179 {"Id": 10052, "Type": 1000, "Identifier": "PQRS1234"},
180 {"Id": 10054, "Type": 1000, "Identifier": "ABCD1234"}]},
181 'message': INVALID_SLOT_DEVICE,
182 "success": True},
183 ])
184 def test_get_device_slot_config_errors(self, params, ome_connection_mock_for_chassis_slots, ome_response_mock, ome_default_args, module_mock):
185 # Test device slot config error
186 result = self._run_config_error_test(params, ome_response_mock, ome_connection_mock_for_chassis_slots, ome_default_args)
187 expected_msg = params["message"].format(';'.join(set(params.get("invalid_list"))))
188 assert result["msg"] == expected_msg
189
190 @pytest.mark.parametrize("params", [{"json_data": {'Name': 'j1', 'Id': 24}, "slot_data": {
191 "ABC1234": {"ChassisId": "123", "SlotNumber": "1", "SlotType": "2000"}}, "failed_jobs": {}}])
192 def test_start_slot_name_jobs(
193 self, params, ome_connection_mock_for_chassis_slots, ome_response_mock):
194 ome_response_mock.success = params.get("success", True)
195 # ome_connection_mock_for_chassis_slots.job_submission.return_value = params['json_data']
196 ome_response_mock.json_data = params["json_data"]
197 failed_jobs = self.module.start_slot_name_jobs(
198 ome_connection_mock_for_chassis_slots, params.get('slot_data'))
199 assert failed_jobs == params['failed_jobs']
200
201 @pytest.mark.parametrize("params", [
202 {"json_data": {"value": [{'Name': 'j1', 'Id': 12, "LastRunStatus": {"Id": 2060, "Name": "Completed"}}]},
203 "slot_data": {"ABC1234": {"new_name": "s1", "SlotNumber": "1", "SlotType": "2000", "JobId": 12}},
204 "failed_jobs": {}}])
205 def test_get_job_states(
206 self, params, ome_connection_mock_for_chassis_slots, ome_response_mock):
207 ome_response_mock.success = params.get("success", True)
208 f_module = self.get_module_mock()
209 ome_response_mock.json_data = params["json_data"]
210 failed_jobs = self.module.get_job_states(f_module, ome_connection_mock_for_chassis_slots,
211 params.get('slot_data'))
212 assert failed_jobs == params['failed_jobs']
213
214 @pytest.mark.parametrize("params", [{'mparams': {"device_options": [{"slot_name": "my_840c", "device_id": 10054},
215 {"slot_name": "my_740c",
216 "device_service_tag": "ABCD1234"}]},
217 "json_data": {"value": [{"Id": 10053, "Identifier": "ABCD1234",
218 "SlotConfiguration": {"DeviceType": "1000",
219 "ChassisId": "10053",
220 "SlotNumber": "1",
221 "SlotName": "my_740c",
222 "SlotType": "2000"}},
223 {"Id": 10054, "Type": 1000, "Identifier": "PQRS1234",
224 "SlotConfiguration": {"DeviceType": "1000",
225 "ChassisId": "10053",
226 "SlotNumber": "1",
227 "SlotName": "my_840c",
228 "SlotType": "2000"}}]},
229 'message': NO_CHANGES_MSG, "check_mode": True}, {'mparams': {
230 "device_options": [{"slot_name": "my_840", "device_id": 10054},
231 {"slot_name": "my_740",
232 "device_service_tag": "ABCD1234"}]},
233 "json_data": {"value": [{"Id": 10053, "Identifier": "ABCD1234",
234 "SlotConfiguration": {"DeviceType": "1000",
235 "ChassisId": "10053", "SlotNumber": "1", "SlotName": "my_740c",
236 "SlotType": "2000"}},
237 {"Id": 10054, "Type": 1000, "Identifier": "PQRS1234",
238 "SlotConfiguration": {"DeviceType": "1000", "ChassisId": "10053", "SlotNumber": "1",
239 "SlotName": "my_840c", "SlotType": "2000"}}]},
240 'message': CHANGES_FOUND, "check_mode": True}, ])
241 def test_check_mode_idempotency(
242 self, params, ome_connection_mock_for_chassis_slots, ome_default_args):
243 ome_connection_mock_for_chassis_slots.get_all_items_with_pagination.return_value = params[
244 'json_data']
245 ome_default_args.update(params['mparams'])
246 result = self._run_module(
247 ome_default_args, check_mode=params.get(
248 'check_mode', False))
249 assert result['msg'] == params['message']
250
251 @pytest.mark.parametrize("params", [{'mparams': {"device_options": [{"slot_name": "t1", "device_id": 10053},
252 {"slot_name": "t1",
253 "device_service_tag": "ABCD1234"}]},
254 "json_data": {"value": [{"Id": 10053, "Identifier": "2H5DNX2",
255 "SlotConfiguration": {"DeviceType": "1000",
256 "ChassisId": "10053",
257 "SlotNumber": "1",
258 "SlotName": "my_840c",
259 "SlotType": "2000"}},
260 {"Id": 10054, "Identifier": "ABCD1234",
261 "SlotConfiguration": {"DeviceType": "1000",
262 "ChassisId": "10053",
263 "SlotNumber": "1",
264 "SlotName": "my_840c",
265 "SlotType": "2000"}}], },
266 'message': SUCCESS_MSG, "success": True}])
267 def test_ome_chassis_slots_success_case(self, params, ome_connection_mock_for_chassis_slots, ome_response_mock,
268 ome_default_args, mocker):
269 ome_response_mock.success = params.get("success", True)
270 ome_connection_mock_for_chassis_slots.get_all_items_with_pagination.return_value = params[
271 'json_data']
272 ome_connection_mock_for_chassis_slots.job_tracking.return_value = (
273 False, "job_track_msg")
274 mocker.patch(
275 MODULE_PATH +
276 'trigger_refresh_inventory',
277 return_value=[1])
278 mocker.patch(
279 MODULE_PATH +
280 'start_slot_name_jobs',
281 return_value=params.get(
282 'start_slot_name_jobs',
283 {}))
284 mocker.patch(
285 MODULE_PATH +
286 'get_job_states',
287 return_value=params.get(
288 'get_job_states',
289 {}))
290 ome_default_args.update(params['mparams'])
291 result = self._run_module(
292 ome_default_args, check_mode=params.get(
293 'check_mode', False))
294 assert result['msg'] == params['message']
295
296 @pytest.mark.parametrize("params", [{'mparams': {"slot_options": [{"chassis_service_tag": "ABC1234",
297 "slots": [{"slot_name": "t1", "slot_number": 1},
298 {"slot_name": "s1",
299 "slot_number": 5}]}]},
300 "chassi": {'value': [{"Identifier": "ABC1234", "Id": 1234}]},
301 "bladeslots": {'ABC1234_1': {"SlotNumber": "1", "SlotName": "myslotty"}},
302 "storageslots": {'value': [{"SlotNumber": "5", "SlotName": "stor-slot1"}]},
303 "slot_data": {"ABC1234_1": {"SlotNumber": "1", "SlotName": "myslotty"}}}])
304 def test_slot_number_config(self, params, ome_connection_mock_for_chassis_slots, ome_response_mock,
305 ome_default_args, mocker):
306 mocker.patch(
307 MODULE_PATH + 'get_device_type',
308 return_value=params.get('chassi'))
309 mocker.patch(
310 MODULE_PATH + 'get_slot_data',
311 return_value=params.get('bladeslots'))
312 f_module = self.get_module_mock(params=params["mparams"])
313 slot_data = self.module.slot_number_config(
314 f_module, ome_connection_mock_for_chassis_slots)
315 assert slot_data == params['slot_data']
316
317 @pytest.mark.parametrize("params", [{"slot_options": {"chassis_service_tag": "ABC1234",
318 "slots": [{"slot_name": "t1", "slot_number": 1},
319 {"slot_name": "s1", "slot_number": 5}]},
320 "chass_id": 1234, "chassi": {'value': [{"Identifier": "ABC1234", "Id": 1234}]},
321 "bladeslots": {'value': [{"SlotNumber": "1", "SlotName": "blade-slot1",
322 "Id": 234}]},
323 "storageslots": {'value': [{"ChassisServiceTag": "ABC1234",
324 "SlotConfiguration": {
325 "SlotId": "123", "SlotNumber": "5",
326 "SlotName": "stor-slot1"}}]},
327 "slot_dict_diff": {'ABC1234_5': {'SlotNumber': '5', 'SlotName': 'stor-slot1',
328 'ChassisId': 1234, 'SlotId': "123",
329 'ChassisServiceTag': 'ABC1234',
330 'new_name': 's1'},
331 'ABC1234_1': {'SlotNumber': '1', 'SlotName': 'blade-slot1',
332 'ChassisId': 1234, 'SlotId': "234",
333 "Id": 234,
334 'ChassisServiceTag': 'ABC1234',
335 'new_name': 't1'}}}])
336 def test_get_slot_data(self, params, ome_connection_mock_for_chassis_slots, ome_response_mock, ome_default_args,
337 mocker):
338 mocker.patch(
339 MODULE_PATH + 'get_device_type',
340 return_value=params.get('storageslots'))
341 ome_response_mock.success = params.get("success", True)
342 ome_response_mock.json_data = params["bladeslots"]
343 ch_slots = params['slot_options']
344 f_module = self.get_module_mock()
345 slot_dict_diff = self.module.get_slot_data(f_module, ome_connection_mock_for_chassis_slots, ch_slots,
346 params['chass_id'])
347 assert slot_dict_diff == params['slot_dict_diff']
348
349 @pytest.mark.parametrize("params", [{"json_data": {'Name': 'j1', 'Id': 24}, "slot_data": {
350 "ABC1234": {"ChassisId": "123", "SlotNumber": "1", "ChassisServiceTag": "ABC1234"}}, "jobs": [1]}])
351 def test_trigger_refresh_inventory(
352 self, params, ome_connection_mock_for_chassis_slots, ome_response_mock):
353 ome_response_mock.success = params.get("success", True)
354 ome_response_mock.json_data = params["json_data"]
355 jobs = self.module.trigger_refresh_inventory(
356 ome_connection_mock_for_chassis_slots, params.get('slot_data'))
357 assert jobs == params['jobs']
358
359 @pytest.mark.parametrize("exc_type",
360 [IOError, ValueError, SSLError, TypeError, ConnectionError, HTTPError, URLError])
361 def test_ome_groups_main_exception_failure_case(self, exc_type, mocker, ome_default_args,
362 ome_connection_mock_for_chassis_slots, ome_response_mock):
363 ome_default_args.update(
364 {"device_options": [{"slot_name": "t1", "device_id": 1234}]})
365 ome_response_mock.status_code = 400
366 ome_response_mock.success = False
367 json_str = to_text(json.dumps({"info": "error_details"}))
368 if exc_type == URLError:
369 mocker.patch(
370 MODULE_PATH + 'get_device_slot_config',
371 side_effect=exc_type("url open error"))
372 result = self._run_module(ome_default_args)
373 assert result["unreachable"] is True
374 elif exc_type not in [HTTPError, SSLValidationError]:
375 mocker.patch(
376 MODULE_PATH + 'get_device_slot_config',
377 side_effect=exc_type("exception message"))
378 result = self._run_module(ome_default_args)
379 assert result['failed'] is True
380 else:
381 mocker.patch(MODULE_PATH + 'get_device_slot_config',
382 side_effect=exc_type('https://testhost.com', 400, 'http error message',
383 {"accept-type": "application/json"}, StringIO(json_str)))
384 result = self._run_module(ome_default_args)
385 assert result['failed'] is True
386 assert 'msg' in result
387
388 @pytest.mark.parametrize(
389 "params",
390 [
391 {
392 "slot_options": {
393 "chassis_service_tag": "ABC1234",
394 "slots": [
395 {"slot_name": "t1", "slot_number": 1},
396 {"slot_name": "s1", "slot_number": 5},
397 {"slot_name": "s2", "slot_number": 6},
398 ],
399 },
400 "chass_id": 1234,
401 "chassi": {
402 "value": [{"Identifier": "ABC1234", "Id": 1234}]
403 },
404 "bladeslots": {
405 "value": [{"SlotNumber": "1", "SlotName": "blade-slot1", "Id": 234}]
406 },
407 "invalid_list": {"6"},
408 "message": INVALID_SLOT_NUMBERS,
409 "storageslots": {
410 "value": [
411 {
412 "ChassisServiceTag": "ABC1234",
413 "SlotConfiguration": {
414 "SlotId": "123",
415 "SlotNumber": "5",
416 "SlotName": "stor-slot1",
417 },
418 }
419 ]
420 },
421 },
422 {
423 "slot_options": {
424 "chassis_service_tag": "ABC1234",
425 "slots": [
426 {"slot_name": "t1", "slot_number": 1},
427 {"slot_name": "s1", "slot_number": 5},
428 {"slot_name": "s1", "slot_number": 5},
429 ],
430 },
431 "chass_id": 1234,
432 "chassi": {
433 "value": [{"Identifier": "ABC1234", "Id": 1234}]
434 },
435 "bladeslots": {
436 "value": [{"SlotNumber": "1", "SlotName": "blade-slot1", "Id": 234}]
437 },
438 "invalid_list": {"ABC1234"},
439 "message": SLOT_NUM_DUP,
440 "storageslots": {
441 "value": [
442 {
443 "ChassisServiceTag": "ABC1234",
444 "SlotConfiguration": {
445 "SlotId": "123",
446 "SlotNumber": "5",
447 "SlotName": "stor-slot1",
448 },
449 }
450 ]
451 },
452 },
453 ]
454 )
455 def test_invalid_slot_numbers(self, params, ome_connection_mock_for_chassis_slots, ome_response_mock, mocker):
456 mocker.patch(
457 MODULE_PATH + 'get_device_type',
458 return_value=params.get('storageslots'))
459 ome_response_mock.json_data = params["bladeslots"]
460 ch_slots = params['slot_options']
461 f_module = self.get_module_mock()
462 with pytest.raises(AnsibleFailJSonException) as exc_info:
463 self.module.get_slot_data(f_module, ome_connection_mock_for_chassis_slots, ch_slots, params['chass_id'])
464
465 # Validate the error message
466 expected_msg = params['message'].format(';'.join(params['invalid_list']))
467 assert str(exc_info.value) == expected_msg