From dd003e5ff0edc5415e70e99dbf9586e990301ec9 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Thu, 29 Mar 2018 08:12:29 +0100 Subject: [PATCH] mgr/dashboard: fix exception serialization for tasks Signed-off-by: Ricardo Dias --- src/pybind/mgr/dashboard/tests/test_task.py | 37 +++++++++++++++++++++ src/pybind/mgr/dashboard/tools.py | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/pybind/mgr/dashboard/tests/test_task.py b/src/pybind/mgr/dashboard/tests/test_task.py index 5beb1c2e5ba..3978211c446 100644 --- a/src/pybind/mgr/dashboard/tests/test_task.py +++ b/src/pybind/mgr/dashboard/tests/test_task.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import +import json import unittest import threading import time @@ -263,6 +264,17 @@ class TaskTest(unittest.TestCase): ex_t, fn_t = TaskManager.list_serializable('test8/*') self.assertEqual(len(ex_t), 1) self.assertEqual(len(fn_t), 1) + + try: + json.dumps(ex_t) + except ValueError as ex: + self.fail("Failed to serialize executing tasks: {}".format(str(ex))) + + try: + json.dumps(fn_t) + except ValueError as ex: + self.fail("Failed to serialize finished tasks: {}".format(str(ex))) + # validate executing tasks attributes self.assertEqual(len(ex_t[0].keys()), 4) self.assertEqual(ex_t[0]['name'], 'test8/task1') @@ -345,3 +357,28 @@ class TaskTest(unittest.TestCase): self.assertEqual(len(fn_t), 1) self.assertIsNone(fn_t[0].ret_value) self.assertEqual(str(fn_t[0].exception), "Task Unexpected Exception") + + def test_task_serialization_format_on_failure(self): + task1 = MyTask(1, fail=True) + task1.run('test14/task1', 0.5) + self.wait_for_task('test14/task1') + ex_t, fn_t = TaskManager.list_serializable('test14/*') + self.assertEqual(len(ex_t), 0) + self.assertEqual(len(fn_t), 1) + # validate finished tasks attributes + + try: + json.dumps(fn_t) + except TypeError as ex: + self.fail("Failed to serialize finished tasks: {}".format(str(ex))) + + self.assertEqual(len(fn_t[0].keys()), 9) + self.assertEqual(fn_t[0]['name'], 'test14/task1') + self.assertEqual(fn_t[0]['metadata'], task1.metadata()) + self.assertIsNotNone(fn_t[0]['begin_time']) + self.assertIsNotNone(fn_t[0]['end_time']) + self.assertGreaterEqual(fn_t[0]['duration'], 1.0) + self.assertEqual(fn_t[0]['progress'], 50) + self.assertFalse(fn_t[0]['success']) + self.assertIsNotNone(fn_t[0]['exception']) + self.assertEqual(fn_t[0]['exception'], "Task Unexpected Exception") diff --git a/src/pybind/mgr/dashboard/tools.py b/src/pybind/mgr/dashboard/tools.py index d4f175d8925..b366d337f6e 100644 --- a/src/pybind/mgr/dashboard/tools.py +++ b/src/pybind/mgr/dashboard/tools.py @@ -828,7 +828,7 @@ class TaskManager(object): 'progress': t.progress, 'success': not t.exception, 'ret_value': t.ret_value, - 'exception': t.exception + 'exception': str(t.exception) if t.exception else None } for t in fn_t]