Skip to content

Commit 53d83f1

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

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)