Debian Codegraph @

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

raw
1 # Copyright (c) Ansible project
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 os
8 import shutil
9 import tempfile
10 import unittest
11 from unittest.mock import patch
12 from urllib.parse import urljoin
13
14 from ansible.module_utils import basic
15 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
16 AnsibleExitJson,
17 AnsibleFailJson,
18 exit_json,
19 fail_json,
20 set_module_args,
21 )
22
23 import ansible_collections.community.general.plugins.modules.ocapi_command as module
24
25 MOCK_BASE_URI = "mockBaseUri/"
26 OPERATING_SYSTEM_URI = "OperatingSystem"
27 MOCK_JOB_NAME = "MockJob"
28
29 ACTION_WAS_SUCCESSFUL = "Action was successful."
30 UPDATE_NOT_PERFORMED_IN_CHECK_MODE = "Update not performed in check mode."
31 NO_ACTION_PERFORMED_IN_CHECK_MODE = "No action performed in check mode."
32
33 MOCK_SUCCESSFUL_HTTP_RESPONSE_LED_INDICATOR_OFF_WITH_ETAG = {
34 "ret": True,
35 "data": {"IndicatorLED": {"ID": 4, "Name": "Off"}, "PowerState": {"ID": 2, "Name": "On"}},
36 "headers": {"etag": "MockETag"},
37 }
38
39 MOCK_SUCCESSFUL_HTTP_RESPONSE = {"ret": True, "data": {}}
40
41 MOCK_404_RESPONSE = {"ret": False, "status": 404}
42
43 MOCK_SUCCESSFUL_HTTP_RESPONSE_WITH_LOCATION_HEADER = {"ret": True, "data": {}, "headers": {"location": "mock_location"}}
44
45 MOCK_HTTP_RESPONSE_CONFLICT = {"ret": False, "msg": "Conflict", "status": 409}
46
47 MOCK_HTTP_RESPONSE_JOB_IN_PROGRESS = {"ret": True, "data": {"PercentComplete": 99}, "headers": {"etag": "12345"}}
48
49 MOCK_HTTP_RESPONSE_JOB_COMPLETE = {"ret": True, "data": {"PercentComplete": 100}, "headers": {"etag": "12345"}}
50
51
52 def get_bin_path(self, arg, required=False):
53 """Mock AnsibleModule.get_bin_path"""
54 return arg
55
56
57 def get_exception_message(ansible_exit_json):
58 """From an AnsibleExitJson exception, get the message string."""
59 return ansible_exit_json.exception.args[0]["msg"]
60
61
62 def is_changed(ansible_exit_json):
63 """From an AnsibleExitJson exception, return the value of the changed flag"""
64 return ansible_exit_json.exception.args[0]["changed"]
65
66
67 def mock_get_request(*args, **kwargs):
68 """Mock for get_request."""
69 url = args[1]
70 if url == f"https://{MOCK_BASE_URI}":
71 return MOCK_SUCCESSFUL_HTTP_RESPONSE_LED_INDICATOR_OFF_WITH_ETAG
72 elif url == "mock_location":
73 return MOCK_SUCCESSFUL_HTTP_RESPONSE
74 raise RuntimeError(f"Illegal call to get_request in test: {args[1]}")
75
76
77 def mock_get_request_job_does_not_exist(*args, **kwargs):
78 """Mock for get_request."""
79 url = args[1]
80 if url == f"https://{MOCK_BASE_URI}":
81 return MOCK_SUCCESSFUL_HTTP_RESPONSE_LED_INDICATOR_OFF_WITH_ETAG
82 elif url == urljoin(f"https://{MOCK_BASE_URI}", f"Jobs/{MOCK_JOB_NAME}"):
83 return MOCK_404_RESPONSE
84 raise RuntimeError(f"Illegal call to get_request in test: {args[1]}")
85
86
87 def mock_get_request_job_in_progress(*args, **kwargs):
88 url = args[1]
89 if url == f"https://{MOCK_BASE_URI}":
90 return MOCK_SUCCESSFUL_HTTP_RESPONSE_LED_INDICATOR_OFF_WITH_ETAG
91 elif url == urljoin(f"https://{MOCK_BASE_URI}", f"Jobs/{MOCK_JOB_NAME}"):
92 return MOCK_HTTP_RESPONSE_JOB_IN_PROGRESS
93 raise RuntimeError(f"Illegal call to get_request in test: {args[1]}")
94
95
96 def mock_get_request_job_complete(*args, **kwargs):
97 url = args[1]
98 if url == f"https://{MOCK_BASE_URI}":
99 return MOCK_SUCCESSFUL_HTTP_RESPONSE_LED_INDICATOR_OFF_WITH_ETAG
100 elif url == urljoin(f"https://{MOCK_BASE_URI}", f"Jobs/{MOCK_JOB_NAME}"):
101 return MOCK_HTTP_RESPONSE_JOB_COMPLETE
102 raise RuntimeError(f"Illegal call to get_request in test: {args[1]}")
103
104
105 def mock_put_request(*args, **kwargs):
106 """Mock put_request."""
107 url = args[1]
108 if url == f"https://{MOCK_BASE_URI}":
109 return MOCK_SUCCESSFUL_HTTP_RESPONSE_WITH_LOCATION_HEADER
110 raise RuntimeError(f"Illegal PUT call to: {args[1]}")
111
112
113 def mock_delete_request(*args, **kwargs):
114 """Mock delete request."""
115 url = args[1]
116 if url == urljoin(f"https://{MOCK_BASE_URI}", f"Jobs/{MOCK_JOB_NAME}"):
117 return MOCK_SUCCESSFUL_HTTP_RESPONSE
118 raise RuntimeError(f"Illegal DELETE call to: {args[1]}")
119
120
121 def mock_post_request(*args, **kwargs):
122 """Mock post_request."""
123 url = args[1]
124 if url == urljoin(f"https://{MOCK_BASE_URI}", OPERATING_SYSTEM_URI):
125 return MOCK_SUCCESSFUL_HTTP_RESPONSE
126 raise RuntimeError(f"Illegal POST call to: {args[1]}")
127
128
129 def mock_http_request_conflict(*args, **kwargs):
130 """Mock to make an HTTP request return 409 Conflict"""
131 return MOCK_HTTP_RESPONSE_CONFLICT
132
133
134 def mock_invalid_http_request(*args, **kwargs):
135 """Mock to make an HTTP request invalid. Raises an exception."""
136 raise RuntimeError(f"Illegal HTTP call to {args[1]}")
137
138
139 class TestOcapiCommand(unittest.TestCase):
140 def setUp(self):
141 self.mock_module_helper = patch.multiple(
142 basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json, get_bin_path=get_bin_path
143 )
144 self.mock_module_helper.start()
145 self.addCleanup(self.mock_module_helper.stop)
146 self.tempdir = tempfile.mkdtemp()
147
148 def tearDown(self):
149 shutil.rmtree(self.tempdir)
150
151 def test_module_fail_when_required_args_missing(self):
152 with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
153 with set_module_args({}):
154 module.main()
155 self.assertIn("missing required arguments:", get_exception_message(ansible_fail_json))
156
157 def test_module_fail_when_unknown_category(self):
158 with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
159 with set_module_args(
160 {
161 "category": "unknown",
162 "command": "IndicatorLedOn",
163 "username": "USERID",
164 "password": "PASSW0RD=21",
165 "baseuri": MOCK_BASE_URI,
166 }
167 ):
168 module.main()
169 self.assertIn("Invalid Category 'unknown", get_exception_message(ansible_fail_json))
170
171 def test_set_power_mode(self):
172 """Test that we can set chassis power mode"""
173 with patch.multiple(
174 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
175 get_request=mock_get_request,
176 put_request=mock_put_request,
177 ):
178 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
179 with set_module_args(
180 {
181 "category": "Chassis",
182 "command": "PowerModeLow",
183 "baseuri": MOCK_BASE_URI,
184 "username": "USERID",
185 "password": "PASSWORD=21",
186 }
187 ):
188 module.main()
189 self.assertEqual(ACTION_WAS_SUCCESSFUL, get_exception_message(ansible_exit_json))
190 self.assertTrue(is_changed(ansible_exit_json))
191
192 def test_set_chassis_led_indicator(self):
193 """Test that we can set chassis LED indicator."""
194 with patch.multiple(
195 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
196 get_request=mock_get_request,
197 put_request=mock_put_request,
198 ):
199 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
200 with set_module_args(
201 {
202 "category": "Chassis",
203 "command": "IndicatorLedOn",
204 "baseuri": MOCK_BASE_URI,
205 "username": "USERID",
206 "password": "PASSWORD=21",
207 }
208 ):
209 module.main()
210 self.assertEqual(ACTION_WAS_SUCCESSFUL, get_exception_message(ansible_exit_json))
211 self.assertTrue(is_changed(ansible_exit_json))
212
213 def test_set_power_mode_already_set(self):
214 """Test that if we set Power Mode to normal when it's already normal, we get changed=False."""
215 with patch.multiple(
216 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
217 get_request=mock_get_request,
218 put_request=mock_invalid_http_request,
219 ):
220 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
221 with set_module_args(
222 {
223 "category": "Chassis",
224 "command": "PowerModeNormal",
225 "baseuri": MOCK_BASE_URI,
226 "username": "USERID",
227 "password": "PASSWORD=21",
228 }
229 ):
230 module.main()
231 self.assertEqual(ACTION_WAS_SUCCESSFUL, get_exception_message(ansible_exit_json))
232 self.assertFalse(is_changed(ansible_exit_json))
233
234 def test_set_power_mode_check_mode(self):
235 """Test check mode when setting chassis Power Mode."""
236 with patch.multiple(
237 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
238 get_request=mock_get_request,
239 put_request=mock_invalid_http_request,
240 ):
241 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
242 with set_module_args(
243 {
244 "category": "Chassis",
245 "command": "IndicatorLedOn",
246 "baseuri": MOCK_BASE_URI,
247 "username": "USERID",
248 "password": "PASSWORD=21",
249 "_ansible_check_mode": True,
250 }
251 ):
252 module.main()
253 self.assertEqual(UPDATE_NOT_PERFORMED_IN_CHECK_MODE, get_exception_message(ansible_exit_json))
254 self.assertTrue(is_changed(ansible_exit_json))
255
256 def test_set_chassis_led_indicator_check_mode(self):
257 """Test check mode when setting chassis LED indicator"""
258 with patch.multiple(
259 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
260 get_request=mock_get_request,
261 put_request=mock_invalid_http_request,
262 ):
263 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
264 with set_module_args(
265 {
266 "category": "Chassis",
267 "command": "IndicatorLedOn",
268 "baseuri": MOCK_BASE_URI,
269 "username": "USERID",
270 "password": "PASSWORD=21",
271 "_ansible_check_mode": True,
272 }
273 ):
274 module.main()
275 self.assertEqual(UPDATE_NOT_PERFORMED_IN_CHECK_MODE, get_exception_message(ansible_exit_json))
276 self.assertTrue(is_changed(ansible_exit_json))
277
278 def test_set_chassis_led_indicator_already_set(self):
279 """Test that if we set LED Indicator to off when it's already off, we get changed=False."""
280 with patch.multiple(
281 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
282 get_request=mock_get_request,
283 put_request=mock_invalid_http_request,
284 ):
285 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
286 with set_module_args(
287 {
288 "category": "Chassis",
289 "command": "IndicatorLedOff",
290 "baseuri": MOCK_BASE_URI,
291 "username": "USERID",
292 "password": "PASSWORD=21",
293 }
294 ):
295 module.main()
296 self.assertEqual(ACTION_WAS_SUCCESSFUL, get_exception_message(ansible_exit_json))
297 self.assertFalse(is_changed(ansible_exit_json))
298
299 def test_set_chassis_led_indicator_already_set_check_mode(self):
300 """Test that if we set LED Indicator to off when it's already off, we get changed=False even in check mode."""
301 with patch.multiple(
302 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
303 get_request=mock_get_request,
304 put_request=mock_invalid_http_request,
305 ):
306 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
307 with set_module_args(
308 {
309 "category": "Chassis",
310 "command": "IndicatorLedOff",
311 "baseuri": MOCK_BASE_URI,
312 "username": "USERID",
313 "password": "PASSWORD=21",
314 "_ansible_check_mode": True,
315 }
316 ):
317 module.main()
318 self.assertEqual(NO_ACTION_PERFORMED_IN_CHECK_MODE, get_exception_message(ansible_exit_json))
319 self.assertFalse(is_changed(ansible_exit_json))
320
321 def test_set_chassis_invalid_indicator_command(self):
322 with patch.multiple(
323 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
324 get_request=mock_get_request,
325 put_request=mock_put_request,
326 ):
327 with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
328 with set_module_args(
329 {
330 "category": "Chassis",
331 "command": "IndicatorLedBright",
332 "baseuri": MOCK_BASE_URI,
333 "username": "USERID",
334 "password": "PASSWORD=21",
335 }
336 ):
337 module.main()
338 self.assertIn("Invalid Command", get_exception_message(ansible_fail_json))
339
340 def test_reset_enclosure(self):
341 with patch.multiple(
342 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
343 get_request=mock_get_request,
344 put_request=mock_put_request,
345 ):
346 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
347 with set_module_args(
348 {
349 "category": "Systems",
350 "command": "PowerGracefulRestart",
351 "baseuri": MOCK_BASE_URI,
352 "username": "USERID",
353 "password": "PASSWORD=21",
354 }
355 ):
356 module.main()
357 self.assertEqual(ACTION_WAS_SUCCESSFUL, get_exception_message(ansible_exit_json))
358 self.assertTrue(is_changed(ansible_exit_json))
359
360 def test_reset_enclosure_check_mode(self):
361 with patch.multiple(
362 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
363 get_request=mock_get_request,
364 put_request=mock_invalid_http_request,
365 ):
366 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
367 with set_module_args(
368 {
369 "category": "Systems",
370 "command": "PowerGracefulRestart",
371 "baseuri": MOCK_BASE_URI,
372 "username": "USERID",
373 "password": "PASSWORD=21",
374 "_ansible_check_mode": True,
375 }
376 ):
377 module.main()
378 self.assertEqual(UPDATE_NOT_PERFORMED_IN_CHECK_MODE, get_exception_message(ansible_exit_json))
379 self.assertTrue(is_changed(ansible_exit_json))
380
381 def test_firmware_upload_missing_update_image_path(self):
382 with patch.multiple(
383 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
384 get_request=mock_get_request,
385 put_request=mock_put_request,
386 ):
387 with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
388 with set_module_args(
389 {
390 "category": "Update",
391 "command": "FWUpload",
392 "baseuri": MOCK_BASE_URI,
393 "username": "USERID",
394 "password": "PASSWORD=21",
395 }
396 ):
397 module.main()
398 self.assertEqual("Missing update_image_path.", get_exception_message(ansible_fail_json))
399
400 def test_firmware_upload_file_not_found(self):
401 with patch.multiple(
402 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
403 get_request=mock_get_request,
404 put_request=mock_invalid_http_request,
405 ):
406 with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
407 with set_module_args(
408 {
409 "category": "Update",
410 "command": "FWUpload",
411 "update_image_path": "nonexistentfile.bin",
412 "baseuri": MOCK_BASE_URI,
413 "username": "USERID",
414 "password": "PASSWORD=21",
415 }
416 ):
417 module.main()
418 self.assertEqual("File does not exist.", get_exception_message(ansible_fail_json))
419
420 def test_firmware_upload(self):
421 filename = "fake_firmware.bin"
422 filepath = os.path.join(self.tempdir, filename)
423 file_contents = b"\x00\x01\x02\x03\x04"
424 with open(filepath, "wb+") as f:
425 f.write(file_contents)
426
427 with patch.multiple(
428 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
429 get_request=mock_get_request,
430 put_request=mock_put_request,
431 post_request=mock_post_request,
432 ):
433 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
434 with set_module_args(
435 {
436 "category": "Update",
437 "command": "FWUpload",
438 "update_image_path": filepath,
439 "baseuri": MOCK_BASE_URI,
440 "username": "USERID",
441 "password": "PASSWORD=21",
442 }
443 ):
444 module.main()
445 self.assertEqual(ACTION_WAS_SUCCESSFUL, get_exception_message(ansible_exit_json))
446 self.assertTrue(is_changed(ansible_exit_json))
447
448 def test_firmware_upload_check_mode(self):
449 filename = "fake_firmware.bin"
450 filepath = os.path.join(self.tempdir, filename)
451 file_contents = b"\x00\x01\x02\x03\x04"
452 with open(filepath, "wb+") as f:
453 f.write(file_contents)
454
455 with patch.multiple(
456 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
457 get_request=mock_get_request,
458 put_request=mock_put_request,
459 post_request=mock_invalid_http_request,
460 ):
461 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
462 with set_module_args(
463 {
464 "category": "Update",
465 "command": "FWUpload",
466 "update_image_path": filepath,
467 "baseuri": MOCK_BASE_URI,
468 "username": "USERID",
469 "password": "PASSWORD=21",
470 "_ansible_check_mode": True,
471 }
472 ):
473 module.main()
474 self.assertEqual(UPDATE_NOT_PERFORMED_IN_CHECK_MODE, get_exception_message(ansible_exit_json))
475 self.assertTrue(is_changed(ansible_exit_json))
476
477 def test_firmware_update(self):
478 with patch.multiple(
479 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
480 get_request=mock_get_request,
481 put_request=mock_put_request,
482 post_request=mock_invalid_http_request,
483 ):
484 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
485 with set_module_args(
486 {
487 "category": "Update",
488 "command": "FWUpdate",
489 "baseuri": MOCK_BASE_URI,
490 "username": "USERID",
491 "password": "PASSWORD=21",
492 }
493 ):
494 module.main()
495 self.assertEqual(ACTION_WAS_SUCCESSFUL, get_exception_message(ansible_exit_json))
496 self.assertTrue(is_changed(ansible_exit_json))
497
498 def test_firmware_update_check_mode(self):
499 with patch.multiple(
500 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
501 get_request=mock_get_request,
502 put_request=mock_invalid_http_request,
503 post_request=mock_invalid_http_request,
504 ):
505 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
506 with set_module_args(
507 {
508 "category": "Update",
509 "command": "FWUpdate",
510 "baseuri": MOCK_BASE_URI,
511 "username": "USERID",
512 "password": "PASSWORD=21",
513 "_ansible_check_mode": True,
514 }
515 ):
516 module.main()
517 self.assertEqual(UPDATE_NOT_PERFORMED_IN_CHECK_MODE, get_exception_message(ansible_exit_json))
518 self.assertTrue(is_changed(ansible_exit_json))
519
520 def test_firmware_activate(self):
521 with patch.multiple(
522 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
523 get_request=mock_get_request,
524 put_request=mock_put_request,
525 post_request=mock_invalid_http_request,
526 ):
527 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
528 with set_module_args(
529 {
530 "category": "Update",
531 "command": "FWActivate",
532 "baseuri": MOCK_BASE_URI,
533 "username": "USERID",
534 "password": "PASSWORD=21",
535 }
536 ):
537 module.main()
538 self.assertEqual(ACTION_WAS_SUCCESSFUL, get_exception_message(ansible_exit_json))
539 self.assertTrue(is_changed(ansible_exit_json))
540
541 def test_firmware_activate_check_mode(self):
542 with patch.multiple(
543 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
544 get_request=mock_get_request,
545 put_request=mock_invalid_http_request,
546 post_request=mock_invalid_http_request,
547 ):
548 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
549 with set_module_args(
550 {
551 "category": "Update",
552 "command": "FWActivate",
553 "baseuri": MOCK_BASE_URI,
554 "username": "USERID",
555 "password": "PASSWORD=21",
556 "_ansible_check_mode": True,
557 }
558 ):
559 module.main()
560 self.assertEqual(UPDATE_NOT_PERFORMED_IN_CHECK_MODE, get_exception_message(ansible_exit_json))
561 self.assertTrue(is_changed(ansible_exit_json))
562
563 def test_delete_job(self):
564 with patch.multiple(
565 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
566 get_request=mock_get_request_job_complete,
567 delete_request=mock_delete_request,
568 put_request=mock_invalid_http_request,
569 post_request=mock_invalid_http_request,
570 ):
571 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
572 with set_module_args(
573 {
574 "category": "Jobs",
575 "command": "DeleteJob",
576 "baseuri": MOCK_BASE_URI,
577 "job_name": MOCK_JOB_NAME,
578 "username": "USERID",
579 "password": "PASSWORD=21",
580 }
581 ):
582 module.main()
583 self.assertEqual(ACTION_WAS_SUCCESSFUL, get_exception_message(ansible_exit_json))
584 self.assertTrue(is_changed(ansible_exit_json))
585
586 def test_delete_job_in_progress(self):
587 with patch.multiple(
588 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
589 get_request=mock_get_request_job_in_progress,
590 delete_request=mock_invalid_http_request,
591 put_request=mock_invalid_http_request,
592 post_request=mock_invalid_http_request,
593 ):
594 with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
595 with set_module_args(
596 {
597 "category": "Jobs",
598 "command": "DeleteJob",
599 "baseuri": MOCK_BASE_URI,
600 "job_name": MOCK_JOB_NAME,
601 "username": "USERID",
602 "password": "PASSWORD=21",
603 }
604 ):
605 module.main()
606 self.assertEqual("Cannot delete job because it is in progress.", get_exception_message(ansible_fail_json))
607
608 def test_delete_job_in_progress_only_on_delete(self):
609 with patch.multiple(
610 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
611 get_request=mock_get_request_job_complete,
612 delete_request=mock_http_request_conflict,
613 put_request=mock_invalid_http_request,
614 post_request=mock_invalid_http_request,
615 ):
616 with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
617 with set_module_args(
618 {
619 "category": "Jobs",
620 "command": "DeleteJob",
621 "baseuri": MOCK_BASE_URI,
622 "job_name": MOCK_JOB_NAME,
623 "username": "USERID",
624 "password": "PASSWORD=21",
625 }
626 ):
627 module.main()
628 self.assertEqual("Cannot delete job because it is in progress.", get_exception_message(ansible_fail_json))
629
630 def test_delete_job_check_mode(self):
631 with patch.multiple(
632 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
633 get_request=mock_get_request_job_complete,
634 delete_request=mock_delete_request,
635 put_request=mock_invalid_http_request,
636 post_request=mock_invalid_http_request,
637 ):
638 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
639 with set_module_args(
640 {
641 "category": "Jobs",
642 "command": "DeleteJob",
643 "baseuri": MOCK_BASE_URI,
644 "job_name": MOCK_JOB_NAME,
645 "username": "USERID",
646 "password": "PASSWORD=21",
647 "_ansible_check_mode": True,
648 }
649 ):
650 module.main()
651 self.assertEqual(UPDATE_NOT_PERFORMED_IN_CHECK_MODE, get_exception_message(ansible_exit_json))
652 self.assertTrue(is_changed(ansible_exit_json))
653
654 def test_delete_job_check_mode_job_not_found(self):
655 with patch.multiple(
656 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
657 get_request=mock_get_request_job_does_not_exist,
658 delete_request=mock_delete_request,
659 put_request=mock_invalid_http_request,
660 post_request=mock_invalid_http_request,
661 ):
662 with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
663 with set_module_args(
664 {
665 "category": "Jobs",
666 "command": "DeleteJob",
667 "baseuri": MOCK_BASE_URI,
668 "job_name": MOCK_JOB_NAME,
669 "username": "USERID",
670 "password": "PASSWORD=21",
671 "_ansible_check_mode": True,
672 }
673 ):
674 module.main()
675 self.assertEqual("Job already deleted.", get_exception_message(ansible_exit_json))
676 self.assertFalse(is_changed(ansible_exit_json))
677
678 def test_delete_job_check_mode_job_in_progress(self):
679 with patch.multiple(
680 "ansible_collections.community.general.plugins.module_utils._ocapi_utils.OcapiUtils",
681 get_request=mock_get_request_job_in_progress,
682 delete_request=mock_delete_request,
683 put_request=mock_invalid_http_request,
684 post_request=mock_invalid_http_request,
685 ):
686 with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
687 with set_module_args(
688 {
689 "category": "Jobs",
690 "command": "DeleteJob",
691 "baseuri": MOCK_BASE_URI,
692 "job_name": MOCK_JOB_NAME,
693 "username": "USERID",
694 "password": "PASSWORD=21",
695 "_ansible_check_mode": True,
696 }
697 ):
698 module.main()
699 self.assertEqual("Cannot delete job because it is in progress.", get_exception_message(ansible_fail_json))