|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# # Copyright: (c) 2023, XLAB Steampunk <steampunk@xlab.si> |
| 3 | +# |
| 4 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | + |
| 6 | +from __future__ import absolute_import, division, print_function |
| 7 | + |
| 8 | +__metaclass__ = type |
| 9 | + |
| 10 | +import sys |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from ansible_collections.scale_computing.hypercore.plugins.module_utils.task_tag import ( |
| 15 | + TaskTag, |
| 16 | +) |
| 17 | +from ansible_collections.scale_computing.hypercore.plugins.module_utils import errors |
| 18 | +from ansible_collections.scale_computing.hypercore.plugins.module_utils.utils import ( |
| 19 | + MIN_PYTHON_VERSION, |
| 20 | +) |
| 21 | + |
| 22 | +pytestmark = pytest.mark.skipif( |
| 23 | + sys.version_info < MIN_PYTHON_VERSION, |
| 24 | + reason=f"requires python{MIN_PYTHON_VERSION[0]}.{MIN_PYTHON_VERSION[1]} or higher", |
| 25 | +) |
| 26 | + |
| 27 | +error_task_tag = { |
| 28 | + "taskTag": "36199", |
| 29 | + "progressPercent": 0, |
| 30 | + "state": "ERROR", |
| 31 | + "formattedDescription": "Delete block device %@ for Virtual Machine %@", |
| 32 | + "descriptionParameters": ["3aad4f9d", "demo-vm"], |
| 33 | + "formattedMessage": "Unable to delete block device from VM '%@': Still in use", |
| 34 | + "messageParameters": ["demo-vm"], |
| 35 | + "objectUUID": "8c6196be-ddb5-4357-9783-50869dc60969", |
| 36 | + "created": 1687925214, |
| 37 | + "modified": 1687925276, |
| 38 | + "completed": 1687925276, |
| 39 | + "sessionID": "d4fa7269-caa0-4a0a-b5d8-85d8601e93c4", |
| 40 | + "nodeUUIDs": ["3dcb0c96-f013-4ccc-b639-33605ea78c44"], |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +ok_task_tag = { |
| 45 | + "taskTag": "10", |
| 46 | + "progressPercent": 100, |
| 47 | + "state": "COMPLETE", |
| 48 | + "formattedDescription": "Delete Alert Email Target", |
| 49 | + "descriptionParameters": [], |
| 50 | + "formattedMessage": "", |
| 51 | + "messageParameters": [], |
| 52 | + "objectUUID": "default-target", |
| 53 | + "created": 1686128448, |
| 54 | + "modified": 1686128448, |
| 55 | + "completed": 1686128448, |
| 56 | + "sessionID": "d339e80b-e2a9-4b81-a55f-d1d13d6c8645", |
| 57 | + "nodeUUIDs": [], |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +class TestTaskTag: |
| 62 | + def test_wait_task_ok(self, mocker): |
| 63 | + task = dict(taskTag=10) |
| 64 | + rest_client = mocker.MagicMock() |
| 65 | + rest_client.get_record.return_value = ok_task_tag |
| 66 | + TaskTag.wait_task(rest_client, task) |
| 67 | + |
| 68 | + def test_wait_task_error(self, mocker): |
| 69 | + # Ensure a meaningful error message is shown in ansible stderr if tasktag fails. |
| 70 | + task = dict(taskTag=36199) |
| 71 | + rest_client = mocker.MagicMock() |
| 72 | + rest_client.get_record.return_value = error_task_tag |
| 73 | + with pytest.raises( |
| 74 | + errors.ScaleComputingError, |
| 75 | + match="There was a problem during this task execution. Task details: " |
| 76 | + + '{"taskTag": "36199", "progressPercent": 0, "state": "ERROR",', |
| 77 | + ): |
| 78 | + TaskTag.wait_task(rest_client, task) |
0 commit comments