Skip to content

Commit c681c08

Browse files
committed
Show details about failed task
Partially fixes #249 Signed-off-by: Justin Cinkelj <justin.cinkelj@xlab.si>
1 parent 5df4397 commit c681c08

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

plugins/module_utils/task_tag.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
__metaclass__ = type
1111

12+
import json
1213
from time import sleep
1314

1415
from ..module_utils import errors
@@ -46,9 +47,9 @@ def wait_task(
4647
"ERROR",
4748
"UNINITIALIZED",
4849
): # TaskTag has finished unsucessfully or was never initialized, both are errors.
49-
raise errors.ScaleComputingError(
50-
"There was a problem during this task execution."
51-
)
50+
msg = "There was a problem during this task execution."
51+
msg += f" Task details: {json.dumps(task_status)}"
52+
raise errors.ScaleComputingError(msg)
5253
if task_status.get("state", "") not in (
5354
"RUNNING",
5455
"QUEUED",
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
import json
14+
15+
from ansible_collections.scale_computing.hypercore.plugins.module_utils.task_tag import (
16+
TaskTag,
17+
)
18+
from ansible_collections.scale_computing.hypercore.plugins.module_utils import errors
19+
from ansible_collections.scale_computing.hypercore.plugins.module_utils.utils import (
20+
MIN_PYTHON_VERSION,
21+
)
22+
23+
pytestmark = pytest.mark.skipif(
24+
sys.version_info < MIN_PYTHON_VERSION,
25+
reason=f"requires python{MIN_PYTHON_VERSION[0]}.{MIN_PYTHON_VERSION[1]} or higher",
26+
)
27+
28+
error_task_tag = {
29+
"taskTag": "36199",
30+
"progressPercent": 0,
31+
"state": "ERROR",
32+
"formattedDescription": "Delete block device %@ for Virtual Machine %@",
33+
"descriptionParameters": ["3aad4f9d", "demo-vm"],
34+
"formattedMessage": "Unable to delete block device from VM '%@': Still in use",
35+
"messageParameters": ["demo-vm"],
36+
"objectUUID": "8c6196be-ddb5-4357-9783-50869dc60969",
37+
"created": 1687925214,
38+
"modified": 1687925276,
39+
"completed": 1687925276,
40+
"sessionID": "d4fa7269-caa0-4a0a-b5d8-85d8601e93c4",
41+
"nodeUUIDs": ["3dcb0c96-f013-4ccc-b639-33605ea78c44"],
42+
}
43+
44+
45+
ok_task_tag = {
46+
"taskTag": "10",
47+
"progressPercent": 100,
48+
"state": "COMPLETE",
49+
"formattedDescription": "Delete Alert Email Target",
50+
"descriptionParameters": [],
51+
"formattedMessage": "",
52+
"messageParameters": [],
53+
"objectUUID": "default-target",
54+
"created": 1686128448,
55+
"modified": 1686128448,
56+
"completed": 1686128448,
57+
"sessionID": "d339e80b-e2a9-4b81-a55f-d1d13d6c8645",
58+
"nodeUUIDs": [],
59+
}
60+
61+
62+
class TestTaskTag:
63+
def test_wait_task_ok(self, mocker):
64+
task = dict(taskTag=10)
65+
rest_client = mocker.MagicMock()
66+
rest_client.get_record.return_value = ok_task_tag
67+
TaskTag.wait_task(rest_client, task)
68+
69+
def test_wait_task_error(self, mocker):
70+
# Ensure a meaningful error message is shown in ansible stderr if tasktag fails.
71+
task = dict(taskTag=36199)
72+
rest_client = mocker.MagicMock()
73+
rest_client.get_record.return_value = error_task_tag
74+
with pytest.raises(
75+
errors.ScaleComputingError,
76+
match="There was a problem during this task execution. Task details: "
77+
+ '{"taskTag": "36199", "progressPercent": 0, "state": "ERROR",',
78+
):
79+
TaskTag.wait_task(rest_client, task)

0 commit comments

Comments
 (0)